|
| 1 | +import { createTestProject, readProjectConfig, runCLI } from '../src/test-utils/index.js'; |
| 2 | +import type { TestProject } from '../src/test-utils/index.js'; |
| 3 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 4 | + |
| 5 | +const OPERATOR_ROLE_ARN = 'arn:aws:iam::123456789012:role/MyOperatorRole'; |
| 6 | + |
| 7 | +describe('integration: add and remove capacity providers', () => { |
| 8 | + let project: TestProject; |
| 9 | + |
| 10 | + beforeAll(async () => { |
| 11 | + project = await createTestProject({ noAgent: true }); |
| 12 | + }); |
| 13 | + |
| 14 | + afterAll(async () => { |
| 15 | + await project.cleanup(); |
| 16 | + }); |
| 17 | + |
| 18 | + describe('capacity provider lifecycle', () => { |
| 19 | + const cpName = `IntegCp${Date.now().toString().slice(-6)}`; |
| 20 | + |
| 21 | + it('adds a capacity provider', async () => { |
| 22 | + const result = await runCLI( |
| 23 | + [ |
| 24 | + 'add', |
| 25 | + 'capacity-provider', |
| 26 | + '--name', |
| 27 | + cpName, |
| 28 | + '--operator-role-arn', |
| 29 | + OPERATOR_ROLE_ARN, |
| 30 | + '--subnets', |
| 31 | + 'subnet-0123456789abcdef0', |
| 32 | + '--security-groups', |
| 33 | + 'sg-0123456789abcdef0', |
| 34 | + '--os', |
| 35 | + 'LINUX_X86_64', |
| 36 | + '--instance-types', |
| 37 | + 'c6a.large', |
| 38 | + '--json', |
| 39 | + ], |
| 40 | + project.projectPath |
| 41 | + ); |
| 42 | + |
| 43 | + expect(result.exitCode, `stdout: ${result.stdout}, stderr: ${result.stderr}`).toBe(0); |
| 44 | + const json = JSON.parse(result.stdout); |
| 45 | + expect(json.success).toBe(true); |
| 46 | + expect(json.capacityProviderName).toBe(cpName); |
| 47 | + |
| 48 | + const config = await readProjectConfig(project.projectPath); |
| 49 | + const cp = config.capacityProviders?.find((c: Record<string, unknown>) => c.name === cpName); |
| 50 | + expect(cp, `Capacity provider "${cpName}" should be in config`).toBeTruthy(); |
| 51 | + expect(cp!.operatorRoleArn).toBe(OPERATOR_ROLE_ARN); |
| 52 | + const ec2 = (cp as any).computeConfiguration.ec2Configuration; |
| 53 | + expect(ec2.launchTemplateSource.launchParameters.operatingSystem).toBe('LINUX_X86_64'); |
| 54 | + expect(ec2.launchTemplateSource.launchParameters.instanceRequirements.allowedInstanceTypes).toEqual([ |
| 55 | + 'c6a.large', |
| 56 | + ]); |
| 57 | + expect(ec2.vpcConfiguration.subnets).toEqual(['subnet-0123456789abcdef0']); |
| 58 | + expect(ec2.vpcConfiguration.securityGroups).toEqual(['sg-0123456789abcdef0']); |
| 59 | + }); |
| 60 | + |
| 61 | + it('adds a capacity provider with volumes, lifecycle, and description', async () => { |
| 62 | + const richName = `${cpName}Rich`; |
| 63 | + const result = await runCLI( |
| 64 | + [ |
| 65 | + 'add', |
| 66 | + 'capacity-provider', |
| 67 | + '--name', |
| 68 | + richName, |
| 69 | + '--operator-role-arn', |
| 70 | + OPERATOR_ROLE_ARN, |
| 71 | + '--description', |
| 72 | + 'my rich capacity provider', |
| 73 | + '--subnets', |
| 74 | + 'subnet-0123456789abcdef0,subnet-0fedcba9876543210', |
| 75 | + '--security-groups', |
| 76 | + 'sg-0123456789abcdef0', |
| 77 | + '--os', |
| 78 | + 'LINUX_ARM64', |
| 79 | + '--instance-types', |
| 80 | + 'c7g.large,c7g.xlarge', |
| 81 | + '--volume', |
| 82 | + 'data:20', |
| 83 | + '--volume-encrypted', |
| 84 | + '--idle-instance-timeout', |
| 85 | + '3600', |
| 86 | + '--max-lifetime', |
| 87 | + '28800', |
| 88 | + '--json', |
| 89 | + ], |
| 90 | + project.projectPath |
| 91 | + ); |
| 92 | + |
| 93 | + expect(result.exitCode, `stdout: ${result.stdout}, stderr: ${result.stderr}`).toBe(0); |
| 94 | + expect(JSON.parse(result.stdout).success).toBe(true); |
| 95 | + |
| 96 | + const config = await readProjectConfig(project.projectPath); |
| 97 | + const cp = config.capacityProviders?.find((c: Record<string, unknown>) => c.name === richName); |
| 98 | + expect(cp).toBeTruthy(); |
| 99 | + expect(cp!.description).toBe('my rich capacity provider'); |
| 100 | + const ec2 = (cp as any).computeConfiguration.ec2Configuration; |
| 101 | + expect(ec2.launchTemplateSource.launchParameters.operatingSystem).toBe('LINUX_ARM64'); |
| 102 | + expect(ec2.vpcConfiguration.subnets).toHaveLength(2); |
| 103 | + expect(ec2.volumes).toEqual([{ ebsConfiguration: { name: 'data', sizeGiB: 20, encrypted: true } }]); |
| 104 | + expect(ec2.lifecycleConfiguration).toEqual({ idleInstanceTimeout: 3600, maxLifetime: 28800 }); |
| 105 | + |
| 106 | + await runCLI(['remove', 'capacity-provider', '--name', richName, '--yes'], project.projectPath); |
| 107 | + }); |
| 108 | + |
| 109 | + it('rejects a duplicate capacity provider name', async () => { |
| 110 | + const result = await runCLI( |
| 111 | + [ |
| 112 | + 'add', |
| 113 | + 'capacity-provider', |
| 114 | + '--name', |
| 115 | + cpName, |
| 116 | + '--operator-role-arn', |
| 117 | + OPERATOR_ROLE_ARN, |
| 118 | + '--subnets', |
| 119 | + 'subnet-0123456789abcdef0', |
| 120 | + '--security-groups', |
| 121 | + 'sg-0123456789abcdef0', |
| 122 | + '--instance-types', |
| 123 | + 'c6a.large', |
| 124 | + '--json', |
| 125 | + ], |
| 126 | + project.projectPath |
| 127 | + ); |
| 128 | + |
| 129 | + expect(result.exitCode).toBe(1); |
| 130 | + const json = JSON.parse(result.stdout); |
| 131 | + expect(json.success).toBe(false); |
| 132 | + expect(json.error).toContain('already exists'); |
| 133 | + }); |
| 134 | + |
| 135 | + it('removes the capacity provider', async () => { |
| 136 | + const result = await runCLI( |
| 137 | + ['remove', 'capacity-provider', '--name', cpName, '--yes', '--json'], |
| 138 | + project.projectPath |
| 139 | + ); |
| 140 | + |
| 141 | + expect(result.exitCode, `stdout: ${result.stdout}, stderr: ${result.stderr}`).toBe(0); |
| 142 | + const json = JSON.parse(result.stdout); |
| 143 | + expect(json.success).toBe(true); |
| 144 | + |
| 145 | + const config = await readProjectConfig(project.projectPath); |
| 146 | + const found = config.capacityProviders?.some((c: Record<string, unknown>) => c.name === cpName); |
| 147 | + expect(found, `Capacity provider "${cpName}" should be removed`).toBeFalsy(); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + describe('validation', () => { |
| 152 | + it('rejects a missing required option', async () => { |
| 153 | + const result = await runCLI(['add', 'capacity-provider', '--name', 'noRole', '--json'], project.projectPath); |
| 154 | + expect(result.exitCode).toBe(1); |
| 155 | + const json = JSON.parse(result.stdout); |
| 156 | + expect(json.success).toBe(false); |
| 157 | + }); |
| 158 | + |
| 159 | + it('rejects an unsupported operating system', async () => { |
| 160 | + const result = await runCLI( |
| 161 | + [ |
| 162 | + 'add', |
| 163 | + 'capacity-provider', |
| 164 | + '--name', |
| 165 | + 'badOs', |
| 166 | + '--operator-role-arn', |
| 167 | + OPERATOR_ROLE_ARN, |
| 168 | + '--subnets', |
| 169 | + 'subnet-0123456789abcdef0', |
| 170 | + '--security-groups', |
| 171 | + 'sg-0123456789abcdef0', |
| 172 | + '--os', |
| 173 | + 'WINDOWS_X86_64', |
| 174 | + '--instance-types', |
| 175 | + 'c6a.large', |
| 176 | + '--json', |
| 177 | + ], |
| 178 | + project.projectPath |
| 179 | + ); |
| 180 | + expect(result.exitCode).toBe(1); |
| 181 | + }); |
| 182 | + |
| 183 | + it('rejects a malformed operator role ARN', async () => { |
| 184 | + const result = await runCLI( |
| 185 | + [ |
| 186 | + 'add', |
| 187 | + 'capacity-provider', |
| 188 | + '--name', |
| 189 | + 'badArn', |
| 190 | + '--operator-role-arn', |
| 191 | + 'not-an-arn', |
| 192 | + '--subnets', |
| 193 | + 'subnet-0123456789abcdef0', |
| 194 | + '--security-groups', |
| 195 | + 'sg-0123456789abcdef0', |
| 196 | + '--instance-types', |
| 197 | + 'c6a.large', |
| 198 | + '--json', |
| 199 | + ], |
| 200 | + project.projectPath |
| 201 | + ); |
| 202 | + expect(result.exitCode).toBe(1); |
| 203 | + }); |
| 204 | + |
| 205 | + it('passes agentcore validate after add/remove lifecycle', async () => { |
| 206 | + const result = await runCLI(['validate'], project.projectPath); |
| 207 | + expect(result.exitCode).toBe(0); |
| 208 | + }); |
| 209 | + }); |
| 210 | +}); |
0 commit comments