Skip to content

Commit

Permalink
add namespace check to pr logs command
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhelfand authored and tekton-robot committed Oct 27, 2019
1 parent 3d2f6e4 commit 8743d54
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 11 deletions.
127 changes: 117 additions & 10 deletions pkg/cmd/pipelinerun/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,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"
"k8s.io/apimachinery/pkg/watch"
k8stest "k8s.io/client-go/testing"
"knative.dev/pkg/apis"
)

func TestLog_invalid_namespace(t *testing.T) {
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}
c := Command(p)

_, err := test.ExecuteCommand(c, "logs", "pipelinerun", "-n", "invalid")

if err == nil {
t.Errorf("Expecting error for invalid namespace")
}

expected := "namespaces \"invalid\" not found"
test.AssertOutput(t, expected, err.Error())
}

func TestLog_no_pipelinerun_argument(t *testing.T) {
c := Command(&test.Params{})
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}
c := Command(p)

_, err := test.ExecuteCommand(c, "logs", "-n", "ns")

Expand All @@ -58,7 +92,16 @@ func TestLog_run_not_found(t *testing.T) {
),
),
}
cs, _ := test.SeedTestData(t, pipelinetest.Data{PipelineRuns: pr})

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

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

c := Command(p)
Expand Down Expand Up @@ -91,6 +134,14 @@ func TestPipelinerunLogs(t *testing.T) {
nopStep = "nop"
)

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

trs := []*v1alpha1.TaskRun{
tb.TaskRun(tr1Name, ns,
tb.TaskRunSpec(
Expand Down Expand Up @@ -246,7 +297,7 @@ func TestPipelinerunLogs(t *testing.T) {

for _, s := range scenarios {
t.Run(s.name, func(t *testing.T) {
cs, _ := test.SeedTestData(t, pipelinetest.Data{PipelineRuns: prs, Pipelines: pps, TaskRuns: trs, Pods: p})
cs, _ := test.SeedTestData(t, pipelinetest.Data{PipelineRuns: prs, Pipelines: pps, TaskRuns: trs, Pods: p, Namespaces: nsList})

prlo := logOpts(prName, ns, cs, fake.Streamer(fakeLogs), s.allSteps, false, s.tasks...)
output, _ := fetchLogs(prlo)
Expand Down Expand Up @@ -281,6 +332,14 @@ func TestPipelinerunLog_completed_taskrun_only(t *testing.T) {
task4Name = "teardown"
)

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

trs := []*v1alpha1.TaskRun{
tb.TaskRun(tr1Name, ns,
tb.TaskRunSpec(
Expand Down Expand Up @@ -352,7 +411,7 @@ func TestPipelinerunLog_completed_taskrun_only(t *testing.T) {
),
)

cs, _ := test.SeedTestData(t, pipelinetest.Data{PipelineRuns: prs, Pipelines: pps, TaskRuns: trs, Pods: p})
cs, _ := test.SeedTestData(t, pipelinetest.Data{PipelineRuns: prs, Pipelines: pps, TaskRuns: trs, Pods: p, Namespaces: nsList})
prlo := logOpts(prName, ns, cs, fake.Streamer(fakeLogStream), false, false)
output, _ := fetchLogs(prlo)

Expand All @@ -379,6 +438,14 @@ func TestPipelinerunLog_follow_mode(t *testing.T) {
tr1Step1Name = "writefile-step"
)

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

trs := []*v1alpha1.TaskRun{
tb.TaskRun(tr1Name, ns,
tb.TaskRunSpec(
Expand Down Expand Up @@ -455,7 +522,7 @@ func TestPipelinerunLog_follow_mode(t *testing.T) {
),
)

cs, _ := test.SeedTestData(t, pipelinetest.Data{PipelineRuns: prs, Pipelines: pps, TaskRuns: trs, Pods: p})
cs, _ := test.SeedTestData(t, pipelinetest.Data{PipelineRuns: prs, Pipelines: pps, TaskRuns: trs, Pods: p, Namespaces: nsList})
prlo := logOpts(prName, ns, cs, fake.Streamer(fakeLogStream), false, true)
output, _ := fetchLogs(prlo)

Expand All @@ -480,6 +547,14 @@ func TestLogs_error_log(t *testing.T) {
errMsg = "Pipeline tektoncd/errlog-pipeline can't be Run; it contains Tasks that don't exist: Couldn't retrieve Task errlog-tasks: task.tekton.dev errlog-tasks not found"
)

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

ts := []*v1alpha1.Task{
tb.Task(taskName, ns,
tb.TaskSpec()),
Expand All @@ -505,7 +580,7 @@ func TestLogs_error_log(t *testing.T) {
),
),
}
cs, _ := test.SeedTestData(t, pipelinetest.Data{PipelineRuns: prs, Pipelines: ps, Tasks: ts})
cs, _ := test.SeedTestData(t, pipelinetest.Data{PipelineRuns: prs, Pipelines: ps, Tasks: ts, Namespaces: nsList})
prlo := logOpts(prName, ns, cs, fake.Streamer([]fake.Log{}), false, false)
output, err := fetchLogs(prlo)
if err != nil {
Expand All @@ -522,6 +597,14 @@ func TestLogs_nologs(t *testing.T) {
taskName = "nologs-task"
)

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

prs := []*v1alpha1.PipelineRun{
tb.PipelineRun(prName, ns,
tb.PipelineRunLabel("tekton.dev/pipeline", prName),
Expand All @@ -542,7 +625,7 @@ func TestLogs_nologs(t *testing.T) {
),
),
}
cs, _ := test.SeedTestData(t, pipelinetest.Data{PipelineRuns: prs, Pipelines: ps})
cs, _ := test.SeedTestData(t, pipelinetest.Data{PipelineRuns: prs, Pipelines: ps, Namespaces: nsList})
prlo := logOpts(prName, ns, cs, fake.Streamer([]fake.Log{}), false, false)
output, err := fetchLogs(prlo)
if err != nil {
Expand All @@ -560,6 +643,14 @@ func TestLog_run_failed_with_and_without_follow(t *testing.T) {
failMessage = "Failed because I wanted"
)

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

prs := []*v1alpha1.PipelineRun{
tb.PipelineRun(prName, ns,
tb.PipelineRunLabel("tekton.dev/pipeline", prName),
Expand All @@ -581,7 +672,7 @@ func TestLog_run_failed_with_and_without_follow(t *testing.T) {
),
),
}
cs, _ := test.SeedTestData(t, pipelinetest.Data{PipelineRuns: prs, Pipelines: ps})
cs, _ := test.SeedTestData(t, pipelinetest.Data{PipelineRuns: prs, Pipelines: ps, Namespaces: nsList})

// follow mode disabled
prlo := logOpts(prName, ns, cs, fake.Streamer([]fake.Log{}), false, false)
Expand All @@ -608,6 +699,14 @@ func TestLog_pipeline_still_running(t *testing.T) {
taskName = "inprogress-task"
)

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

initialPRs := []*v1alpha1.PipelineRun{
tb.PipelineRun(prName, ns,
tb.PipelineRunLabel("tekton.dev/pipeline", prName),
Expand Down Expand Up @@ -651,7 +750,7 @@ func TestLog_pipeline_still_running(t *testing.T) {
),
),
}
cs, _ := test.SeedTestData(t, pipelinetest.Data{PipelineRuns: initialPRs, Pipelines: ps})
cs, _ := test.SeedTestData(t, pipelinetest.Data{PipelineRuns: initialPRs, Pipelines: ps, Namespaces: nsList})
watcher := watch.NewFake()
cs.Pipeline.PrependWatchReactor("pipelineruns", k8stest.DefaultWatchReactor(watcher, nil))
prlo := logOpts(prName, ns, cs, fake.Streamer([]fake.Log{}), false, false)
Expand All @@ -674,6 +773,14 @@ func TestLog_pipeline_status_done(t *testing.T) {
taskName = "done-task"
)

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

prs := []*v1alpha1.PipelineRun{
tb.PipelineRun(prName, ns,
tb.PipelineRunLabel("tekton.dev/pipeline", prName),
Expand All @@ -695,7 +802,7 @@ func TestLog_pipeline_status_done(t *testing.T) {
),
),
}
cs, _ := test.SeedTestData(t, pipelinetest.Data{PipelineRuns: prs, Pipelines: ps})
cs, _ := test.SeedTestData(t, pipelinetest.Data{PipelineRuns: prs, Pipelines: ps, Namespaces: nsList})
watcher := watch.NewFake()
cs.Pipeline.PrependWatchReactor("pipelineruns", k8stest.DefaultWatchReactor(watcher, nil))
prlo := logOpts(prName, ns, cs, fake.Streamer([]fake.Log{}), false, false)
Expand Down
8 changes: 7 additions & 1 deletion pkg/cmd/pipelinerun/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/helper/pods"
"github.com/tektoncd/cli/pkg/helper/pods/stream"
validate "github.com/tektoncd/cli/pkg/helper/validate"
)

type LogOptions struct {
Expand Down Expand Up @@ -59,11 +60,16 @@ func logCommand(p cli.Params) *cobra.Command {
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.PipelineRunName = args[0]

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

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

return opts.Run()
},
}
Expand Down

0 comments on commit 8743d54

Please sign in to comment.