@@ -10,14 +10,18 @@ import (
10
10
"time"
11
11
12
12
"github.com/go-logr/logr"
13
+ "github.com/google/uuid"
14
+ "github.com/open-telemetry/opamp-go/protobufs"
13
15
"github.com/stretchr/testify/assert"
16
+ "github.com/stretchr/testify/require"
14
17
)
15
18
16
19
func TestLoad (t * testing.T ) {
17
20
type args struct {
18
21
file string
19
22
envVariables map [string ]string
20
23
}
24
+ instanceId := uuid .New ()
21
25
tests := []struct {
22
26
name string
23
27
args args
@@ -30,6 +34,7 @@ func TestLoad(t *testing.T) {
30
34
file : "./testdata/agent.yaml" ,
31
35
},
32
36
want : & Config {
37
+ instanceId : instanceId ,
33
38
RootLogger : logr .Discard (),
34
39
Endpoint : "ws://127.0.0.1:4320/v1/opamp" ,
35
40
Capabilities : map [Capability ]bool {
@@ -55,6 +60,7 @@ func TestLoad(t *testing.T) {
55
60
file : "./testdata/agenthttpbasic.yaml" ,
56
61
},
57
62
want : & Config {
63
+ instanceId : instanceId ,
58
64
RootLogger : logr .Discard (),
59
65
Endpoint : "http://127.0.0.1:4320/v1/opamp" ,
60
66
HeartbeatInterval : 45 * time .Second ,
@@ -82,6 +88,7 @@ func TestLoad(t *testing.T) {
82
88
file : "./testdata/agentbasiccomponentsallowed.yaml" ,
83
89
},
84
90
want : & Config {
91
+ instanceId : instanceId ,
85
92
RootLogger : logr .Discard (),
86
93
Endpoint : "ws://127.0.0.1:4320/v1/opamp" ,
87
94
Capabilities : map [Capability ]bool {
@@ -133,6 +140,7 @@ func TestLoad(t *testing.T) {
133
140
},
134
141
},
135
142
want : & Config {
143
+ instanceId : instanceId ,
136
144
RootLogger : logr .Discard (),
137
145
Endpoint : "ws://127.0.0.1:4320/v1/opamp" ,
138
146
Headers : map [string ]string {
@@ -158,6 +166,41 @@ func TestLoad(t *testing.T) {
158
166
},
159
167
wantErr : assert .NoError ,
160
168
},
169
+ {
170
+ name : "base case with nonidentify attributes" ,
171
+ args : args {
172
+ file : "./testdata/agentwithdescription.yaml" ,
173
+ envVariables : map [string ]string {
174
+ "MY_ENV_VAR_1" : "my-env-variable-1-value" ,
175
+ "MY_ENV_VAR_2" : "my-env-variable-2-value" ,
176
+ },
177
+ },
178
+ want : & Config {
179
+ instanceId : instanceId ,
180
+ RootLogger : logr .Discard (),
181
+ Endpoint : "ws://127.0.0.1:4320/v1/opamp" ,
182
+ AgentDescription : AgentDescription {
183
+ NonIdentifyingAttributes : map [string ]string {
184
+ "custom.attribute" : "custom-value" ,
185
+ },
186
+ },
187
+ Capabilities : map [Capability ]bool {
188
+ AcceptsRemoteConfig : true ,
189
+ ReportsEffectiveConfig : true ,
190
+ ReportsOwnTraces : true ,
191
+ ReportsOwnMetrics : true ,
192
+ ReportsOwnLogs : true ,
193
+ AcceptsOpAMPConnectionSettings : true ,
194
+ AcceptsOtherConnectionSettings : true ,
195
+ AcceptsRestartCommand : true ,
196
+ ReportsHealth : true ,
197
+ ReportsRemoteConfig : true ,
198
+ AcceptsPackages : false ,
199
+ ReportsPackageStatuses : false ,
200
+ },
201
+ },
202
+ wantErr : assert .NoError ,
203
+ },
161
204
}
162
205
for _ , tt := range tests {
163
206
t .Run (tt .name , func (t * testing.T ) {
@@ -173,9 +216,41 @@ func TestLoad(t *testing.T) {
173
216
return
174
217
}
175
218
// there are some fields we don't care about, so we ignore them.
219
+ got .instanceId = tt .want .instanceId
176
220
got .ClusterConfig = tt .want .ClusterConfig
177
221
got .RootLogger = tt .want .RootLogger
178
222
assert .Equalf (t , tt .want , got , "Load(%v)" , tt .args .file )
179
223
})
180
224
}
181
225
}
226
+
227
+ func TestGetDescription (t * testing.T ) {
228
+ got := NewConfig (logr .Discard ())
229
+ instanceId := uuid .New ()
230
+ got .instanceId = instanceId
231
+ err := LoadFromFile (got , "./testdata/agentwithdescription.yaml" )
232
+ require .NoError (t , err , fmt .Sprintf ("Load(%v)" , "./testdata/agentwithdescription.yaml" ))
233
+ desc := got .GetDescription ()
234
+ assert .Len (t , desc .IdentifyingAttributes , 3 )
235
+ assert .Contains (t , desc .IdentifyingAttributes , & protobufs.KeyValue {Key : "service.instance.id" , Value : & protobufs.AnyValue {
236
+ Value : & protobufs.AnyValue_StringValue {StringValue : instanceId .String ()},
237
+ }})
238
+ assert .Len (t , desc .NonIdentifyingAttributes , 3 )
239
+ assert .Contains (t , desc .NonIdentifyingAttributes , & protobufs.KeyValue {Key : "custom.attribute" , Value : & protobufs.AnyValue {
240
+ Value : & protobufs.AnyValue_StringValue {StringValue : "custom-value" },
241
+ }})
242
+ }
243
+
244
+ func TestGetDescriptionNoneSet (t * testing.T ) {
245
+ got := NewConfig (logr .Discard ())
246
+ instanceId := uuid .New ()
247
+ got .instanceId = instanceId
248
+ err := LoadFromFile (got , "./testdata/agent.yaml" )
249
+ require .NoError (t , err , fmt .Sprintf ("Load(%v)" , "./testdata/agent.yaml" ))
250
+ desc := got .GetDescription ()
251
+ assert .Len (t , desc .IdentifyingAttributes , 3 )
252
+ assert .Contains (t , desc .IdentifyingAttributes , & protobufs.KeyValue {Key : "service.instance.id" , Value : & protobufs.AnyValue {
253
+ Value : & protobufs.AnyValue_StringValue {StringValue : instanceId .String ()},
254
+ }})
255
+ assert .Len (t , desc .NonIdentifyingAttributes , 2 )
256
+ }
0 commit comments