| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package logtest |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
|
| 14 |
"go.opentelemetry.io/otel/log" |
| 15 |
) |
| 16 |
|
| 17 |
var y2k = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC) |
| 18 |
|
| 19 |
// Compile-time check to ensure testing structs implement TestingT. |
| 20 |
var ( |
| 21 |
_ TestingT = (*testing.T)(nil) |
| 22 |
_ TestingT = (*testing.B)(nil) |
| 23 |
_ TestingT = (*testing.F)(nil) |
| 24 |
_ TestingT = (*mockTestingT)(nil) |
| 25 |
) |
| 26 |
|
| 27 |
type mockTestingT struct { |
| 28 |
errors []string |
| 29 |
} |
| 30 |
|
| 31 |
func (m *mockTestingT) Errorf(format string, args ...any) { |
| 32 |
m.errors = append(m.errors, fmt.Sprintf(format, args...)) |
| 33 |
} |
| 34 |
|
| 35 |
func TestAssertEqual(t *testing.T) { |
| 36 |
a := Recording{ |
| 37 |
Scope{Name: t.Name()}: []Record{ |
| 38 |
{Body: log.StringValue("msg"), Attributes: []log.KeyValue{log.String("foo", "bar"), log.Int("n", 1)}}, |
| 39 |
}, |
| 40 |
} |
| 41 |
b := Recording{ |
| 42 |
Scope{Name: t.Name()}: []Record{ |
| 43 |
{Body: log.StringValue("msg"), Attributes: []log.KeyValue{log.Int("n", 1), log.String("foo", "bar")}}, |
| 44 |
}, |
| 45 |
} |
| 46 |
|
| 47 |
got := AssertEqual(t, a, b) |
| 48 |
assert.True(t, got, "expected recordings to be equal") |
| 49 |
} |
| 50 |
|
| 51 |
func TestAssertEqualRecording(t *testing.T) { |
| 52 |
tests := []struct { |
| 53 |
name string |
| 54 |
a Recording |
| 55 |
b Recording |
| 56 |
opts []AssertOption |
| 57 |
want bool |
| 58 |
}{ |
| 59 |
{ |
| 60 |
name: "equal recordings", |
| 61 |
a: Recording{ |
| 62 |
Scope{Name: t.Name()}: []Record{ |
| 63 |
{ |
| 64 |
Timestamp: y2k, |
| 65 |
Context: t.Context(), |
| 66 |
Attributes: []log.KeyValue{log.Int("n", 1), log.String("foo", "bar")}, |
| 67 |
}, |
| 68 |
}, |
| 69 |
}, |
| 70 |
b: Recording{ |
| 71 |
Scope{Name: t.Name()}: []Record{ |
| 72 |
{ |
| 73 |
Timestamp: y2k, |
| 74 |
Context: t.Context(), |
| 75 |
Attributes: []log.KeyValue{log.String("foo", "bar"), log.Int("n", 1)}, |
| 76 |
}, |
| 77 |
}, |
| 78 |
}, |
| 79 |
want: true, |
| 80 |
}, |
| 81 |
{ |
| 82 |
name: "different recordings", |
| 83 |
a: Recording{ |
| 84 |
Scope{Name: t.Name()}: []Record{ |
| 85 |
{Attributes: []log.KeyValue{log.String("foo", "bar")}}, |
| 86 |
}, |
| 87 |
}, |
| 88 |
b: Recording{ |
| 89 |
Scope{Name: t.Name()}: []Record{ |
| 90 |
{Attributes: []log.KeyValue{log.Int("n", 1)}}, |
| 91 |
}, |
| 92 |
}, |
| 93 |
want: false, |
| 94 |
}, |
| 95 |
{ |
| 96 |
name: "equal empty scopes", |
| 97 |
a: Recording{ |
| 98 |
Scope{Name: t.Name()}: nil, |
| 99 |
}, |
| 100 |
b: Recording{ |
| 101 |
Scope{Name: t.Name()}: []Record{}, |
| 102 |
}, |
| 103 |
want: true, |
| 104 |
}, |
| 105 |
{ |
| 106 |
name: "equal empty attributes", |
| 107 |
a: Recording{ |
| 108 |
Scope{Name: t.Name()}: []Record{ |
| 109 |
{Body: log.StringValue("msg"), Attributes: []log.KeyValue{}}, |
| 110 |
}, |
| 111 |
}, |
| 112 |
b: Recording{ |
| 113 |
Scope{Name: t.Name()}: []Record{ |
| 114 |
{Body: log.StringValue("msg"), Attributes: nil}, |
| 115 |
}, |
| 116 |
}, |
| 117 |
want: true, |
| 118 |
}, |
| 119 |
} |
| 120 |
|
| 121 |
for _, tc := range tests { |
| 122 |
t.Run(tc.name, func(t *testing.T) { |
| 123 |
mockT := &mockTestingT{} |
| 124 |
result := AssertEqual(mockT, tc.a, tc.b, tc.opts...) |
| 125 |
if result != tc.want { |
| 126 |
t.Errorf("AssertEqual() = %v, want %v", result, tc.want) |
| 127 |
} |
| 128 |
if !tc.want && len(mockT.errors) == 0 { |
| 129 |
t.Errorf("expected Errorf call but got none") |
| 130 |
} |
| 131 |
}) |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
func TestAssertEqualRecord(t *testing.T) { |
| 136 |
tests := []struct { |
| 137 |
name string |
| 138 |
a Record |
| 139 |
b Record |
| 140 |
opts []AssertOption |
| 141 |
want bool |
| 142 |
}{ |
| 143 |
{ |
| 144 |
name: "equal records", |
| 145 |
a: Record{ |
| 146 |
Timestamp: y2k, |
| 147 |
Context: t.Context(), |
| 148 |
Attributes: []log.KeyValue{log.Int("n", 1), log.String("foo", "bar")}, |
| 149 |
}, |
| 150 |
b: Record{ |
| 151 |
Timestamp: y2k, |
| 152 |
Context: t.Context(), |
| 153 |
Attributes: []log.KeyValue{log.String("foo", "bar"), log.Int("n", 1)}, |
| 154 |
}, |
| 155 |
want: true, |
| 156 |
}, |
| 157 |
{ |
| 158 |
name: "different records", |
| 159 |
a: Record{ |
| 160 |
Attributes: []log.KeyValue{log.String("foo", "bar")}, |
| 161 |
}, |
| 162 |
b: Record{ |
| 163 |
Attributes: []log.KeyValue{log.Int("n", 1)}, |
| 164 |
}, |
| 165 |
want: false, |
| 166 |
}, |
| 167 |
{ |
| 168 |
name: "Transform to ignore timestamps", |
| 169 |
a: Record{ |
| 170 |
Attributes: []log.KeyValue{log.Int("n", 1), log.String("foo", "bar")}, |
| 171 |
}, |
| 172 |
b: Record{ |
| 173 |
Timestamp: y2k, |
| 174 |
Attributes: []log.KeyValue{log.String("foo", "bar"), log.Int("n", 1)}, |
| 175 |
}, |
| 176 |
opts: []AssertOption{ |
| 177 |
Transform(func(time.Time) time.Time { |
| 178 |
return time.Time{} |
| 179 |
}), |
| 180 |
}, |
| 181 |
want: true, |
| 182 |
}, |
| 183 |
} |
| 184 |
|
| 185 |
for _, tc := range tests { |
| 186 |
t.Run(tc.name, func(t *testing.T) { |
| 187 |
mockT := &mockTestingT{} |
| 188 |
result := AssertEqual(mockT, tc.a, tc.b, tc.opts...) |
| 189 |
if result != tc.want { |
| 190 |
t.Errorf("AssertEqual() = %v, want %v", result, tc.want) |
| 191 |
} |
| 192 |
if !tc.want && len(mockT.errors) == 0 { |
| 193 |
t.Errorf("expected Errorf call but got none") |
| 194 |
} |
| 195 |
}) |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
func TestDesc(t *testing.T) { |
| 200 |
mockT := &mockTestingT{} |
| 201 |
a := Record{ |
| 202 |
Attributes: []log.KeyValue{log.String("foo", "bar")}, |
| 203 |
} |
| 204 |
b := Record{ |
| 205 |
Attributes: []log.KeyValue{log.Int("n", 1)}, |
| 206 |
} |
| 207 |
|
| 208 |
AssertEqual(mockT, a, b, Desc("custom message, %s", "test")) |
| 209 |
|
| 210 |
require.Len(t, mockT.errors, 1, "expected one error") |
| 211 |
assert.Contains(t, mockT.errors[0], "custom message, test\n", "expected custom message") |
| 212 |
} |