Skip to content

Commit

Permalink
Deletion of a resource in a namespace before provisioning.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorge-romero committed May 28, 2024
1 parent 052a145 commit 7a1443b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
23 changes: 23 additions & 0 deletions tests/quickstarter/openshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ func deleteOpenShiftResources(projectID string, componentID string, namespace st
return nil
}

func deleteOpenShiftResourceByName(resourceType string, resourceName string, namespace string) error {
fmt.Printf("-- starting cleanup for resource: %s/%s in %s\n", resourceType, resourceName, namespace)
resource := fmt.Sprintf("%s/%s", resourceType, resourceName)

stdout, stderr, err := runOcCmd([]string{
"-n", namespace,
"delete", resource,
})

if err != nil {
return fmt.Errorf(
"Could not delete resource %s: \nStdOut: %s\nStdErr: %s\n\nErr: %w",
resource,
stdout,
stderr,
err,
)
}

fmt.Printf("-- cleaned up resource: %s\n", resource)
return nil
}

func runOcCmd(args []string) (string, string, error) {
cmd := exec.Command("oc", args...)
var stdout, stderr bytes.Buffer
Expand Down
17 changes: 15 additions & 2 deletions tests/quickstarter/quickstarter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ func TestQuickstarter(t *testing.T) {
}

func executeProvision(t *testing.T, step TestStep, testdataPath string, tmplData TemplateData, repoName string, quickstarterRepo string, quickstarterName string, config map[string]string) {
fmt.Printf("== executeProvision %s-%s\n", utils.PROJECT_NAME, repoName)

// cleanup and create bb resources for this test
err := recreateBitbucketRepo(config, utils.PROJECT_NAME, repoName)
if err != nil {
Expand All @@ -154,6 +156,17 @@ func executeProvision(t *testing.T, step TestStep, testdataPath string, tmplData
t.Fatal(err)
}

if step.ProvisionParams.TestResourcesCleanUp != nil && len(step.ProvisionParams.TestResourcesCleanUp) > 0 {
for _, it := range step.ProvisionParams.TestResourcesCleanUp {
tmp_namespace := it.Namespace
if tmp_namespace == "" || len(tmp_namespace) == 0 {
tmp_namespace = "dev"
}
namespace := fmt.Sprintf("%s-%s", utils.PROJECT_NAME, tmp_namespace)
err = deleteOpenShiftResourceByName(it.ResourceType, it.ResourceName, namespace)
}
}

branch := config["ODS_GIT_REF"]
if len(step.ProvisionParams.Branch) > 0 {
branch = renderTemplate(t, step.ProvisionParams.Branch, tmplData)
Expand Down Expand Up @@ -284,7 +297,7 @@ func executeStepUpload(t *testing.T, step TestStep, testdataPath string, tmplDat
fileToUpload := fmt.Sprintf("%s/%s", testdataPath, step.UploadParams.File)

if step.UploadParams.Render {
fmt.Printf("Rendering template to upload")
fmt.Printf("Rendering template to upload.\n")
tmpl, err := template.ParseFiles(fileToUpload)
if err != nil {
t.Fatalf("Failed to load file to upload: \nErr: %s\n", err)
Expand All @@ -296,7 +309,7 @@ func executeStepUpload(t *testing.T, step TestStep, testdataPath string, tmplDat

}
defer outputFile.Close()
fmt.Printf("Execute render")
fmt.Printf("Rendering file.\n")
err = tmpl.Execute(outputFile, tmplData)
if err != nil {
t.Fatalf("Failed to render file: \nErr: %s\n", err)
Expand Down
9 changes: 9 additions & 0 deletions tests/quickstarter/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ type TestStepProvisionParams struct {
Verify *TestStepVerify `json:"verify"`
// In case we want to override the repository, it is relative to the project where we run it.
Repository string `json:"repository"`
// Extra resources to remove
TestResourcesCleanUp []struct {
// Type of the resource
ResourceType string `json:"resourceType"`
// Name of the resource
ResourceName string `json:"resourceName"`
// Namespace
Namespace string `json:"namespace"`
} `json:"testResourcesCleanUp"`
}

// TestStepBuildParams defines the parameters for the "build" step type.
Expand Down
3 changes: 2 additions & 1 deletion tests/scripts/upload-file-to-bitbucket.sh
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ if [ $httpCode != "200" ]; then
fi

else
echo "File update"
echo "Update existing file."

lastCommit=$(curl --insecure -sS \
-u "${BITBUCKET_USER}:${BITBUCKET_PWD}" \
Expand All @@ -122,6 +122,7 @@ else
-o /dev/null \
-w "%{http_code}")

echo "Upload error code: $httpCode"
if [ $httpCode != "200" ] && [ $httpCode != "409"]; then
echo "An error occured during update of ${BITBUCKET_URL}/rest/api/latest/projects/${BITBUCKET_PROJECT}/repos/${REPOSITORY}/browse/${REPO_FILE} - error:$httpCode"
exit 1
Expand Down

0 comments on commit 7a1443b

Please sign in to comment.