-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathcommand_executor_test.rb
More file actions
1073 lines (946 loc) · 47.1 KB
/
command_executor_test.rb
File metadata and controls
1073 lines (946 loc) · 47.1 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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'test_helper'
require 'certificate_helper'
require 'stringio'
require 'aws-sdk-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
def s3_env_vars()
return {
"BUNDLE_BUCKET" => @s3Revision["Bucket"],
"BUNDLE_KEY" => @s3Revision["Key"],
"BUNDLE_VERSION" => @s3Revision["Version"],
"BUNDLE_ETAG" => @s3Revision["Etag"]
}
end
def github_env_vars()
return {
"BUNDLE_COMMIT" => @githubRevision["CommitId"]
}
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'
@partition = 'aws'
@domain = 'amazonaws.com'
InstanceMetadata.stubs(:region).returns(@aws_region)
InstanceMetadata.stubs(:partition).returns(@partition)
InstanceMetadata.stubs(:domain).returns(@domain)
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"
}
@githubRevision = {
'Account' => 'account',
'Repository' => 'repository',
'CommitId' => 'commitid',
}
@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
should "be a noop" do
assert_true @command_executor.is_command_noop?(@command.command_name, @deployment_spec)
end
should "have a total timeout of nil" do
assert_nil(
@command_executor.total_timeout_for_all_lifecycle_events(@command.command_name, @deployment_spec),
"Unknown command should have a total timeout of nil")
end
end
context "when executing a valid non-hardcoded command" do
setup do
@command.command_name = "ValidateService"
@command_executor.stubs(:validate_service)
@app_spec = mock("parsed application specification")
File.stubs(:exist?).with("#@archive_root_dir/appspec.yml").returns(true)
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 the deployment root directory" do
FileUtils.expects(:mkdir_p).with(@deployment_root_dir)
@command_executor.execute_command(@command, @deployment_spec)
end
context "when the bundle is from github" do
setup do
@deployment_spec = generate_signed_message_for({
"DeploymentId" => @deployment_id.to_s,
"DeploymentGroupId" => @deployment_group_id.to_s,
"ApplicationName" => @application_name,
"DeploymentCreator" => @deployment_creator,
"DeploymentGroupName" => @deployment_group_name,
"Revision" => {
"RevisionType" => "GitHub",
"GitHubRevision" => @githubRevision
}
})
@hook_executor_constructor_hash = {
:lifecycle_event => @command.command_name,
: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',
:revision_envs => github_env_vars()}
@mock_hook_executor = mock
@command_executor.unstub(:validate_service)
@command_executor.stubs(:last_successful_deployment_dir).returns(nil)
@command_executor.stubs(:most_recent_deployment_dir).returns(nil)
end
should "create a hook executor with the commit hash as an environment variable" 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 "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(:exist?).with("#@archive_root_dir/appspec.yml").returns(true)
File.stubs(:read).with("#@archive_root_dir/appspec.yml").returns("APP SPEC")
ApplicationSpecification::ApplicationSpecification.stubs(:parse).with("APP SPEC").returns(@app_spec)
end
should "not be a noop command" do
assert_false @command_executor.is_command_noop?(@command.command_name, @deployment_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
should 'honor the AppSpecFilename command variable' do
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,
"AppSpecFilename" => "appspec-override.yaml",
"Revision" => {
"RevisionType" => "S3",
"S3Revision" => @s3Revision
}
})
File.expects(:exist?).with("#@archive_root_dir/appspec-override.yaml").returns(true)
File.expects(:read).with("#@archive_root_dir/appspec-override.yaml").returns("APP SPEC")
@command_executor.execute_command(@command, deployment_spec)
end
should 'fallback to appspec.yaml if provided AppSpecFilename variable points to a file which does\'t exist' do
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,
"AppSpecFilename" => "appspec-override.yaml",
"Revision" => {
"RevisionType" => "S3",
"S3Revision" => @s3Revision
}
})
File.expects(:exist?).with("#@archive_root_dir/appspec-override.yaml").returns(false)
File.expects(:exist?).with("#@archive_root_dir/appspec.yaml").returns(true)
File.expects(:read).with("#@archive_root_dir/appspec.yaml").returns("APP SPEC")
@command_executor.execute_command(@command, deployment_spec)
end
should 'fallback to appspec.yaml if both AppSpecFilename variable and appspec.yaml don\'t exist' do
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,
"AppSpecFilename" => "appspec-override.yaml",
"Revision" => {
"RevisionType" => "S3",
"S3Revision" => @s3Revision
}
})
File.expects(:exist?).with("#@archive_root_dir/appspec-override.yaml").returns(false)
File.expects(:exist?).with("#@archive_root_dir/appspec.yaml").returns(false)
File.expects(:read).with("#@archive_root_dir/appspec.yml").returns("APP SPEC")
@command_executor.execute_command(@command, deployment_spec)
end
should 'fallback to appspec.yaml if appspec.yml is not there and no AppSpecFilename arg is specified' do
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
}
})
File.expects(:exist?).with("#@archive_root_dir/appspec.yml").returns(false)
File.expects(:exist?).with("#@archive_root_dir/appspec.yaml").returns(true)
File.expects(:read).with("#@archive_root_dir/appspec.yaml").returns("APP SPEC")
@command_executor.execute_command(@command, deployment_spec)
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
@s3.stubs(:config).returns("hello")
Aws::S3::Client.stubs(:new).returns(@s3)
end
should "not be a noop" do
assert_false @command_executor.is_command_noop?(@command.command_name, @deployment_spec)
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 "when creating S3 options" do
should "use right signature version" do
assert_equal 'v4', @command_executor.s3_options[:signature_version]
end
context "when override endpoint provided" do
setup do
InstanceAgent::Config.config[:s3_endpoint_override] = "https://example.override.endpoint.com"
end
should "use the override endpoint" do
assert_equal "https://example.override.endpoint.com", @command_executor.s3_options[:endpoint].to_s
end
end
context "when no override endpoint provided and not using fips" do
setup do
InstanceAgent::Config.config[:s3_endpoint_override] = nil
InstanceAgent::Config.config[:use_fips_mode] = false
end
should "use correct region and custom endpoint" do
assert_equal 'us-east-1', @command_executor.s3_options[:region]
assert_false @command_executor.s3_options.include? :endpoint
end
end
context "when no override endpoint provided and using fips" do
setup do
InstanceAgent::Config.config[:s3_endpoint_override] = nil
InstanceAgent::Config.config[:use_fips_mode] = true
end
should "use correct region and custom endpoint" do
assert_equal 'us-east-1', @command_executor.s3_options[:region]
assert_true @command_executor.s3_options.include? :endpoint
end
end
context "when region is EU GovCloud with instance metadata" do
setup do
InstanceAgent::Config.config[:s3_endpoint_override] = nil
InstanceAgent::Config.config[:use_fips_mode] = false
ENV['AWS_REGION'] = nil
InstanceMetadata.stubs(:region).returns('eusc-de-east-1')
end
should "use S3 EU GovCloud specific endpoint" do
assert_equal 'eusc-de-east-1', @command_executor.s3_options[:region]
assert_equal 'https://s3.eusc-de-east-1.amazonaws.eu', @command_executor.s3_options[:endpoint]
end
end
context "when region is EU GovCloud" do
setup do
InstanceAgent::Config.config[:s3_endpoint_override] = nil
InstanceAgent::Config.config[:use_fips_mode] = false
ENV['AWS_REGION'] = 'eusc-de-east-1'
end
should "use S3 EU GovCloud specific endpoint" do
assert_equal 'eusc-de-east-1', @command_executor.s3_options[:region]
assert_equal 'https://s3.eusc-de-east-1.amazonaws.eu', @command_executor.s3_options[:endpoint]
end
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',
:revision_envs => s3_env_vars()}
@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
should "be a noop" do
HookExecutor.expects(:new).with(@hook_executor_constructor_hash).returns(@mock_hook_executor)
@mock_hook_executor.expects(:is_noop?).returns(true)
assert_true @command_executor.is_command_noop?(@command.command_name, @deployment_spec)
end
should "have a total script timeout of nil" do
assert_nil(
@command_executor.total_timeout_for_all_lifecycle_events(@command, @deployment_spec),
"Total timeout should be whatever's returned by HookExecutor")
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
should "be a noop" do
HookExecutor.expects(:new).with(@hook_executor_constructor_hash).returns(@mock_hook_executor)
@mock_hook_executor.expects(:is_noop?).returns(true)
assert_true @command_executor.is_command_noop?(@command.command_name, @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
should "be a noop" do
HookExecutor.expects(:new).with(@hook_executor_constructor_hash).returns(@mock_hook_executor)
@mock_hook_executor.expects(:is_noop?).returns(true)
assert_true @command_executor.is_command_noop?(@command.command_name, @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
should "be a noop" do
HookExecutor.expects(:new).with(@hook_executor_constructor_hash).returns(@mock_hook_executor)
@mock_hook_executor.expects(:is_noop?).returns(true)
assert_true @command_executor.is_command_noop?(@command.command_name, @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
should "be a noop" do
HookExecutor.expects(:new).with(@hook_executor_constructor_hash).returns(@mock_hook_executor)
@mock_hook_executor.expects(:is_noop?).returns(true)
assert_true @command_executor.is_command_noop?(@command.command_name, @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
should "be a noop" do
HookExecutor.expects(:new).with(@hook_executor_constructor_hash).returns(@mock_hook_executor)
@mock_hook_executor.expects(:is_noop?).returns(true)
assert_true @command_executor.is_command_noop?(@command.command_name, @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
should "be a noop" do
HookExecutor.expects(:new).with(@hook_executor_constructor_hash).returns(@mock_hook_executor)
@mock_hook_executor.expects(:is_noop?).returns(true)
assert_true @command_executor.is_command_noop?(@command.command_name, @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
should "be a noop" do
HookExecutor.expects(:new).with(@hook_executor_constructor_hash).returns(@mock_hook_executor)
@mock_hook_executor.expects(:is_noop?).returns(true)
assert_true @command_executor.is_command_noop?(@command.command_name, @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
should "be a noop" do
HookExecutor.expects(:new).with(@hook_executor_constructor_hash).returns(@mock_hook_executor)
@mock_hook_executor.expects(:is_noop?).returns(true)
assert_true @command_executor.is_command_noop?(@command.command_name, @deployment_spec)
end
end
end
#non 1:1 mapping tests
context "one command hooks to multiple lifecycle events" do
setup do
@command.command_name = "TestCommand"
@test_hook_mapping = { "TestCommand" => ["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 = {