|
| 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( |
| 20 | + microvmDynamicLabelProvider.selectQueue({ |
| 21 | + queue, |
| 22 | + nonGhrLabels: ['self-hosted', 'microvm'], |
| 23 | + sanitizedGhrLabels: dynamicLabels, |
| 24 | + }), |
| 25 | + ).toEqual({ queue, labels: ['self-hosted', 'microvm', ...dynamicLabels] }); |
| 26 | + }); |
| 27 | + |
| 28 | + it('rejects unsupported MicroVM resource overrides', () => { |
| 29 | + expect( |
| 30 | + microvmDynamicLabelProvider.selectQueue({ |
| 31 | + queue: microvmQueue(), |
| 32 | + nonGhrLabels: ['self-hosted', 'microvm'], |
| 33 | + sanitizedGhrLabels: ['ghr-microvm-memory:8192'], |
| 34 | + }), |
| 35 | + ).toBeUndefined(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('rejects labels owned by another runner provider', () => { |
| 39 | + expect( |
| 40 | + microvmDynamicLabelProvider.selectQueue({ |
| 41 | + queue: microvmQueue(), |
| 42 | + nonGhrLabels: ['self-hosted', 'microvm'], |
| 43 | + sanitizedGhrLabels: ['ghr-ec2-instance-type:m7i.large'], |
| 44 | + }), |
| 45 | + ).toBeUndefined(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('requires dynamic labels to be enabled for the queue', () => { |
| 49 | + const queue = microvmQueue(); |
| 50 | + queue.matcherConfig.enableDynamicLabels = false; |
| 51 | + |
| 52 | + expect( |
| 53 | + microvmDynamicLabelProvider.selectQueue({ |
| 54 | + queue, |
| 55 | + nonGhrLabels: ['self-hosted', 'microvm'], |
| 56 | + sanitizedGhrLabels: ['ghr-microvm-image-version:3.0'], |
| 57 | + }), |
| 58 | + ).toBeUndefined(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('enforces the AWS dynamic-label policy', () => { |
| 62 | + const queue = microvmQueue(); |
| 63 | + queue.matcherConfig.awsDynamicLabelsPolicy = { |
| 64 | + restricted_keys: { 'maximum-duration-in-seconds': { max: 3600 } }, |
| 65 | + }; |
| 66 | + |
| 67 | + expect( |
| 68 | + microvmDynamicLabelProvider.selectQueue({ |
| 69 | + queue, |
| 70 | + nonGhrLabels: ['self-hosted', 'microvm'], |
| 71 | + sanitizedGhrLabels: ['ghr-microvm-maximum-duration-in-seconds:7200'], |
| 72 | + }), |
| 73 | + ).toBeUndefined(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('applies allowed patterns to the complete image ARN', () => { |
| 77 | + const queue = microvmQueue(); |
| 78 | + queue.matcherConfig.awsDynamicLabelsPolicy = { |
| 79 | + restricted_keys: { |
| 80 | + 'image-arn': { |
| 81 | + allowed: ['arn:aws:lambda:eu-west-1:123456789012:microvm-image:approved-*'], |
| 82 | + }, |
| 83 | + }, |
| 84 | + }; |
| 85 | + |
| 86 | + expect( |
| 87 | + microvmDynamicLabelProvider.selectQueue({ |
| 88 | + queue, |
| 89 | + nonGhrLabels: ['self-hosted', 'microvm'], |
| 90 | + sanitizedGhrLabels: [ |
| 91 | + 'ghr-microvm-image-arn:arn:aws:lambda:eu-west-1:123456789012:microvm-image:approved-large', |
| 92 | + ], |
| 93 | + }), |
| 94 | + ).toBeDefined(); |
| 95 | + expect( |
| 96 | + microvmDynamicLabelProvider.selectQueue({ |
| 97 | + queue, |
| 98 | + nonGhrLabels: ['self-hosted', 'microvm'], |
| 99 | + sanitizedGhrLabels: ['ghr-microvm-image-arn:arn:aws:lambda:eu-west-1:123456789012:microvm-image:unapproved'], |
| 100 | + }), |
| 101 | + ).toBeUndefined(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('applies the policy to each egress connector label', () => { |
| 105 | + const queue = microvmQueue(); |
| 106 | + queue.matcherConfig.awsDynamicLabelsPolicy = { |
| 107 | + restricted_keys: { |
| 108 | + 'egress-network-connectors': { |
| 109 | + allowed: ['arn:aws:lambda:eu-west-1:123456789012:network-connector:approved-*'], |
| 110 | + }, |
| 111 | + }, |
| 112 | + }; |
| 113 | + |
| 114 | + expect( |
| 115 | + microvmDynamicLabelProvider.selectQueue({ |
| 116 | + queue, |
| 117 | + nonGhrLabels: ['self-hosted', 'microvm'], |
| 118 | + sanitizedGhrLabels: [ |
| 119 | + 'ghr-microvm-egress-network-connectors:arn:aws:lambda:eu-west-1:123456789012:network-connector:approved-private', |
| 120 | + ], |
| 121 | + }), |
| 122 | + ).toBeDefined(); |
| 123 | + expect( |
| 124 | + microvmDynamicLabelProvider.selectQueue({ |
| 125 | + queue, |
| 126 | + nonGhrLabels: ['self-hosted', 'microvm'], |
| 127 | + sanitizedGhrLabels: [ |
| 128 | + 'ghr-microvm-egress-network-connectors:arn:aws:lambda:eu-west-1:123456789012:network-connector:unapproved', |
| 129 | + ], |
| 130 | + }), |
| 131 | + ).toBeUndefined(); |
| 132 | + }); |
| 133 | +}); |
| 134 | + |
| 135 | +function microvmQueue(): RunnerMatcherConfig { |
| 136 | + return { |
| 137 | + id: 'microvm', |
| 138 | + arn: 'arn:aws:sqs:eu-west-1:123456789012:microvm', |
| 139 | + runnerProvider: 'microvm', |
| 140 | + matcherConfig: { |
| 141 | + labelMatchers: [['self-hosted', 'linux', 'arm64', 'microvm']], |
| 142 | + exactMatch: false, |
| 143 | + enableDynamicLabels: true, |
| 144 | + }, |
| 145 | + }; |
| 146 | +} |
0 commit comments