File size: 5,835 Bytes
a3dcdac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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