diff --git a/test/imagebuilder.test.ts b/test/imagebuilder.test.ts index 3c5285b..f56c5a7 100644 --- a/test/imagebuilder.test.ts +++ b/test/imagebuilder.test.ts @@ -1,5 +1,6 @@ import * as cdk from 'aws-cdk-lib'; import { aws_ec2 as ec2 } from 'aws-cdk-lib'; +import { Template } from 'aws-cdk-lib/assertions'; import { Architecture, ContainerImageBuilder, Os } from '../src'; import { AmiBuilder } from '../src/providers/image-builders/ami'; @@ -83,3 +84,161 @@ test('Container image builder supported OS', () => { }); }).toThrowError('Unsupported architecture: ARM64. Consider CodeBuild for faster image builds.'); }); + +function countDockerComponents(stack: cdk.Stack): number { + const template = Template.fromStack(stack); + const components = template.findResources('AWS::ImageBuilder::Component'); + let count = 0; + for (const key of Object.keys(components)) { + const props = components[key].Properties; + if (props && props.Description === 'Install latest version of Docker') { + count += 1; + } + } + return count; +} + + + + + +function dockerProps(value: boolean): any { + return { installDocker: value, docker: value }; +} + +test('AMI builder installs Docker by default on Linux', () => { + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'test'); + const vpc = new ec2.Vpc(stack, 'vpc'); + + new AmiBuilder(stack, 'linux x64', { + os: Os.LINUX, + architecture: Architecture.X86_64, + vpc, + }); + + expect(countDockerComponents(stack)).toBe(1); +}); + +test('AMI builder installs Docker by default on Windows', () => { + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'test'); + const vpc = new ec2.Vpc(stack, 'vpc'); + + new AmiBuilder(stack, 'win x64', { + os: Os.WINDOWS, + architecture: Architecture.X86_64, + vpc, + }); + + expect(countDockerComponents(stack)).toBe(1); +}); + +test('AMI builder skips Docker on Windows when installDocker is false', () => { + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'test'); + const vpc = new ec2.Vpc(stack, 'vpc'); + + new AmiBuilder(stack, 'win x64', { + os: Os.WINDOWS, + architecture: Architecture.X86_64, + vpc, + ...dockerProps(false), + }); + + expect(countDockerComponents(stack)).toBe(0); +}); + +test('AMI builder skips Docker on Linux when installDocker is false', () => { + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'test'); + const vpc = new ec2.Vpc(stack, 'vpc'); + + new AmiBuilder(stack, 'linux x64', { + os: Os.LINUX, + architecture: Architecture.X86_64, + vpc, + ...dockerProps(false), + }); + + expect(countDockerComponents(stack)).toBe(0); +}); + +test('AMI builder installs Docker on Windows when installDocker is explicitly true', () => { + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'test'); + const vpc = new ec2.Vpc(stack, 'vpc'); + + new AmiBuilder(stack, 'win x64', { + os: Os.WINDOWS, + architecture: Architecture.X86_64, + vpc, + ...dockerProps(true), + }); + + expect(countDockerComponents(stack)).toBe(1); +}); + +test('AMI builder installs Docker on Linux when installDocker is explicitly true', () => { + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'test'); + const vpc = new ec2.Vpc(stack, 'vpc'); + + new AmiBuilder(stack, 'linux x64', { + os: Os.LINUX, + architecture: Architecture.X86_64, + vpc, + ...dockerProps(true), + }); + + expect(countDockerComponents(stack)).toBe(1); +}); + +test('AMI builder retains non-Docker components when installDocker is false on Windows', () => { + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'test'); + const vpc = new ec2.Vpc(stack, 'vpc'); + + new AmiBuilder(stack, 'win x64', { + os: Os.WINDOWS, + architecture: Architecture.X86_64, + vpc, + ...dockerProps(false), + }); + + const template = Template.fromStack(stack); + const components = template.findResources('AWS::ImageBuilder::Component'); + const descriptions = Object.keys(components).map(k => components[k].Properties?.Description); + + expect(descriptions).toEqual(expect.arrayContaining([ + expect.stringMatching(/CloudWatch/), + 'Install latest version of AWS CLI', + 'Install latest version of gh', + 'Install latest version of git', + 'Install latest version of GitHub Actions Runner', + ])); + expect(descriptions).not.toContain('Install latest version of Docker'); +}); + +test('AMI builder retains non-Docker components when installDocker is false on Linux', () => { + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'test'); + const vpc = new ec2.Vpc(stack, 'vpc'); + + new AmiBuilder(stack, 'linux x64', { + os: Os.LINUX, + architecture: Architecture.X86_64, + vpc, + ...dockerProps(false), + }); + + const template = Template.fromStack(stack); + const components = template.findResources('AWS::ImageBuilder::Component'); + const descriptions = Object.keys(components).map(k => components[k].Properties?.Description); + expect(descriptions).toEqual(expect.arrayContaining([ + 'Install latest version of gh', + 'Install latest version of git', + 'Install latest version of GitHub Actions Runner', + ])); + expect(descriptions).not.toContain('Install latest version of Docker'); +}); diff --git a/test_commands.json b/test_commands.json new file mode 100644 index 0000000..b489dc5 --- /dev/null +++ b/test_commands.json @@ -0,0 +1,3 @@ +{ + "test_commands": ["npx jest test/imagebuilder.test.ts --no-coverage"] +} diff --git a/mimo_test_command.sh b/mimo_test_command.sh new file mode 100755 --- /dev/null +++ b/mimo_test_command.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +set -uo pipefail +cd /testbed +rc=0 +npx jest test/imagebuilder.test.ts --no-coverage +rc=$(( rc | $? )) +exit $rc