diff --git a/lib/instance_agent/plugins/codedeploy/command_executor.rb b/lib/instance_agent/plugins/codedeploy/command_executor.rb index 5d7e198..6ab2d86 100644 --- a/lib/instance_agent/plugins/codedeploy/command_executor.rb +++ b/lib/instance_agent/plugins/codedeploy/command_executor.rb @@ -231,13 +231,23 @@ def get_revision_envs(deployment_spec) case deployment_spec.revision_source when 'S3' return get_s3_envs(deployment_spec) - when 'GitHub', 'Local File', 'Local Directory' + when 'GitHub' + return get_github_envs(deployment_spec) + when 'Local File', 'Local Directory' return {} else raise "Unknown revision type '#{deployment_spec.revision_source}'" end end + private + def get_github_envs(deployment_spec) + # TODO(CDAGENT-387): expose the repository name and account, but we'll likely need to go through AppSec before doing so. + return { + "BUNDLE_COMMIT" => deployment_spec.commit_id + } + end + private def get_s3_envs(deployment_spec) return { diff --git a/test/instance_agent/plugins/codedeploy/command_executor_test.rb b/test/instance_agent/plugins/codedeploy/command_executor_test.rb index a10a983..1d6e4cd 100644 --- a/test/instance_agent/plugins/codedeploy/command_executor_test.rb +++ b/test/instance_agent/plugins/codedeploy/command_executor_test.rb @@ -25,6 +25,12 @@ def s3_env_vars() } 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"], @@ -69,6 +75,11 @@ def s3_env_vars() "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} @@ -126,10 +137,15 @@ def s3_env_vars() end end - context "when executing a valid command" do + context "when executing a valid non-hardcoded command" do setup do - @command.command_name = "Install" - @command_executor.stubs(:install) + @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 @@ -138,8 +154,45 @@ def s3_env_vars() @command_executor.execute_command(@command, @deployment_spec) end - should "not be a noop command" do - assert_false @command_executor.is_command_noop?(@command.command_name, @deployment_spec) + 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 diff --git a/test/instance_agent/plugins/codedeploy/hook_executor_test.rb b/test/instance_agent/plugins/codedeploy/hook_executor_test.rb index 2b24c18..eb091a6 100644 --- a/test/instance_agent/plugins/codedeploy/hook_executor_test.rb +++ b/test/instance_agent/plugins/codedeploy/hook_executor_test.rb @@ -6,7 +6,7 @@ class HookExecutorTest < InstanceAgentTestCase include InstanceAgent::Plugins::CodeDeployPlugin - def create_hook_executor(map = {}) + def create_hook_executor(revision_envs = nil) HookExecutor.new ({:lifecycle_event => @lifecycle_event, :application_name => @application_name, :deployment_id => @deployment_id, @@ -17,8 +17,8 @@ def create_hook_executor(map = {}) :deployment_root_dir => @deployment_root_dir, :last_successful_deployment_dir => @last_successful_deployment_dir, :most_recent_deployment_dir => @most_recent_deployment_dir, - :app_spec_path => @app_spec_path} - .merge(map)) + :app_spec_path => @app_spec_path, + :revision_envs => revision_envs}) end context "testing hook executor" do @@ -246,7 +246,7 @@ def create_hook_executor(map = {}) setup do revision_envs = {"TEST_ENVIRONMENT_VARIABLE" => "ONE", "ANOTHER_ENV_VARIABLE" => "TWO"} @child_env.merge!(revision_envs) - @hook_executor = create_hook_executor(:revision_envs => revision_envs) + @hook_executor = create_hook_executor(revision_envs) end should "call popen with the environment variables" do