kitchen-ec2 2.2.2 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,612 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
- # Author:: Tyler Ball (<tball@chef.io>)
4
- #
5
- # Copyright:: 2015-2018, Fletcher Nichol
6
- # Copyright:: 2016-2018, Chef Software, Inc.
7
- #
8
- # Licensed under the Apache License, Version 2.0 (the "License");
9
- # you may not use this file except in compliance with the License.
10
- # You may obtain a copy of the License at
11
- #
12
- # http://www.apache.org/licenses/LICENSE-2.0
13
- #
14
- # Unless required by applicable law or agreed to in writing, software
15
- # distributed under the License is distributed on an "AS IS" BASIS,
16
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
- # See the License for the specific language governing permissions and
18
- # limitations under the License.
19
-
20
- require "kitchen/driver/aws/instance_generator"
21
- require "kitchen/driver/aws/client"
22
- require "tempfile"
23
- require "base64"
24
- require "aws-sdk"
25
-
26
- describe Kitchen::Driver::Aws::InstanceGenerator do
27
-
28
- let(:config) { Hash.new }
29
- let(:resource) { instance_double(Aws::EC2::Resource) }
30
- let(:ec2) { instance_double(Kitchen::Driver::Aws::Client, :resource => resource) }
31
- let(:logger) { instance_double(Logger) }
32
- let(:generator) { Kitchen::Driver::Aws::InstanceGenerator.new(config, ec2, logger) }
33
-
34
- describe "#prepared_user_data" do
35
- context "when config[:user_data] is a file" do
36
- let(:tmp_file) { Tempfile.new("prepared_user_data_test") }
37
- let(:config) { { :user_data => tmp_file.path } }
38
-
39
- before do
40
- tmp_file.write("foo\nbar")
41
- tmp_file.rewind
42
- end
43
-
44
- after do
45
- tmp_file.close
46
- tmp_file.unlink
47
- end
48
-
49
- it "reads the file contents" do
50
- expect(Base64.decode64(generator.prepared_user_data)).to eq("foo\nbar")
51
- end
52
-
53
- it "memoizes the file contents" do
54
- decoded = Base64.decode64(generator.prepared_user_data)
55
- expect(decoded).to eq("foo\nbar")
56
- tmp_file.write("other\nvalue")
57
- tmp_file.rewind
58
- expect(decoded).to eq("foo\nbar")
59
- end
60
- end
61
-
62
- context "when config[:user_data] is binary" do
63
- let(:config) { { :user_data => "foo\0bar" } }
64
-
65
- it "handles nulls in user_data" do
66
- expect(Base64.decode64(generator.prepared_user_data)).to eq "foo\0bar"
67
- end
68
- end
69
- end
70
-
71
- describe "#ec2_instance_data" do
72
- ec2_stub = Aws::EC2::Client.new(:stub_responses => true)
73
-
74
- ec2_stub.stub_responses(
75
- :describe_subnets,
76
- :subnets => [
77
- {
78
- :subnet_id => "s-123",
79
- :tags => [{ :key => "foo", :value => "bar" }],
80
- },
81
- ]
82
- )
83
-
84
- ec2_stub.stub_responses(
85
- :describe_security_groups,
86
- :security_groups => [
87
- {
88
- :group_id => "sg-123",
89
- :tags => [{ :key => "foo", :value => "bar" }],
90
- },
91
- ]
92
- )
93
-
94
- it "returns empty on nil" do
95
- expect(generator.ec2_instance_data).to eq(
96
- :instance_type => nil,
97
- :ebs_optimized => nil,
98
- :image_id => nil,
99
- :key_name => nil,
100
- :subnet_id => nil,
101
- :private_ip_address => nil
102
- )
103
- end
104
-
105
- context "when populated with minimum requirements" do
106
- let(:config) do
107
- {
108
- :instance_type => "micro",
109
- :ebs_optimized => true,
110
- :image_id => "ami-123",
111
- :subnet_id => "s-456",
112
- :private_ip_address => "0.0.0.0",
113
- }
114
- end
115
-
116
- it "returns the minimum data" do
117
- expect(generator.ec2_instance_data).to eq(
118
- :instance_type => "micro",
119
- :ebs_optimized => true,
120
- :image_id => "ami-123",
121
- :key_name => nil,
122
- :subnet_id => "s-456",
123
- :private_ip_address => "0.0.0.0"
124
- )
125
- end
126
- end
127
-
128
- context "when populated with ssh key" do
129
- let(:config) do
130
- {
131
- :instance_type => "micro",
132
- :ebs_optimized => true,
133
- :image_id => "ami-123",
134
- :aws_ssh_key_id => "key",
135
- :subnet_id => "s-456",
136
- :private_ip_address => "0.0.0.0",
137
- }
138
- end
139
-
140
- it "returns the minimum data" do
141
- expect(generator.ec2_instance_data).to eq(
142
- :instance_type => "micro",
143
- :ebs_optimized => true,
144
- :image_id => "ami-123",
145
- :key_name => "key",
146
- :subnet_id => "s-456",
147
- :private_ip_address => "0.0.0.0"
148
- )
149
- end
150
- end
151
-
152
- context "when provided subnet tag instead of id" do
153
- let(:config) do
154
- {
155
- :instance_type => "micro",
156
- :ebs_optimized => true,
157
- :image_id => "ami-123",
158
- :aws_ssh_key_id => "key",
159
- :subnet_id => nil,
160
- :region => "us-west-2",
161
- :subnet_filter =>
162
- {
163
- :tag => "foo",
164
- :value => "bar",
165
- },
166
- }
167
- end
168
-
169
- it "generates id from the provided tag" do
170
- allow(::Aws::EC2::Client).to receive(:new).and_return(ec2_stub)
171
- expect(ec2_stub).to receive(:describe_subnets).with(
172
- :filters => [
173
- {
174
- :name => "tag:foo",
175
- :values => ["bar"],
176
- },
177
- ]
178
- ).and_return(ec2_stub.describe_subnets)
179
- expect(generator.ec2_instance_data[:subnet_id]).to eq("s-123")
180
- end
181
- end
182
-
183
- context "when provided security_group tag instead of id" do
184
- let(:config) do
185
- {
186
- :instance_type => "micro",
187
- :ebs_optimized => true,
188
- :image_id => "ami-123",
189
- :aws_ssh_key_id => "key",
190
- :subnet_id => "s-123",
191
- :security_group_ids => nil,
192
- :region => "us-west-2",
193
- :security_group_filter =>
194
- {
195
- :tag => "foo",
196
- :value => "bar",
197
- },
198
- }
199
- end
200
-
201
- it "generates id from the provided tag" do
202
- allow(::Aws::EC2::Client).to receive(:new).and_return(ec2_stub)
203
- expect(ec2_stub).to receive(:describe_security_groups).with(
204
- :filters => [
205
- {
206
- :name => "tag:foo",
207
- :values => ["bar"],
208
- },
209
- ]
210
- ).and_return(ec2_stub.describe_security_groups)
211
- expect(generator.ec2_instance_data[:security_group_ids]).to eq(["sg-123"])
212
- end
213
- end
214
-
215
- context "when provided a non existing security_group tag filter" do
216
- ec2_stub_whithout_security_group = Aws::EC2::Client.new(:stub_responses => true)
217
-
218
- let(:config) do
219
- {
220
- :instance_type => "micro",
221
- :ebs_optimized => true,
222
- :image_id => "ami-123",
223
- :aws_ssh_key_id => "key",
224
- :subnet_id => "s-123",
225
- :security_group_ids => nil,
226
- :region => "us-west-2",
227
- :security_group_filter =>
228
- {
229
- :tag => "foo",
230
- :value => "bar",
231
- },
232
- }
233
- end
234
-
235
- it "generates id from the provided tag" do
236
- allow(::Aws::EC2::Client).to receive(:new).and_return(ec2_stub_whithout_security_group)
237
- expect(ec2_stub_whithout_security_group).to receive(:describe_security_groups).with(
238
- :filters => [
239
- {
240
- :name => "tag:foo",
241
- :values => ["bar"],
242
- },
243
- ]
244
- ).and_return(ec2_stub_whithout_security_group.describe_security_groups)
245
-
246
- expect { generator.ec2_instance_data }.to raise_error("The group tagged '#{config[:security_group_filter][:tag]} " +
247
- "#{config[:security_group_filter][:value]}' does not exist!")
248
- end
249
- end
250
-
251
- context "when passed an empty block_device_mappings" do
252
- let(:config) do
253
- {
254
- :instance_type => "micro",
255
- :ebs_optimized => true,
256
- :image_id => "ami-123",
257
- :aws_ssh_key_id => "key",
258
- :subnet_id => "s-456",
259
- :private_ip_address => "0.0.0.0",
260
- :block_device_mappings => [],
261
- }
262
- end
263
-
264
- it "does not return block_device_mappings" do
265
- expect(generator.ec2_instance_data).to eq(
266
- :instance_type => "micro",
267
- :ebs_optimized => true,
268
- :image_id => "ami-123",
269
- :key_name => "key",
270
- :subnet_id => "s-456",
271
- :private_ip_address => "0.0.0.0"
272
- )
273
- end
274
- end
275
-
276
- context "when availability_zone is provided as 'eu-west-1c'" do
277
- let(:config) do
278
- {
279
- :region => "eu-east-1",
280
- :availability_zone => "eu-west-1c",
281
- }
282
- end
283
- it "returns that in the instance data" do
284
- expect(generator.ec2_instance_data).to eq(
285
- :instance_type => nil,
286
- :ebs_optimized => nil,
287
- :image_id => nil,
288
- :key_name => nil,
289
- :subnet_id => nil,
290
- :private_ip_address => nil,
291
- :placement => { :availability_zone => "eu-west-1c" }
292
- )
293
- end
294
- end
295
-
296
- context "when availability_zone is provided as 'c'" do
297
- let(:config) do
298
- {
299
- :region => "eu-east-1",
300
- :availability_zone => "c",
301
- }
302
- end
303
- it "adds the region to it in the instance data" do
304
- expect(generator.ec2_instance_data).to eq(
305
- :instance_type => nil,
306
- :ebs_optimized => nil,
307
- :image_id => nil,
308
- :key_name => nil,
309
- :subnet_id => nil,
310
- :private_ip_address => nil,
311
- :placement => { :availability_zone => "eu-east-1c" }
312
- )
313
- end
314
- end
315
-
316
- context "when availability_zone is not provided" do
317
- let(:config) do
318
- {
319
- :region => "eu-east-1",
320
- }
321
- end
322
- it "is not added to the instance data" do
323
- expect(generator.ec2_instance_data).to eq(
324
- :instance_type => nil,
325
- :ebs_optimized => nil,
326
- :image_id => nil,
327
- :key_name => nil,
328
- :subnet_id => nil,
329
- :private_ip_address => nil
330
- )
331
- end
332
- end
333
-
334
- context "when availability_zone and tenancy are provided" do
335
- let(:config) do
336
- {
337
- :region => "eu-east-1",
338
- :availability_zone => "c",
339
- :tenancy => "dedicated",
340
- }
341
- end
342
- it "adds the region to it in the instance data" do
343
- expect(generator.ec2_instance_data).to eq(
344
- :instance_type => nil,
345
- :ebs_optimized => nil,
346
- :image_id => nil,
347
- :key_name => nil,
348
- :subnet_id => nil,
349
- :private_ip_address => nil,
350
- :placement => { :availability_zone => "eu-east-1c",
351
- :tenancy => "dedicated" }
352
- )
353
- end
354
- end
355
-
356
- context "when tenancy is provided but availability_zone isn't" do
357
- let(:config) do
358
- {
359
- :region => "eu-east-1",
360
- :tenancy => "default",
361
- }
362
- end
363
- it "is not added to the instance data" do
364
- expect(generator.ec2_instance_data).to eq(
365
- :instance_type => nil,
366
- :ebs_optimized => nil,
367
- :image_id => nil,
368
- :key_name => nil,
369
- :subnet_id => nil,
370
- :private_ip_address => nil,
371
- :placement => { :tenancy => "default" }
372
- )
373
- end
374
- end
375
-
376
- context "when availability_zone and tenancy are provided" do
377
- let(:config) do
378
- {
379
- :region => "eu-east-1",
380
- :availability_zone => "c",
381
- :tenancy => "dedicated",
382
- }
383
- end
384
- it "adds the region to it in the instance data" do
385
- expect(generator.ec2_instance_data).to eq(
386
- :instance_type => nil,
387
- :ebs_optimized => nil,
388
- :image_id => nil,
389
- :key_name => nil,
390
- :subnet_id => nil,
391
- :private_ip_address => nil,
392
- :placement => { :availability_zone => "eu-east-1c",
393
- :tenancy => "dedicated" }
394
- )
395
- end
396
- end
397
-
398
- context "when tenancy is provided but availability_zone isn't" do
399
- let(:config) do
400
- {
401
- :region => "eu-east-1",
402
- :tenancy => "default",
403
- }
404
- end
405
- it "is not added to the instance data" do
406
- expect(generator.ec2_instance_data).to eq(
407
- :instance_type => nil,
408
- :ebs_optimized => nil,
409
- :image_id => nil,
410
- :key_name => nil,
411
- :subnet_id => nil,
412
- :private_ip_address => nil,
413
- :placement => { :tenancy => "default" }
414
- )
415
- end
416
- end
417
-
418
- context "when subnet_id is provided" do
419
- let(:config) do
420
- {
421
- :subnet_id => "s-456",
422
- }
423
- end
424
-
425
- it "adds a network_interfaces block" do
426
- expect(generator.ec2_instance_data).to eq(
427
- :instance_type => nil,
428
- :ebs_optimized => nil,
429
- :image_id => nil,
430
- :key_name => nil,
431
- :subnet_id => "s-456",
432
- :private_ip_address => nil
433
- )
434
- end
435
- end
436
-
437
- context "when associate_public_ip is provided" do
438
- let(:config) do
439
- {
440
- :associate_public_ip => true,
441
- }
442
- end
443
-
444
- it "adds a network_interfaces block" do
445
- expect(generator.ec2_instance_data).to eq(
446
- :instance_type => nil,
447
- :ebs_optimized => nil,
448
- :image_id => nil,
449
- :key_name => nil,
450
- :subnet_id => nil,
451
- :private_ip_address => nil,
452
- :network_interfaces => [{
453
- :device_index => 0,
454
- :associate_public_ip_address => true,
455
- :delete_on_termination => true,
456
- }]
457
- )
458
- end
459
-
460
- context "and subnet is provided" do
461
- let(:config) do
462
- {
463
- :associate_public_ip => true,
464
- :subnet_id => "s-456",
465
- }
466
- end
467
-
468
- it "adds a network_interfaces block" do
469
- expect(generator.ec2_instance_data).to eq(
470
- :instance_type => nil,
471
- :ebs_optimized => nil,
472
- :image_id => nil,
473
- :key_name => nil,
474
- :private_ip_address => nil,
475
- :network_interfaces => [{
476
- :device_index => 0,
477
- :associate_public_ip_address => true,
478
- :delete_on_termination => true,
479
- :subnet_id => "s-456",
480
- }]
481
- )
482
- end
483
- end
484
-
485
- context "and security_group_ids is provided" do
486
- let(:config) do
487
- {
488
- :associate_public_ip => true,
489
- :security_group_ids => ["sg-789"],
490
- }
491
- end
492
-
493
- it "adds a network_interfaces block" do
494
- expect(generator.ec2_instance_data).to eq(
495
- :instance_type => nil,
496
- :ebs_optimized => nil,
497
- :image_id => nil,
498
- :key_name => nil,
499
- :subnet_id => nil,
500
- :private_ip_address => nil,
501
- :network_interfaces => [{
502
- :device_index => 0,
503
- :associate_public_ip_address => true,
504
- :delete_on_termination => true,
505
- :groups => ["sg-789"],
506
- }]
507
- )
508
- end
509
-
510
- it "accepts a single string value" do
511
- config[:security_group_ids] = "only-one"
512
-
513
- expect(generator.ec2_instance_data).to include(
514
- :network_interfaces => [{
515
- :device_index => 0,
516
- :associate_public_ip_address => true,
517
- :delete_on_termination => true,
518
- :groups => ["only-one"],
519
- }]
520
- )
521
- end
522
- end
523
-
524
- context "and private_ip_address is provided" do
525
- let(:config) do
526
- {
527
- :associate_public_ip => true,
528
- :private_ip_address => "0.0.0.0",
529
- }
530
- end
531
-
532
- it "adds a network_interfaces block" do
533
- expect(generator.ec2_instance_data).to eq(
534
- :instance_type => nil,
535
- :ebs_optimized => nil,
536
- :image_id => nil,
537
- :key_name => nil,
538
- :subnet_id => nil,
539
- :network_interfaces => [{
540
- :device_index => 0,
541
- :associate_public_ip_address => true,
542
- :delete_on_termination => true,
543
- :private_ip_address => "0.0.0.0",
544
- }]
545
- )
546
- end
547
- end
548
- end
549
-
550
- context "when provided the maximum config" do
551
- let(:config) do
552
- {
553
- :availability_zone => "eu-west-1a",
554
- :instance_type => "micro",
555
- :ebs_optimized => true,
556
- :image_id => "ami-123",
557
- :aws_ssh_key_id => "key",
558
- :subnet_id => "s-456",
559
- :private_ip_address => "0.0.0.0",
560
- :block_device_mappings => [
561
- {
562
- :device_name => "/dev/sda2",
563
- :virtual_name => "test",
564
- :ebs => {
565
- :volume_size => 15,
566
- :delete_on_termination => false,
567
- :volume_type => "gp2",
568
- :snapshot_id => "id",
569
- },
570
- },
571
- ],
572
- :security_group_ids => ["sg-789"],
573
- :user_data => "foo",
574
- :iam_profile_name => "iam-123",
575
- :associate_public_ip => true,
576
- }
577
- end
578
-
579
- it "returns the maximum data" do
580
- expect(generator.ec2_instance_data).to eq(
581
- :instance_type => "micro",
582
- :ebs_optimized => true,
583
- :image_id => "ami-123",
584
- :key_name => "key",
585
- :block_device_mappings => [
586
- {
587
- :device_name => "/dev/sda2",
588
- :virtual_name => "test",
589
- :ebs => {
590
- :volume_size => 15,
591
- :delete_on_termination => false,
592
- :volume_type => "gp2",
593
- :snapshot_id => "id",
594
- },
595
- },
596
- ],
597
- :iam_instance_profile => { :name => "iam-123" },
598
- :network_interfaces => [{
599
- :device_index => 0,
600
- :associate_public_ip_address => true,
601
- :subnet_id => "s-456",
602
- :delete_on_termination => true,
603
- :groups => ["sg-789"],
604
- :private_ip_address => "0.0.0.0",
605
- }],
606
- :placement => { :availability_zone => "eu-west-1a" },
607
- :user_data => Base64.encode64("foo")
608
- )
609
- end
610
- end
611
- end
612
- end