|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import type { RunnerMatcherConfig } from '../../../../contracts'; |
| 4 | +import { microvmDynamicLabelProvider } from './dynamic-labels'; |
| 5 | + |
| 6 | +const imageArn = 'arn:aws:lambda:eu-west-1:123456789012:microvm-image:runner-large'; |
| 7 | +const egressConnectorArn = 'arn:aws:lambda:eu-west-1:123456789012:network-connector:github-runner-private-egress'; |
| 8 | + |
| 9 | +describe('microvmDynamicLabelProvider', () => { |
| 10 | + it('accepts supported MicroVM overrides', () => { |
| 11 | + const queue = microvmQueue(); |
| 12 | + const dynamicLabels = [ |
| 13 | + `ghr-microvm-egress-network-connectors:${egressConnectorArn}`, |
| 14 | + `ghr-microvm-image-arn:${imageArn}`, |
| 15 | + 'ghr-microvm-image-version:3.0', |
| 16 | + 'ghr-microvm-maximum-duration-in-seconds:7200', |
| 17 | + ]; |
| 18 | + |
| 19 | + expect(getViolations(queue, dynamicLabels)).toEqual([]); |
| 20 | + }); |
| 21 | + |
| 22 | + it('rejects unsupported MicroVM resource overrides', () => { |
| 23 | + expect(getViolations(microvmQueue(), ['ghr-microvm-memory:8192'])).toEqual([ |
| 24 | + { |
| 25 | + label: 'ghr-microvm-memory:8192', |
| 26 | + reason: "key 'memory' is not a supported MicroVM override", |
| 27 | + }, |
| 28 | + ]); |
| 29 | + }); |
| 30 | + |
| 31 | + it('enforces the AWS dynamic-label policy', () => { |
| 32 | + const queue = microvmQueue(); |
| 33 | + queue.matcherConfig.awsDynamicLabelsPolicy = { |
| 34 | + restricted_keys: { 'maximum-duration-in-seconds': { max: 3600 } }, |
| 35 | + }; |
| 36 | + |
| 37 | + expect(getViolations(queue, ['ghr-microvm-maximum-duration-in-seconds:7200'])).toEqual([ |
| 38 | + { |
| 39 | + label: 'ghr-microvm-maximum-duration-in-seconds:7200', |
| 40 | + reason: "value '7200' exceeds max '3600'", |
| 41 | + }, |
| 42 | + ]); |
| 43 | + }); |
| 44 | + |
| 45 | + it('applies allowed patterns to the complete image ARN', () => { |
| 46 | + const queue = microvmQueue(); |
| 47 | + queue.matcherConfig.awsDynamicLabelsPolicy = { |
| 48 | + restricted_keys: { |
| 49 | + 'image-arn': { |
| 50 | + allowed: ['arn:aws:lambda:eu-west-1:123456789012:microvm-image:approved-*'], |
| 51 | + }, |
| 52 | + }, |
| 53 | + }; |
| 54 | + |
| 55 | + expect( |
| 56 | + getViolations(queue, [ |
| 57 | + 'ghr-microvm-image-arn:arn:aws:lambda:eu-west-1:123456789012:microvm-image:approved-large', |
| 58 | + ]), |
| 59 | + ).toEqual([]); |
| 60 | + expect( |
| 61 | + getViolations(queue, ['ghr-microvm-image-arn:arn:aws:lambda:eu-west-1:123456789012:microvm-image:unapproved']), |
| 62 | + ).toHaveLength(1); |
| 63 | + }); |
| 64 | + |
| 65 | + it('applies the policy to each egress connector label', () => { |
| 66 | + const queue = microvmQueue(); |
| 67 | + queue.matcherConfig.awsDynamicLabelsPolicy = { |
| 68 | + restricted_keys: { |
| 69 | + 'egress-network-connectors': { |
| 70 | + allowed: ['arn:aws:lambda:eu-west-1:123456789012:network-connector:approved-*'], |
| 71 | + }, |
| 72 | + }, |
| 73 | + }; |
| 74 | + |
| 75 | + expect( |
| 76 | + getViolations(queue, [ |
| 77 | + 'ghr-microvm-egress-network-connectors:arn:aws:lambda:eu-west-1:123456789012:network-connector:approved-private', |
| 78 | + ]), |
| 79 | + ).toEqual([]); |
| 80 | + expect( |
| 81 | + getViolations(queue, [ |
| 82 | + 'ghr-microvm-egress-network-connectors:arn:aws:lambda:eu-west-1:123456789012:network-connector:unapproved', |
| 83 | + ]), |
| 84 | + ).toHaveLength(1); |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +function getViolations(queue: RunnerMatcherConfig, labels: string[]) { |
| 89 | + return microvmDynamicLabelProvider.getViolations({ queue, labels }); |
| 90 | +} |
| 91 | + |
| 92 | +function microvmQueue(): RunnerMatcherConfig { |
| 93 | + return { |
| 94 | + id: 'microvm', |
| 95 | + arn: 'arn:aws:sqs:eu-west-1:123456789012:microvm', |
| 96 | + computeProvider: 'microvm', |
| 97 | + matcherConfig: { |
| 98 | + labelMatchers: [['self-hosted', 'linux', 'arm64', 'microvm']], |
| 99 | + exactMatch: false, |
| 100 | + enableDynamicLabels: true, |
| 101 | + }, |
| 102 | + }; |
| 103 | +} |
0 commit comments