Skip to content

Commit

Permalink
fix: allow to delete tests (#814)
Browse files Browse the repository at this point in the history
* fix: allow to delete tests

* fix: allow to delete all tests in ns

* fix: delete all without validator
  • Loading branch information
exu authored Jan 20, 2022
1 parent e2ea863 commit 61f1f1b
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/kubectl-testkube/commands/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubectl-testkube/commands/tests/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/spf13/cobra"
)

func NewDeleteTestsCmd() *cobra.Command {
func NewDeleteTestCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "delete <testName>",
Short: "Delete tests",
Expand Down
26 changes: 26 additions & 0 deletions cmd/kubectl-testkube/commands/tests/deleteall.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions internal/app/api/v1/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
35 changes: 35 additions & 0 deletions internal/app/api/v1/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions pkg/api/v1/client/direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions pkg/api/v1/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion pkg/api/v1/client/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 61f1f1b

Please sign in to comment.