Skip to content

Commit

Permalink
get a particular service and revision based on name parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
savitaashture committed Jul 9, 2019
1 parent 2ae037f commit 3ecad95
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 8 deletions.
8 changes: 7 additions & 1 deletion docs/cmd/kn_revision_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ List available revisions.
List revisions for a given service.

```
kn revision list [flags]
kn revision list NAME [flags]
```

### Examples
Expand All @@ -19,6 +19,12 @@ kn revision list [flags]
# List revisions for a service 'svc1' in namespace 'myapp'
kn revision list -s svc1 -n myapp
# List all revisions in JSON output format
kn revision list -o json
# List revision 'web'
kn revision list web
```

### Options
Expand Down
16 changes: 15 additions & 1 deletion docs/cmd/kn_service_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,21 @@ List available services.
List available services.

```
kn service list [flags]
kn service list NAME [flags]
```

### Examples

```
# List all services
kn service list
# List all services in JSON output format
kn service list -o json
# List service 'web'
kn service list web
```

### Options
Expand Down
29 changes: 25 additions & 4 deletions pkg/kn/commands/revision/revision_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,21 @@ func NewRevisionListCommand(p *commands.KnParams) *cobra.Command {
revisionListFlags := NewRevisionListFlags()

revisionListCommand := &cobra.Command{
Use: "list",
Use: "list NAME",
Short: "List available revisions.",
Long: "List revisions for a given service.",
Example: `
# List all revisions
kn revision list
# List revisions for a service 'svc1' in namespace 'myapp'
kn revision list -s svc1 -n myapp`,
kn revision list -s svc1 -n myapp
# List all revisions in JSON output format
kn revision list -o json
# List revision 'web'
kn revision list web`,
RunE: func(cmd *cobra.Command, args []string) error {
namespace, err := p.GetNamespace(cmd)
if err != nil {
Expand Down Expand Up @@ -65,7 +71,7 @@ func NewRevisionListCommand(p *commands.KnParams) *cobra.Command {
return nil
}
} else {
revisionList, err = client.ListRevisions()
revisionList, err = getRevisionInfo(args, client)
if err != nil {
return err
}
Expand All @@ -74,7 +80,6 @@ func NewRevisionListCommand(p *commands.KnParams) *cobra.Command {
return nil
}
}

printer, err := revisionListFlags.ToPrinter()
if err != nil {
return err
Expand All @@ -90,3 +95,19 @@ func NewRevisionListCommand(p *commands.KnParams) *cobra.Command {
revisionListFlags.AddFlags(revisionListCommand)
return revisionListCommand
}

func getRevisionInfo(args []string, client v1alpha12.KnClient) (*v1alpha1.RevisionList, error) {
var (
revisionList *v1alpha1.RevisionList
err error
)
switch len(args) {
case 0:
revisionList, err = client.ListRevisions()
case 1:
revisionList, err = client.ListRevisions(v1alpha12.WithName(args[0]))
default:
return nil, fmt.Errorf("'kn revision list' accepts maximum 1 argument")
}
return revisionList, err
}
39 changes: 39 additions & 0 deletions pkg/kn/commands/revision/revision_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ func TestRevisionListEmpty(t *testing.T) {
}
}

func TestRevisionListEmptyByName(t *testing.T) {
action, _, err := fakeRevisionList([]string{"revision", "list", "name"}, &v1alpha1.RevisionList{})
if err != nil {
t.Error(err)
return
}
if action == nil {
t.Errorf("No action")
} else if !action.Matches("list", "revisions") {
t.Errorf("Bad action %v", action)
}
}

func TestRevisionListDefaultOutput(t *testing.T) {
revision1 := createMockRevisionWithParams("foo-abcd", "foo")
revision2 := createMockRevisionWithParams("bar-wxyz", "bar")
Expand Down Expand Up @@ -123,6 +136,32 @@ func TestRevisionListForService(t *testing.T) {
assert.Assert(t, util.ContainsAll(output[0], "No", "revisions", "svc3"), "no revisions")
}

func TestRevisionListOneOutput(t *testing.T) {
revision := createMockRevisionWithParams("foo-abcd", "foo")
RevisionList := &v1alpha1.RevisionList{Items: []v1alpha1.Revision{*revision}}
action, output, err := fakeRevisionList([]string{"revision", "list", "foo-abcd"}, RevisionList)
if err != nil {
t.Fatal(err)
}
if action == nil {
t.Errorf("No action")
} else if !action.Matches("list", "revisions") {
t.Errorf("Bad action %v", action)
}

assert.Assert(t, util.ContainsAll(output[0], "NAME", "SERVICE", "AGE", "CONDITIONS", "READY", "REASON"))
assert.Assert(t, util.ContainsAll(output[1], "foo", "foo-abcd"))
}

func TestRevisionListOutputWithTwoRevName(t *testing.T) {
t.Log("verify by passing two revision name to get command")
RevisionList := &v1alpha1.RevisionList{Items: []v1alpha1.Revision{}}
_, _, err := fakeRevisionList([]string{"revision", "list", "foo-abcd", "bar-abcd"}, RevisionList)
if err != nil {
assert.Equal(t, "'kn revision list' accepts maximum 1 argument", err.Error())
}
}

func createMockRevisionWithParams(name, svcName string) *v1alpha1.Revision {
revision := &v1alpha1.Revision{
TypeMeta: metav1.TypeMeta{
Expand Down
31 changes: 29 additions & 2 deletions pkg/kn/commands/service/service_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"fmt"

"github.com/knative/client/pkg/kn/commands"
v1alpha12 "github.com/knative/client/pkg/serving/v1alpha1"
"github.com/knative/serving/pkg/apis/serving/v1alpha1"
"github.com/spf13/cobra"
)

Expand All @@ -26,8 +28,17 @@ func NewServiceListCommand(p *commands.KnParams) *cobra.Command {
serviceListFlags := NewServiceListFlags()

serviceListCommand := &cobra.Command{
Use: "list",
Use: "list NAME",
Short: "List available services.",
Example: `
# List all services
kn service list
# List all services in JSON output format
kn service list -o json
# List service 'web'
kn service list web`,
RunE: func(cmd *cobra.Command, args []string) error {
namespace, err := p.GetNamespace(cmd)
if err != nil {
Expand All @@ -37,7 +48,7 @@ func NewServiceListCommand(p *commands.KnParams) *cobra.Command {
if err != nil {
return err
}
serviceList, err := client.ListServices()
serviceList, err := getServiceInfo(args, client)
if err != nil {
return err
}
Expand All @@ -61,3 +72,19 @@ func NewServiceListCommand(p *commands.KnParams) *cobra.Command {
serviceListFlags.AddFlags(serviceListCommand)
return serviceListCommand
}

func getServiceInfo(args []string, client v1alpha12.KnClient) (*v1alpha1.ServiceList, error) {
var (
serviceList *v1alpha1.ServiceList
err error
)
switch len(args) {
case 0:
serviceList, err = client.ListServices()
case 1:
serviceList, err = client.ListServices(v1alpha12.WithName(args[0]))
default:
return nil, fmt.Errorf("'kn service list' accepts maximum 1 argument")
}
return serviceList, err
}
38 changes: 38 additions & 0 deletions pkg/kn/commands/service/service_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ func TestListEmpty(t *testing.T) {
}
}

func TestGetEmpty(t *testing.T) {
action, _, err := fakeServiceList([]string{"service", "list", "name"}, &v1alpha1.ServiceList{})
if err != nil {
t.Error(err)
return
}
if action == nil {
t.Errorf("No action")
} else if !action.Matches("list", "services") {
t.Errorf("Bad action %v", action)
}
}

func TestServiceListDefaultOutput(t *testing.T) {
service1 := createMockServiceWithParams("foo", "foo.default.example.com", 1)
service2 := createMockServiceWithParams("bar", "bar.default.example.com", 2)
Expand All @@ -78,6 +91,31 @@ func TestServiceListDefaultOutput(t *testing.T) {
assert.Check(t, util.ContainsAll(output[2], "bar", "bar.default.example.com", "2"))
}

func TestServiceGetOneOutput(t *testing.T) {
service := createMockServiceWithParams("foo", "foo.default.example.com", 1)
serviceList := &v1alpha1.ServiceList{Items: []v1alpha1.Service{*service}}
action, output, err := fakeServiceList([]string{"service", "list", "foo"}, serviceList)
if err != nil {
t.Fatal(err)
}
if action == nil {
t.Errorf("No action")
} else if !action.Matches("list", "services") {
t.Errorf("Bad action %v", action)
}
assert.Check(t, util.ContainsAll(output[0], "NAME", "DOMAIN", "GENERATION", "AGE", "CONDITIONS", "READY", "REASON"))
assert.Check(t, util.ContainsAll(output[1], "foo", "foo.default.example.com", "1"))
}

func TestServiceGetWithTwoSrvName(t *testing.T) {
service := createMockServiceWithParams("foo", "foo.default.example.com", 1)
serviceList := &v1alpha1.ServiceList{Items: []v1alpha1.Service{*service}}
_, _, err := fakeServiceList([]string{"service", "list", "foo", "bar"}, serviceList)
if err != nil {
assert.Equal(t, "'kn service list' accepts maximum 1 argument", err.Error())
}
}

func createMockServiceWithParams(name, domain string, generation int64) *v1alpha1.Service {
service := &v1alpha1.Service{
TypeMeta: metav1.TypeMeta{
Expand Down

0 comments on commit 3ecad95

Please sign in to comment.