Skip to content

Commit

Permalink
add e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
Daisy Guo committed Nov 12, 2019
1 parent f397792 commit bee8514
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
7 changes: 4 additions & 3 deletions pkg/kn/commands/service/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
},
Expand Down
25 changes: 23 additions & 2 deletions test/e2e/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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")
}

0 comments on commit bee8514

Please sign in to comment.