Skip to content

Commit

Permalink
Fix test cases related to tracing
Browse files Browse the repository at this point in the history
Signed-off-by: Jayadeep KM <kmjayadeep@gmail.com>
  • Loading branch information
kmjayadeep authored and tekton-robot committed Jan 23, 2023
1 parent bcbe2ea commit db5e021
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 47 deletions.
41 changes: 17 additions & 24 deletions pkg/reconciler/pipelinerun/tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package pipelinerun

import (
"context"
"encoding/json"
"testing"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
Expand All @@ -33,8 +32,8 @@ func TestInitTracing(t *testing.T) {
name string
pipelineRun *v1beta1.PipelineRun
tracerProvider trace.TracerProvider
expectAnnotations bool
expectSpanContext bool
expectSpanContextStatus bool
expectValidSpanContext bool
parentTraceID string
}{{
name: "with-tracerprovider-no-parent-trace",
Expand All @@ -45,8 +44,8 @@ func TestInitTracing(t *testing.T) {
},
},
tracerProvider: tracesdk.NewTracerProvider(),
expectAnnotations: true,
expectSpanContext: true,
expectSpanContextStatus: true,
expectValidSpanContext: true,
}, {
name: "with-tracerprovider-with-parent-trace",
pipelineRun: &v1beta1.PipelineRun{
Expand All @@ -59,8 +58,8 @@ func TestInitTracing(t *testing.T) {
},
},
tracerProvider: tracesdk.NewTracerProvider(),
expectAnnotations: true,
expectSpanContext: true,
expectSpanContextStatus: true,
expectValidSpanContext: true,
parentTraceID: "00-0f57e147e992b304d977436289d10628-73d5909e31793992-01",
}, {
name: "without-tracerprovider",
Expand All @@ -71,8 +70,8 @@ func TestInitTracing(t *testing.T) {
},
},
tracerProvider: trace.NewNoopTracerProvider(),
expectAnnotations: false,
expectSpanContext: false,
expectSpanContextStatus: false,
expectValidSpanContext: false,
}, {
name: "without-tracerprovider-existing-annotations",
pipelineRun: &v1beta1.PipelineRun{
Expand All @@ -85,8 +84,8 @@ func TestInitTracing(t *testing.T) {
},
},
tracerProvider: trace.NewNoopTracerProvider(),
expectAnnotations: true,
expectSpanContext: false,
expectSpanContextStatus: true,
expectValidSpanContext: false,
}}

for _, tc := range testcases {
Expand All @@ -98,31 +97,25 @@ func TestInitTracing(t *testing.T) {
t.Fatalf("returned nil context from initTracing")
}

if tc.expectAnnotations && pr.Annotations == nil {
t.Fatalf("annotations are empty after initializing tracing")
if tc.expectSpanContextStatus && pr.Status.SpanContext == nil {
t.Fatalf("spanContext is empty after initializing tracing")
}

if tc.expectSpanContext {
if _, e := pr.Annotations["tekton.dev/pipelinerunSpanContext"]; !e {
if tc.expectValidSpanContext {
if len(pr.Status.SpanContext) == 0 {
t.Fatalf("spanContext not added to annotations")
}

sc := pr.Annotations["tekton.dev/pipelinerunSpanContext"]
carrier := make(map[string]string)
err := json.Unmarshal([]byte(sc), &carrier)
if err != nil {
t.Errorf("Unable to unmarshal spancontext, err: %s", err)
}

parentID := carrier["traceparent"]
parentID := pr.Status.SpanContext["traceparent"]
if len(parentID) != 55 {
t.Errorf("invalid trace Id")
}

if tc.parentTraceID != "" && parentID != tc.parentTraceID {
t.Errorf("invalid trace Id propagated")
t.Errorf("invalid trace Id propagated, %s", parentID)
}
}

})

}
Expand Down
42 changes: 19 additions & 23 deletions pkg/reconciler/taskrun/tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package taskrun

import (
"context"
"encoding/json"
"testing"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
Expand All @@ -33,8 +32,8 @@ func TestInitTracing(t *testing.T) {
name string
taskRun *v1beta1.TaskRun
tracerProvider trace.TracerProvider
expectAnnotations bool
expectSpanContext bool
expectSpanContextStatus bool
expectValidSpanContext bool
parentTraceID string
}{{
name: "with-tracerprovider-no-parent-trace",
Expand All @@ -45,8 +44,8 @@ func TestInitTracing(t *testing.T) {
},
},
tracerProvider: tracesdk.NewTracerProvider(),
expectAnnotations: true,
expectSpanContext: true,
expectSpanContextStatus: true,
expectValidSpanContext: true,
}, {
name: "with-tracerprovider-with-parent-trace",
taskRun: &v1beta1.TaskRun{
Expand All @@ -59,8 +58,8 @@ func TestInitTracing(t *testing.T) {
},
},
tracerProvider: tracesdk.NewTracerProvider(),
expectAnnotations: true,
expectSpanContext: true,
expectSpanContextStatus: true,
expectValidSpanContext: true,
parentTraceID: "00-0f57e147e992b304d977436289d10628-73d5909e31793992-01",
}, {
name: "without-tracerprovider",
Expand All @@ -71,8 +70,8 @@ func TestInitTracing(t *testing.T) {
},
},
tracerProvider: trace.NewNoopTracerProvider(),
expectAnnotations: false,
expectSpanContext: false,
expectSpanContextStatus: false,
expectValidSpanContext: false,
}, {
name: "without-tracerprovider-existing-annotations",
taskRun: &v1beta1.TaskRun{
Expand All @@ -85,8 +84,8 @@ func TestInitTracing(t *testing.T) {
},
},
tracerProvider: trace.NewNoopTracerProvider(),
expectAnnotations: true,
expectSpanContext: false,
expectSpanContextStatus: true,
expectValidSpanContext: false,
}}

for _, tc := range testcases {
Expand All @@ -98,23 +97,20 @@ func TestInitTracing(t *testing.T) {
t.Fatalf("returned nil context from initTracing")
}

if tc.expectAnnotations && tr.Annotations == nil {
t.Fatalf("annotations are empty after initializing tracing")
if tc.expectSpanContextStatus && tr.Status.SpanContext == nil {
t.Fatalf("spanContext is empty after initializing tracing")
}

if tc.expectSpanContext {
if _, e := tr.Annotations["tekton.dev/taskrunSpanContext"]; !e {
t.Fatalf("spanContext not added to annotations")
}
if !tc.expectSpanContextStatus && len(tr.Status.SpanContext) > 0 {
t.Fatalf("spanContext is not empty")
}

sc := tr.Annotations["tekton.dev/taskrunSpanContext"]
carrier := make(map[string]string)
err := json.Unmarshal([]byte(sc), &carrier)
if err != nil {
t.Errorf("Unable to unmarshal spancontext, err: %s", err)
if tc.expectValidSpanContext {
if len(tr.Status.SpanContext) == 0 {
t.Fatalf("spanContext not added to annotations")
}

parentID := carrier["traceparent"]
parentID := tr.Status.SpanContext["traceparent"]
if len(parentID) != 55 {
t.Errorf("invalid trace Id")
}
Expand Down

0 comments on commit db5e021

Please sign in to comment.