|
| 1 | +import * as fs from 'fs' |
| 2 | +import { cleanupJob } from '../src/hooks' |
| 3 | +import { prepareJob } from '../src/hooks/prepare-job' |
| 4 | +import { TestHelper } from './test-setup' |
| 5 | +import { getPodByName } from '../src/k8s' |
| 6 | +import { ENV_USE_KUBE_SCHEDULER } from '../src/k8s/utils' |
| 7 | + |
| 8 | +jest.useRealTimers() |
| 9 | + |
| 10 | +let testHelper: TestHelper |
| 11 | +let prepareJobData: any |
| 12 | +let prepareJobOutputFilePath: string |
| 13 | + |
| 14 | +describe('RWO Affinity Behavior (Scheduler Mode)', () => { |
| 15 | + beforeEach(async () => { |
| 16 | + testHelper = new TestHelper() |
| 17 | + await testHelper.initialize() |
| 18 | + prepareJobData = testHelper.getPrepareJobDefinition() |
| 19 | + prepareJobOutputFilePath = testHelper.createFile('prepare-job-output.json') |
| 20 | + }) |
| 21 | + |
| 22 | + afterEach(async () => { |
| 23 | + await cleanupJob() |
| 24 | + await testHelper.cleanup() |
| 25 | + delete process.env[ENV_USE_KUBE_SCHEDULER] |
| 26 | + }) |
| 27 | + |
| 28 | + it('should add nodeAffinity with hostname selector when scheduler mode is enabled', async () => { |
| 29 | + process.env[ENV_USE_KUBE_SCHEDULER] = 'true' |
| 30 | + |
| 31 | + await prepareJob(prepareJobData.args, prepareJobOutputFilePath) |
| 32 | + |
| 33 | + const content = JSON.parse( |
| 34 | + fs.readFileSync(prepareJobOutputFilePath).toString() |
| 35 | + ) |
| 36 | + |
| 37 | + const pod = await getPodByName(content.state.jobPod) |
| 38 | + |
| 39 | + expect(pod.spec?.affinity).toBeDefined() |
| 40 | + expect(pod.spec?.affinity?.nodeAffinity).toBeDefined() |
| 41 | + |
| 42 | + const nodeAffinity = pod.spec?.affinity?.nodeAffinity |
| 43 | + expect( |
| 44 | + nodeAffinity?.requiredDuringSchedulingIgnoredDuringExecution |
| 45 | + ).toBeDefined() |
| 46 | + |
| 47 | + const nodeSelectorTerms = |
| 48 | + nodeAffinity?.requiredDuringSchedulingIgnoredDuringExecution |
| 49 | + ?.nodeSelectorTerms |
| 50 | + |
| 51 | + expect(nodeSelectorTerms).toBeDefined() |
| 52 | + expect(nodeSelectorTerms?.length).toBeGreaterThan(0) |
| 53 | + |
| 54 | + const matchExpressions = nodeSelectorTerms?.[0].matchExpressions |
| 55 | + expect(matchExpressions).toBeDefined() |
| 56 | + expect(matchExpressions?.length).toBeGreaterThan(0) |
| 57 | + |
| 58 | + const hostnameExpression = matchExpressions?.[0] |
| 59 | + expect(hostnameExpression?.key).toBe('kubernetes.io/hostname') |
| 60 | + expect(hostnameExpression?.operator).toBe('In') |
| 61 | + |
| 62 | + expect(hostnameExpression?.values).toBeDefined() |
| 63 | + expect(hostnameExpression?.values?.length).toBeGreaterThan(0) |
| 64 | + expect(hostnameExpression?.values?.[0]).toBeTruthy() |
| 65 | + }) |
| 66 | + |
| 67 | + it('should NOT add nodeAffinity when scheduler mode is disabled', async () => { |
| 68 | + process.env[ENV_USE_KUBE_SCHEDULER] = 'false' |
| 69 | + |
| 70 | + await prepareJob(prepareJobData.args, prepareJobOutputFilePath) |
| 71 | + |
| 72 | + const content = JSON.parse( |
| 73 | + fs.readFileSync(prepareJobOutputFilePath).toString() |
| 74 | + ) |
| 75 | + |
| 76 | + const pod = await getPodByName(content.state.jobPod) |
| 77 | + |
| 78 | + if (pod.spec?.affinity) { |
| 79 | + expect(pod.spec.affinity.nodeAffinity).toBeUndefined() |
| 80 | + } |
| 81 | + |
| 82 | + expect(pod.spec?.nodeName).toBeDefined() |
| 83 | + }) |
| 84 | + |
| 85 | + it('should fail assertion if affinity block is missing when scheduler mode is enabled', async () => { |
| 86 | + process.env[ENV_USE_KUBE_SCHEDULER] = 'true' |
| 87 | + |
| 88 | + await prepareJob(prepareJobData.args, prepareJobOutputFilePath) |
| 89 | + |
| 90 | + const content = JSON.parse( |
| 91 | + fs.readFileSync(prepareJobOutputFilePath).toString() |
| 92 | + ) |
| 93 | + |
| 94 | + const pod = await getPodByName(content.state.jobPod) |
| 95 | + |
| 96 | + expect(pod.spec?.affinity).toBeDefined() |
| 97 | + expect(pod.spec?.affinity?.nodeAffinity).toBeDefined() |
| 98 | + expect( |
| 99 | + pod.spec?.affinity?.nodeAffinity |
| 100 | + ?.requiredDuringSchedulingIgnoredDuringExecution |
| 101 | + ).toBeDefined() |
| 102 | + |
| 103 | + const nodeSelectorTerms = |
| 104 | + pod.spec?.affinity?.nodeAffinity |
| 105 | + ?.requiredDuringSchedulingIgnoredDuringExecution?.nodeSelectorTerms |
| 106 | + |
| 107 | + expect(nodeSelectorTerms?.[0]?.matchExpressions?.[0]?.key).toBe( |
| 108 | + 'kubernetes.io/hostname' |
| 109 | + ) |
| 110 | + expect(nodeSelectorTerms?.[0]?.matchExpressions?.[0]?.operator).toBe('In') |
| 111 | + expect( |
| 112 | + nodeSelectorTerms?.[0]?.matchExpressions?.[0]?.values?.length |
| 113 | + ).toBeGreaterThan(0) |
| 114 | + }) |
| 115 | + |
| 116 | + it('should use correct node name from runner pod in affinity values', async () => { |
| 117 | + process.env[ENV_USE_KUBE_SCHEDULER] = 'true' |
| 118 | + |
| 119 | + const runnerPodName = process.env.ACTIONS_RUNNER_POD_NAME |
| 120 | + |
| 121 | + await prepareJob(prepareJobData.args, prepareJobOutputFilePath) |
| 122 | + |
| 123 | + const content = JSON.parse( |
| 124 | + fs.readFileSync(prepareJobOutputFilePath).toString() |
| 125 | + ) |
| 126 | + |
| 127 | + const jobPod = await getPodByName(content.state.jobPod) |
| 128 | + |
| 129 | + const runnerPod = await getPodByName(runnerPodName!) |
| 130 | + |
| 131 | + const affinityValues = |
| 132 | + jobPod.spec?.affinity?.nodeAffinity |
| 133 | + ?.requiredDuringSchedulingIgnoredDuringExecution?.nodeSelectorTerms?.[0] |
| 134 | + ?.matchExpressions?.[0]?.values |
| 135 | + |
| 136 | + expect(affinityValues).toBeDefined() |
| 137 | + expect(affinityValues?.length).toBeGreaterThan(0) |
| 138 | + |
| 139 | + if (runnerPod.spec?.nodeName) { |
| 140 | + expect(affinityValues).toContain(runnerPod.spec.nodeName) |
| 141 | + } |
| 142 | + }) |
| 143 | +}) |
0 commit comments