-
Notifications
You must be signed in to change notification settings - Fork 824
Expand file tree
/
Copy pathautodetect_test.go
More file actions
200 lines (165 loc) · 4.63 KB
/
Copy pathautodetect_test.go
File metadata and controls
200 lines (165 loc) · 4.63 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package autodetect
import (
"context"
"errors"
"testing"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
)
type testDetector struct {
schemaURL string
attr []attribute.KeyValue
err error
}
var _ resource.Detector = (*testDetector)(nil)
func testFactory() func() resource.Detector {
return func() resource.Detector { return &testDetector{} }
}
func (d *testDetector) Detect(context.Context) (*resource.Resource, error) {
return resource.NewWithAttributes(d.schemaURL, d.attr...), d.err
}
func TestRegisterAndDetector(t *testing.T) {
id := ID("custom")
Register(id, testFactory())
detector, err := Detector(id)
if err != nil {
t.Fatalf("got error: %v, expected no error", err)
}
c, ok := detector.(*composite)
if !ok {
t.Errorf("got %T, expected composite detector", detector)
}
if len(c.detectors) != 1 {
t.Fatalf("got %d detectors, expected 1 detector", len(c.detectors))
}
if _, ok := c.detectors[0].(*testDetector); !ok {
t.Errorf("got %T, expected testDetector", c.detectors[0])
}
}
func TestRegisterDuplicate(t *testing.T) {
id := ID("duplicate")
Register(id, testFactory())
defer func() {
if r := recover(); r == nil {
t.Errorf("got no panic, expected panic for duplicate registration")
}
}()
Register(id, testFactory())
}
func TestParse(t *testing.T) {
ids := make([]ID, 0, len(registry))
for id := range registry {
ids = append(ids, id)
}
detector, err := Detector(ids...)
if err != nil {
t.Fatalf("got error: %v, expected no error", err)
}
c, ok := detector.(*composite)
if !ok {
t.Errorf("got %T, expected composite detector", detector)
}
if len(c.detectors) != len(registry) {
t.Errorf("got %d detectors, expected %d detectors", len(c.detectors), len(registry))
}
}
func TestParseUnknown(t *testing.T) {
_, err := Detector(ID("unknown"))
if !errors.Is(err, ErrUnknownDetector) {
t.Errorf("got %v, expected ErrUnknownDetector", err)
}
}
func TestIBMCloudVPCDetectorRegistered(t *testing.T) {
detector, err := Detector(IDIBMCloudVPC)
if err != nil {
t.Fatalf("got error: %v, expected no error", err)
}
c, ok := detector.(*composite)
if !ok {
t.Fatalf("got %T, expected composite detector", detector)
}
if len(c.detectors) != 1 {
t.Fatalf("got %d detectors, expected 1 detector", len(c.detectors))
}
}
func TestKubeadmDetectorRegistered(t *testing.T) {
detector, err := Detector(IDKubeadm)
if err != nil {
t.Fatalf("got error: %v, expected no error", err)
}
c, ok := detector.(*composite)
if !ok {
t.Fatalf("got %T, expected composite detector", detector)
}
if len(c.detectors) != 1 {
t.Fatalf("got %d detectors, expected 1 detector", len(c.detectors))
}
}
func TestOptDetectorDetect(t *testing.T) {
want := attribute.String("key", "value")
opt := resource.WithAttributes(want)
detector := optDetector{opt: opt}
res, err := detector.Detect(t.Context())
if err != nil {
t.Fatalf("got error: %v, expected no error", err)
}
if res == nil {
t.Errorf("got nil resource, expected non-nil resource")
}
got := res.Attributes()
if len(got) != 1 || got[0] != want {
t.Errorf("got %v, expected %v", got, []attribute.KeyValue{want})
}
}
func TestCompositeDetect(t *testing.T) {
a, b := attribute.Int("a", 0), attribute.Int("b", 0)
knownErr := errors.New("known error")
detectors := []resource.Detector{
&testDetector{attr: []attribute.KeyValue{a}},
&testDetector{
attr: []attribute.KeyValue{b},
err: knownErr,
},
}
comp := newComposite(detectors)
res, err := comp.Detect(t.Context())
if !errors.Is(err, knownErr) {
t.Errorf("got error %v, expected %v", err, knownErr)
}
if res == nil {
t.Errorf("got nil resource, expected non-nil resource")
}
got := res.Attributes()
if len(got) != 2 {
t.Fatalf("got %d attributes, expected 2 attributes", len(got))
}
if got[0].Key != a.Key && got[1].Key != a.Key {
t.Errorf("got %v, expected attribute %s", got, a.Key)
}
if got[0].Key != b.Key && got[1].Key != b.Key {
t.Errorf("got %v, expected attribute %s", got, b.Key)
}
}
func TestCompositeDetectMergeError(t *testing.T) {
a, b := attribute.Int("a", 0), attribute.Int("b", 0)
detectors := []resource.Detector{
&testDetector{
schemaURL: "a",
attr: []attribute.KeyValue{a},
},
&testDetector{
schemaURL: "b",
attr: []attribute.KeyValue{b},
},
}
comp := newComposite(detectors)
res, err := comp.Detect(t.Context())
if !errors.Is(err, resource.ErrSchemaURLConflict) {
t.Errorf("got error %v, expected %v", err, resource.ErrSchemaURLConflict)
}
if res != nil {
t.Error("got non-nil resource, expected nil resource")
}
}