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

Add Namespace Check to PipelineRun List Command #407

Merged
merged 1 commit into from
Oct 28, 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
5 changes: 5 additions & 0 deletions pkg/cmd/pipelinerun/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/formatted"
prhsort "github.com/tektoncd/cli/pkg/helper/pipelinerun/sort"
validate "github.com/tektoncd/cli/pkg/helper/validate"
"github.com/tektoncd/cli/pkg/printer"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -66,6 +67,10 @@ tkn pr list -n foo
pipeline = args[0]
}

if err := validate.NamespaceExists(p); err != nil {
return err
}

if opts.Limit < 0 {
fmt.Fprintf(os.Stderr, "Limit was %d but must be a positive number\n", opts.Limit)
return nil
Expand Down
96 changes: 64 additions & 32 deletions pkg/cmd/pipelinerun/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
pipelinetest "github.com/tektoncd/pipeline/test"
tb "github.com/tektoncd/pipeline/test/builder"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
)

Expand Down Expand Up @@ -83,26 +84,44 @@ func TestListPipelineRuns(t *testing.T) {
),
}

ns := []*corev1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "namespace",
},
},
}

tests := []struct {
name string
command *cobra.Command
args []string
expected []string
name string
command *cobra.Command
args []string
wantError bool
expected []string
}{
{
name: "by pipeline name",
command: command(t, prs, clock.Now()),
args: []string{"list", "pipeline", "-n", "namespace"},
name: "Invalid namespace",
command: command(t, prs, clock.Now(), ns),
args: []string{"list", "-n", "invalid"},
wantError: true,
expected: []string{"Error: namespaces \"invalid\" not found\n"},
},
{
name: "by pipeline name",
command: command(t, prs, clock.Now(), ns),
args: []string{"list", "pipeline", "-n", "namespace"},
wantError: false,
expected: []string{
"NAME STARTED DURATION STATUS ",
"pr1-1 59 minutes ago 1 minute Succeeded ",
"",
},
},
{
name: "all in namespace",
command: command(t, prs, clock.Now()),
args: []string{"list", "-n", "namespace"},
name: "all in namespace",
command: command(t, prs, clock.Now(), ns),
args: []string{"list", "-n", "namespace"},
wantError: false,
expected: []string{
"NAME STARTED DURATION STATUS ",
"pr0-1 --- --- --- ",
Expand All @@ -114,9 +133,10 @@ func TestListPipelineRuns(t *testing.T) {
},
},
{
name: "by template",
command: command(t, prs, clock.Now()),
args: []string{"list", "-n", "namespace", "-o", "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}"},
name: "by template",
command: command(t, prs, clock.Now(), ns),
args: []string{"list", "-n", "namespace", "-o", "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}"},
wantError: false,
expected: []string{
"pr0-1",
"pr3-1",
Expand All @@ -127,27 +147,30 @@ func TestListPipelineRuns(t *testing.T) {
},
},
{
name: "limit pipelineruns returned to 1",
command: command(t, prs, clock.Now()),
args: []string{"list", "-n", "namespace", "--limit", fmt.Sprintf("%d", 1)},
name: "limit pipelineruns returned to 1",
command: command(t, prs, clock.Now(), ns),
args: []string{"list", "-n", "namespace", "--limit", fmt.Sprintf("%d", 1)},
wantError: false,
expected: []string{
"NAME STARTED DURATION STATUS ",
"pr0-1 --- --- --- ",
"",
},
},
{
name: "limit pipelineruns negative case",
command: command(t, prs, clock.Now()),
args: []string{"list", "-n", "namespace", "--limit", fmt.Sprintf("%d", -1)},
name: "limit pipelineruns negative case",
command: command(t, prs, clock.Now(), ns),
args: []string{"list", "-n", "namespace", "--limit", fmt.Sprintf("%d", -1)},
wantError: false,
expected: []string{
"",
},
},
{
name: "limit pipelineruns greater than maximum case",
command: command(t, prs, clock.Now()),
args: []string{"list", "-n", "namespace", "--limit", fmt.Sprintf("%d", 7)},
name: "limit pipelineruns greater than maximum case",
command: command(t, prs, clock.Now(), ns),
args: []string{"list", "-n", "namespace", "--limit", fmt.Sprintf("%d", 7)},
wantError: false,
expected: []string{
"NAME STARTED DURATION STATUS ",
"pr0-1 --- --- --- ",
Expand All @@ -159,9 +182,10 @@ func TestListPipelineRuns(t *testing.T) {
},
},
{
name: "limit pipelineruns with output flag set",
command: command(t, prs, clock.Now()),
args: []string{"list", "-n", "namespace", "-o", "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}", "--limit", fmt.Sprintf("%d", 2)},
name: "limit pipelineruns with output flag set",
command: command(t, prs, clock.Now(), ns),
args: []string{"list", "-n", "namespace", "-o", "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}", "--limit", fmt.Sprintf("%d", 2)},
wantError: false,
expected: []string{
"pr0-1",
"pr3-1",
Expand All @@ -174,7 +198,7 @@ func TestListPipelineRuns(t *testing.T) {
t.Run(td.name, func(t *testing.T) {
got, err := test.ExecuteCommand(td.command, td.args...)

if err != nil {
if !td.wantError && err != nil {
t.Errorf("Unexpected error: %v", err)
}
test.AssertOutput(t, strings.Join(td.expected, "\n"), got)
Expand All @@ -183,26 +207,34 @@ func TestListPipelineRuns(t *testing.T) {
}

func TestListPipeline_empty(t *testing.T) {
cs, _ := test.SeedTestData(t, pipelinetest.Data{})
p := &test.Params{Tekton: cs.Pipeline}
ns := []*corev1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "ns",
},
},
}

cs, _ := test.SeedTestData(t, pipelinetest.Data{Namespaces: ns})
p := &test.Params{Tekton: cs.Pipeline, Kube: cs.Kube}

pipeline := Command(p)
output, err := test.ExecuteCommand(pipeline, "list", "-n", "ns")

if err != nil {
t.Errorf("Unexpected error: %v", err)
}

test.AssertOutput(t, emptyMsg+"\n", output)
}

func command(t *testing.T, prs []*v1alpha1.PipelineRun, now time.Time) *cobra.Command {
func command(t *testing.T, prs []*v1alpha1.PipelineRun, now time.Time, ns []*corev1.Namespace) *cobra.Command {
// fake clock advanced by 1 hour
clock := clockwork.NewFakeClockAt(now)
clock.Advance(time.Duration(60) * time.Minute)

cs, _ := test.SeedTestData(t, pipelinetest.Data{PipelineRuns: prs})
cs, _ := test.SeedTestData(t, pipelinetest.Data{PipelineRuns: prs, Namespaces: ns})

p := &test.Params{Tekton: cs.Pipeline, Clock: clock}
p := &test.Params{Tekton: cs.Pipeline, Clock: clock, Kube: cs.Kube}

return Command(p)
}