diff --git a/pkg/kn/commands/service/delete.go b/pkg/kn/commands/service/delete.go index 77f39316c4..161c76d39d 100644 --- a/pkg/kn/commands/service/delete.go +++ b/pkg/kn/commands/service/delete.go @@ -36,7 +36,7 @@ func NewServiceDeleteCommand(p *commands.KnParams) *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { - return errors.New("requires the service name.") + return errors.New("requires the service name") } namespace, err := p.GetNamespace(cmd) @@ -51,9 +51,10 @@ func NewServiceDeleteCommand(p *commands.KnParams) *cobra.Command { for _, name := range args { err = client.DeleteService(name) if err != nil { - return err + fmt.Fprintf(cmd.OutOrStdout(), "%s.\n", err) + } else { + fmt.Fprintf(cmd.OutOrStdout(), "Service '%s' successfully deleted in namespace '%s'.\n", name, namespace) } - fmt.Fprintf(cmd.OutOrStdout(), "Service '%s' successfully deleted in namespace '%s'.\n", name, namespace) } return nil }, diff --git a/test/e2e/service_test.go b/test/e2e/service_test.go index fc854e508c..4c5d2b02b6 100644 --- a/test/e2e/service_test.go +++ b/test/e2e/service_test.go @@ -43,6 +43,11 @@ func TestService(t *testing.T) { test.serviceDelete(t, "hello") test.serviceDeleteNonexistent(t, "hello") }) + + t.Run("delete two services with a service nonexistent", func(t *testing.T) { + test.serviceCreate(t, "hello") + test.serviceMultipleDelete(t, []string{"hello123", "hello"}) + }) } func (test *e2eTest) serviceCreateDuplicate(t *testing.T, serviceName string) { @@ -69,8 +74,24 @@ func (test *e2eTest) serviceDeleteNonexistent(t *testing.T, serviceName string) assert.NilError(t, err) assert.Check(t, !strings.Contains(out, serviceName), "The service exists") - _, err = test.kn.RunWithOpts([]string{"service", "delete", serviceName}, runOpts{NoNamespace: false, AllowError: true}) + out, err = test.kn.RunWithOpts([]string{"service", "delete", serviceName}, runOpts{NoNamespace: false, AllowError: true}) expectedErr := fmt.Sprintf(`services.serving.knative.dev "%s" not found`, serviceName) - assert.ErrorContains(t, err, expectedErr) + assert.Check(t, strings.Contains(out, expectedErr), "Failed to get 'not found' error") +} + +func (test *e2eTest) serviceMultipleDelete(t *testing.T, serviceName []string) { + existService := serviceName[1] + nonexistService := serviceName[0] + out, err := test.kn.RunWithOpts([]string{"service", "list", existService}, runOpts{NoNamespace: false}) + assert.NilError(t, err) + assert.Check(t, strings.Contains(out, existService), "The service not exists") + assert.Check(t, !strings.Contains(out, nonexistService), "The service exists") + + out, err = test.kn.RunWithOpts([]string{"service", "delete", serviceName[0], serviceName[1]}, runOpts{NoNamespace: false, AllowError: true}) + + expectedSuccess := fmt.Sprintf(`Service '%s' successfully deleted in namespace '%s'.`, existService, test.kn.namespace) + expectedErr := fmt.Sprintf(`services.serving.knative.dev "%s" not found`, nonexistService) + assert.Check(t, strings.Contains(out, expectedSuccess), "Failed to get 'successfully deleted' message") + assert.Check(t, strings.Contains(out, expectedErr), "Failed to get 'not found' error") }