From 1a2365e5d267fa04cd4dad85c6b35ef4e7d414dc Mon Sep 17 00:00:00 2001 From: Senjuti De Date: Thu, 1 Feb 2024 20:24:36 +0530 Subject: [PATCH] Resolved the logs issue, unit tests pending --- docs/cmd/tkn_pipeline_logs.md | 1 + docs/man/man1/tkn-pipeline-logs.1 | 4 ++ pkg/cmd/pipeline/logs.go | 2 +- pkg/cmd/pipeline/logs_test.go | 69 ++++++++++++++++++++++++++++++- 4 files changed, 74 insertions(+), 2 deletions(-) diff --git a/docs/cmd/tkn_pipeline_logs.md b/docs/cmd/tkn_pipeline_logs.md index 6df6e856e1..7be16f2a5c 100644 --- a/docs/cmd/tkn_pipeline_logs.md +++ b/docs/cmd/tkn_pipeline_logs.md @@ -40,6 +40,7 @@ Show logs for given Pipeline and PipelineRun: -h, --help help for logs -L, --last show logs for last PipelineRun --limit int lists number of PipelineRuns (default 5) + --prefix prefix each log line with the log source (task name and step name) (default true) -t, --timestamps show logs with timestamp ``` diff --git a/docs/man/man1/tkn-pipeline-logs.1 b/docs/man/man1/tkn-pipeline-logs.1 index e8676ff797..df89e77ddd 100644 --- a/docs/man/man1/tkn-pipeline-logs.1 +++ b/docs/man/man1/tkn-pipeline-logs.1 @@ -39,6 +39,10 @@ Show Pipeline logs \fB\-\-limit\fP=5 lists number of PipelineRuns +.PP +\fB\-\-prefix\fP[=true] + prefix each log line with the log source (task name and step name) + .PP \fB\-t\fP, \fB\-\-timestamps\fP[=false] show logs with timestamp diff --git a/pkg/cmd/pipeline/logs.go b/pkg/cmd/pipeline/logs.go index d7b6cb26d5..68541384c3 100644 --- a/pkg/cmd/pipeline/logs.go +++ b/pkg/cmd/pipeline/logs.go @@ -97,7 +97,7 @@ Show logs for given Pipeline and PipelineRun: c.Flags().BoolVarP(&opts.Follow, "follow", "f", false, "stream live logs") c.Flags().BoolVarP(&opts.Timestamps, "timestamps", "t", false, "show logs with timestamp") c.Flags().IntVarP(&opts.Limit, "limit", "", 5, "lists number of PipelineRuns") - + c.Flags().BoolVarP(&opts.Prefixing, "prefix", "", true, "prefix each log line with the log source (task name and step name)") return c } diff --git a/pkg/cmd/pipeline/logs_test.go b/pkg/cmd/pipeline/logs_test.go index c5b64df510..680117a39b 100644 --- a/pkg/cmd/pipeline/logs_test.go +++ b/pkg/cmd/pipeline/logs_test.go @@ -200,7 +200,8 @@ func TestPipelineLog_v1beta1(t *testing.T) { dynamic dynamic.Interface input test.Clients wantError bool - want string + prefixing bool + want string }{ { name: "Invalid namespace", @@ -209,6 +210,7 @@ func TestPipelineLog_v1beta1(t *testing.T) { dynamic: dc, input: cs, wantError: false, + prefixing: true, want: "No Pipelines found in namespace invalid", }, { @@ -218,6 +220,7 @@ func TestPipelineLog_v1beta1(t *testing.T) { dynamic: dc, input: cs, wantError: false, + prefixing: true, want: "No Pipelines found in namespace ns", }, { @@ -227,6 +230,7 @@ func TestPipelineLog_v1beta1(t *testing.T) { dynamic: dc2, input: cs2, wantError: false, + prefixing: true, want: "No PipelineRuns found for Pipeline output-pipeline", }, { @@ -236,6 +240,7 @@ func TestPipelineLog_v1beta1(t *testing.T) { dynamic: dc2, input: cs2, wantError: true, + prefixing: true, want: "pipelines.tekton.dev \"pipeline\" not found", }, { @@ -245,6 +250,7 @@ func TestPipelineLog_v1beta1(t *testing.T) { dynamic: dc, input: cs, wantError: true, + prefixing: true, want: "pipelineruns.tekton.dev \"pipelinerun\" not found", }, { @@ -254,6 +260,7 @@ func TestPipelineLog_v1beta1(t *testing.T) { dynamic: dc2, input: cs2, wantError: true, + prefixing: true, want: "limit was -1 but must be a positive number", }, { @@ -263,12 +270,34 @@ func TestPipelineLog_v1beta1(t *testing.T) { dynamic: dc3, input: cs3, wantError: false, + prefixing: true, want: "", }, + { + name: "Prefixing enabled for Pipelines", + command: []string{"logs", prName, "--prefix=true"}, + namespace: "", + dynamic: dc3, + input: cs3, + wantError: false, + prefixing: true, + want: "[output-pipeline-run] No logs available\n", + }, + { + name: "Prefixing disabled for Pipelines", + command: []string{"logs", prName, "--prefix=false"}, + namespace: "", + dynamic: dc3, + input: cs3, + wantError: false, + prefixing: false, + want: "No logs available\n", + }, } for _, tp := range testParams { t.Run(tp.name, func(t *testing.T) { + p := &test.Params{Tekton: tp.input.Pipeline, Clock: clock, Kube: tp.input.Kube, Dynamic: tp.dynamic} if tp.namespace != "" { p.SetNamespace(tp.namespace) @@ -437,6 +466,7 @@ func TestPipelineLog(t *testing.T) { dynamic dynamic.Interface input pipelinetest.Clients wantError bool + prefixing bool want string }{ { @@ -446,6 +476,7 @@ func TestPipelineLog(t *testing.T) { dynamic: dc, input: cs, wantError: false, + prefixing: true, want: "No Pipelines found in namespace invalid", }, { @@ -455,6 +486,7 @@ func TestPipelineLog(t *testing.T) { dynamic: dc, input: cs, wantError: false, + prefixing: true, want: "No Pipelines found in namespace ns", }, { @@ -464,6 +496,7 @@ func TestPipelineLog(t *testing.T) { dynamic: dc2, input: cs2, wantError: false, + prefixing: true, want: "No PipelineRuns found for Pipeline output-pipeline", }, { @@ -473,6 +506,7 @@ func TestPipelineLog(t *testing.T) { dynamic: dc2, input: cs2, wantError: true, + prefixing: true, want: "pipelines.tekton.dev \"pipeline\" not found", }, { @@ -482,6 +516,7 @@ func TestPipelineLog(t *testing.T) { dynamic: dc, input: cs, wantError: true, + prefixing: true, want: "pipelineruns.tekton.dev \"pipelinerun\" not found", }, { @@ -491,6 +526,7 @@ func TestPipelineLog(t *testing.T) { dynamic: dc2, input: cs2, wantError: true, + prefixing: true, want: "limit was -1 but must be a positive number", }, { @@ -500,8 +536,29 @@ func TestPipelineLog(t *testing.T) { dynamic: dc3, input: cs3, wantError: false, + prefixing: true, want: "", }, + { + name: "Prefixing enabled for Pipelines", + command: []string{"logs", prName2, "--prefix=true"}, + namespace: "", + dynamic: dc3, + input: cs3, + wantError: false, + prefixing: true, + want: "[output-pipeline-run] No logs available", + }, + { + name: "Prefixing disabled for Pipelines", + command: []string{"logs", prName2, "--prefix=false"}, + namespace: "", + dynamic: dc3, + input: cs3, + wantError: false, + prefixing: false, + want: "No logs available", + }, } for _, tp := range testParams { @@ -510,6 +567,7 @@ func TestPipelineLog(t *testing.T) { if tp.namespace != "" { p.SetNamespace(tp.namespace) } + c := Command(p) out, err := test.ExecuteCommand(c, tp.command...) @@ -1410,6 +1468,15 @@ func TestLogs_Auto_Select_FirstPipeline(t *testing.T) { Limit: 5, Params: &p, } + + // Set the prefixing flag to true or false as needed + prefixing := true + + // Set the prefix flag based on the prefixing flag + if prefixing { + lopt.Prefixing = true + } + err = getAllInputs(lopt) if err != nil { t.Errorf("Unexpected error: %v", err)