Skip to content

Commit 638bfe1

Browse files
aj-awsmwjones-aws
authored andcommitted
Add fast-fail unit tests and diagnostics method
1 parent a88759f commit 638bfe1

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

lib/instance_agent/plugins/codedeploy/command_poller.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,16 @@ def gather_diagnostics_from_error(error)
255255
gather_diagnostics_from_script_error(script_error)
256256
end
257257

258+
private
259+
def gather_diagnostics_from_failure_after_restart(msg = "")
260+
begin
261+
raise ScriptError.new(ScriptError::FAILED_AFTER_RESTART_CODE, "", ScriptLog.new), "Failed: #{msg}"
262+
rescue ScriptError => e
263+
script_error = e
264+
end
265+
gather_diagnostics_from_script_error(script_error)
266+
end
267+
258268
private
259269
def gather_diagnostics(msg = "")
260270
begin

test/instance_agent/plugins/codedeploy/command_poller_test.rb

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,43 @@ def get_ack_diagnostics(is_command_noop)
410410

411411
end
412412

413+
context 'calling recover_from_crash when a lifecycle event is in-progress' do
414+
415+
setup do
416+
InstanceAgent::Plugins::CodeDeployPlugin::DeploymentCommandTracker.stubs(:check_deployment_event_inprogress?).returns(true)
417+
InstanceAgent::Plugins::CodeDeployPlugin::DeploymentCommandTracker.stubs(:deployment_dir_path).returns("deployment-path")
418+
InstanceAgent::Plugins::CodeDeployPlugin::DeploymentCommandTracker.stubs(:most_recent_host_command_identifier).returns("i-123")
419+
InstanceAgent::Plugins::CodeDeployPlugin::DeploymentCommandTracker.stubs(:clean_ongoing_deployment_dir)
420+
end
421+
422+
should 'call PutHostCommandComplete' do
423+
@deploy_control_client.expects(:put_host_command_complete).
424+
with(:command_status => "Failed",
425+
:diagnostics => {:format => "JSON", :payload => gather_diagnostics_from_failure_after_restart("", "Failing in-progress lifecycle event after an agent restart.")},
426+
:host_command_identifier => "i-123")
427+
428+
@poller.recover_from_crash?
429+
end
430+
end
431+
432+
context 'calling recover_from_crash when a lifecycle event is not in-progress' do
433+
434+
setup do
435+
InstanceAgent::Plugins::CodeDeployPlugin::DeploymentCommandTracker.stubs(:check_deployment_event_inprogress?).returns(false)
436+
InstanceAgent::Plugins::CodeDeployPlugin::DeploymentCommandTracker.stubs(:deployment_dir_path).returns("deployment-path")
437+
InstanceAgent::Plugins::CodeDeployPlugin::DeploymentCommandTracker.stubs(:most_recent_host_command_identifier).returns("i-123")
438+
InstanceAgent::Plugins::CodeDeployPlugin::DeploymentCommandTracker.stubs(:clean_ongoing_deployment_dir)
439+
end
440+
441+
should 'not call PutHostCommandComplete' do
442+
@put_host_command_complete_state.become('never')
443+
@deploy_control_client.expects(:put_host_command_complete).never.
444+
when(@put_host_command_complete_state.is('never'))
445+
446+
@poller.recover_from_crash?
447+
end
448+
end
449+
413450
context 'when no deployment specification is given by GetDeploymentSpecification' do
414451

415452
setup do
@@ -447,7 +484,7 @@ def get_ack_diagnostics(is_command_noop)
447484

448485
should 'allow exceptions from execute_command to propagate to caller' do
449486
@executor.expects(:execute_command).
450-
raises("some error")
487+
raises("some error")
451488
@deploy_control_client.expects(:put_host_command_complete).
452489
with(:command_status => "Failed",
453490
:diagnostics => {:format => "JSON", :payload => gather_diagnostics_from_error(RuntimeError.new("some error"))},

0 commit comments

Comments
 (0)