|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +import { load as yamlLoad } from 'js-yaml'; |
| 11 | +import { |
| 12 | + getAgentImageConfig, |
| 13 | + expandAgentQueue, |
| 14 | + DEFAULT_AGENT_IMAGE_CONFIG, |
| 15 | + ELASTIC_IMAGES_QA_PROJECT, |
| 16 | + FIPS_140_3_IMAGE, |
| 17 | + FIPS_140_2_IMAGE, |
| 18 | + USE_QA_IMAGE_GH_LABEL, |
| 19 | +} from './agent_images'; |
| 20 | +import { FIPS_GH_LABELS, FIPS_VERSION } from './pr_labels'; |
| 21 | + |
| 22 | +const mockSetAnnotation = jest.fn(); |
| 23 | + |
| 24 | +jest.mock('./buildkite', () => { |
| 25 | + const actual = jest.requireActual('./buildkite'); |
| 26 | + return { |
| 27 | + ...actual, |
| 28 | + BuildkiteClient: jest.fn().mockImplementation(() => ({ |
| 29 | + setAnnotation: mockSetAnnotation, |
| 30 | + })), |
| 31 | + }; |
| 32 | +}); |
| 33 | + |
| 34 | +const ORIGINAL_ENV = process.env; |
| 35 | + |
| 36 | +describe('agent_images', () => { |
| 37 | + beforeEach(() => { |
| 38 | + jest.clearAllMocks(); |
| 39 | + process.env = { ...ORIGINAL_ENV }; |
| 40 | + delete process.env.GITHUB_PR_LABELS; |
| 41 | + delete process.env.TEST_ENABLE_FIPS_VERSION; |
| 42 | + delete process.env.USE_QA_IMAGE_FOR_PR; |
| 43 | + }); |
| 44 | + |
| 45 | + afterAll(() => { |
| 46 | + process.env = ORIGINAL_ENV; |
| 47 | + }); |
| 48 | + |
| 49 | + describe('getAgentImageConfig', () => { |
| 50 | + it('returns default config when no FIPS or QA env is set', () => { |
| 51 | + const config = getAgentImageConfig(); |
| 52 | + |
| 53 | + expect(config).toEqual(DEFAULT_AGENT_IMAGE_CONFIG); |
| 54 | + expect(mockSetAnnotation).not.toHaveBeenCalled(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('returns FIPS 140-2 image when env is set', () => { |
| 58 | + process.env.TEST_ENABLE_FIPS_VERSION = FIPS_VERSION.TWO; |
| 59 | + const config = getAgentImageConfig(); |
| 60 | + |
| 61 | + expect(config).toEqual( |
| 62 | + expect.objectContaining({ |
| 63 | + image: expect.stringContaining(FIPS_140_2_IMAGE), |
| 64 | + imageProject: DEFAULT_AGENT_IMAGE_CONFIG.imageProject, |
| 65 | + }) |
| 66 | + ); |
| 67 | + expect(mockSetAnnotation).toHaveBeenCalledWith( |
| 68 | + 'agent image config', |
| 69 | + 'info', |
| 70 | + expect.stringContaining('FIPS Agents Enabled') |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + it('returns FIPS 140-3 image when env is set', () => { |
| 75 | + process.env.TEST_ENABLE_FIPS_VERSION = FIPS_VERSION.THREE; |
| 76 | + const config = getAgentImageConfig(); |
| 77 | + |
| 78 | + expect(config).toEqual( |
| 79 | + expect.objectContaining({ |
| 80 | + image: expect.stringContaining(FIPS_140_3_IMAGE), |
| 81 | + }) |
| 82 | + ); |
| 83 | + expect(mockSetAnnotation).toHaveBeenCalled(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('returns FIPS image when FIPS label is present', () => { |
| 87 | + process.env.GITHUB_PR_LABELS = FIPS_GH_LABELS[FIPS_VERSION.TWO]; |
| 88 | + const config = getAgentImageConfig(); |
| 89 | + |
| 90 | + expect(config).toEqual( |
| 91 | + expect.objectContaining({ |
| 92 | + image: expect.stringContaining(FIPS_140_2_IMAGE), |
| 93 | + }) |
| 94 | + ); |
| 95 | + expect(mockSetAnnotation).toHaveBeenCalled(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('uses QA image project when env is set', () => { |
| 99 | + process.env.USE_QA_IMAGE_FOR_PR = 'true'; |
| 100 | + const config = getAgentImageConfig(); |
| 101 | + |
| 102 | + expect(config).toEqual( |
| 103 | + expect.objectContaining({ |
| 104 | + imageProject: ELASTIC_IMAGES_QA_PROJECT, |
| 105 | + }) |
| 106 | + ); |
| 107 | + }); |
| 108 | + |
| 109 | + it('uses QA image project when label is set', () => { |
| 110 | + process.env.GITHUB_PR_LABELS = USE_QA_IMAGE_GH_LABEL; |
| 111 | + const config = getAgentImageConfig(); |
| 112 | + |
| 113 | + expect(config).toEqual( |
| 114 | + expect.objectContaining({ |
| 115 | + imageProject: ELASTIC_IMAGES_QA_PROJECT, |
| 116 | + }) |
| 117 | + ); |
| 118 | + }); |
| 119 | + |
| 120 | + it('returns valid YAML when returnYaml is true', () => { |
| 121 | + const yaml = getAgentImageConfig({ returnYaml: true }); |
| 122 | + |
| 123 | + expect(typeof yaml).toBe('string'); |
| 124 | + const parsed = yamlLoad(yaml) as Record<string, unknown>; |
| 125 | + expect(parsed).toHaveProperty('agents'); |
| 126 | + const agents = parsed.agents as Record<string, unknown>; |
| 127 | + expect(agents).toHaveProperty('provider', DEFAULT_AGENT_IMAGE_CONFIG.provider); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + describe('expandAgentQueue', () => { |
| 132 | + it('returns spot config for default queue', () => { |
| 133 | + const config = expandAgentQueue(); |
| 134 | + |
| 135 | + expect(config).toEqual( |
| 136 | + expect.objectContaining({ |
| 137 | + machineType: 'n2-standard-4', |
| 138 | + preemptible: true, |
| 139 | + provider: DEFAULT_AGENT_IMAGE_CONFIG.provider, |
| 140 | + }) |
| 141 | + ); |
| 142 | + }); |
| 143 | + |
| 144 | + it('returns virt config for virt queue', () => { |
| 145 | + const config = expandAgentQueue('n2-4-virt'); |
| 146 | + |
| 147 | + expect(config).toEqual( |
| 148 | + expect.objectContaining({ |
| 149 | + machineType: 'n2-standard-4', |
| 150 | + enableNestedVirtualization: true, |
| 151 | + }) |
| 152 | + ); |
| 153 | + }); |
| 154 | + |
| 155 | + it('uses custom disk size when provided', () => { |
| 156 | + const config = expandAgentQueue('n2-4-spot', 200); |
| 157 | + |
| 158 | + expect(config.diskSizeGb).toBe(200); |
| 159 | + }); |
| 160 | + |
| 161 | + it('returns base config for queue without spot or virt suffix', () => { |
| 162 | + const config = expandAgentQueue('c2-8'); |
| 163 | + |
| 164 | + expect(config).toEqual( |
| 165 | + expect.objectContaining({ |
| 166 | + machineType: 'c2-standard-8', |
| 167 | + provider: DEFAULT_AGENT_IMAGE_CONFIG.provider, |
| 168 | + }) |
| 169 | + ); |
| 170 | + expect(config).not.toHaveProperty('preemptible'); |
| 171 | + expect(config).not.toHaveProperty('enableNestedVirtualization'); |
| 172 | + }); |
| 173 | + }); |
| 174 | +}); |
0 commit comments