-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathcommand_executor_test.rb
More file actions
743 lines (659 loc) · 31.8 KB
/
command_executor_test.rb
File metadata and controls
743 lines (659 loc) · 31.8 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
require 'test_helper'
require 'certificate_helper'
require 'stringio'
require 'aws-sdk-core/s3'
require 'aws/codedeploy/local/deployer'
class CodeDeployPluginCommandExecutorTest < InstanceAgentTestCase
include InstanceAgent::Plugins::CodeDeployPlugin
def generate_signed_message_for(map)
message = @cert_helper.sign_message(map.to_json)
spec = OpenStruct.new({ :payload => message })
spec.format = "PKCS7/JSON"
return spec
end
context 'The CodeDeploy Plugin Command Executor' do
setup do
@test_hook_mapping = { "BeforeBlockTraffic"=>["BeforeBlockTraffic"],
"AfterBlockTraffic"=>["AfterBlockTraffic"],
"ApplicationStop"=>["ApplicationStop"],
"BeforeInstall"=>["BeforeInstall"],
"AfterInstall"=>["AfterInstall"],
"ApplicationStart"=>["ApplicationStart"],
"BeforeAllowTraffic"=>["BeforeAllowTraffic"],
"AfterAllowTraffic"=>["AfterAllowTraffic"],
"ValidateService"=>["ValidateService"]}
@deploy_control_client = mock
@command_executor = InstanceAgent::Plugins::CodeDeployPlugin::CommandExecutor.new({
:deploy_control_client => @deploy_control_client,
:hook_mapping => @test_hook_mapping})
@aws_region = 'us-east-1'
InstanceMetadata.stubs(:region).returns(@aws_region)
end
context "deployment_system method" do
should "always return CodeDeploy" do
assert_equal "CodeDeploy", @command_executor.deployment_system
end
end
context "when executing a command" do
setup do
@cert_helper = CertificateHelper.new
@deployment_id = SecureRandom.uuid
@deployment_group_name = "TestDeploymentGroup"
@application_name = "TestApplicationName"
@deployment_group_id = "foo"
@deployment_creator = "User"
@deployment_type = "IN_PLACE"
@s3Revision = {
"Bucket" => "mybucket",
"Key" => "mykey",
"BundleType" => "tar"
}
@file_exists_behavior = "RETAIN"
@agent_actions_overrides_map = {"FileExistsBehavior" => @file_exists_behavior}
@agent_actions_overrides = {"AgentOverrides" => @agent_actions_overrides_map}
@deployment_spec = generate_signed_message_for({
"DeploymentId" => @deployment_id.to_s,
"DeploymentGroupId" => @deployment_group_id,
"ApplicationName" => @application_name,
"DeploymentGroupName" => @deployment_group_name,
"DeploymentCreator" => @deployment_creator,
"DeploymentType" => @deployment_type,
"AgentActionOverrides" => @agent_actions_overrides,
"Revision" => {
"RevisionType" => "S3",
"S3Revision" => @s3Revision
}
})
@command = Aws::CodeDeployCommand::Types::HostCommandInstance.new(
:host_command_identifier => "command-1",
:deployment_execution_id => "test-execution")
@root_dir = '/tmp/codedeploy/'
@deployment_root_dir = File.join(@root_dir, @deployment_group_id.to_s, @deployment_id.to_s)
@deployment_instructions_dir = File.join(@root_dir, 'deployment-instructions')
@archive_root_dir = File.join(@deployment_root_dir, 'deployment-archive')
ProcessManager::Config.config[:root_dir] = @root_dir
FileUtils.stubs(:mkdir_p)
File.stubs(:directory?).with(@deployment_root_dir).returns(true)
@last_successful_install_file_location = File.join(@deployment_instructions_dir, "#{@deployment_group_id}_last_successful_install")
@most_recent_install_file_location = File.join(@deployment_instructions_dir, "#{@deployment_group_id}_most_recent_install")
end
context "when executing an unknown command" do
setup do
@command.command_name = "unknown-command"
end
should "not create the deployment root directory" do
# Need to unstub the :mkdir_p method otherwise the never expectation doesn't work
FileUtils.unstub(:mkdir_p)
FileUtils.expects(:mkdir_p).never
assert_raised_with_message('Unsupported command type: unknown-command.', InstanceAgent::Plugins::CodeDeployPlugin::CommandExecutor::InvalidCommandNameFailure) do
@command_executor.execute_command(@command, @deployment_spec)
end
end
should "throw an exception" do
assert_raised_with_message('Unsupported command type: unknown-command.', InstanceAgent::Plugins::CodeDeployPlugin::CommandExecutor::InvalidCommandNameFailure) do
@command_executor.execute_command(@command, @deployment_spec)
end
end
end
context "when executing a valid command" do
setup do
@command.command_name = "Install"
@command_executor.stubs(:install)
end
should "create the deployment root directory" do
FileUtils.expects(:mkdir_p).with(@deployment_root_dir)
@command_executor.execute_command(@command, @deployment_spec)
end
context "when failed to create root directory" do
setup do
File.stubs(:directory?).with(@deployment_root_dir).returns(false)
end
should "raise an exception" do
assert_raised_with_message("Error creating deployment root directory #{@deployment_root_dir}") do
@command_executor.execute_command(@command, @deployment_spec)
end
end
end
end
context "when executing the Install command" do
setup do
@command.command_name = "Install"
InstanceAgent::Plugins::CodeDeployPlugin::ApplicationSpecification::ApplicationSpecification.stubs(:parse).returns(@app_spec)
@installer = stub("installer", :install => nil)
Installer.stubs(:new).returns(@installer)
File.stubs(:directory?).with(@deployment_instructions_dir).returns(true)
File.stubs(:exist?).with(@last_successful_install_file_location).returns(true)
File.stubs(:exist?).with(@archive_root_dir).returns(true)
File.stubs(:open).with(@last_successful_install_file_location, 'w+')
File.stubs(:open).with(@last_successful_install_file_location)
@app_spec = mock("parsed application specification")
File.
stubs(:read).
with("#@archive_root_dir/appspec.yml").
returns("APP SPEC")
ApplicationSpecification::ApplicationSpecification.stubs(:parse).with("APP SPEC", {}).returns(@app_spec)
end
should "create an appropriate Installer" do
Installer.
expects(:new).
with(:deployment_instructions_dir => @deployment_instructions_dir,
:deployment_archive_dir => @archive_root_dir,
:file_exists_behavior => @file_exists_behavior).
returns(@installer)
@command_executor.execute_command(@command, @deployment_spec)
end
should "perform the installation for the current IG, revision and app spec" do
@installer.expects(:install).with(@deployment_group_id, @app_spec)
@command_executor.execute_command(@command, @deployment_spec)
end
should "write the archive root dir to the install instructions file" do
mock_file = mock
File.expects(:open).with(@last_successful_install_file_location, 'w+').yields(mock_file)
mock_file.expects(:write).with(@deployment_root_dir)
@command_executor.execute_command(@command, @deployment_spec)
end
should 'raise ArgumentError if appspec contains unknown hook and deployment_spec includes all_possible_lifecycle_events' do
all_possible_lifecycle_events = ['ExampleLifecycleEvent', 'SecondLifecycleEvent']
deployment_spec = generate_signed_message_for({
"DeploymentId" => @deployment_id.to_s,
"DeploymentGroupId" => @deployment_group_id,
"ApplicationName" => @application_name,
"DeploymentGroupName" => @deployment_group_name,
"DeploymentCreator" => @deployment_creator,
"DeploymentType" => @deployment_type,
"AgentActionOverrides" => @agent_actions_overrides,
"AllPossibleLifecycleEvents" => all_possible_lifecycle_events,
"Revision" => {
"RevisionType" => "S3",
"S3Revision" => @s3Revision
}
})
app_spec = mock("parsed application specification")
app_spec_hooks = {'UnknownHook' => nil}
app_spec.expects(:hooks).returns(app_spec_hooks)
File.stubs(:read).with("#@archive_root_dir/appspec.yml").returns("APP SPEC")
ApplicationSpecification::ApplicationSpecification.stubs(:parse).with("APP SPEC", {}).returns(app_spec)
unknown_hooks = app_spec_hooks.merge(@test_hook_mapping)
assert_raised_with_message("appspec.yml file contains unknown lifecycle events: #{unknown_hooks.keys}", ArgumentError) do
@command_executor.execute_command(@command, deployment_spec)
end
end
should 'raise ArgumentError if appspec custom hook specified that does not exist in appspec' do
all_possible_lifecycle_events = AWS::CodeDeploy::Local::Deployer::DEFAULT_ORDERED_LIFECYCLE_EVENTS + ['ExampleLifecycleEvent', 'SecondLifecycleEvent', 'CustomHookNotInAppspec']
deployment_spec = generate_signed_message_for({
"DeploymentId" => @deployment_id.to_s,
"DeploymentGroupId" => @deployment_group_id,
"ApplicationName" => @application_name,
"DeploymentGroupName" => @deployment_group_name,
"DeploymentCreator" => @deployment_creator,
"DeploymentType" => @deployment_type,
"AgentActionOverrides" => @agent_actions_overrides,
"AllPossibleLifecycleEvents" => all_possible_lifecycle_events,
"Revision" => {
"RevisionType" => "S3",
"S3Revision" => @s3Revision
}
})
app_spec = mock("parsed application specification")
app_spec_hooks = InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller::DEFAULT_HOOK_MAPPING.merge({'ExampleLifecycleEvent' => nil, 'SecondLifecycleEvent' => nil})
app_spec.expects(:hooks).twice.returns(app_spec_hooks)
File.stubs(:read).with("#@archive_root_dir/appspec.yml").returns("APP SPEC")
ApplicationSpecification::ApplicationSpecification.stubs(:parse).with("APP SPEC", {}).returns(app_spec)
assert_raised_with_message("You specified a lifecycle event which is not a default one and doesn't exist in your appspec.yml file: CustomHookNotInAppspec", ArgumentError) do
@command_executor.execute_command(@command, deployment_spec)
end
end
end
context "when executing the DownloadBundle command" do
setup do
InstanceAgent::LinuxUtil.stubs(:extract_tar)
InstanceAgent::LinuxUtil.stubs(:extract_tgz)
@command.command_name = "DownloadBundle"
@http = mock
@mock_file = mock
Net::HTTP.stubs(:start).yields(@http)
File.stubs(:open).returns @mock_file
Dir.stubs(:entries).returns []
@mock_file.stubs(:close)
@http.stubs(:request_get)
@s3 = mock
Aws::S3::Client.stubs(:new).returns(@s3)
end
context "when GitHub revision specified" do
setup do
File.stubs(:directory?).with(@archive_root_dir).returns(true)
FileUtils.stubs(:mv)
FileUtils.stubs(:rmdir)
@mock_file.stubs(:write)
@deployment_spec = generate_signed_message_for({
"DeploymentId" => @deployment_id.to_s,
"DeploymentGroupId" => @deployment_group_id.to_s,
"ApplicationName" => @application_name,
"DeploymentGroupName" => @deployment_group_name,
"Revision" => {
"RevisionType" => "GitHub",
"GitHubRevision" => {
'Account' => 'account',
'Repository' => 'repository',
'CommitId' => 'commitid',
}
}
})
ENV['AWS_SSL_CA_DIRECTORY'] = 'aws_ssl_ca_directory'
@mock_uri = mock
uri_options = {:ssl_verify_mode => OpenSSL::SSL::VERIFY_PEER, :redirect => true, :ssl_ca_cert => ENV['AWS_SSL_CA_DIRECTORY']}
@mock_buffer = mock
@mock_github_response = mock
@mock_github_response.stubs(:read).returns(@mock_buffer)
@mock_uri.stubs(:open).with(uri_options).yields(@mock_github_response)
end
should 'download file from github' do
URI.expects(:parse).with("https://api.github.com/repos/account/repository/tarball/commitid").returns(@mock_uri)
@command_executor.execute_command(@command, @deployment_spec)
end
context 'when Github bundle_type is specified' do
setup do
@bundle_type = 'zip'
@deployment_spec = generate_signed_message_for({
"DeploymentId" => @deployment_id.to_s,
"DeploymentGroupId" => @deployment_group_id.to_s,
"ApplicationName" => @application_name,
"DeploymentGroupName" => @deployment_group_name,
"Revision" => {
"RevisionType" => "GitHub",
"GitHubRevision" => {
'Account' => 'account',
'Repository' => 'repository',
'CommitId' => 'commitid',
'BundleType' => @bundle_type
}
}
})
end
should 'downloads from github with the corresponding format' do
URI.expects(:parse).with("https://api.github.com/repos/account/repository/zipball/commitid").returns(@mock_uri)
Zip::File.expects(:open).with(File.join(@deployment_root_dir, 'bundle.tar'))
@command_executor.execute_command(@command, @deployment_spec)
end
end
end
context "downloading bundle from S3" do
setup do
File.expects(:open).with(File.join(@deployment_root_dir, 'bundle.tar'), 'wb').yields(@mock_file)
@object = mock
@s3.stubs(:get_object).returns(@object)
@io = mock
@object.stubs(:etag).returns("myetag")
@deployment_spec = generate_signed_message_for({
"DeploymentId" => @deployment_id.to_s,
"DeploymentGroupId" => @deployment_group_id.to_s,
"ApplicationName" => @application_name,
"DeploymentGroupName" => @deployment_group_name,
"Revision" => {
"RevisionType" => "S3",
"S3Revision" => {
"Bucket" => "mybucket",
"Key" => "mykey",
"BundleType" => "tar"
}
}
})
end
context "when setting up the S3 client" do
setup do
ENV['AWS_REGION'] = nil
InstanceMetadata.stubs(:region).returns('us-east-1')
end
should "read from the InstanceMetadata to get the region" do
InstanceMetadata.expects(:region)
@command_executor.execute_command(@command, @deployment_spec)
end
end
should "verify etag" do
@deployment_spec = generate_signed_message_for({
"DeploymentId" => @deployment_id.to_s,
"DeploymentGroupId" => @deployment_group_id.to_s,
"ApplicationName" => @application_name,
"DeploymentGroupName" => @deployment_group_name,
"Revision" => {
"RevisionType" => "S3",
"S3Revision" => {
"Bucket" => "mybucket",
"Key" => "mykey",
"BundleType" => "tar",
"ETag" => "myetag"
}
}
})
@command_executor.execute_command(@command, @deployment_spec)
end
should "verify etag that contains quotations still matches" do
@object.stubs(:etag).returns("\"myetag\"")
@deployment_spec = generate_signed_message_for({
"DeploymentId" => @deployment_id.to_s,
"DeploymentGroupId" => @deployment_group_id.to_s,
"ApplicationName" => @application_name,
"DeploymentGroupName" => @deployment_group_name,
"Revision" => {
"RevisionType" => "S3",
"S3Revision" => {
"Bucket" => "mybucket",
"Key" => "mykey",
"BundleType" => "tar",
"ETag" => "myetag"
}
}
})
@command_executor.execute_command(@command, @deployment_spec)
end
should "verify version" do
@object.stubs(:body).with(:bucket => "mybucket", :key => "mykey", :version_id => "myversion").returns(@io)
@deployment_spec = generate_signed_message_for({
"DeploymentId" => @deployment_id.to_s,
"DeploymentGroupId" => @deployment_group_id.to_s,
"ApplicationName" => @application_name,
"DeploymentGroupName" => @deployment_group_name,
"Revision" => {
"RevisionType" => "S3",
"S3Revision" => {
"Bucket" => "mybucket",
"Key" => "mykey",
"BundleType" => "tar",
"Version" => "myversion"
}
}
})
@command_executor.execute_command(@command, @deployment_spec)
end
should "call zip for zip BundleTypes" do
@deployment_spec = generate_signed_message_for({
"DeploymentId" => @deployment_id.to_s,
"DeploymentGroupId" => @deployment_group_id.to_s,
"ApplicationName" => @application_name,
"DeploymentGroupName" => @deployment_group_name,
"Revision" => {
"RevisionType" => "S3",
"S3Revision" => {
"Bucket" => "mybucket",
"Key" => "mykey",
"BundleType" => "zip"
}
}
})
Zip::File.expects(:open).with(File.join(@deployment_root_dir, 'bundle.tar'))
@command_executor.execute_command(@command, @deployment_spec)
end
should "call extract_tgz for Gzipped tar BundleTypes" do
@deployment_spec = generate_signed_message_for({
"DeploymentId" => @deployment_id.to_s,
"DeploymentGroupId" => @deployment_group_id.to_s,
"ApplicationName" => @application_name,
"DeploymentGroupName" => @deployment_group_name,
"Revision" => {
"RevisionType" => "S3",
"S3Revision" => {
"Bucket" => "mybucket",
"Key" => "mykey",
"BundleType" => "tgz"
}
}
})
InstanceAgent::LinuxUtil.expects(:extract_tgz).with(File.join(@deployment_root_dir, 'bundle.tar'), @archive_root_dir)
@command_executor.execute_command(@command, @deployment_spec)
end
should "call extract_tar for tar BundleTypes" do
@deployment_spec = generate_signed_message_for({
"DeploymentId" => @deployment_id.to_s,
"DeploymentGroupId" => @deployment_group_id.to_s,
"ApplicationName" => @application_name,
"DeploymentGroupName" => @deployment_group_name,
"Revision" => {
"RevisionType" => "S3",
"S3Revision" => {
"Bucket" => "mybucket",
"Key" => "mykey",
"BundleType" => "tar"
}
}
})
InstanceAgent::LinuxUtil.expects(:extract_tar).with(File.join(@deployment_root_dir, 'bundle.tar'), @archive_root_dir)
@command_executor.execute_command(@command, @deployment_spec)
end
end
context "extract bundle from local file" do
setup do
InstanceAgent::LinuxUtil.stubs(:extract_tgz)
@command.command_name = "DownloadBundle"
@mock_file = mock
@mock_file_location = '/mock/file/location.tgz'
File.stubs(:symlink)
Dir.stubs(:entries).returns []
@mock_file.stubs(:close)
@deployment_spec = generate_signed_message_for({
"DeploymentId" => @deployment_id.to_s,
"DeploymentGroupId" => @deployment_group_id.to_s,
"ApplicationName" => @application_name,
"DeploymentGroupName" => @deployment_group_name,
"Revision" => {
"RevisionType" => "Local File",
"LocalRevision" => {
"Location" => @mock_file_location,
"BundleType" => 'tgz'
}
}
})
end
should 'symlink the file to the bundle location' do
File.expects(:symlink).with(@mock_file_location, File.join(@deployment_root_dir, 'bundle.tar'))
@command_executor.execute_command(@command, @deployment_spec)
end
end
context "handle bundle from local directory" do
setup do
@command.command_name = "DownloadBundle"
@mock_directory_location = '/mock/directory/location/'
FileUtils.stubs(:cp_r)
Dir.stubs(:entries).returns []
@mock_file.stubs(:close)
@deployment_spec = generate_signed_message_for({
"DeploymentId" => @deployment_id.to_s,
"DeploymentGroupId" => @deployment_group_id.to_s,
"ApplicationName" => @application_name,
"DeploymentGroupName" => @deployment_group_name,
"Revision" => {
"RevisionType" => "Local Directory",
"LocalRevision" => {
"Location" => @mock_directory_location,
"BundleType" => 'directory'
}
}
})
end
should 'copy recursively the directory to the bundle location' do
FileUtils.expects(:cp_r).with(@mock_directory_location, @archive_root_dir)
@command_executor.execute_command(@command, @deployment_spec)
end
end
should "unpack the bundle to the right directory" do
InstanceAgent::LinuxUtil.expects(:extract_tar).with(File.join(@deployment_root_dir, 'bundle.tar'), @archive_root_dir)
@command_executor.execute_command(@command, @deployment_spec)
end
should "remove the directory before unpacking" do
call_sequence = sequence("call sequence")
FileUtils.expects(:rm_rf).with(@archive_root_dir).in_sequence(call_sequence)
InstanceAgent::LinuxUtil.expects(:extract_tar).in_sequence(call_sequence)
@command_executor.execute_command(@command, @deployment_spec)
end
should "idempotently create the instructions directory" do
FileUtils.expects(:mkdir_p).with(@deployment_instructions_dir)
@command_executor.execute_command(@command, @deployment_spec)
end
should "write the archive root dir to the install instructions file" do
mock_file = mock
File.expects(:open).with(@most_recent_install_file_location, 'w+').yields(mock_file)
mock_file.expects(:write).with(@deployment_root_dir)
@command_executor.execute_command(@command, @deployment_spec)
end
end
context "I have an empty app spec (for script mapping)" do
setup do
File.stubs(:read).with(File.join(@archive_root_dir, 'appspec.yml')).returns(nil)
InstanceAgent::Plugins::CodeDeployPlugin::ApplicationSpecification::ApplicationSpecification.stubs(:parse).returns(nil)
@hook_executor_constructor_hash = {
:application_name => @application_name,
:deployment_id => @deployment_id,
:deployment_group_name => @deployment_group_name,
:deployment_group_id => @deployment_group_id,
:deployment_creator => @deployment_creator,
:deployment_type => @deployment_type,
:deployment_root_dir => @deployment_root_dir,
:last_successful_deployment_dir => nil,
:most_recent_deployment_dir => nil,
:app_spec_path => 'appspec.yml'}
@mock_hook_executor = mock
end
context "BeforeBlockTraffic" do
setup do
@command.command_name = "BeforeBlockTraffic"
@hook_executor_constructor_hash[:lifecycle_event] = "BeforeBlockTraffic"
end
should "call execute a hook executor object with BeforeBlockTraffic as one of the params" do
HookExecutor.expects(:new).with(@hook_executor_constructor_hash).returns(@mock_hook_executor)
@mock_hook_executor.expects(:execute)
@command_executor.execute_command(@command, @deployment_spec)
end
end
context "AfterBlockTraffic" do
setup do
@command.command_name = "AfterBlockTraffic"
@hook_executor_constructor_hash[:lifecycle_event] = "AfterBlockTraffic"
end
should "call execute a hook executor object with AfterBlockTraffic as one of the params" do
HookExecutor.expects(:new).with(@hook_executor_constructor_hash).returns(@mock_hook_executor)
@mock_hook_executor.expects(:execute)
@command_executor.execute_command(@command, @deployment_spec)
end
end
context "ApplicationStop" do
setup do
@command.command_name = "ApplicationStop"
@hook_executor_constructor_hash[:lifecycle_event] = "ApplicationStop"
end
should "call execute a hook executor object with ApplicationStop as one of the params" do
HookExecutor.expects(:new).with(@hook_executor_constructor_hash).returns(@mock_hook_executor)
@mock_hook_executor.expects(:execute)
@command_executor.execute_command(@command, @deployment_spec)
end
end
context "BeforeInstall" do
setup do
@command.command_name = "BeforeInstall"
@hook_executor_constructor_hash[:lifecycle_event] = "BeforeInstall"
end
should "call execute a hook executor object with BeforeInstall as one of the params" do
HookExecutor.expects(:new).with(@hook_executor_constructor_hash).returns(@mock_hook_executor)
@mock_hook_executor.expects(:execute)
@command_executor.execute_command(@command, @deployment_spec)
end
end
context "AfterInstall" do
setup do
@command.command_name = "AfterInstall"
@hook_executor_constructor_hash[:lifecycle_event] = "AfterInstall"
end
should "call execute a hook executor object with AfterInstall as one of the params" do
HookExecutor.expects(:new).with(@hook_executor_constructor_hash).returns(@mock_hook_executor)
@mock_hook_executor.expects(:execute)
@command_executor.execute_command(@command, @deployment_spec)
end
end
context "ApplicationStart" do
setup do
@command.command_name = "ApplicationStart"
@hook_executor_constructor_hash[:lifecycle_event] = "ApplicationStart"
end
should "call execute a hook executor object with ApplicationStart as one of the params" do
HookExecutor.expects(:new).with(@hook_executor_constructor_hash).returns(@mock_hook_executor)
@mock_hook_executor.expects(:execute)
@command_executor.execute_command(@command, @deployment_spec)
end
end
context "BeforeAllowTraffic" do
setup do
@command.command_name = "BeforeAllowTraffic"
@hook_executor_constructor_hash[:lifecycle_event] = "BeforeAllowTraffic"
end
should "call execute a hook executor object with BeforeAllowTraffic as one of the params" do
HookExecutor.expects(:new).with(@hook_executor_constructor_hash).returns(@mock_hook_executor)
@mock_hook_executor.expects(:execute)
@command_executor.execute_command(@command, @deployment_spec)
end
end
context "AfterAllowTraffic" do
setup do
@command.command_name = "AfterAllowTraffic"
@hook_executor_constructor_hash[:lifecycle_event] = "AfterAllowTraffic"
end
should "call execute a hook executor object with AfterAllowTraffic as one of the params" do
HookExecutor.expects(:new).with(@hook_executor_constructor_hash).returns(@mock_hook_executor)
@mock_hook_executor.expects(:execute)
@command_executor.execute_command(@command, @deployment_spec)
end
end
context "ValidateService" do
setup do
@command.command_name = "ValidateService"
@hook_executor_constructor_hash[:lifecycle_event] = "ValidateService"
end
should "call execute a hook executor object with ValidateService as one of the params" do
HookExecutor.expects(:new).with(@hook_executor_constructor_hash).returns(@mock_hook_executor)
@mock_hook_executor.expects(:execute)
@command_executor.execute_command(@command, @deployment_spec)
end
end
end
#non 1:1 mapping tests
context "one command hooks to multiple lifecycle events" do
setup do
@command.command_name = "test_command"
@test_hook_mapping = { "test_command" => ["lifecycle_event_1","lifecycle_event_2"]}
@deploy_control_client = mock
@command_executor = InstanceAgent::Plugins::CodeDeployPlugin::CommandExecutor.new({
:deploy_control_client => @deploy_control_client,
:hook_mapping => @test_hook_mapping})
hook_executor_constructor_hash = {
:deployment_root_dir => @deployment_root_dir,
:application_name => @application_name,
:deployment_id => @deployment_id,
:deployment_group_name => @deployment_group_name,
:deployment_group_id => @deployment_group_id,
:deployment_creator => @deployment_creator,
:deployment_type => @deployment_type,
:last_successful_deployment_dir => nil,
:most_recent_deployment_dir => nil,
:app_spec_path => 'appspec.yml'}
@hook_executor_constructor_hash_1 = hook_executor_constructor_hash.merge({:lifecycle_event => "lifecycle_event_1"})
@hook_executor_constructor_hash_2 = hook_executor_constructor_hash.merge({:lifecycle_event => "lifecycle_event_2"})
@mock_hook_executor = mock
end
should "call both lifecycle events" do
HookExecutor.expects(:new).with(@hook_executor_constructor_hash_1).returns(@mock_hook_executor)
HookExecutor.expects(:new).with(@hook_executor_constructor_hash_2).returns(@mock_hook_executor)
@mock_hook_executor.expects(:execute).twice
@command_executor.execute_command(@command, @deployment_spec)
end
context "when the first script is forced to fail" do
setup do
HookExecutor.stubs(:new).with(@hook_executor_constructor_hash_1).raises("failed to create hook command")
end
should "calls lifecycle event 1 and fails but not lifecycle event 2" do
assert_raised_with_message('failed to create hook command') do
@command_executor.execute_command(@command, @deployment_spec)
end
HookExecutor.expects(:new).with(@hook_executor_constructor_hash_2).never
end
end
end
end
end
end