forked from elastic/elastic-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiagnostics_test.go
644 lines (574 loc) · 17 KB
/
diagnostics_test.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package coordinator
import (
"context"
"errors"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
"github.com/elastic/elastic-agent-client/v7/pkg/client"
"github.com/elastic/elastic-agent-client/v7/pkg/proto"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/details"
"github.com/elastic/elastic-agent/internal/pkg/agent/configuration"
"github.com/elastic/elastic-agent/internal/pkg/agent/transpiler"
monitoringCfg "github.com/elastic/elastic-agent/internal/pkg/core/monitoring/config"
"github.com/elastic/elastic-agent/internal/pkg/diagnostics"
"github.com/elastic/elastic-agent/internal/pkg/remote"
"github.com/elastic/elastic-agent/pkg/component"
"github.com/elastic/elastic-agent/pkg/component/runtime"
agentclient "github.com/elastic/elastic-agent/pkg/control/v2/client"
"github.com/elastic/elastic-agent/pkg/utils/broadcaster"
)
func TestCoordinatorExpectedDiagnosticHooks(t *testing.T) {
expected := []string{
"agent-info",
"local-config",
"pre-config",
"variables",
"computed-config",
"components-expected",
"components-actual",
"state",
}
coord := &Coordinator{}
hooks := diagnosticHooksMap(coord)
assert.Equal(t, len(expected), len(hooks), "Wrong number of diagnostic hooks (did you forget to update diagnostics_test with your diagnostics change?)")
for _, name := range expected {
assert.Contains(t, hooks, name, "No hook returned for expected diagnostic %q", name)
}
}
func TestDiagnosticLocalConfig(t *testing.T) {
// Create a Coordinator with a test configuration and make sure the
// local-config hook correctly returns it.
cfg := &configuration.Configuration{
Fleet: &configuration.FleetAgentConfig{
Enabled: true,
AccessAPIKey: "test-key",
Client: remote.Config{
Protocol: "test-protocol",
},
},
Settings: &configuration.SettingsConfig{
MonitoringConfig: &monitoringCfg.MonitoringConfig{
MonitorTraces: true,
APM: monitoringCfg.APMConfig{
Environment: "diag-unit-test",
APIKey: "apikey",
SecretToken: "secret",
Hosts: []string{"host1", "host2"},
GlobalLabels: map[string]string{"k1": "v1", "k2": "v2"},
TLS: monitoringCfg.APMTLS{
SkipVerify: false,
ServerCertificate: "/path/to/server/cert",
ServerCA: "/path/to/server/ca",
},
},
},
},
}
// The YAML we expect to see from the preceding config
expectedCfg := `
agent:
download: null
grpc: null
id: ""
path: ""
process: null
reload: null
upgrade: null
v1_monitoring_enabled: false
monitoring:
enabled: false
http: null
logs: false
metrics: false
metrics_period: ""
namespace: ""
pprof: null
traces: true
apm:
hosts:
- host1
- host2
environment: diag-unit-test
apikey: apikey
secrettoken: secret
globallabels:
k1: v1
k2: v2
tls:
skipverify: false
servercertificate: "/path/to/server/cert"
serverca: "/path/to/server/ca"
fleet:
enabled: true
access_api_key: "test-key"
agent:
protocol: "test-protocol"
`
coord := &Coordinator{cfg: cfg}
hook, ok := diagnosticHooksMap(coord)["local-config"]
require.True(t, ok, "diagnostic hooks should have an entry for local-config")
result := hook.Hook(context.Background())
assert.YAMLEq(t, expectedCfg, string(result), "local-config diagnostic returned unexpected value")
}
func TestDiagnosticAgentInfo(t *testing.T) {
// Create a coordinator with an info.Agent and ensure its included in diagnostics.
coord := &Coordinator{agentInfo: fakeAgentInfo{
agentID: "agent-id",
headers: map[string]string{
"header1": "value1",
"header2": "value2",
},
logLevel: "trace",
snapshot: true,
version: "8.14.0",
unprivileged: true,
}}
expected := `
agent_id: agent-id
headers:
header1: value1
header2: value2
log_level: trace
snapshot: true
version: 8.14.0
unprivileged: true
`
hook, ok := diagnosticHooksMap(coord)["agent-info"]
require.True(t, ok, "diagnostic hooks should have an entry for agent-info")
result := hook.Hook(context.Background())
assert.YAMLEq(t, expected, string(result), "agent-info diagnostic returned unexpected value")
}
func TestDiagnosticPreConfig(t *testing.T) {
// Create a coordinator with a test AST and make sure it's returned
// by the pre-config diagnostic.
cfgStr := `
outputs:
default:
type: elasticsearch
inputs:
- id: test-input
type: filestream
use_output: default
`
cfgMap := mapFromRawYAML(t, cfgStr)
cfgAST, err := transpiler.NewAST(cfgMap)
require.NoError(t, err, "Couldn't create test AST")
coord := &Coordinator{ast: cfgAST}
hook, ok := diagnosticHooksMap(coord)["pre-config"]
require.True(t, ok, "diagnostic hooks should have an entry for pre-config")
result := hook.Hook(context.Background())
assert.YAMLEq(t, cfgStr, string(result), "pre-config diagnostic returned unexpected value")
}
func TestDiagnosticVariables(t *testing.T) {
vars, err := transpiler.NewVars(
"id",
map[string]interface{}{
"testvar": "testvalue",
},
nil)
require.NoError(t, err)
expected := `
variables:
- testvar: testvalue
`
coord := &Coordinator{vars: []*transpiler.Vars{vars}}
hook, ok := diagnosticHooksMap(coord)["variables"]
require.True(t, ok, "diagnostic hooks should have an entry for variables")
result := hook.Hook(context.Background())
assert.YAMLEq(t, expected, string(result), "variables diagnostic returned unexpected value")
}
func TestDiagnosticComputedConfig(t *testing.T) {
// Create a Coordinator with a test value in derivedConfig and make sure
// it's reported by the computed-config diagnostic.
expected := `
test:
values:
type: elasticsearch
something:
- id: thing one
- id: thing two
`
derivedCfg := mapFromRawYAML(t, expected)
coord := &Coordinator{derivedConfig: derivedCfg}
hook, ok := diagnosticHooksMap(coord)["computed-config"]
require.True(t, ok, "diagnostic hooks should have an entry for computed-config")
result := hook.Hook(context.Background())
assert.YAMLEq(t, expected, string(result), "computed-config diagnostic returned unexpected value")
}
func TestDiagnosticComponentsExpected(t *testing.T) {
// Create a Coordinator with a test component model and make sure it's
// reported by the components-expected diagnostic
components := []component.Component{
{
ID: "filestream-component",
InputType: "filestream",
OutputType: "elasticsearch",
InputSpec: &component.InputRuntimeSpec{
InputType: "filestream",
BinaryName: "filestream-binary",
BinaryPath: "filestream-path",
Spec: component.InputSpec{
Name: "filestream-spec",
Description: "filestream description",
},
},
Units: []component.Unit{
{ID: "filestream-input", Type: client.UnitTypeInput, LogLevel: 2},
{ID: "filestream-output", Type: client.UnitTypeOutput, LogLevel: 2},
},
},
}
expected := `
components:
- id: filestream-component
input_type: filestream
output_type: elasticsearch
input_spec:
binary_name: filestream-binary
binary_path: filestream-path
input_type: filestream
spec:
name: filestream-spec
description: "filestream description"
platforms: []
units:
- id: filestream-input
log_level: 2
type: 0
- id: filestream-output
log_level: 2
type: 1
`
coord := &Coordinator{componentModel: components}
hook, ok := diagnosticHooksMap(coord)["components-expected"]
require.True(t, ok, "diagnostic hooks should have an entry for components-expected")
result := hook.Hook(context.Background())
assert.YAMLEq(t, expected, string(result), "components-expected diagnostic returned unexpected value")
}
func TestDiagnosticComponentsExpectedWithAPM(t *testing.T) {
// Create a Coordinator with a test component model and make sure it's
// reported by the components-expected diagnostic
components := []component.Component{
{
ID: "some-apm-aware-component",
InputType: "filestream",
OutputType: "elasticsearch",
Component: &proto.Component{
ApmConfig: &proto.APMConfig{
Elastic: &proto.ElasticAPM{
Environment: "diag-unit-test",
ApiKey: "apikey",
SecretToken: "st",
Hosts: []string{"host1", "host2"},
GlobalLabels: "k=v",
Tls: &proto.ElasticAPMTLS{
SkipVerify: true,
ServerCert: "servercert",
ServerCa: "serverca",
},
},
},
},
},
}
expected := `
components:
- id: some-apm-aware-component
input_type: filestream
output_type: elasticsearch
units: []
component:
limits: null
apmconfig:
elastic:
environment: diag-unit-test
apikey: apikey
secrettoken: st
globallabels: "k=v"
hosts:
- host1
- host2
tls:
skipverify: true
servercert: servercert
serverca: serverca
`
coord := &Coordinator{componentModel: components}
hook, ok := diagnosticHooksMap(coord)["components-expected"]
require.True(t, ok, "diagnostic hooks should have an entry for components-expected")
result := hook.Hook(context.Background())
assert.YAMLEq(t, expected, string(result), "components-expected diagnostic returned unexpected value")
}
func TestDiagnosticComponentsActual(t *testing.T) {
// Create a Coordinator with observed component data in the state broadcaster
// and make sure the components-actual diagnostic reports it
state := State{
Components: []runtime.ComponentComponentState{
{
Component: component.Component{
ID: "component-1",
Err: errors.New("component error"),
InputType: "test-input",
OutputType: "test-output",
Units: []component.Unit{
{
ID: "test-unit",
Type: client.UnitTypeInput,
LogLevel: 1,
Err: errors.New("unit error"),
},
},
},
State: runtime.ComponentState{
State: client.UnitStateFailed,
Message: "error running component",
Units: map[runtime.ComponentUnitKey]runtime.ComponentUnitState{
{
UnitType: client.UnitTypeInput,
UnitID: "test-unit",
}: {State: client.UnitStateFailed},
},
},
},
},
}
expected := `
components:
- id: component-1
error: "component error"
input_type: "test-input"
output_type: "test-output"
units:
- id: test-unit
error: {}
log_level: 1
type: 0
`
coord := &Coordinator{
// This test needs a broadcaster since the components-actual diagnostic
// fetches the state via State().
stateBroadcaster: broadcaster.New(state, 0, 0),
}
hook, ok := diagnosticHooksMap(coord)["components-actual"]
require.True(t, ok, "diagnostic hooks should have an entry for components-actual")
result := hook.Hook(context.Background())
assert.YAMLEq(t, expected, string(result), "components-actual diagnostic returned unexpected value")
}
// TestDiagnosticState creates a coordinator with a test state and verify that
// the state diagnostic reports it.
func TestDiagnosticState(t *testing.T) {
now := time.Now().UTC()
state1 := State{
State: agentclient.Starting,
Message: "starting up",
FleetState: agentclient.Configuring,
FleetMessage: "configuring",
LogLevel: 1,
Components: []runtime.ComponentComponentState{
{
Component: component.Component{ID: "comp-1"},
State: runtime.ComponentState{
State: client.UnitStateStarting,
Message: "starting message",
VersionInfo: runtime.ComponentVersionInfo{
Name: "version name",
BuildHash: "a-build-hash",
Meta: map[string]string{},
},
},
},
},
UpgradeDetails: &details.Details{
TargetVersion: "8.12.0",
State: "UPG_DOWNLOADING",
ActionID: "foobar",
Metadata: details.Metadata{
DownloadPercent: 0.17469,
ScheduledAt: &now,
DownloadRate: 123.56,
RetryUntil: &now,
},
},
}
state2 := state1
state2.Components[0].State.VersionInfo.Meta["commit"] = "a-commit-hash"
expected := fmt.Sprintf(`
state: 0
message: "starting up"
fleet_state: 1
fleet_message: "configuring"
log_level: "warning"
components:
- id: "comp-1"
state:
pid: 0
state: 0
message: "starting message"
features_idx: 0
component_idx: 0
units: {}
version_info:
name: "version name"
build_hash: "a-build-hash"
meta:
commit: "a-commit-hash"
upgrade_details:
target_version: 8.12.0
state: UPG_DOWNLOADING
action_id: foobar
metadata:
download_percent: 0.17469
scheduled_at: %s
download_rate: 123.56
retry_until: %s
`, now.Format(time.RFC3339Nano), now.Format(time.RFC3339Nano))
coord := &Coordinator{
// This test needs a broadcaster since the components-actual diagnostic
// fetches the state via State().
stateBroadcaster: broadcaster.New(state1, 0, 0),
}
coord.stateBroadcaster.InputChan <- state2
hook, ok := diagnosticHooksMap(coord)["state"]
require.True(t, ok, "diagnostic hooks should have an entry for state")
result := hook.Hook(context.Background())
assert.YAMLEq(t, expected, string(result), "state diagnostic returned unexpected value")
}
func TestDiagnosticStateForAPM(t *testing.T) {
// Create a coordinator with a test state and verify that the state
// diagnostic reports it
token := "st"
state := State{
State: agentclient.Starting,
Message: "starting up",
FleetState: agentclient.Configuring,
FleetMessage: "configuring",
LogLevel: 1,
Components: []runtime.ComponentComponentState{
{
Component: component.Component{ID: "comp-1"},
State: runtime.ComponentState{
State: client.UnitStateDegraded,
Message: "degraded message",
VersionInfo: runtime.ComponentVersionInfo{
Name: "version name",
BuildHash: "a-build-hash",
},
Component: &proto.Component{
ApmConfig: &proto.APMConfig{
Elastic: &proto.ElasticAPM{
Environment: "diag-state-ut",
SecretToken: token,
Hosts: []string{"apmhost"},
Tls: &proto.ElasticAPMTLS{
SkipVerify: true,
ServerCert: "sc",
ServerCa: "sca",
},
},
},
},
ComponentIdx: 1,
},
},
},
}
expected := `
state: 0
message: "starting up"
fleet_state: 1
fleet_message: "configuring"
log_level: "warning"
components:
- id: "comp-1"
state:
pid: 0
state: 3
message: "degraded message"
features_idx: 0
units: {}
version_info:
name: "version name"
build_hash: "a-build-hash"
component:
apmconfig:
elastic:
apikey: ""
environment: diag-state-ut
hosts: [apmhost]
secrettoken: st
globallabels: ""
tls:
skipverify: true
serverca: sca
servercert: sc
limits: null
component_idx: 1
`
coord := &Coordinator{
// This test needs a broadcaster since the components-actual diagnostic
// fetches the state via State().
stateBroadcaster: broadcaster.New(state, 0, 0),
}
hook, ok := diagnosticHooksMap(coord)["state"]
require.True(t, ok, "diagnostic hooks should have an entry for state")
result := hook.Hook(context.Background())
assert.YAMLEq(t, expected, string(result), "state diagnostic returned unexpected value")
}
// Fetch the diagnostic hooks and add them to a lookup table for
// easier verification
func diagnosticHooksMap(coord *Coordinator) map[string]diagnostics.Hook {
diagHooks := coord.DiagnosticHooks()
hooksMap := map[string]diagnostics.Hook{}
for i, h := range diagHooks {
hooksMap[h.Name] = diagHooks[i]
}
return hooksMap
}
func mapFromRawYAML(t *testing.T, str string) map[string]interface{} {
var result map[string]interface{}
err := yaml.Unmarshal([]byte(str), &result)
require.NoError(t, err, "Parsing of YAML test string must succeed")
return result
}
type fakeAgentInfo struct {
agentID string
headers map[string]string
logLevel string
snapshot bool
version string
unprivileged bool
isStandalone bool
}
func (a fakeAgentInfo) AgentID() string {
return a.agentID
}
func (a fakeAgentInfo) Headers() map[string]string {
return a.headers
}
func (a fakeAgentInfo) LogLevel() string {
return a.logLevel
}
func (a fakeAgentInfo) RawLogLevel() string {
return a.logLevel
}
func (a fakeAgentInfo) Snapshot() bool {
return a.snapshot
}
func (a fakeAgentInfo) Version() string {
return a.version
}
func (a fakeAgentInfo) Unprivileged() bool {
return a.unprivileged
}
func (a fakeAgentInfo) IsStandalone() bool {
return a.isStandalone
}
func (a fakeAgentInfo) ReloadID(ctx context.Context) error { panic("implement me") }
func (a fakeAgentInfo) SetLogLevel(ctx context.Context, level string) error { panic("implement me") }