Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get a particular service and revision based on name parameter #150

Merged
merged 1 commit into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
37 changes: 37 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,30 @@ 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)
assert.ErrorContains(t, err, "'kn revision list' accepts maximum 1 argument")
}

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
}
36 changes: 36 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,29 @@ 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"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The DOMAIN field is being replaced in #247 with URL, one of these two PRs need to rebase and update based on which gets merged first.

This PR is around since a while, probably we can consider updating #247 once this is in.
WDYT @rhuss ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with either. However I suggest to merge this PR first, and then update #247 based on this.

assert.Check(t, util.ContainsAll(output[1], "foo", "foo.default.example.com", "1"))
}

func TestServiceGetWithTwoSrvName(t *testing.T) {
savitaashture marked this conversation as resolved.
Show resolved Hide resolved
service := createMockServiceWithParams("foo", "foo.default.example.com", 1)
serviceList := &v1alpha1.ServiceList{Items: []v1alpha1.Service{*service}}
_, _, err := fakeServiceList([]string{"service", "list", "foo", "bar"}, serviceList)
assert.ErrorContains(t, err, "'kn service list' accepts maximum 1 argument")
}

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