Skip to content

Commit

Permalink
check if namespace exists for task list command
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhelfand authored and tekton-robot committed Oct 8, 2019
1 parent 604f6cf commit aea0869
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
6 changes: 6 additions & 0 deletions pkg/cmd/task/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ func printTaskDetails(s *cli.Stream, p cli.Params) error {
return err
}

// Check if namespace exists. Return error if namespace specified with -n doesn't exist or if user doesn't have permissions to view.
_, err = cs.Kube.CoreV1().Namespaces().Get(p.Namespace(), metav1.GetOptions{})
if err != nil {
return err
}

tasks, err := listAllTasks(cs.Tekton, p.Namespace())
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to list tasks from %s namespace \n", p.Namespace())
Expand Down
44 changes: 39 additions & 5 deletions pkg/cmd/task/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,36 @@ import (
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
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"
)

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

task := Command(p)
output, _ := test.ExecuteCommand(task, "list", "-n", "foo")
test.AssertOutput(t, "Error: namespaces \"foo\" not found\n", output)
}

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

task := Command(p)
output, err := test.ExecuteCommand(task, "list", "-n", "foo")
Expand All @@ -40,16 +65,25 @@ func TestTaskList_Empty(t *testing.T) {
test.AssertOutput(t, emptyMsg+"\n", output)
}

func TestTaskListOnlyTasks(t *testing.T) {
func TestTaskList_Only_Tasks(t *testing.T) {
clock := clockwork.NewFakeClock()

tasks := []*v1alpha1.Task{
tb.Task("tomatoes", "namespace", cb.TaskCreationTime(clock.Now().Add(-1*time.Minute))),
tb.Task("mangoes", "namespace", cb.TaskCreationTime(clock.Now().Add(-20*time.Second))),
tb.Task("bananas", "namespace", cb.TaskCreationTime(clock.Now().Add(-512*time.Hour))),
}

cs, _ := test.SeedTestData(t, pipelinetest.Data{Tasks: tasks})
p := &test.Params{Tekton: cs.Pipeline, Clock: clock}
ns := []*corev1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "namespace",
},
},
}

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

task := Command(p)
output, err := test.ExecuteCommand(task, "list", "-n", "namespace")
Expand Down
1 change: 1 addition & 0 deletions pkg/test/cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func ExecuteCommandC(c *cobra.Command, args ...string) (*cobra.Command, string,
buf := new(bytes.Buffer)
c.SetOutput(buf)
c.SetArgs(args)
c.SilenceUsage = true

root, err := c.ExecuteC()

Expand Down

0 comments on commit aea0869

Please sign in to comment.