diff --git a/internal/app/api/v1/tests.go b/internal/app/api/v1/tests.go index 8bc8069f8c6..0c91b92eda7 100644 --- a/internal/app/api/v1/tests.go +++ b/internal/app/api/v1/tests.go @@ -540,9 +540,12 @@ func (s TestkubeAPI) DeleteTestHandler() fiber.Handler { return s.Error(c, http.StatusInternalServerError, fmt.Errorf("%s: client could not delete test: %w", errPrefix, err)) } - // delete executions for test - if err = s.ExecutionResults.DeleteByTest(c.Context(), name); err != nil { - return s.Warn(c, http.StatusInternalServerError, fmt.Errorf("test %s was deleted but deleting test executions returned error: %w", name, err)) + skipExecutions := c.Query("skipDeleteExecutions", "") + if skipExecutions != "true" { + // delete executions for test + if err = s.ExecutionResults.DeleteByTest(c.Context(), name); err != nil { + return s.Warn(c, http.StatusInternalServerError, fmt.Errorf("test %s was deleted but deleting test executions returned error: %w", name, err)) + } } return c.SendStatus(http.StatusNoContent) diff --git a/internal/app/api/v1/tests_test.go b/internal/app/api/v1/tests_test.go new file mode 100644 index 00000000000..a6c3f9b9721 --- /dev/null +++ b/internal/app/api/v1/tests_test.go @@ -0,0 +1,66 @@ +package v1 + +import ( + "net/http/httptest" + "testing" + + testsv3 "github.com/kubeshop/testkube-operator/api/tests/v3" + testsclientv3 "github.com/kubeshop/testkube-operator/pkg/client/tests/v3" + "github.com/kubeshop/testkube/pkg/log" + "github.com/kubeshop/testkube/pkg/server" + + "github.com/gofiber/fiber/v2" + "github.com/stretchr/testify/assert" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + k8sclient "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/fake" +) + +func TestTestkubeAPI_DeleteTest(t *testing.T) { + app := fiber.New() + + s := &TestkubeAPI{ + HTTPServer: server.HTTPServer{ + Mux: app, + Log: log.DefaultLogger, + }, + TestsClient: getMockTestClient(), + } + + app.Delete("/tests/:id", s.DeleteTestHandler()) + + req := httptest.NewRequest("DELETE", "http://localhost/tests/k6?skipDeleteExecutions=true", nil) + resp, err := app.Test(req, -1) + + assert.NoError(t, err) + defer resp.Body.Close() + +} + +func getMockTestClient() *testsclientv3.TestsClient { + scheme := runtime.NewScheme() + testsv3.AddToScheme(scheme) + corev1.AddToScheme(scheme) + initObjects := []k8sclient.Object{ + &testsv3.Test{ + TypeMeta: metav1.TypeMeta{ + Kind: "Test", + APIVersion: "tests.testkube.io/v3", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "k6", + Namespace: "", + }, + Spec: testsv3.TestSpec{}, + }, + } + + fakeClient := fake.NewClientBuilder(). + WithScheme(scheme). + WithObjects(initObjects...). + Build() + + return testsclientv3.NewClient(fakeClient, "") +}