diff --git a/cmd/kubectl-testkube/commands/tests.go b/cmd/kubectl-testkube/commands/tests.go index de432d20f39..16c4e611f27 100644 --- a/cmd/kubectl-testkube/commands/tests.go +++ b/cmd/kubectl-testkube/commands/tests.go @@ -47,6 +47,7 @@ func NewTestsCmd() *cobra.Command { cmd.AddCommand(tests.NewStartTestCmd()) cmd.AddCommand(tests.NewCreateTestsCmd()) cmd.AddCommand(tests.NewUpdateTestsCmd()) + cmd.AddCommand(tests.NewDeleteTestCmd()) cmd.AddCommand(tests.NewDeleteTestsCmd()) cmd.AddCommand(tests.NewTestExecutionCmd()) cmd.AddCommand(tests.NewWatchTestExecutionCmd()) diff --git a/cmd/kubectl-testkube/commands/tests/delete.go b/cmd/kubectl-testkube/commands/tests/delete.go index 448c70b870d..be755ca2fbd 100644 --- a/cmd/kubectl-testkube/commands/tests/delete.go +++ b/cmd/kubectl-testkube/commands/tests/delete.go @@ -7,7 +7,7 @@ import ( "github.com/spf13/cobra" ) -func NewDeleteTestsCmd() *cobra.Command { +func NewDeleteTestCmd() *cobra.Command { cmd := &cobra.Command{ Use: "delete ", Short: "Delete tests", diff --git a/cmd/kubectl-testkube/commands/tests/deleteall.go b/cmd/kubectl-testkube/commands/tests/deleteall.go new file mode 100644 index 00000000000..68746363be3 --- /dev/null +++ b/cmd/kubectl-testkube/commands/tests/deleteall.go @@ -0,0 +1,26 @@ +package tests + +import ( + "github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common" + "github.com/kubeshop/testkube/pkg/ui" + "github.com/spf13/cobra" +) + +func NewDeleteTestsCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "delete-all", + Short: "Delete all tests in namespace", + Run: func(cmd *cobra.Command, args []string) { + ui.Logo() + + client, namespace := common.GetClient(cmd) + + name := args[0] + err := client.DeleteTests(namespace) + ui.ExitOnError("delete all tests from namespace "+namespace, err) + ui.Success("Succesfully deleted", name) + }, + } + + return cmd +} diff --git a/internal/app/api/v1/server.go b/internal/app/api/v1/server.go index 55013a2b8e4..75c9063f4a7 100644 --- a/internal/app/api/v1/server.go +++ b/internal/app/api/v1/server.go @@ -125,7 +125,9 @@ func (s TestKubeAPI) Init() { tests.Post("/", s.CreateTestHandler()) tests.Get("/", s.ListTestsHandler()) + tests.Delete("/", s.DeleteTestsHandler()) tests.Get("/:id", s.GetTestHandler()) + tests.Delete("/:id", s.DeleteTestHandler()) tests.Post("/:id/executions", s.ExecuteTestHandler()) tests.Get("/:id/executions", s.ListTestExecutionsHandler()) diff --git a/internal/app/api/v1/tests.go b/internal/app/api/v1/tests.go index 975e8266503..6d9fd52d8fa 100644 --- a/internal/app/api/v1/tests.go +++ b/internal/app/api/v1/tests.go @@ -60,6 +60,41 @@ func (s TestKubeAPI) GetTestHandler() fiber.Handler { } } +// DeleteTestHandler for deleting a test with id +func (s TestKubeAPI) DeleteTestHandler() fiber.Handler { + return func(c *fiber.Ctx) error { + name := c.Params("id") + namespace := c.Query("namespace", "testkube") + err := s.TestsClient.Delete(namespace, name) + if err != nil { + if errors.IsNotFound(err) { + return s.Warn(c, http.StatusNotFound, err) + } + + return s.Error(c, http.StatusBadGateway, err) + } + + return c.SendStatus(fiber.StatusNoContent) + } +} + +// DeleteTestsHandler for deleting all Tests +func (s TestKubeAPI) DeleteTestsHandler() fiber.Handler { + return func(c *fiber.Ctx) error { + namespace := c.Query("namespace", "testkube") + err := s.TestsClient.DeleteAll(namespace) + if err != nil { + if errors.IsNotFound(err) { + return s.Warn(c, http.StatusNotFound, err) + } + + return s.Error(c, http.StatusBadGateway, err) + } + + return c.SendStatus(fiber.StatusNoContent) + } +} + // ListTestsHandler for getting list of all available tests func (s TestKubeAPI) ListTestsHandler() fiber.Handler { return func(c *fiber.Ctx) error { diff --git a/pkg/api/v1/client/direct.go b/pkg/api/v1/client/direct.go index 5c351ac7958..0195a481be2 100644 --- a/pkg/api/v1/client/direct.go +++ b/pkg/api/v1/client/direct.go @@ -553,6 +553,25 @@ func (c DirectScriptsAPI) DeleteTest(name, namespace string) (err error) { return } +func (c DirectScriptsAPI) DeleteTests(namespace string) (err error) { + uri := c.getURI("/tests?namespace=%s", namespace) + req, err := http.NewRequest("DELETE", uri, bytes.NewReader([]byte(""))) + if err != nil { + return fmt.Errorf("prepare request error: %w", err) + } + resp, err := c.client.Do(req) + if err != nil { + return fmt.Errorf("client.Do error: %w", err) + } + defer resp.Body.Close() + + if err := c.responseError(resp); err != nil { + return fmt.Errorf("api/delete-tests returned error: %w", err) + } + + return +} + // UpdateTest creates new Test Custom Resource func (c DirectScriptsAPI) UpdateTest(options UpsertTestOptions) (test testkube.Test, err error) { uri := c.getURI("/tests/%s", options.Name) diff --git a/pkg/api/v1/client/interface.go b/pkg/api/v1/client/interface.go index bd99ca1aed6..8a11bc2bbad 100644 --- a/pkg/api/v1/client/interface.go +++ b/pkg/api/v1/client/interface.go @@ -41,6 +41,7 @@ type Client interface { GetTest(id string, namespace string) (script testkube.Test, err error) ListTests(namespace string, tags []string) (scripts testkube.Tests, err error) DeleteTest(name string, namespace string) error + DeleteTests(namespace string) error ExecuteTest(id, namespace, executionName string, executionParams map[string]string) (execution testkube.TestExecution, err error) GetTestExecution(executionID string) (execution testkube.TestExecution, err error) diff --git a/pkg/api/v1/client/proxy.go b/pkg/api/v1/client/proxy.go index 39d1cc024b2..aa7f332233f 100644 --- a/pkg/api/v1/client/proxy.go +++ b/pkg/api/v1/client/proxy.go @@ -545,7 +545,12 @@ func (c ProxyScriptsAPI) DeleteTest(name string, namespace string) error { if name == "" { return fmt.Errorf("script name '%s' is not valid", name) } - uri := c.getURI("/scripts/%s", name) + uri := c.getURI("/tests/%s", name) + return c.makeDeleteRequest(uri, namespace, true) +} + +func (c ProxyScriptsAPI) DeleteTests(namespace string) error { + uri := c.getURI("/tests") return c.makeDeleteRequest(uri, namespace, true) }