|
| 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 artifact |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/elastic/elastic-agent/internal/pkg/config" |
| 12 | + "github.com/elastic/elastic-agent/pkg/core/logger" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func TestReload(t *testing.T) { |
| 17 | + type testCase struct { |
| 18 | + input string |
| 19 | + initialConfig *Config |
| 20 | + expectedSourceURI string |
| 21 | + expectedTargetDirectory string |
| 22 | + expectedInstallDirectory string |
| 23 | + expectedDropDirectory string |
| 24 | + expectedFingerprint string |
| 25 | + expectedTLS bool |
| 26 | + expectedTLSEnabled bool |
| 27 | + expectedDisableProxy bool |
| 28 | + expectedTimeout time.Duration |
| 29 | + } |
| 30 | + defaultValues := DefaultConfig() |
| 31 | + testCases := []testCase{ |
| 32 | + { |
| 33 | + input: `agent.download: |
| 34 | + sourceURI: "testing.uri" |
| 35 | + target_directory: "a/b/c" |
| 36 | + install_path: "i/p" |
| 37 | + drop_path: "d/p" |
| 38 | + proxy_disable: true |
| 39 | + timeout: 33s |
| 40 | + ssl.enabled: true |
| 41 | + ssl.ca_trusted_fingerprint: "my_finger_print" |
| 42 | +`, |
| 43 | + initialConfig: DefaultConfig(), |
| 44 | + expectedSourceURI: "testing.uri", |
| 45 | + expectedTargetDirectory: "a/b/c", |
| 46 | + expectedInstallDirectory: "i/p", |
| 47 | + expectedDropDirectory: "d/p", |
| 48 | + expectedFingerprint: "my_finger_print", |
| 49 | + expectedTLS: true, |
| 50 | + expectedTLSEnabled: true, |
| 51 | + expectedDisableProxy: true, |
| 52 | + expectedTimeout: 33 * time.Second, |
| 53 | + }, |
| 54 | + { |
| 55 | + input: `agent.download: |
| 56 | + sourceURI: "testing.uri" |
| 57 | +`, |
| 58 | + initialConfig: DefaultConfig(), |
| 59 | + expectedSourceURI: "testing.uri", |
| 60 | + expectedTargetDirectory: defaultValues.TargetDirectory, |
| 61 | + expectedInstallDirectory: defaultValues.InstallPath, |
| 62 | + expectedDropDirectory: defaultValues.DropPath, |
| 63 | + expectedFingerprint: "", |
| 64 | + expectedTLS: defaultValues.TLS != nil, |
| 65 | + expectedTLSEnabled: false, |
| 66 | + expectedDisableProxy: defaultValues.Proxy.Disable, |
| 67 | + expectedTimeout: defaultValues.Timeout, |
| 68 | + }, |
| 69 | + { |
| 70 | + input: `agent.download: |
| 71 | + sourceURI: "" |
| 72 | +`, |
| 73 | + initialConfig: &Config{ |
| 74 | + SourceURI: "testing.uri", |
| 75 | + HTTPTransportSettings: defaultValues.HTTPTransportSettings, |
| 76 | + }, |
| 77 | + expectedSourceURI: defaultValues.SourceURI, // fallback to default when set to empty |
| 78 | + expectedTargetDirectory: defaultValues.TargetDirectory, |
| 79 | + expectedInstallDirectory: defaultValues.InstallPath, |
| 80 | + expectedDropDirectory: defaultValues.DropPath, |
| 81 | + expectedFingerprint: "", |
| 82 | + expectedTLS: defaultValues.TLS != nil, |
| 83 | + expectedTLSEnabled: false, |
| 84 | + expectedDisableProxy: defaultValues.Proxy.Disable, |
| 85 | + expectedTimeout: defaultValues.Timeout, |
| 86 | + }, |
| 87 | + { |
| 88 | + input: ``, |
| 89 | + initialConfig: &Config{ |
| 90 | + SourceURI: "testing.uri", |
| 91 | + HTTPTransportSettings: defaultValues.HTTPTransportSettings, |
| 92 | + }, |
| 93 | + expectedSourceURI: defaultValues.SourceURI, // fallback to default when not set |
| 94 | + expectedTargetDirectory: defaultValues.TargetDirectory, |
| 95 | + expectedInstallDirectory: defaultValues.InstallPath, |
| 96 | + expectedDropDirectory: defaultValues.DropPath, |
| 97 | + expectedFingerprint: "", |
| 98 | + expectedTLS: defaultValues.TLS != nil, |
| 99 | + expectedTLSEnabled: false, |
| 100 | + expectedDisableProxy: defaultValues.Proxy.Disable, |
| 101 | + expectedTimeout: defaultValues.Timeout, |
| 102 | + }, |
| 103 | + { |
| 104 | + input: `agent.download: |
| 105 | + sourceURI: " " |
| 106 | +`, |
| 107 | + initialConfig: &Config{ |
| 108 | + SourceURI: "testing.uri", |
| 109 | + HTTPTransportSettings: defaultValues.HTTPTransportSettings, |
| 110 | + }, |
| 111 | + expectedSourceURI: defaultValues.SourceURI, // fallback to default when set to whitespace |
| 112 | + expectedTargetDirectory: defaultValues.TargetDirectory, |
| 113 | + expectedInstallDirectory: defaultValues.InstallPath, |
| 114 | + expectedDropDirectory: defaultValues.DropPath, |
| 115 | + expectedFingerprint: "", |
| 116 | + expectedTLS: defaultValues.TLS != nil, |
| 117 | + expectedTLSEnabled: false, |
| 118 | + expectedDisableProxy: defaultValues.Proxy.Disable, |
| 119 | + expectedTimeout: defaultValues.Timeout, |
| 120 | + }, |
| 121 | + { |
| 122 | + input: `agent.download: |
| 123 | + source_uri: " " |
| 124 | +`, |
| 125 | + initialConfig: &Config{ |
| 126 | + SourceURI: "testing.uri", |
| 127 | + HTTPTransportSettings: defaultValues.HTTPTransportSettings, |
| 128 | + }, |
| 129 | + expectedSourceURI: defaultValues.SourceURI, // fallback to default when set to whitespace |
| 130 | + expectedTargetDirectory: defaultValues.TargetDirectory, |
| 131 | + expectedInstallDirectory: defaultValues.InstallPath, |
| 132 | + expectedDropDirectory: defaultValues.DropPath, |
| 133 | + expectedFingerprint: "", |
| 134 | + expectedTLS: defaultValues.TLS != nil, |
| 135 | + expectedTLSEnabled: false, |
| 136 | + expectedDisableProxy: defaultValues.Proxy.Disable, |
| 137 | + expectedTimeout: defaultValues.Timeout, |
| 138 | + }, |
| 139 | + { |
| 140 | + input: `agent.download: |
| 141 | + source_uri: " " |
| 142 | + sourceURI: " " |
| 143 | +`, |
| 144 | + initialConfig: DefaultConfig(), |
| 145 | + expectedSourceURI: defaultValues.SourceURI, // fallback to default when set to whitespace |
| 146 | + expectedTargetDirectory: defaultValues.TargetDirectory, |
| 147 | + expectedInstallDirectory: defaultValues.InstallPath, |
| 148 | + expectedDropDirectory: defaultValues.DropPath, |
| 149 | + expectedFingerprint: "", |
| 150 | + expectedTLS: defaultValues.TLS != nil, |
| 151 | + expectedTLSEnabled: false, |
| 152 | + expectedDisableProxy: defaultValues.Proxy.Disable, |
| 153 | + expectedTimeout: defaultValues.Timeout, |
| 154 | + }, |
| 155 | + { |
| 156 | + input: ``, |
| 157 | + initialConfig: &Config{ |
| 158 | + SourceURI: "testing.uri", |
| 159 | + HTTPTransportSettings: defaultValues.HTTPTransportSettings, |
| 160 | + }, |
| 161 | + expectedSourceURI: defaultValues.SourceURI, |
| 162 | + expectedTargetDirectory: defaultValues.TargetDirectory, |
| 163 | + expectedInstallDirectory: defaultValues.InstallPath, |
| 164 | + expectedDropDirectory: defaultValues.DropPath, |
| 165 | + expectedFingerprint: "", |
| 166 | + expectedTLS: defaultValues.TLS != nil, |
| 167 | + expectedTLSEnabled: false, |
| 168 | + expectedDisableProxy: defaultValues.Proxy.Disable, |
| 169 | + expectedTimeout: defaultValues.Timeout, |
| 170 | + }, |
| 171 | + { |
| 172 | + input: `agent.download: |
| 173 | + source_uri: " " |
| 174 | + sourceURI: "testing.uri" |
| 175 | +`, |
| 176 | + initialConfig: DefaultConfig(), |
| 177 | + expectedSourceURI: "testing.uri", |
| 178 | + expectedTargetDirectory: defaultValues.TargetDirectory, |
| 179 | + expectedInstallDirectory: defaultValues.InstallPath, |
| 180 | + expectedDropDirectory: defaultValues.DropPath, |
| 181 | + expectedFingerprint: "", |
| 182 | + expectedTLS: defaultValues.TLS != nil, |
| 183 | + expectedTLSEnabled: false, |
| 184 | + expectedDisableProxy: defaultValues.Proxy.Disable, |
| 185 | + expectedTimeout: defaultValues.Timeout, |
| 186 | + }, |
| 187 | + { |
| 188 | + input: `agent.download: |
| 189 | + source_uri: "testing.uri" |
| 190 | + sourceURI: " " |
| 191 | +`, |
| 192 | + initialConfig: DefaultConfig(), |
| 193 | + expectedSourceURI: "testing.uri", |
| 194 | + expectedTargetDirectory: defaultValues.TargetDirectory, |
| 195 | + expectedInstallDirectory: defaultValues.InstallPath, |
| 196 | + expectedDropDirectory: defaultValues.DropPath, |
| 197 | + expectedFingerprint: "", |
| 198 | + expectedTLS: defaultValues.TLS != nil, |
| 199 | + expectedTLSEnabled: false, |
| 200 | + expectedDisableProxy: defaultValues.Proxy.Disable, |
| 201 | + expectedTimeout: defaultValues.Timeout, |
| 202 | + }, |
| 203 | + { |
| 204 | + input: `agent.download: |
| 205 | + source_uri: "testing.uri" |
| 206 | + sourceURI: "another.uri" |
| 207 | +`, |
| 208 | + initialConfig: DefaultConfig(), |
| 209 | + expectedSourceURI: "testing.uri", |
| 210 | + expectedTargetDirectory: defaultValues.TargetDirectory, |
| 211 | + expectedInstallDirectory: defaultValues.InstallPath, |
| 212 | + expectedDropDirectory: defaultValues.DropPath, |
| 213 | + expectedFingerprint: "", |
| 214 | + expectedTLS: defaultValues.TLS != nil, |
| 215 | + expectedTLSEnabled: false, |
| 216 | + expectedDisableProxy: defaultValues.Proxy.Disable, |
| 217 | + expectedTimeout: defaultValues.Timeout, |
| 218 | + }, |
| 219 | + } |
| 220 | + |
| 221 | + l, _ := logger.NewTesting("t") |
| 222 | + for _, tc := range testCases { |
| 223 | + cfg := tc.initialConfig |
| 224 | + reloader := NewReloader(cfg, l) |
| 225 | + |
| 226 | + c, err := config.NewConfigFrom(tc.input) |
| 227 | + require.NoError(t, err) |
| 228 | + |
| 229 | + require.NoError(t, reloader.Reload(c)) |
| 230 | + |
| 231 | + require.Equal(t, tc.expectedSourceURI, cfg.SourceURI) |
| 232 | + require.Equal(t, tc.expectedTargetDirectory, cfg.TargetDirectory) |
| 233 | + require.Equal(t, tc.expectedInstallDirectory, cfg.InstallPath) |
| 234 | + require.Equal(t, tc.expectedDropDirectory, cfg.DropPath) |
| 235 | + require.Equal(t, tc.expectedTimeout, cfg.Timeout) |
| 236 | + |
| 237 | + require.Equal(t, tc.expectedDisableProxy, cfg.Proxy.Disable) |
| 238 | + |
| 239 | + if tc.expectedTLS { |
| 240 | + require.NotNil(t, cfg.TLS) |
| 241 | + require.Equal(t, tc.expectedTLSEnabled, *cfg.TLS.Enabled) |
| 242 | + require.Equal(t, tc.expectedFingerprint, cfg.TLS.CATrustedFingerprint) |
| 243 | + } else { |
| 244 | + require.Nil(t, cfg.TLS) |
| 245 | + } |
| 246 | + } |
| 247 | +} |
0 commit comments