|
| 1 | +import { |
| 2 | + getJobPodName, |
| 3 | + getRunnerPodName, |
| 4 | + getSecretName, |
| 5 | + getStepPodName, |
| 6 | + getVolumeClaimName, |
| 7 | + MAX_POD_NAME_LENGTH, |
| 8 | + RunnerInstanceLabel, |
| 9 | + STEP_POD_NAME_SUFFIX_LENGTH |
| 10 | +} from '../src/hooks/constants' |
| 11 | + |
| 12 | +describe('constants', () => { |
| 13 | + describe('runner instance label', () => { |
| 14 | + beforeEach(() => { |
| 15 | + process.env.ACTIONS_RUNNER_POD_NAME = 'example' |
| 16 | + }) |
| 17 | + it('should throw if ACTIONS_RUNNER_POD_NAME env is not set', () => { |
| 18 | + delete process.env.ACTIONS_RUNNER_POD_NAME |
| 19 | + expect(() => new RunnerInstanceLabel()).toThrow() |
| 20 | + }) |
| 21 | + |
| 22 | + it('should have key truthy', () => { |
| 23 | + const runnerInstanceLabel = new RunnerInstanceLabel() |
| 24 | + expect(typeof runnerInstanceLabel.key).toBe('string') |
| 25 | + expect(runnerInstanceLabel.key).toBeTruthy() |
| 26 | + expect(runnerInstanceLabel.key.length).toBeGreaterThan(0) |
| 27 | + }) |
| 28 | + |
| 29 | + it('should have value as runner pod name', () => { |
| 30 | + const name = process.env.ACTIONS_RUNNER_POD_NAME as string |
| 31 | + const runnerInstanceLabel = new RunnerInstanceLabel() |
| 32 | + expect(typeof runnerInstanceLabel.value).toBe('string') |
| 33 | + expect(runnerInstanceLabel.value).toBe(name) |
| 34 | + }) |
| 35 | + |
| 36 | + it('should have toString combination of key and value', () => { |
| 37 | + const runnerInstanceLabel = new RunnerInstanceLabel() |
| 38 | + expect(runnerInstanceLabel.toString()).toBe( |
| 39 | + `${runnerInstanceLabel.key}=${runnerInstanceLabel.value}` |
| 40 | + ) |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + describe('getRunnerPodName', () => { |
| 45 | + it('should throw if ACTIONS_RUNNER_POD_NAME env is not set', () => { |
| 46 | + delete process.env.ACTIONS_RUNNER_POD_NAME |
| 47 | + expect(() => getRunnerPodName()).toThrow() |
| 48 | + |
| 49 | + process.env.ACTIONS_RUNNER_POD_NAME = '' |
| 50 | + expect(() => getRunnerPodName()).toThrow() |
| 51 | + }) |
| 52 | + |
| 53 | + it('should return corrent ACTIONS_RUNNER_POD_NAME name', () => { |
| 54 | + const name = 'example' |
| 55 | + process.env.ACTIONS_RUNNER_POD_NAME = name |
| 56 | + expect(getRunnerPodName()).toBe(name) |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + describe('getJobPodName', () => { |
| 61 | + it('should throw on getJobPodName if ACTIONS_RUNNER_POD_NAME env is not set', () => { |
| 62 | + delete process.env.ACTIONS_RUNNER_POD_NAME |
| 63 | + expect(() => getJobPodName()).toThrow() |
| 64 | + |
| 65 | + process.env.ACTIONS_RUNNER_POD_NAME = '' |
| 66 | + expect(() => getRunnerPodName()).toThrow() |
| 67 | + }) |
| 68 | + |
| 69 | + it('should contain suffix -workflow', () => { |
| 70 | + const tableTests = [ |
| 71 | + { |
| 72 | + podName: 'test', |
| 73 | + expect: 'test-workflow' |
| 74 | + }, |
| 75 | + { |
| 76 | + // podName.length == 63 |
| 77 | + podName: |
| 78 | + 'abcdaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', |
| 79 | + expect: |
| 80 | + 'abcdaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-workflow' |
| 81 | + } |
| 82 | + ] |
| 83 | + |
| 84 | + for (const tt of tableTests) { |
| 85 | + process.env.ACTIONS_RUNNER_POD_NAME = tt.podName |
| 86 | + const actual = getJobPodName() |
| 87 | + expect(actual).toBe(tt.expect) |
| 88 | + } |
| 89 | + }) |
| 90 | + }) |
| 91 | + |
| 92 | + describe('getVolumeClaimName', () => { |
| 93 | + it('should throw if ACTIONS_RUNNER_POD_NAME env is not set', () => { |
| 94 | + delete process.env.ACTIONS_RUNNER_CLAIM_NAME |
| 95 | + delete process.env.ACTIONS_RUNNER_POD_NAME |
| 96 | + expect(() => getVolumeClaimName()).toThrow() |
| 97 | + |
| 98 | + process.env.ACTIONS_RUNNER_POD_NAME = '' |
| 99 | + expect(() => getVolumeClaimName()).toThrow() |
| 100 | + }) |
| 101 | + |
| 102 | + it('should return ACTIONS_RUNNER_CLAIM_NAME env if set', () => { |
| 103 | + const claimName = 'testclaim' |
| 104 | + process.env.ACTIONS_RUNNER_CLAIM_NAME = claimName |
| 105 | + process.env.ACTIONS_RUNNER_POD_NAME = 'example' |
| 106 | + expect(getVolumeClaimName()).toBe(claimName) |
| 107 | + }) |
| 108 | + |
| 109 | + it('should contain suffix -work if ACTIONS_RUNNER_CLAIM_NAME is not set', () => { |
| 110 | + delete process.env.ACTIONS_RUNNER_CLAIM_NAME |
| 111 | + process.env.ACTIONS_RUNNER_POD_NAME = 'example' |
| 112 | + expect(getVolumeClaimName()).toBe('example-work') |
| 113 | + }) |
| 114 | + }) |
| 115 | + |
| 116 | + describe('getSecretName', () => { |
| 117 | + it('should throw if ACTIONS_RUNNER_POD_NAME env is not set', () => { |
| 118 | + delete process.env.ACTIONS_RUNNER_POD_NAME |
| 119 | + expect(() => getSecretName()).toThrow() |
| 120 | + |
| 121 | + process.env.ACTIONS_RUNNER_POD_NAME = '' |
| 122 | + expect(() => getSecretName()).toThrow() |
| 123 | + }) |
| 124 | + |
| 125 | + it('should contain suffix -secret- and name trimmed', () => { |
| 126 | + const podNames = [ |
| 127 | + 'test', |
| 128 | + 'abcdaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' |
| 129 | + ] |
| 130 | + |
| 131 | + for (const podName of podNames) { |
| 132 | + process.env.ACTIONS_RUNNER_POD_NAME = podName |
| 133 | + const actual = getSecretName() |
| 134 | + const re = new RegExp( |
| 135 | + `${podName.substring( |
| 136 | + MAX_POD_NAME_LENGTH - |
| 137 | + '-secret-'.length - |
| 138 | + STEP_POD_NAME_SUFFIX_LENGTH |
| 139 | + )}-secret-[a-z0-9]{8,}` |
| 140 | + ) |
| 141 | + expect(actual).toMatch(re) |
| 142 | + } |
| 143 | + }) |
| 144 | + }) |
| 145 | + |
| 146 | + describe('getStepPodName', () => { |
| 147 | + it('should throw if ACTIONS_RUNNER_POD_NAME env is not set', () => { |
| 148 | + delete process.env.ACTIONS_RUNNER_POD_NAME |
| 149 | + expect(() => getStepPodName()).toThrow() |
| 150 | + |
| 151 | + process.env.ACTIONS_RUNNER_POD_NAME = '' |
| 152 | + expect(() => getStepPodName()).toThrow() |
| 153 | + }) |
| 154 | + |
| 155 | + it('should contain suffix -step- and name trimmed', () => { |
| 156 | + const podNames = [ |
| 157 | + 'test', |
| 158 | + 'abcdaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' |
| 159 | + ] |
| 160 | + |
| 161 | + for (const podName of podNames) { |
| 162 | + process.env.ACTIONS_RUNNER_POD_NAME = podName |
| 163 | + const actual = getStepPodName() |
| 164 | + const re = new RegExp( |
| 165 | + `${podName.substring( |
| 166 | + MAX_POD_NAME_LENGTH - '-step-'.length - STEP_POD_NAME_SUFFIX_LENGTH |
| 167 | + )}-step-[a-z0-9]{8,}` |
| 168 | + ) |
| 169 | + expect(actual).toMatch(re) |
| 170 | + } |
| 171 | + }) |
| 172 | + }) |
| 173 | +}) |
0 commit comments