From dbd318558c890dfe1291ffa86f108622f970ddee Mon Sep 17 00:00:00 2001 From: Loic Devulder Date: Mon, 22 Jan 2024 16:54:01 +0100 Subject: [PATCH] ci: delete Qase Run if job has been cancelled Signed-off-by: Loic Devulder --- .github/workflows/master-e2e.yaml | 6 ++++ tests/Makefile | 6 ++-- tests/e2e/helpers/qase/qase.go | 35 +++++++++++++++++-- .../qase/{create_qase_run.go => qase_cmd.go} | 25 ++++++++++++- 4 files changed, 66 insertions(+), 6 deletions(-) rename tests/qase/{create_qase_run.go => qase_cmd.go} (54%) diff --git a/.github/workflows/master-e2e.yaml b/.github/workflows/master-e2e.yaml index 6261938ff..828c38ffa 100644 --- a/.github/workflows/master-e2e.yaml +++ b/.github/workflows/master-e2e.yaml @@ -692,6 +692,12 @@ jobs: echo "Channel: ${{ inputs.upgrade_os_channel }}" >> ${GITHUB_STEP_SUMMARY} echo "Upgrade image: ${{ inputs.upgrade_image }}" >> ${GITHUB_STEP_SUMMARY} fi + - name: Delete Qase Run if job has been cancelled + if: ${{ failure() && job.status == 'cancelled' }} + run: | + if [[ -n "${QASE_RUN_ID}" ]]; then + cd tests && make delete-qase-run + fi - name: Send failed status to slack if: ${{ failure() && github.event_name == 'schedule' }} uses: slackapi/slack-github-action@v1.23.0 diff --git a/tests/Makefile b/tests/Makefile index 3707c5c09..66b4047c3 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -44,9 +44,11 @@ deps: generate-readme: @./scripts/generate-readme > README.md -# Qase deps +# Qase commands create-qase-run: deps - @go run qase/create_qase_run.go + @go run qase/qase_cmd.go -create +delete-qase-run: deps + @go run qase/qase_cmd.go -delete ${QASE_RUN_ID} # E2E tests e2e-airgap-rancher: deps diff --git a/tests/e2e/helpers/qase/qase.go b/tests/e2e/helpers/qase/qase.go index 0555119f1..c2b485751 100644 --- a/tests/e2e/helpers/qase/qase.go +++ b/tests/e2e/helpers/qase/qase.go @@ -141,7 +141,9 @@ func CreateRun() int32 { logrus.Debugf("Run named '%s' with description '%s' created with id %d", runName, runDescription, createdID) // Check that we can access the run - checkRun(client, createdID) + if itExists := checkRun(client, createdID); itExists == false { + logrus.Fatalf("Run %d doesn't exist", createdID) + } // Export runID for all functions to use it os.Setenv("QASE_RUN_ID", fmt.Sprint(runID)) @@ -156,11 +158,13 @@ This function checks the availability of a specific run. - @param id ID of the run to check - @return Fatal on error */ -func checkRun(client *qase.APIClient, id int32) { +func checkRun(client *qase.APIClient, id int32) bool { runResponse, _, err := client.RunsApi.GetRun(context.TODO(), projectCode, id) - if err != nil || !runResponse.Status { + if err != nil { logrus.Fatalf("Error on checking run: %v", err) } + + return runResponse.Status } /* @@ -189,6 +193,31 @@ func deleteRun(client *qase.APIClient, id int32) { } } +/* +This function creates a run in a specific project. + - @param name Name of the run + - @param description Short description of the run + - @return ID of the created run +*/ +func DeleteRun(id int32) { + cfg := qase.NewConfiguration() + cfg.AddDefaultHeader("Token", apiToken) + client := qase.NewAPIClient(cfg) + + if checkProject(client, projectCode) { + logrus.Debugf("Project %s is validated", projectCode) + + // Delete test run + deleteRun(client, id) + logrus.Debugf("Run id %d has been deleted", id) + + // Check that we can't access the run + if itExists := checkRun(client, id); itExists == true { + logrus.Fatalf("Run %d still exists", id) + } + } +} + /* This function finalises the results for a specific run. - @return Fatal on error diff --git a/tests/qase/create_qase_run.go b/tests/qase/qase_cmd.go similarity index 54% rename from tests/qase/create_qase_run.go rename to tests/qase/qase_cmd.go index 02da5f294..fdaea2d47 100644 --- a/tests/qase/create_qase_run.go +++ b/tests/qase/qase_cmd.go @@ -15,12 +15,35 @@ limitations under the License. package main import ( + "flag" "fmt" "github.com/rancher/elemental/tests/e2e/helpers/qase" + "github.com/sirupsen/logrus" ) // Only use to create a run in Qase and return the corresponding created ID func main() { - fmt.Printf("%d", qase.CreateRun()) + // Define the allowed options + createRun := flag.Bool("create", false, "create a new Qase run") + deleteRun := flag.Int("delete", 0, "delete a Qase run") + + // Parse the arguments + flag.Parse() + + // Only one option at a time is allowed + if *createRun { + id := qase.CreateRun() + if id <= 0 { + logrus.Fatalln("Error on creating Qase run") + } + logrus.Debugf("Qase run id %d created", id) + fmt.Printf("%d", id) + } else if *deleteRun > 0 { + qase.DeleteRun(int32(*deleteRun)) + logrus.Debugf("Qase run id %d deleted", *deleteRun) + } else { + logrus.Debugln("Nothing do to!") + } + }