|
| 1 | +/** |
| 2 | + * MIT No Attribution |
| 3 | + * |
| 4 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 7 | + * the Software without restriction, including without limitation the rights to |
| 8 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 9 | + * the Software, and to permit persons to whom the Software is furnished to do so. |
| 10 | + * |
| 11 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 12 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 14 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 15 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 16 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 17 | + * SOFTWARE. |
| 18 | + */ |
| 19 | + |
| 20 | +import { App, Tags } from 'aws-cdk-lib'; |
| 21 | +import { Template } from 'aws-cdk-lib/assertions'; |
| 22 | +import { AgentStack } from '../../src/stacks/agent'; |
| 23 | + |
| 24 | +const GITHUB_TAG_KEYS = [ |
| 25 | + 'github:sha', |
| 26 | + 'github:ref', |
| 27 | + 'github:ref-type', |
| 28 | + 'github:actor', |
| 29 | + 'github:head-ref', |
| 30 | + 'github:run-id', |
| 31 | + 'github:event', |
| 32 | + 'github:repository', |
| 33 | + 'github:clean', |
| 34 | +] as const; |
| 35 | + |
| 36 | +function synthWithTags(context: Record<string, string> = {}): Template { |
| 37 | + const app = new App({ context }); |
| 38 | + const stackName = app.node.tryGetContext('stackName') ?? 'backgroundagent-dev'; |
| 39 | + const stack = new AgentStack(app, stackName, { |
| 40 | + env: { account: '123456789012', region: 'us-east-1' }, |
| 41 | + }); |
| 42 | + |
| 43 | + const githubTagKeys = [ |
| 44 | + 'sha', 'ref', 'ref-type', 'actor', 'head-ref', |
| 45 | + 'run-id', 'event', 'repository', 'clean', |
| 46 | + ] as const; |
| 47 | + |
| 48 | + for (const key of githubTagKeys) { |
| 49 | + const value = app.node.tryGetContext(`github:${key}`); |
| 50 | + Tags.of(stack).add(`github:${key}`, value || 'none'); |
| 51 | + } |
| 52 | + |
| 53 | + return Template.fromStack(stack); |
| 54 | +} |
| 55 | + |
| 56 | +describe('github:* resource tags', () => { |
| 57 | + let templateWithDefaults: Template; |
| 58 | + let templateWithValues: Template; |
| 59 | + |
| 60 | + beforeAll(() => { |
| 61 | + templateWithDefaults = synthWithTags(); |
| 62 | + templateWithValues = synthWithTags({ |
| 63 | + 'github:sha': 'f36d352c5a1bc3a90d3e60e30e2f9d4345426724', |
| 64 | + 'github:ref': 'main', |
| 65 | + 'github:ref-type': 'branch', |
| 66 | + 'github:actor': 'scottschreckengaust', |
| 67 | + 'github:head-ref': '', |
| 68 | + 'github:run-id': '12345678', |
| 69 | + 'github:event': 'push', |
| 70 | + 'github:repository': 'aws-samples/sample-autonomous-cloud-coding-agents', |
| 71 | + 'github:clean': 'true', |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + test('all 9 github:* tags default to "none" when no context is provided', () => { |
| 76 | + const resources = templateWithDefaults.findResources('AWS::DynamoDB::Table'); |
| 77 | + const firstResource = Object.values(resources)[0]; |
| 78 | + const tags: Array<{ Key: string; Value: string }> = firstResource?.Properties?.Tags ?? []; |
| 79 | + |
| 80 | + for (const tagKey of GITHUB_TAG_KEYS) { |
| 81 | + const tag = tags.find(t => t.Key === tagKey); |
| 82 | + expect(tag).toBeDefined(); |
| 83 | + expect(tag!.Value).toBe('none'); |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + test('github:* tags reflect context values when provided', () => { |
| 88 | + const resources = templateWithValues.findResources('AWS::DynamoDB::Table'); |
| 89 | + const firstResource = Object.values(resources)[0]; |
| 90 | + const tags: Array<{ Key: string; Value: string }> = firstResource?.Properties?.Tags ?? []; |
| 91 | + |
| 92 | + expect(tags.find(t => t.Key === 'github:sha')!.Value).toBe('f36d352c5a1bc3a90d3e60e30e2f9d4345426724'); |
| 93 | + expect(tags.find(t => t.Key === 'github:ref')!.Value).toBe('main'); |
| 94 | + expect(tags.find(t => t.Key === 'github:ref-type')!.Value).toBe('branch'); |
| 95 | + expect(tags.find(t => t.Key === 'github:actor')!.Value).toBe('scottschreckengaust'); |
| 96 | + expect(tags.find(t => t.Key === 'github:head-ref')!.Value).toBe('none'); |
| 97 | + expect(tags.find(t => t.Key === 'github:run-id')!.Value).toBe('12345678'); |
| 98 | + expect(tags.find(t => t.Key === 'github:event')!.Value).toBe('push'); |
| 99 | + expect(tags.find(t => t.Key === 'github:repository')!.Value).toBe('aws-samples/sample-autonomous-cloud-coding-agents'); |
| 100 | + expect(tags.find(t => t.Key === 'github:clean')!.Value).toBe('true'); |
| 101 | + }); |
| 102 | + |
| 103 | + test('empty string context values resolve to "none"', () => { |
| 104 | + const template = synthWithTags({ |
| 105 | + 'github:sha': '', |
| 106 | + 'github:head-ref': '', |
| 107 | + }); |
| 108 | + const resources = template.findResources('AWS::DynamoDB::Table'); |
| 109 | + const firstResource = Object.values(resources)[0]; |
| 110 | + const tags: Array<{ Key: string; Value: string }> = firstResource?.Properties?.Tags ?? []; |
| 111 | + |
| 112 | + expect(tags.find(t => t.Key === 'github:sha')!.Value).toBe('none'); |
| 113 | + expect(tags.find(t => t.Key === 'github:head-ref')!.Value).toBe('none'); |
| 114 | + }); |
| 115 | +}); |
0 commit comments