-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
test.environment.ts
244 lines (216 loc) · 5.93 KB
/
test.environment.ts
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import { countResources, expect, haveResource } from '@aws-cdk/assert-internal';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as ecs from '@aws-cdk/aws-ecs';
import * as cdk from '@aws-cdk/core';
import { Test } from 'nodeunit';
import { Container, Environment, EnvironmentCapacityType, Service, ServiceDescription } from '../lib';
export = {
'should be able to add a service to an environment'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
// WHEN
const environment = new Environment(stack, 'production');
const serviceDescription = new ServiceDescription();
serviceDescription.add(new Container({
cpu: 256,
memoryMiB: 512,
trafficPort: 80,
image: ecs.ContainerImage.fromRegistry('nathanpeck/name'),
}));
new Service(stack, 'my-service', {
environment,
serviceDescription,
});
// THEN
expect(stack).to(countResources('AWS::ECS::Service', 1));
expect(stack).to(haveResource('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
Cpu: 256,
Essential: true,
Image: 'nathanpeck/name',
Memory: 512,
Name: 'app',
PortMappings: [
{
ContainerPort: 80,
Protocol: 'tcp',
},
],
Ulimits: [
{
HardLimit: 1024000,
Name: 'nofile',
SoftLimit: 1024000,
},
],
},
],
Cpu: '256',
Family: 'myservicetaskdefinition',
Memory: '512',
NetworkMode: 'awsvpc',
RequiresCompatibilities: [
'EC2',
'FARGATE',
],
TaskRoleArn: {
'Fn::GetAtt': [
'myservicetaskdefinitionTaskRole92ACD903',
'Arn',
],
},
}));
test.done();
},
'should be able to create a Fargate environment with a given VPC and cluster'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
// WHEN
const vpc = new ec2.Vpc(stack, 'VPC');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });
const environment = new Environment(stack, 'production', {
vpc,
cluster,
});
const serviceDescription = new ServiceDescription();
serviceDescription.add(new Container({
cpu: 256,
memoryMiB: 512,
trafficPort: 80,
image: ecs.ContainerImage.fromRegistry('nathanpeck/name'),
}));
new Service(stack, 'my-service', {
environment,
serviceDescription,
});
// THEN
expect(stack).to(countResources('AWS::ECS::Service', 1));
expect(stack).to(haveResource('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
Cpu: 256,
Essential: true,
Image: 'nathanpeck/name',
Memory: 512,
Name: 'app',
PortMappings: [
{
ContainerPort: 80,
Protocol: 'tcp',
},
],
Ulimits: [
{
HardLimit: 1024000,
Name: 'nofile',
SoftLimit: 1024000,
},
],
},
],
Cpu: '256',
Family: 'myservicetaskdefinition',
Memory: '512',
NetworkMode: 'awsvpc',
RequiresCompatibilities: [
'EC2',
'FARGATE',
],
TaskRoleArn: {
'Fn::GetAtt': [
'myservicetaskdefinitionTaskRole92ACD903',
'Arn',
],
},
}));
test.done();
},
'should be able to create an environment for EC2'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
// WHEN
const vpc = new ec2.Vpc(stack, 'VPC');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });
cluster.addCapacity('DefaultAutoScalingGroup', {
instanceType: new ec2.InstanceType('t2.micro'),
});
const environment = new Environment(stack, 'production', {
vpc,
cluster,
capacityType: EnvironmentCapacityType.EC2,
});
const serviceDescription = new ServiceDescription();
serviceDescription.add(new Container({
cpu: 256,
memoryMiB: 512,
trafficPort: 80,
image: ecs.ContainerImage.fromRegistry('nathanpeck/name'),
}));
new Service(stack, 'my-service', {
environment,
serviceDescription,
});
// THEN
expect(stack).to(countResources('AWS::ECS::Service', 1));
expect(stack).to(haveResource('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
Cpu: 256,
Essential: true,
Image: 'nathanpeck/name',
Memory: 512,
Name: 'app',
PortMappings: [
{
ContainerPort: 80,
Protocol: 'tcp',
},
],
Ulimits: [
{
HardLimit: 1024000,
Name: 'nofile',
SoftLimit: 1024000,
},
],
},
],
Cpu: '256',
Family: 'myservicetaskdefinition',
Memory: '512',
NetworkMode: 'awsvpc',
RequiresCompatibilities: [
'EC2',
'FARGATE',
],
TaskRoleArn: {
'Fn::GetAtt': [
'myservicetaskdefinitionTaskRole92ACD903',
'Arn',
],
},
}));
test.done();
},
'should be able to create an environment from attributes'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'VPC');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });
cluster.addCapacity('DefaultAutoScalingGroup', {
instanceType: new ec2.InstanceType('t2.micro'),
});
// WHEN
const environment = Environment.fromEnvironmentAttributes(stack, 'Environment', {
capacityType: EnvironmentCapacityType.EC2,
cluster: cluster,
});
// THEN
test.equal(environment.capacityType, EnvironmentCapacityType.EC2);
test.equal(environment.cluster, cluster);
test.equal(environment.vpc, vpc);
test.equal(environment.id, 'Environment');
test.done();
},
};