|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License; |
| 3 | +// you may not use this file except in compliance with the Elastic License. |
| 4 | + |
| 5 | +package monitoring |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "runtime" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + "gopkg.in/yaml.v2" |
| 16 | + |
| 17 | + "github.com/elastic/elastic-agent/internal/pkg/agent/application/info" |
| 18 | + monitoringcfg "github.com/elastic/elastic-agent/internal/pkg/core/monitoring/config" |
| 19 | +) |
| 20 | + |
| 21 | +func TestMonitoringConfigMetricsInterval(t *testing.T) { |
| 22 | + |
| 23 | + agentInfo, err := info.NewAgentInfo(context.Background(), false) |
| 24 | + require.NoError(t, err, "Error creating agent info") |
| 25 | + |
| 26 | + mCfg := &monitoringConfig{ |
| 27 | + C: &monitoringcfg.MonitoringConfig{ |
| 28 | + Enabled: true, |
| 29 | + MonitorMetrics: true, |
| 30 | + HTTP: &monitoringcfg.MonitoringHTTPConfig{ |
| 31 | + Enabled: false, |
| 32 | + }, |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + policy := map[string]any{ |
| 37 | + "agent": map[string]any{ |
| 38 | + "monitoring": map[string]any{ |
| 39 | + "metrics": true, |
| 40 | + "http": map[string]any{ |
| 41 | + "enabled": false, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + "outputs": map[string]any{ |
| 46 | + "default": map[string]any{}, |
| 47 | + }, |
| 48 | + } |
| 49 | + b := &BeatsMonitor{ |
| 50 | + enabled: true, |
| 51 | + config: mCfg, |
| 52 | + operatingSystem: runtime.GOOS, |
| 53 | + agentInfo: agentInfo, |
| 54 | + } |
| 55 | + got, err := b.MonitoringConfig(policy, nil, map[string]string{"foobeat": "filebeat"}) // put a componentID/binary mapping to have something in the beats monitoring input |
| 56 | + assert.NoError(t, err) |
| 57 | + |
| 58 | + rawInputs, ok := got["inputs"] |
| 59 | + require.True(t, ok, "monitoring config contains no input") |
| 60 | + inputs, ok := rawInputs.([]any) |
| 61 | + require.True(t, ok, "monitoring inputs are not a list") |
| 62 | + marshaledInputs, err := yaml.Marshal(inputs) |
| 63 | + if assert.NoError(t, err, "error marshaling monitoring inputs") { |
| 64 | + t.Logf("marshaled monitoring inputs:\n%s\n", marshaledInputs) |
| 65 | + } |
| 66 | + |
| 67 | + // loop over the created inputs |
| 68 | + for _, i := range inputs { |
| 69 | + input, ok := i.(map[string]any) |
| 70 | + if assert.Truef(t, ok, "input is not represented as a map: %v", i) { |
| 71 | + inputID := input["id"] |
| 72 | + t.Logf("input %q", inputID) |
| 73 | + // check the streams created for the input, should be a list of objects |
| 74 | + if assert.Contains(t, input, "streams", "input %q does not contain any stream", inputID) && |
| 75 | + assert.IsTypef(t, []any{}, input["streams"], "streams for input %q are not a list of objects", inputID) { |
| 76 | + // loop over streams and cast to map[string]any to access keys |
| 77 | + for _, rawStream := range input["streams"].([]any) { |
| 78 | + if assert.IsTypef(t, map[string]any{}, rawStream, "stream %v for input %q is not a map", rawStream, inputID) { |
| 79 | + stream := rawStream.(map[string]any) |
| 80 | + // check period and assert its value |
| 81 | + streamID := stream["id"] |
| 82 | + if assert.Containsf(t, stream, "period", "stream %q for input %q does not contain a period", streamID, inputID) && |
| 83 | + assert.IsType(t, "", stream["period"], "period for stream %q of input %q is not represented as a string", streamID, inputID) { |
| 84 | + periodString := stream["period"].(string) |
| 85 | + duration, err := time.ParseDuration(periodString) |
| 86 | + if assert.NoErrorf(t, err, "Unparseable period duration %s for stream %q of input %q", periodString, streamID, inputID) { |
| 87 | + assert.Equalf(t, duration, 60*time.Second, "unexpected duration for stream %q of input %q", streamID, inputID) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | +} |
0 commit comments