diff --git a/internal/cmd/skupper/site/nonkube/site_create_test.go b/internal/cmd/skupper/site/nonkube/site_create_test.go index bfda53471..0b5e09235 100644 --- a/internal/cmd/skupper/site/nonkube/site_create_test.go +++ b/internal/cmd/skupper/site/nonkube/site_create_test.go @@ -70,7 +70,7 @@ func TestNonKubeCmdSiteCreate_ValidateInput(t *testing.T) { { name: "kubernetes flags are not valid on this platform", args: []string{"my-site"}, - flags: &common.CommandSiteCreateFlags{BindHost: "bindhost"}, + flags: &common.CommandSiteCreateFlags{ServiceAccount: "not-supported"}, expectedErrors: []string{}, cobraGenericFlags: map[string]string{ common.FlagNameContext: "test", diff --git a/internal/cmd/skupper/site/nonkube/site_delete.go b/internal/cmd/skupper/site/nonkube/site_delete.go index 7b32f8cbb..92aae3e9d 100644 --- a/internal/cmd/skupper/site/nonkube/site_delete.go +++ b/internal/cmd/skupper/site/nonkube/site_delete.go @@ -12,10 +12,13 @@ import ( type CmdSiteDelete struct { siteHandler *fs.SiteHandler routerAccessHandler *fs.RouterAccessHandler + listenerHandler *fs.ListenerHandler + connectorHandler *fs.ConnectorHandler CobraCmd *cobra.Command namespace string siteName string Flags *common.CommandSiteDeleteFlags + all bool } func NewCmdSiteDelete() *CmdSiteDelete { @@ -29,6 +32,8 @@ func (cmd *CmdSiteDelete) NewClient(cobraCommand *cobra.Command, args []string) cmd.siteHandler = fs.NewSiteHandler(cmd.namespace) cmd.routerAccessHandler = fs.NewRouterAccessHandler(cmd.namespace) + cmd.listenerHandler = fs.NewListenerHandler(cmd.namespace) + cmd.connectorHandler = fs.NewConnectorHandler(cmd.namespace) } func (cmd *CmdSiteDelete) ValidateInput(args []string) []error { @@ -74,6 +79,7 @@ func (cmd *CmdSiteDelete) InputToOptions() { if cmd.namespace == "" { cmd.namespace = "default" } + cmd.all = cmd.Flags.All } func (cmd *CmdSiteDelete) Run() error { @@ -85,6 +91,28 @@ func (cmd *CmdSiteDelete) Run() error { if err != nil { return err } + // if delete all, also remove all the other resources + if cmd.all { + listeners, err := cmd.listenerHandler.List() + if listeners != nil && err == nil { + for _, listener := range listeners { + fmt.Println("delete listener", listener.Name) + err = cmd.listenerHandler.Delete(listener.Name) + if err != nil { + return err + } + } + } + connectors, err := cmd.connectorHandler.List() + if connectors != nil && err == nil { + for _, connector := range connectors { + err = cmd.connectorHandler.Delete(connector.Name) + if err != nil { + return err + } + } + } + } return nil } diff --git a/internal/cmd/skupper/site/nonkube/site_delete_test.go b/internal/cmd/skupper/site/nonkube/site_delete_test.go index ae2a437fb..2882fd09e 100644 --- a/internal/cmd/skupper/site/nonkube/site_delete_test.go +++ b/internal/cmd/skupper/site/nonkube/site_delete_test.go @@ -129,38 +129,120 @@ func TestCmdSiteDelete_Run(t *testing.T) { skupperObjects []runtime.Object skupperErrorMessage string errorMessage string + expectedNamespace string + all bool } testTable := []test{ { - name: "run fails default", - deleteName: "my-site", - errorMessage: "error", + name: "run default", + deleteName: "no-site", + errorMessage: "error", + expectedNamespace: "default", + all: false, }, { - name: "run fails", - namespace: "test", - deleteName: "my-site", - errorMessage: "error", + name: "run delete all", + namespace: "test", + deleteName: "my-site", + errorMessage: "error", + expectedNamespace: "test", + all: true, }, } + // Add a temp file so listener/connector/site exists for delete tests + listenerResource := v2alpha1.Listener{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "skupper.io/v2alpha1", + Kind: "Listener", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "my-listener", + Namespace: "test", + }, + } + connectorResource := v2alpha1.Connector{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "skupper.io/v2alpha1", + Kind: "Connector", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "my-connector", + Namespace: "test", + }, + } + siteResource := v2alpha1.Site{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "skupper.io/v2alpha1", + Kind: "Site", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "my-site", + Namespace: "test", + }, + } + + homeDir, err := os.UserHomeDir() + assert.Check(t, err == nil) + path := filepath.Join(homeDir, "/.local/share/skupper/namespaces/test/", string(api.RuntimeSiteStatePath)) + + command := &CmdSiteDelete{Flags: &common.CommandSiteDeleteFlags{}} + command.namespace = "test" + command.siteHandler = fs.NewSiteHandler(command.namespace) + command.listenerHandler = fs.NewListenerHandler(command.namespace) + command.connectorHandler = fs.NewConnectorHandler(command.namespace) + command.routerAccessHandler = fs.NewRouterAccessHandler(command.namespace) + + content, err := command.siteHandler.EncodeToYaml(siteResource) + assert.Check(t, err == nil) + err = command.siteHandler.WriteFile(path, "my-site.yaml", content, common.Sites) + assert.Check(t, err == nil) + defer command.siteHandler.Delete("my-site") + + content, err = command.listenerHandler.EncodeToYaml(listenerResource) + assert.Check(t, err == nil) + err = command.listenerHandler.WriteFile(path, "my-listener.yaml", content, common.Listeners) + assert.Check(t, err == nil) + defer command.listenerHandler.Delete("my-listener") + + content, err = command.connectorHandler.EncodeToYaml(connectorResource) + assert.Check(t, err == nil) + err = command.connectorHandler.WriteFile(path, "my-connector.yaml", content, common.Connectors) + assert.Check(t, err == nil) + defer command.connectorHandler.Delete("my-connector") + for _, test := range testTable { - cmd := &CmdSiteDelete{} t.Run(test.name, func(t *testing.T) { + command.namespace = test.namespace + command.siteName = test.deleteName + command.Flags.All = test.all + command.InputToOptions() - cmd.siteName = test.deleteName - cmd.namespace = test.namespace - cmd.siteHandler = fs.NewSiteHandler(cmd.namespace) - cmd.routerAccessHandler = fs.NewRouterAccessHandler(cmd.namespace) - cmd.InputToOptions() - - err := cmd.Run() + err := command.Run() if err != nil { assert.Check(t, test.errorMessage == err.Error()) } else { assert.Check(t, err == nil) + assert.Equal(t, command.namespace, test.expectedNamespace) + if test.all { + // only deleting from input/resources directory + // expect all resources are deleted + opts := fs.GetOptions{RuntimeFirst: false, LogWarning: false} + site, _ := command.siteHandler.Get(command.siteName, opts) + assert.Check(t, site == nil) + listeners, _ := command.listenerHandler.List() + for _, listener := range listeners { + resource, _ := command.listenerHandler.Get(listener.Name, opts) + assert.Check(t, resource == nil) + } + connectors, _ := command.connectorHandler.List() + for _, connector := range connectors { + resource, _ := command.connectorHandler.Get(connector.Name, opts) + assert.Check(t, resource == nil) + } + } } }) } diff --git a/internal/cmd/skupper/site/nonkube/site_update_test.go b/internal/cmd/skupper/site/nonkube/site_update_test.go index 05ef5d5ca..f34f8ae9f 100644 --- a/internal/cmd/skupper/site/nonkube/site_update_test.go +++ b/internal/cmd/skupper/site/nonkube/site_update_test.go @@ -83,7 +83,7 @@ func TestCmdSiteUpdate_ValidateInput(t *testing.T) { { name: "kubernetes flags are not valid on this platform", args: []string{"my-site"}, - flags: &common.CommandSiteUpdateFlags{}, + flags: &common.CommandSiteUpdateFlags{ServiceAccount: "not-supported"}, cobraGenericFlags: map[string]string{ common.FlagNameContext: "test", common.FlagNameKubeconfig: "test",