|
| 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 2.0; |
| 3 | +// you may not use this file except in compliance with the Elastic License 2.0. |
| 4 | + |
| 5 | +package info |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + |
| 13 | + "github.com/elastic/elastic-agent/pkg/utils" |
| 14 | + |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | +) |
| 17 | + |
| 18 | +func TestNewAgentInfoWithLog(t *testing.T) { |
| 19 | + hasRoot, err := utils.HasRoot() |
| 20 | + require.NoError(t, err, "failed to check for root") |
| 21 | + |
| 22 | + for _, tc := range []struct { |
| 23 | + name string |
| 24 | + levelFromConfig string |
| 25 | + isStandalone bool |
| 26 | + persistentAgentInfo *persistentAgentInfo |
| 27 | + expected *AgentInfo |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "standalone agent", |
| 31 | + levelFromConfig: "debug", |
| 32 | + isStandalone: true, |
| 33 | + persistentAgentInfo: &persistentAgentInfo{ |
| 34 | + ID: "testID", |
| 35 | + Headers: nil, |
| 36 | + LogLevel: "info", |
| 37 | + MonitoringHTTP: nil, |
| 38 | + }, |
| 39 | + expected: &AgentInfo{ |
| 40 | + agentID: "testID", |
| 41 | + logLevel: "debug", |
| 42 | + unprivileged: !hasRoot, |
| 43 | + esHeaders: nil, |
| 44 | + isStandalone: true, |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "fleet managed agent", |
| 49 | + levelFromConfig: "debug", |
| 50 | + isStandalone: false, |
| 51 | + persistentAgentInfo: &persistentAgentInfo{ |
| 52 | + ID: "testID", |
| 53 | + Headers: nil, |
| 54 | + LogLevel: "info", |
| 55 | + MonitoringHTTP: nil, |
| 56 | + }, |
| 57 | + expected: &AgentInfo{ |
| 58 | + agentID: "testID", |
| 59 | + logLevel: "info", |
| 60 | + unprivileged: !hasRoot, |
| 61 | + esHeaders: nil, |
| 62 | + isStandalone: false, |
| 63 | + }, |
| 64 | + }, |
| 65 | + } { |
| 66 | + t.Run(tc.name, func(t *testing.T) { |
| 67 | + prevDoLoadAgentInfoWithBackoff := doLoadAgentInfoWithBackoff |
| 68 | + defer func() { |
| 69 | + doLoadAgentInfoWithBackoff = prevDoLoadAgentInfoWithBackoff |
| 70 | + }() |
| 71 | + doLoadAgentInfoWithBackoff = func(ctx context.Context, forceUpdate bool, logLevel string, createAgentID bool) (*persistentAgentInfo, bool, error) { |
| 72 | + return tc.persistentAgentInfo, tc.isStandalone, nil |
| 73 | + } |
| 74 | + |
| 75 | + ai, err := NewAgentInfoWithLog(context.Background(), tc.levelFromConfig, true) |
| 76 | + assert.NoError(t, err, "could not create agent info") |
| 77 | + assert.Equal(t, tc.expected, ai, "agent info does not match") |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
0 commit comments