Skip to content

Commit e3a557b

Browse files
committed
Resolved the logs issue, unit tests pending
1 parent 464ff8e commit e3a557b

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

docs/cmd/tkn_pipeline_logs.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Show logs for given Pipeline and PipelineRun:
4040
-h, --help help for logs
4141
-L, --last show logs for last PipelineRun
4242
--limit int lists number of PipelineRuns (default 5)
43+
--prefix prefix each log line with the log source (task name and step name) (default true)
4344
-t, --timestamps show logs with timestamp
4445
```
4546

docs/man/man1/tkn-pipeline-logs.1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ Show Pipeline logs
3939
\fB\-\-limit\fP=5
4040
lists number of PipelineRuns
4141

42+
.PP
43+
\fB\-\-prefix\fP[=true]
44+
prefix each log line with the log source (task name and step name)
45+
4246
.PP
4347
\fB\-t\fP, \fB\-\-timestamps\fP[=false]
4448
show logs with timestamp

pkg/cmd/pipeline/logs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Show logs for given Pipeline and PipelineRun:
9797
c.Flags().BoolVarP(&opts.Follow, "follow", "f", false, "stream live logs")
9898
c.Flags().BoolVarP(&opts.Timestamps, "timestamps", "t", false, "show logs with timestamp")
9999
c.Flags().IntVarP(&opts.Limit, "limit", "", 5, "lists number of PipelineRuns")
100-
100+
c.Flags().BoolVarP(&opts.Prefixing, "prefix", "", true, "prefix each log line with the log source (task name and step name)")
101101
return c
102102
}
103103

pkg/cmd/pipeline/logs_test.go

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ func TestPipelineLog_v1beta1(t *testing.T) {
200200
dynamic dynamic.Interface
201201
input test.Clients
202202
wantError bool
203-
want string
203+
prefixing bool
204+
want string
204205
}{
205206
{
206207
name: "Invalid namespace",
@@ -209,6 +210,7 @@ func TestPipelineLog_v1beta1(t *testing.T) {
209210
dynamic: dc,
210211
input: cs,
211212
wantError: false,
213+
prefixing: true,
212214
want: "No Pipelines found in namespace invalid",
213215
},
214216
{
@@ -218,6 +220,7 @@ func TestPipelineLog_v1beta1(t *testing.T) {
218220
dynamic: dc,
219221
input: cs,
220222
wantError: false,
223+
prefixing: true,
221224
want: "No Pipelines found in namespace ns",
222225
},
223226
{
@@ -227,6 +230,7 @@ func TestPipelineLog_v1beta1(t *testing.T) {
227230
dynamic: dc2,
228231
input: cs2,
229232
wantError: false,
233+
prefixing: true,
230234
want: "No PipelineRuns found for Pipeline output-pipeline",
231235
},
232236
{
@@ -236,6 +240,7 @@ func TestPipelineLog_v1beta1(t *testing.T) {
236240
dynamic: dc2,
237241
input: cs2,
238242
wantError: true,
243+
prefixing: true,
239244
want: "pipelines.tekton.dev \"pipeline\" not found",
240245
},
241246
{
@@ -245,6 +250,7 @@ func TestPipelineLog_v1beta1(t *testing.T) {
245250
dynamic: dc,
246251
input: cs,
247252
wantError: true,
253+
prefixing: true,
248254
want: "pipelineruns.tekton.dev \"pipelinerun\" not found",
249255
},
250256
{
@@ -254,6 +260,7 @@ func TestPipelineLog_v1beta1(t *testing.T) {
254260
dynamic: dc2,
255261
input: cs2,
256262
wantError: true,
263+
prefixing: true,
257264
want: "limit was -1 but must be a positive number",
258265
},
259266
{
@@ -263,12 +270,34 @@ func TestPipelineLog_v1beta1(t *testing.T) {
263270
dynamic: dc3,
264271
input: cs3,
265272
wantError: false,
273+
prefixing: true,
266274
want: "",
267275
},
276+
{
277+
name: "Prefixing enabled for Pipelines",
278+
command: []string{"logs", prName, "--prefix=true"},
279+
namespace: "",
280+
dynamic: dc3,
281+
input: cs3,
282+
wantError: false,
283+
prefixing: true,
284+
want: "[output-pipeline-run] No logs available\n",
285+
},
286+
{
287+
name: "Prefixing disabled for Pipelines",
288+
command: []string{"logs", prName, "--prefix=false"},
289+
namespace: "",
290+
dynamic: dc3,
291+
input: cs3,
292+
wantError: false,
293+
prefixing: false,
294+
want: "No logs available\n",
295+
},
268296
}
269297

270298
for _, tp := range testParams {
271299
t.Run(tp.name, func(t *testing.T) {
300+
272301
p := &test.Params{Tekton: tp.input.Pipeline, Clock: clock, Kube: tp.input.Kube, Dynamic: tp.dynamic}
273302
if tp.namespace != "" {
274303
p.SetNamespace(tp.namespace)
@@ -437,6 +466,7 @@ func TestPipelineLog(t *testing.T) {
437466
dynamic dynamic.Interface
438467
input pipelinetest.Clients
439468
wantError bool
469+
prefixing bool
440470
want string
441471
}{
442472
{
@@ -446,6 +476,7 @@ func TestPipelineLog(t *testing.T) {
446476
dynamic: dc,
447477
input: cs,
448478
wantError: false,
479+
prefixing: true,
449480
want: "No Pipelines found in namespace invalid",
450481
},
451482
{
@@ -455,6 +486,7 @@ func TestPipelineLog(t *testing.T) {
455486
dynamic: dc,
456487
input: cs,
457488
wantError: false,
489+
prefixing: true,
458490
want: "No Pipelines found in namespace ns",
459491
},
460492
{
@@ -464,6 +496,7 @@ func TestPipelineLog(t *testing.T) {
464496
dynamic: dc2,
465497
input: cs2,
466498
wantError: false,
499+
prefixing: true,
467500
want: "No PipelineRuns found for Pipeline output-pipeline",
468501
},
469502
{
@@ -473,6 +506,7 @@ func TestPipelineLog(t *testing.T) {
473506
dynamic: dc2,
474507
input: cs2,
475508
wantError: true,
509+
prefixing: true,
476510
want: "pipelines.tekton.dev \"pipeline\" not found",
477511
},
478512
{
@@ -482,6 +516,7 @@ func TestPipelineLog(t *testing.T) {
482516
dynamic: dc,
483517
input: cs,
484518
wantError: true,
519+
prefixing: true,
485520
want: "pipelineruns.tekton.dev \"pipelinerun\" not found",
486521
},
487522
{
@@ -491,6 +526,7 @@ func TestPipelineLog(t *testing.T) {
491526
dynamic: dc2,
492527
input: cs2,
493528
wantError: true,
529+
prefixing: true,
494530
want: "limit was -1 but must be a positive number",
495531
},
496532
{
@@ -500,8 +536,29 @@ func TestPipelineLog(t *testing.T) {
500536
dynamic: dc3,
501537
input: cs3,
502538
wantError: false,
539+
prefixing: true,
503540
want: "",
504541
},
542+
{
543+
name: "Prefixing enabled for Pipelines",
544+
command: []string{"logs", prName2, "--prefix=true"},
545+
namespace: "",
546+
dynamic: dc3,
547+
input: cs3,
548+
wantError: false,
549+
prefixing: true,
550+
want: "[output-pipeline-run] No logs available",
551+
},
552+
{
553+
name: "Prefixing disabled for Pipelines",
554+
command: []string{"logs", prName2, "--prefix=false"},
555+
namespace: "",
556+
dynamic: dc3,
557+
input: cs3,
558+
wantError: false,
559+
prefixing: false,
560+
want: "No logs available",
561+
},
505562
}
506563

507564
for _, tp := range testParams {
@@ -510,6 +567,7 @@ func TestPipelineLog(t *testing.T) {
510567
if tp.namespace != "" {
511568
p.SetNamespace(tp.namespace)
512569
}
570+
513571
c := Command(p)
514572

515573
out, err := test.ExecuteCommand(c, tp.command...)
@@ -1410,6 +1468,15 @@ func TestLogs_Auto_Select_FirstPipeline(t *testing.T) {
14101468
Limit: 5,
14111469
Params: &p,
14121470
}
1471+
1472+
// Set the prefixing flag to true or false as needed
1473+
prefixing := true
1474+
1475+
// Set the prefix flag based on the prefixing flag
1476+
if prefixing {
1477+
lopt.Prefixing = true
1478+
}
1479+
14131480
err = getAllInputs(lopt)
14141481
if err != nil {
14151482
t.Errorf("Unexpected error: %v", err)

0 commit comments

Comments
 (0)