Skip to content

Commit

Permalink
feat: allow skipping execution deletion on test deletion (#4411)
Browse files Browse the repository at this point in the history
  • Loading branch information
povilasv authored Sep 28, 2023
1 parent 4ff6744 commit e594c65
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
9 changes: 6 additions & 3 deletions internal/app/api/v1/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
66 changes: 66 additions & 0 deletions internal/app/api/v1/tests_test.go
Original file line number Diff line number Diff line change
@@ -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, "")
}

0 comments on commit e594c65

Please sign in to comment.