/
tracing_test_helper.go
115 lines (99 loc) · 3.8 KB
/
tracing_test_helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package helpers
import (
"context"
"net/http"
"testing"
"time"
cetest "github.com/cloudevents/sdk-go/v2/test"
"github.com/openzipkin/zipkin-go/model"
"go.opentelemetry.io/otel/api/trace"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/test/zipkin"
tracinghelper "knative.dev/eventing/test/conformance/helpers/tracing"
testlib "knative.dev/eventing/test/lib"
"knative.dev/eventing/test/lib/recordevents"
)
// SetupTracingTestInfrastructureFunc sets up the infrastructure for running tracing tests. It returns the
// expected trace as well as a string that is expected to be in the logger Pod's logs.
type SetupTracingTestInfrastructureFunc func(
ctx context.Context,
t *testing.T,
channel *metav1.TypeMeta,
client *testlib.Client,
loggerPodName string,
senderPublishTrace bool,
) (tracinghelper.TestSpanTree, cetest.EventMatcher)
// tracingTest bootstraps the test and then executes the assertions on the received event and on the spans
func tracingTest(
ctx context.Context,
t *testing.T,
setupClient testlib.SetupClientOption,
setupInfrastructure SetupTracingTestInfrastructureFunc,
channel metav1.TypeMeta,
) {
const (
recordEventsPodName = "recordevents"
)
client := testlib.Setup(t, true, setupClient)
defer testlib.TearDown(client)
// Do NOT call zipkin.CleanupZipkinTracingSetup. That will be called exactly once in
// TestMain.
tracinghelper.Setup(t, client)
// Start the event info store. Note this is done _before_ we setup the infrastructure, which
// sends the event.
targetTracker, err := recordevents.NewEventInfoStore(client, recordEventsPodName, client.Namespace)
if err != nil {
t.Fatal("Pod tracker failed:", err)
}
// Setup the test infrastructure
expectedTestSpan, eventMatcher := setupInfrastructure(ctx, t, &channel, client, recordEventsPodName, true)
// Assert that the event was seen.
matches := targetTracker.AssertAtLeast(1, recordevents.MatchEvent(eventMatcher))
// Match the trace
traceID := getTraceIDHeader(t, matches)
trace, err := zipkin.JSONTracePred(traceID, 5*time.Minute, func(trace []model.SpanModel) bool {
tree, err := tracinghelper.GetTraceTree(trace)
if err != nil {
return false
}
// Do not pass t to prevent unnecessary log output.
return len(expectedTestSpan.MatchesSubtree(nil, tree)) > 0
})
if err != nil {
t.Logf("Unable to get trace %q: %v. Trace so far %+v", traceID, err, tracinghelper.PrettyPrintTrace(trace))
tree, err := tracinghelper.GetTraceTree(trace)
if err != nil {
t.Fatal(err)
}
if len(expectedTestSpan.MatchesSubtree(t, tree)) == 0 {
t.Fatalf("No matching subtree. want: %v got: %v", expectedTestSpan, tree)
}
}
}
// getTraceIDHeader gets the TraceID from the passed in events. It returns the header from the
// first matching event, but registers a fatal error if more than one traceid header is seen
// in that message.
func getTraceIDHeader(t *testing.T, evInfos []recordevents.EventInfo) string {
for i := range evInfos {
if nil != evInfos[i].HTTPHeaders {
sc := trace.RemoteSpanContextFromContext(trace.DefaultHTTPPropagator().Extract(context.TODO(), http.Header(evInfos[i].HTTPHeaders)))
if sc.HasTraceID() {
return sc.TraceIDString()
}
}
}
t.Fatalf("FAIL: No traceid in %d messages: (%v)", len(evInfos), evInfos)
return ""
}