Skip to content

Commit

Permalink
Resolved the logs issue, unit tests pending
Browse files Browse the repository at this point in the history
  • Loading branch information
Senjuti256 committed Mar 19, 2024
1 parent 464ff8e commit e3a557b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/cmd/tkn_pipeline_logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
4 changes: 4 additions & 0 deletions docs/man/man1/tkn-pipeline-logs.1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/pipeline/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
69 changes: 68 additions & 1 deletion pkg/cmd/pipeline/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
},
{
Expand All @@ -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",
},
{
Expand All @@ -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",
},
{
Expand All @@ -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",
},
{
Expand All @@ -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",
},
{
Expand All @@ -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",
},
{
Expand All @@ -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)
Expand Down Expand Up @@ -437,6 +466,7 @@ func TestPipelineLog(t *testing.T) {
dynamic dynamic.Interface
input pipelinetest.Clients
wantError bool
prefixing bool
want string
}{
{
Expand All @@ -446,6 +476,7 @@ func TestPipelineLog(t *testing.T) {
dynamic: dc,
input: cs,
wantError: false,
prefixing: true,
want: "No Pipelines found in namespace invalid",
},
{
Expand All @@ -455,6 +486,7 @@ func TestPipelineLog(t *testing.T) {
dynamic: dc,
input: cs,
wantError: false,
prefixing: true,
want: "No Pipelines found in namespace ns",
},
{
Expand All @@ -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",
},
{
Expand All @@ -473,6 +506,7 @@ func TestPipelineLog(t *testing.T) {
dynamic: dc2,
input: cs2,
wantError: true,
prefixing: true,
want: "pipelines.tekton.dev \"pipeline\" not found",
},
{
Expand All @@ -482,6 +516,7 @@ func TestPipelineLog(t *testing.T) {
dynamic: dc,
input: cs,
wantError: true,
prefixing: true,
want: "pipelineruns.tekton.dev \"pipelinerun\" not found",
},
{
Expand All @@ -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",
},
{
Expand All @@ -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 {
Expand All @@ -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...)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e3a557b

Please sign in to comment.