@@ -6,7 +6,7 @@ class HookExecutorTest < InstanceAgentTestCase
6
6
7
7
include InstanceAgent ::Plugins ::CodeDeployPlugin
8
8
9
- def create_full_hook_executor
9
+ def create_hook_executor ( map = { } )
10
10
HookExecutor . new ( { :lifecycle_event => @lifecycle_event ,
11
11
:application_name => @application_name ,
12
12
:deployment_id => @deployment_id ,
@@ -17,7 +17,8 @@ def create_full_hook_executor
17
17
:deployment_root_dir => @deployment_root_dir ,
18
18
:last_successful_deployment_dir => @last_successful_deployment_dir ,
19
19
:most_recent_deployment_dir => @most_recent_deployment_dir ,
20
- :app_spec_path => @app_spec_path } )
20
+ :app_spec_path => @app_spec_path }
21
+ . merge ( map ) )
21
22
end
22
23
23
24
context "testing hook executor" do
@@ -91,13 +92,13 @@ def create_full_hook_executor
91
92
should "fail if app spec not found" do
92
93
File . stubs ( :exists? ) . with ( ) { |value | value . is_a? ( String ) && value . end_with? ( "/app_spec" ) } . returns ( false )
93
94
assert_raised_with_message ( "The CodeDeploy agent did not find an AppSpec file within the unpacked revision directory at revision-relative path \" app_spec\" . The revision was unpacked to directory \" deployment/root/dir/deployment-archive\" , and the AppSpec file was expected but not found at path \" deployment/root/dir/deployment-archive/app_spec\" . Consult the AWS CodeDeploy Appspec documentation for more information at http://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file.html" , RuntimeError ) do
94
- @hook_executor = create_full_hook_executor
95
+ @hook_executor = create_hook_executor
95
96
end
96
97
end
97
98
98
99
should "parse an app spec from the current deployments directory" do
99
100
File . expects ( :read ) . with ( File . join ( @deployment_root_dir , 'deployment-archive' , @app_spec_path ) )
100
- @hook_executor = create_full_hook_executor
101
+ @hook_executor = create_hook_executor
101
102
end
102
103
103
104
context "hook is before download bundle" do
@@ -107,7 +108,7 @@ def create_full_hook_executor
107
108
108
109
should "parse an app spec from the last successful deployment's directory" do
109
110
File . expects ( :read ) . with ( File . join ( @last_successful_deployment_dir , 'deployment-archive' , @app_spec_path ) )
110
- @hook_executor = create_full_hook_executor
111
+ @hook_executor = create_hook_executor
111
112
end
112
113
end
113
114
@@ -118,7 +119,7 @@ def create_full_hook_executor
118
119
119
120
should "parse an app spec from the last successful deployment's directory" do
120
121
File . expects ( :read ) . with ( File . join ( @last_successful_deployment_dir , 'deployment-archive' , @app_spec_path ) )
121
- @hook_executor = create_full_hook_executor
122
+ @hook_executor = create_hook_executor
122
123
end
123
124
end
124
125
@@ -131,7 +132,7 @@ def create_full_hook_executor
131
132
132
133
should "parse an app spec from the most recent deployment's directory" do
133
134
File . expects ( :read ) . with ( File . join ( @most_recent_deployment_dir , 'deployment-archive' , @app_spec_path ) )
134
- @hook_executor = create_full_hook_executor
135
+ @hook_executor = create_hook_executor
135
136
end
136
137
end
137
138
end
@@ -152,7 +153,7 @@ def create_full_hook_executor
152
153
setup do
153
154
@app_spec = { "version" => 0.0 , "os" => "linux" , "hooks" => { } }
154
155
YAML . stubs ( :load ) . returns ( @app_spec )
155
- @hook_executor = create_full_hook_executor
156
+ @hook_executor = create_hook_executor
156
157
end
157
158
158
159
should "do nothing" do
@@ -169,7 +170,7 @@ def create_full_hook_executor
169
170
@app_spec = { "version" => 0.0 , "os" => "linux" , "hooks" => { 'ValidateService' => [ { 'location' => 'test' } ] } }
170
171
YAML . stubs ( :load ) . returns ( @app_spec )
171
172
@script_location = File . join ( @deployment_root_dir , 'deployment-archive' , 'test' )
172
- @hook_executor = create_full_hook_executor
173
+ @hook_executor = create_hook_executor
173
174
end
174
175
175
176
should "not be a noop" do
@@ -241,11 +242,25 @@ def create_full_hook_executor
241
242
InstanceAgent ::ThreadJoiner . stubs ( :new ) . returns ( @thread_joiner )
242
243
end
243
244
245
+ context "extra child environment variables are added" do
246
+ setup do
247
+ revision_envs = { "TEST_ENVIRONMENT_VARIABLE" => "ONE" , "ANOTHER_ENV_VARIABLE" => "TWO" }
248
+ @child_env . merge! ( revision_envs )
249
+ @hook_executor = create_hook_executor ( :revision_envs => revision_envs )
250
+ end
251
+
252
+ should "call popen with the environment variables" do
253
+ Open3 . stubs ( :popen3 ) . with ( @child_env , @script_location , :pgroup => true ) . yields ( [ @mock_pipe , @mock_pipe , @mock_pipe , @wait_thr ] )
254
+ @value . stubs ( :exitstatus ) . returns ( 0 )
255
+ @hook_executor . execute ( )
256
+ end
257
+ end
258
+
244
259
context 'scripts fail for unknown reason' do
245
260
setup do
246
261
@app_spec = { "version" => 0.0 , "os" => "linux" , "hooks" => { 'ValidateService' => [ { "location" => "test" , "timeout" => "30" } ] } }
247
262
YAML . stubs ( :load ) . returns ( @app_spec )
248
- @hook_executor = create_full_hook_executor
263
+ @hook_executor = create_hook_executor
249
264
@popen_error = Errno ::ENOENT
250
265
Open3 . stubs ( :popen3 ) . with ( @child_env , @script_location , :pgroup => true ) . raises ( @popen_error , 'su' )
251
266
end
@@ -262,7 +277,7 @@ def create_full_hook_executor
262
277
setup do
263
278
@app_spec = { "version" => 0.0 , "os" => "linux" , "hooks" => { 'ValidateService' => [ { "location" => "test" , "timeout" => "30" } ] } }
264
279
YAML . stubs ( :load ) . returns ( @app_spec )
265
- @hook_executor = create_full_hook_executor
280
+ @hook_executor = create_hook_executor
266
281
@thread_joiner . expects ( :joinOrFail ) . with ( @wait_thr ) . yields
267
282
InstanceAgent ::ThreadJoiner . expects ( :new ) . with ( 30 ) . returns ( @thread_joiner )
268
283
@wait_thr . stubs ( :pid ) . returns ( 1234 )
@@ -307,7 +322,7 @@ def create_full_hook_executor
307
322
Open3 . stubs ( :popen3 ) . with ( @child_env , @script_location , :pgroup => true ) . yields ( [ @mock_pipe , @mock_pipe , @mock_pipe , @wait_thr ] )
308
323
@app_spec = { "version" => 0.0 , "os" => "linux" , "hooks" => { 'ValidateService' => [ { 'location' => 'test' , 'timeout' => "#{ timeout } " } ] } }
309
324
YAML . stubs ( :load ) . returns ( @app_spec )
310
- @hook_executor = create_full_hook_executor
325
+ @hook_executor = create_hook_executor
311
326
end
312
327
313
328
context "STDOUT left open" do
@@ -341,7 +356,7 @@ def create_full_hook_executor
341
356
setup do
342
357
@app_spec = { "version" => 0.0 , "os" => "linux" , "hooks" => { 'ValidateService' => [ { "location" => "test" , "runas" => "user" } ] } }
343
358
YAML . stubs ( :load ) . returns ( @app_spec )
344
- @hook_executor = create_full_hook_executor
359
+ @hook_executor = create_hook_executor
345
360
mock_pipe = mock
346
361
Open3 . stubs ( :popen3 ) . with ( @child_env , 'su user -c ' + @script_location , :pgroup => true ) . yields ( [ @mock_pipe , @mock_pipe , @mock_pipe , @wait_thr ] )
347
362
end
@@ -374,7 +389,7 @@ def create_full_hook_executor
374
389
setup do
375
390
@app_spec = { "version" => 0.0 , "os" => "linux" , "hooks" => { 'ValidateService' => [ { "location" => "test" } ] } }
376
391
YAML . stubs ( :load ) . returns ( @app_spec )
377
- @hook_executor = create_full_hook_executor
392
+ @hook_executor = create_hook_executor
378
393
Open3 . stubs ( :popen3 ) . with ( @child_env , @script_location , :pgroup => true ) . yields ( [ @mock_pipe , @mock_pipe , @mock_pipe , @wait_thr ] )
379
394
end
380
395
@@ -406,7 +421,7 @@ def create_full_hook_executor
406
421
setup do
407
422
@app_spec = { "version" => 0.0 , "os" => "linux" , "hooks" => { 'ValidateService' => [ { "location" => "test" } ] } }
408
423
YAML . stubs ( :load ) . returns ( @app_spec )
409
- @hook_executor = create_full_hook_executor
424
+ @hook_executor = create_hook_executor
410
425
Open3 . stubs ( :popen3 ) . with ( @child_env , @script_location , { } ) . yields ( [ @mock_pipe , @mock_pipe , @mock_pipe , @wait_thr ] )
411
426
InstanceAgent ::LinuxUtil . stubs ( :supports_process_groups? ) . returns ( false )
412
427
end
0 commit comments