|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import { |
| 21 | + packageMock, |
| 22 | + mockedRootDir, |
| 23 | + gitRevExecMock, |
| 24 | + devConfigMock, |
| 25 | + readUuidFileMock, |
| 26 | + resetAllMocks, |
| 27 | +} from './config.test.mocks'; |
| 28 | + |
| 29 | +import { ApmConfiguration } from './config'; |
| 30 | + |
| 31 | +describe('ApmConfiguration', () => { |
| 32 | + beforeEach(() => { |
| 33 | + packageMock.raw = { |
| 34 | + version: '8.0.0', |
| 35 | + build: { |
| 36 | + sha: 'sha', |
| 37 | + }, |
| 38 | + }; |
| 39 | + }); |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + resetAllMocks(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('sets the correct service name', () => { |
| 46 | + packageMock.raw = { |
| 47 | + version: '9.2.1', |
| 48 | + }; |
| 49 | + const config = new ApmConfiguration(mockedRootDir, {}, false); |
| 50 | + expect(config.getConfig('myservice').serviceName).toBe('myservice-9_2_1'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('sets the git revision from `git rev-parse` command in non distribution mode', () => { |
| 54 | + gitRevExecMock.mockReturnValue('some-git-rev'); |
| 55 | + const config = new ApmConfiguration(mockedRootDir, {}, false); |
| 56 | + expect(config.getConfig('serviceName').globalLabels.git_rev).toBe('some-git-rev'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('sets the git revision from `pkg.build.sha` in distribution mode', () => { |
| 60 | + gitRevExecMock.mockReturnValue('dev-sha'); |
| 61 | + packageMock.raw = { |
| 62 | + version: '9.2.1', |
| 63 | + build: { |
| 64 | + sha: 'distribution-sha', |
| 65 | + }, |
| 66 | + }; |
| 67 | + const config = new ApmConfiguration(mockedRootDir, {}, true); |
| 68 | + expect(config.getConfig('serviceName').globalLabels.git_rev).toBe('distribution-sha'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('reads the kibana uuid from the uuid file', () => { |
| 72 | + readUuidFileMock.mockReturnValue('instance-uuid'); |
| 73 | + const config = new ApmConfiguration(mockedRootDir, {}, false); |
| 74 | + expect(config.getConfig('serviceName').globalLabels.kibana_uuid).toBe('instance-uuid'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('uses the uuid from the kibana config if present', () => { |
| 78 | + readUuidFileMock.mockReturnValue('uuid-from-file'); |
| 79 | + const kibanaConfig = { |
| 80 | + server: { |
| 81 | + uuid: 'uuid-from-config', |
| 82 | + }, |
| 83 | + }; |
| 84 | + const config = new ApmConfiguration(mockedRootDir, kibanaConfig, false); |
| 85 | + expect(config.getConfig('serviceName').globalLabels.kibana_uuid).toBe('uuid-from-config'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('uses the correct default config depending on the `isDistributable` parameter', () => { |
| 89 | + let config = new ApmConfiguration(mockedRootDir, {}, false); |
| 90 | + expect(config.getConfig('serviceName')).toEqual( |
| 91 | + expect.objectContaining({ |
| 92 | + serverUrl: expect.any(String), |
| 93 | + secretToken: expect.any(String), |
| 94 | + }) |
| 95 | + ); |
| 96 | + |
| 97 | + config = new ApmConfiguration(mockedRootDir, {}, true); |
| 98 | + expect(Object.keys(config.getConfig('serviceName'))).not.toContain('serverUrl'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('loads the configuration from the kibana config file', () => { |
| 102 | + const kibanaConfig = { |
| 103 | + elastic: { |
| 104 | + apm: { |
| 105 | + active: true, |
| 106 | + serverUrl: 'https://url', |
| 107 | + secretToken: 'secret', |
| 108 | + }, |
| 109 | + }, |
| 110 | + }; |
| 111 | + const config = new ApmConfiguration(mockedRootDir, kibanaConfig, true); |
| 112 | + expect(config.getConfig('serviceName')).toEqual( |
| 113 | + expect.objectContaining({ |
| 114 | + active: true, |
| 115 | + serverUrl: 'https://url', |
| 116 | + secretToken: 'secret', |
| 117 | + }) |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + it('loads the configuration from the dev config is present', () => { |
| 122 | + devConfigMock.raw = { |
| 123 | + active: true, |
| 124 | + serverUrl: 'https://dev-url.co', |
| 125 | + }; |
| 126 | + const config = new ApmConfiguration(mockedRootDir, {}, true); |
| 127 | + expect(config.getConfig('serviceName')).toEqual( |
| 128 | + expect.objectContaining({ |
| 129 | + active: true, |
| 130 | + serverUrl: 'https://dev-url.co', |
| 131 | + }) |
| 132 | + ); |
| 133 | + }); |
| 134 | + |
| 135 | + it('respect the precedence of the dev config', () => { |
| 136 | + const kibanaConfig = { |
| 137 | + elastic: { |
| 138 | + apm: { |
| 139 | + active: true, |
| 140 | + serverUrl: 'https://url', |
| 141 | + secretToken: 'secret', |
| 142 | + }, |
| 143 | + }, |
| 144 | + }; |
| 145 | + devConfigMock.raw = { |
| 146 | + active: true, |
| 147 | + serverUrl: 'https://dev-url.co', |
| 148 | + }; |
| 149 | + const config = new ApmConfiguration(mockedRootDir, kibanaConfig, true); |
| 150 | + expect(config.getConfig('serviceName')).toEqual( |
| 151 | + expect.objectContaining({ |
| 152 | + active: true, |
| 153 | + serverUrl: 'https://dev-url.co', |
| 154 | + secretToken: 'secret', |
| 155 | + }) |
| 156 | + ); |
| 157 | + }); |
| 158 | +}); |
0 commit comments