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 TaskRun Describe Command #360

Merged
merged 1 commit into from
Oct 16, 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
11 changes: 11 additions & 0 deletions pkg/cmd/taskrun/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/formatted"
validate "github.com/tektoncd/cli/pkg/helper/validate"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -115,6 +116,16 @@ tkn tr desc foo -n bar
Out: cmd.OutOrStdout(),
Err: cmd.OutOrStderr(),
}

cs, err := p.Clients()
if err != nil {
return fmt.Errorf("failed to create tekton client")
}

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

return printTaskRunDescription(s, args[0], p)
},
}
Expand Down
63 changes: 57 additions & 6 deletions pkg/cmd/taskrun/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,47 @@ 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"
)

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

taskrun := Command(p)
_, err := test.ExecuteCommand(taskrun, "desc", "bar", "-n", "invalid")
if err == nil {
t.Errorf("Expected error but did not get one")
}
expected := "namespaces \"invalid\" not found"
test.AssertOutput(t, expected, err.Error())
}

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

taskrun := Command(p)
_, err := test.ExecuteCommand(taskrun, "desc", "bar", "-n", "ns")
if err == nil {
t.Errorf("Expected error, did not get any")
t.Errorf("Expected error but did not get one")
}
expected := "failed to find taskrun \"bar\""
test.AssertOutput(t, expected, err.Error())
Expand All @@ -60,9 +90,16 @@ func TestTaskRunDescribe_empty_taskrun(t *testing.T) {

cs, _ := test.SeedTestData(t, pipelinetest.Data{
TaskRuns: trs,
Namespaces: []*corev1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "ns",
},
},
},
})

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

taskrun := Command(p)
clock.Advance(10 * time.Minute)
Expand Down Expand Up @@ -128,9 +165,16 @@ func TestTaskRunDescribe_only_taskrun(t *testing.T) {

cs, _ := test.SeedTestData(t, pipelinetest.Data{
TaskRuns: trs,
Namespaces: []*corev1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "ns",
},
},
},
})

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

taskrun := Command(p)
clock.Advance(10 * time.Minute)
Expand Down Expand Up @@ -192,9 +236,16 @@ func TestTaskRunDescribe_failed(t *testing.T) {

cs, _ := test.SeedTestData(t, pipelinetest.Data{
TaskRuns: trs,
Namespaces: []*corev1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "ns",
},
},
},
})

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

taskrun := Command(p)
clock.Advance(10 * time.Minute)
Expand Down