|
5 | 5 | * in compliance with, at your election, the Elastic License 2.0 or the Server
|
6 | 6 | * Side Public License, v 1.
|
7 | 7 | */
|
8 |
| -import type { Labels } from 'elastic-apm-node'; |
| 8 | +import type { AgentConfigOptions, Labels } from 'elastic-apm-node'; |
9 | 9 | import {
|
10 | 10 | packageMock,
|
11 | 11 | mockedRootDir,
|
12 | 12 | gitRevExecMock,
|
13 |
| - devConfigMock, |
14 | 13 | readUuidFileMock,
|
15 | 14 | resetAllMocks,
|
16 | 15 | } from './config.test.mocks';
|
17 | 16 |
|
18 |
| -import { ApmConfiguration } from './config'; |
| 17 | +import { ApmConfiguration, CENTRALIZED_SERVICE_BASE_CONFIG } from './config'; |
19 | 18 |
|
20 | 19 | describe('ApmConfiguration', () => {
|
21 | 20 | beforeEach(() => {
|
22 | 21 | // start with an empty env to avoid CI from spoiling snapshots, env is unique for each jest file
|
23 | 22 | process.env = {};
|
24 |
| - |
25 | 23 | packageMock.raw = {
|
26 | 24 | version: '8.0.0',
|
27 | 25 | build: {
|
@@ -148,82 +146,57 @@ describe('ApmConfiguration', () => {
|
148 | 146 | );
|
149 | 147 | });
|
150 | 148 |
|
151 |
| - it('loads the configuration from the dev config is present', () => { |
152 |
| - devConfigMock.raw = { |
153 |
| - active: true, |
154 |
| - serverUrl: 'https://dev-url.co', |
155 |
| - }; |
156 |
| - const config = new ApmConfiguration(mockedRootDir, {}, false); |
157 |
| - expect(config.getConfig('serviceName')).toEqual( |
158 |
| - expect.objectContaining({ |
159 |
| - active: true, |
160 |
| - serverUrl: 'https://dev-url.co', |
161 |
| - }) |
162 |
| - ); |
163 |
| - }); |
164 |
| - |
165 |
| - it('does not load the configuration from the dev config in distributable', () => { |
166 |
| - devConfigMock.raw = { |
167 |
| - active: true, |
168 |
| - serverUrl: 'https://dev-url.co', |
169 |
| - }; |
170 |
| - const config = new ApmConfiguration(mockedRootDir, {}, true); |
171 |
| - expect(config.getConfig('serviceName')).toEqual( |
172 |
| - expect.objectContaining({ |
173 |
| - active: false, |
174 |
| - }) |
175 |
| - ); |
176 |
| - }); |
177 |
| - |
178 |
| - it('overwrites the standard config file with the dev config', () => { |
179 |
| - const kibanaConfig = { |
180 |
| - elastic: { |
181 |
| - apm: { |
182 |
| - active: true, |
183 |
| - serverUrl: 'https://url', |
184 |
| - secretToken: 'secret', |
185 |
| - }, |
186 |
| - }, |
187 |
| - }; |
188 |
| - devConfigMock.raw = { |
189 |
| - active: true, |
190 |
| - serverUrl: 'https://dev-url.co', |
191 |
| - }; |
192 |
| - const config = new ApmConfiguration(mockedRootDir, kibanaConfig, false); |
193 |
| - expect(config.getConfig('serviceName')).toEqual( |
194 |
| - expect.objectContaining({ |
195 |
| - active: true, |
196 |
| - serverUrl: 'https://dev-url.co', |
197 |
| - secretToken: 'secret', |
198 |
| - }) |
199 |
| - ); |
200 |
| - }); |
201 |
| - |
202 |
| - it('correctly sets environment by reading env vars', () => { |
203 |
| - delete process.env.ELASTIC_APM_ENVIRONMENT; |
204 |
| - delete process.env.NODE_ENV; |
205 |
| - |
206 |
| - let config = new ApmConfiguration(mockedRootDir, {}, false); |
207 |
| - expect(config.getConfig('serviceName')).toEqual( |
208 |
| - expect.objectContaining({ |
209 |
| - environment: 'development', |
210 |
| - }) |
211 |
| - ); |
212 |
| - |
213 |
| - process.env.NODE_ENV = 'production'; |
214 |
| - config = new ApmConfiguration(mockedRootDir, {}, false); |
215 |
| - expect(config.getConfig('serviceName')).toEqual( |
216 |
| - expect.objectContaining({ |
217 |
| - environment: 'production', |
218 |
| - }) |
219 |
| - ); |
220 |
| - |
221 |
| - process.env.ELASTIC_APM_ENVIRONMENT = 'ci'; |
222 |
| - config = new ApmConfiguration(mockedRootDir, {}, false); |
223 |
| - expect(config.getConfig('serviceName')).toEqual( |
224 |
| - expect.objectContaining({ |
225 |
| - environment: 'ci', |
226 |
| - }) |
227 |
| - ); |
| 149 | + describe('env vars', () => { |
| 150 | + beforeEach(() => { |
| 151 | + delete process.env.ELASTIC_APM_ENVIRONMENT; |
| 152 | + delete process.env.ELASTIC_APM_SECRET_TOKEN; |
| 153 | + delete process.env.ELASTIC_APM_SERVER_URL; |
| 154 | + delete process.env.NODE_ENV; |
| 155 | + }); |
| 156 | + |
| 157 | + it('correctly sets environment by reading env vars', () => { |
| 158 | + let config = new ApmConfiguration(mockedRootDir, {}, false); |
| 159 | + expect(config.getConfig('serviceName')).toEqual( |
| 160 | + expect.objectContaining({ |
| 161 | + environment: 'development', |
| 162 | + }) |
| 163 | + ); |
| 164 | + |
| 165 | + process.env.NODE_ENV = 'production'; |
| 166 | + config = new ApmConfiguration(mockedRootDir, {}, false); |
| 167 | + expect(config.getConfig('serviceName')).toEqual( |
| 168 | + expect.objectContaining({ |
| 169 | + environment: 'production', |
| 170 | + }) |
| 171 | + ); |
| 172 | + |
| 173 | + process.env.ELASTIC_APM_ENVIRONMENT = 'ci'; |
| 174 | + config = new ApmConfiguration(mockedRootDir, {}, false); |
| 175 | + expect(config.getConfig('serviceName')).toEqual( |
| 176 | + expect.objectContaining({ |
| 177 | + environment: 'ci', |
| 178 | + }) |
| 179 | + ); |
| 180 | + }); |
| 181 | + |
| 182 | + it('uses default config if serverUrl is not set', () => { |
| 183 | + process.env.ELASTIC_APM_SECRET_TOKEN = 'banana'; |
| 184 | + const config = new ApmConfiguration(mockedRootDir, {}, false); |
| 185 | + const serverConfig = config.getConfig('serviceName'); |
| 186 | + expect(serverConfig).toHaveProperty( |
| 187 | + 'secretToken', |
| 188 | + (CENTRALIZED_SERVICE_BASE_CONFIG as AgentConfigOptions).secretToken |
| 189 | + ); |
| 190 | + expect(serverConfig).toHaveProperty('serverUrl', CENTRALIZED_SERVICE_BASE_CONFIG.serverUrl); |
| 191 | + }); |
| 192 | + |
| 193 | + it('uses env vars config if serverUrl is set', () => { |
| 194 | + process.env.ELASTIC_APM_SECRET_TOKEN = 'banana'; |
| 195 | + process.env.ELASTIC_APM_SERVER_URL = 'http://banana.com/'; |
| 196 | + const config = new ApmConfiguration(mockedRootDir, {}, false); |
| 197 | + const serverConfig = config.getConfig('serviceName'); |
| 198 | + expect(serverConfig).toHaveProperty('secretToken', process.env.ELASTIC_APM_SECRET_TOKEN); |
| 199 | + expect(serverConfig).toHaveProperty('serverUrl', process.env.ELASTIC_APM_SERVER_URL); |
| 200 | + }); |
228 | 201 | });
|
229 | 202 | });
|
0 commit comments