|
| 1 | +import { describe, it, before, after } from 'node:test'; |
| 2 | +import * as assert from 'node:assert'; |
| 3 | +import { InlineProgramArgs } from '@pulumi/pulumi/automation'; |
| 4 | +import * as automation from '../automation'; |
| 5 | +import { VpcTestContext } from './test-context'; |
| 6 | +import { |
| 7 | + DescribeInternetGatewaysCommand, |
| 8 | + DescribeNatGatewaysCommand, |
| 9 | + DescribeRouteTablesCommand, |
| 10 | + DescribeSubnetsCommand, |
| 11 | + DescribeVpcsCommand, |
| 12 | + EC2Client, |
| 13 | + SubnetState, |
| 14 | + VpcState, |
| 15 | +} from '@aws-sdk/client-ec2'; |
| 16 | +import { defaults as vpcDefaults } from '../../src/v2/components/vpc'; |
| 17 | + |
| 18 | +const programArgs: InlineProgramArgs = { |
| 19 | + stackName: 'dev', |
| 20 | + projectName: 'icb-test-vpc', |
| 21 | + program: () => import('./infrastructure'), |
| 22 | +}; |
| 23 | + |
| 24 | +describe('Vpc component deployment', () => { |
| 25 | + const region = process.env.AWS_REGION; |
| 26 | + if (!region) { |
| 27 | + throw new Error('AWS_REGION environment variable is required'); |
| 28 | + } |
| 29 | + |
| 30 | + const ctx: VpcTestContext = { |
| 31 | + outputs: {}, |
| 32 | + config: {}, |
| 33 | + clients: { |
| 34 | + ec2: new EC2Client({ region }), |
| 35 | + }, |
| 36 | + }; |
| 37 | + |
| 38 | + before(async () => { |
| 39 | + ctx.outputs = await automation.deploy(programArgs); |
| 40 | + }); |
| 41 | + |
| 42 | + after(() => automation.destroy(programArgs)); |
| 43 | + |
| 44 | + it('should create a default VPC with the correct configuration', async () => { |
| 45 | + const defaultVpc = ctx.outputs.defaultVpc.value; |
| 46 | + await testVpcConfiguration( |
| 47 | + ctx, |
| 48 | + defaultVpc.vpc.vpcId, |
| 49 | + 6, |
| 50 | + vpcDefaults.numberOfAvailabilityZones, |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should create a VPC with the correct configuration', async () => { |
| 55 | + const vpcOutput = ctx.outputs.vpc.value; |
| 56 | + await testVpcConfiguration(ctx, vpcOutput.vpc.vpcId, 9, 3); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should have internet gateway for public subnets', async () => { |
| 60 | + const defaultVpc = ctx.outputs.defaultVpc.value; |
| 61 | + const vpcId = defaultVpc.vpc.vpcId; |
| 62 | + |
| 63 | + const igwResult = await ctx.clients.ec2.send( |
| 64 | + new DescribeInternetGatewaysCommand({ |
| 65 | + Filters: [{ Name: 'attachment.vpc-id', Values: [vpcId] }], |
| 66 | + }), |
| 67 | + ); |
| 68 | + const igws = igwResult.InternetGateways || []; |
| 69 | + assert.strictEqual( |
| 70 | + igws.length, |
| 71 | + 1, |
| 72 | + 'Should have exactly one internet gateway', |
| 73 | + ); |
| 74 | + |
| 75 | + const attachment = igws[0].Attachments?.find(a => a.VpcId === vpcId); |
| 76 | + assert.ok(attachment, 'Internet gateway attachment should exist'); |
| 77 | + assert.strictEqual( |
| 78 | + attachment.State, |
| 79 | + 'available', |
| 80 | + 'Internet gateway attachment should be available', |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should have a route table for each subnet type', async () => { |
| 85 | + const defaultVpc = ctx.outputs.defaultVpc.value; |
| 86 | + |
| 87 | + const routeTablesResult = await ctx.clients.ec2.send( |
| 88 | + new DescribeRouteTablesCommand({ |
| 89 | + Filters: [{ Name: 'vpc-id', Values: [defaultVpc.vpc.vpcId] }], |
| 90 | + }), |
| 91 | + ); |
| 92 | + const routeTables = routeTablesResult.RouteTables || []; |
| 93 | + assert.ok( |
| 94 | + routeTables.length >= 3, |
| 95 | + 'Should have route tables for different subnet types', |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should have NAT gateways for private subnets', async () => { |
| 100 | + const defaultVpc = ctx.outputs.defaultVpc.value; |
| 101 | + const vpcId = defaultVpc.vpc.vpcId; |
| 102 | + |
| 103 | + const natGwResult = await ctx.clients.ec2.send( |
| 104 | + new DescribeNatGatewaysCommand({ |
| 105 | + Filter: [{ Name: 'vpc-id', Values: [vpcId] }], |
| 106 | + }), |
| 107 | + ); |
| 108 | + const natGateways = natGwResult.NatGateways || []; |
| 109 | + assert.strictEqual( |
| 110 | + natGateways.length, |
| 111 | + vpcDefaults.numberOfAvailabilityZones, |
| 112 | + `Should have ${vpcDefaults.numberOfAvailabilityZones} NAT gateways`, |
| 113 | + ); |
| 114 | + |
| 115 | + natGateways.forEach(nat => { |
| 116 | + assert.strictEqual( |
| 117 | + nat.State, |
| 118 | + 'available', |
| 119 | + 'NAT gateway should be available', |
| 120 | + ); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + async function testVpcConfiguration( |
| 125 | + ctx: VpcTestContext, |
| 126 | + vpcId: string, |
| 127 | + expectedSubnetCount: number, |
| 128 | + expectedAzCount: number, |
| 129 | + ) { |
| 130 | + const vpcResult = await ctx.clients.ec2.send( |
| 131 | + new DescribeVpcsCommand({ |
| 132 | + VpcIds: [vpcId], |
| 133 | + }), |
| 134 | + ); |
| 135 | + const vpc = vpcResult.Vpcs?.[0]; |
| 136 | + assert.ok(vpc, 'VPC should exist'); |
| 137 | + assert.strictEqual( |
| 138 | + vpc.State, |
| 139 | + VpcState.available, |
| 140 | + 'VPC should be available', |
| 141 | + ); |
| 142 | + |
| 143 | + const subnetsResult = await ctx.clients.ec2.send( |
| 144 | + new DescribeSubnetsCommand({ |
| 145 | + Filters: [{ Name: 'vpc-id', Values: [vpcId] }], |
| 146 | + }), |
| 147 | + ); |
| 148 | + const subnets = subnetsResult.Subnets || []; |
| 149 | + assert.ok( |
| 150 | + subnets.length === expectedSubnetCount, |
| 151 | + `Should have ${expectedSubnetCount} subnets defined`, |
| 152 | + ); |
| 153 | + subnets.forEach(subnet => { |
| 154 | + assert.strictEqual( |
| 155 | + subnet.State, |
| 156 | + SubnetState.available, |
| 157 | + 'Subnets should be available', |
| 158 | + ); |
| 159 | + }); |
| 160 | + |
| 161 | + const uniqueAzs = new Set(subnets.map(s => s.AvailabilityZone)); |
| 162 | + assert.strictEqual( |
| 163 | + uniqueAzs.size, |
| 164 | + expectedAzCount, |
| 165 | + `Subnets should span ${expectedAzCount} availability zones`, |
| 166 | + ); |
| 167 | + } |
| 168 | +}); |
0 commit comments