/
decoratorTesting.go
103 lines (82 loc) · 2.67 KB
/
decoratorTesting.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
package logcontext
import (
"bytes"
"strings"
"testing"
)
// DecorationExpect defines the expected values a log decorated by a logcontext v2 decorator should have
type DecorationExpect struct {
DecorationDisabled bool
EntityGUID string
EntityName string
Hostname string
TraceID string
SpanID string
// decorator errors will result in an undecorated log message being printed
// and an error message also being printed in a separate line
DecoratorError error
}
// metadata indexes
const (
entityguid = 1
hostname = 2
traceid = 3
spanid = 4
)
func entityname(vals []string) string {
if len(vals) < 2 {
return ""
}
return vals[len(vals)-2]
}
// ValidateDecoratedOutput is a testing tool that validates whether a bytes buffer decorated by a
// logcontext v2 decorator contains the values we expect it to.
func ValidateDecoratedOutput(t *testing.T, out *bytes.Buffer, expect *DecorationExpect) {
actual := out.String()
if expect.DecorationDisabled {
if strings.Contains(actual, "NR-LINKING") {
t.Fatal("log decoration was expected to be disabled, but were decorated anyway")
} else {
return
}
}
if expect.DecoratorError != nil {
if strings.Contains(actual, "NR-LINKING") {
t.Fatal("logs should not be decorated when a decorator error occurs")
}
msg := expect.DecoratorError.Error()
if !strings.Contains(actual, msg) {
t.Fatalf("an error message debug log was expected, \"%s\", but was not found: %s", msg, actual)
} else {
return
}
}
split := strings.Split(actual, "NR-LINKING")
if len(split) != 2 {
t.Fatalf("expected log decoration, but NR-LINKING data was missing: %s", actual)
}
linkingData := strings.Split(split[1], "|")
if len(linkingData) < 5 {
t.Errorf("linking data is missing required fields: %s", split[1])
}
if linkingData[entityguid] != expect.EntityGUID {
t.Errorf("incorrect entity GUID; expect: %s actual: %s", expect.EntityGUID, linkingData[entityguid])
}
if linkingData[hostname] != expect.Hostname {
t.Errorf("incorrect hostname; expect: %s actual: %s", expect.Hostname, linkingData[hostname])
}
if entityname(linkingData) != expect.EntityName {
t.Errorf("incorrect entity name; expect: %s actual: %s", expect.EntityName, entityname(linkingData))
}
if expect.TraceID != "" && expect.SpanID != "" {
if len(linkingData) < 7 {
t.Errorf("transaction metadata is missing from linking data: %s", split[1])
}
if linkingData[traceid] != expect.TraceID {
t.Errorf("incorrect traceID; expect: %s actual: %s", expect.TraceID, linkingData[traceid])
}
if linkingData[spanid] != expect.SpanID {
t.Errorf("incorrect hostname; expect: %s actual: %s", expect.SpanID, linkingData[spanid])
}
}
}