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 Cancel Command #410

Merged
merged 1 commit into from
Oct 27, 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 pkg/cmd/pipelinerun/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -47,11 +48,16 @@ func cancelCommand(p cli.Params) *cobra.Command {
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
pr := args[0]

s := &cli.Stream{
Out: cmd.OutOrStdout(),
Err: cmd.OutOrStderr(),
}
// opts.Streamer = pods.NewStream

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

return cancelPipelineRun(p, s, pr)
},
}
Expand Down
67 changes: 62 additions & 5 deletions pkg/cmd/pipelinerun/cancel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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"
"k8s.io/apimachinery/pkg/runtime"
k8stest "k8s.io/client-go/testing"
"knative.dev/pkg/apis"
Expand All @@ -35,7 +36,35 @@ var (
failure = apis.Condition{Type: apis.ConditionSucceeded, Status: corev1.ConditionFalse}
)

func Test_cancel_invalid_namespace(t *testing.T) {
ns := []*corev1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "ns",
},
},
}

prName := "test-pipeline-run-123"

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

pRun := Command(p)
got, _ := tu.ExecuteCommand(pRun, "cancel", prName, "-n", "invalid")

expected := "Error: namespaces \"invalid\" not found\n"
tu.AssertOutput(t, expected, got)
}

func Test_cancel_pipelinerun(t *testing.T) {
ns := []*corev1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "ns",
},
},
}

prName := "test-pipeline-run-123"

Expand All @@ -52,7 +81,7 @@ func Test_cancel_pipelinerun(t *testing.T) {
),
}

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

pRun := Command(p)
Expand All @@ -63,10 +92,17 @@ func Test_cancel_pipelinerun(t *testing.T) {
}

func Test_cancel_pipelinerun_not_found(t *testing.T) {
ns := []*corev1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "ns",
},
},
}

prName := "test-pipeline-run-123"

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

pRun := Command(p)
Expand All @@ -77,6 +113,13 @@ func Test_cancel_pipelinerun_not_found(t *testing.T) {
}

func Test_cancel_pipelinerun_client_err(t *testing.T) {
ns := []*corev1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "ns",
},
},
}

prName := "test-pipeline-run-123"
errStr := "test generated error"
Expand All @@ -94,7 +137,7 @@ func Test_cancel_pipelinerun_client_err(t *testing.T) {
),
}

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

cs.Pipeline.PrependReactor("update", "pipelineruns", func(action k8stest.Action) (bool, runtime.Object, error) {
Expand All @@ -109,6 +152,13 @@ func Test_cancel_pipelinerun_client_err(t *testing.T) {
}

func Test_finished_pipelinerun_success(t *testing.T) {
ns := []*corev1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "ns",
},
},
}

prName := "test-pipeline-run-123"

Expand All @@ -128,7 +178,7 @@ func Test_finished_pipelinerun_success(t *testing.T) {
),
}

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

pRun := Command(p)
Expand All @@ -139,6 +189,13 @@ func Test_finished_pipelinerun_success(t *testing.T) {
}

func Test_finished_pipelinerun_failure(t *testing.T) {
ns := []*corev1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "ns",
},
},
}

prName := "test-pipeline-run-123"

Expand All @@ -158,7 +215,7 @@ func Test_finished_pipelinerun_failure(t *testing.T) {
),
}

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

pRun := Command(p)
Expand Down