what-is-quality / aws-codedeploy-agent

Host Agent for AWS CodeDeploy
https://aws.amazon.com/codedeploy
Apache License 2.0
2 stars 0 forks source link

integration test 실행하기 #5

Open wonderer80 opened 5 years ago

wonderer80 commented 5 years ago

기본적으로 README.md 를 보면 rake test-integration 이라고 치면 수행이 된다고 적혀있지만 실제로는 제대로 돌지 않는다.

로컬환경에서 통합 테스트를 돌리기 위한 방법을 알아본다

wonderer80 commented 5 years ago

Rakefile 을 보면 rake test-integration 이라고 치면 다음과 같은 코드를 수행하도록 되어 있다

Cucumber::Rake::Task.new('test-integration-aws-codedeploy-agent', desc) do |t|
    t.cucumber_opts = "features -t ~@Ignore"
  end

실행하자마자 다음과 같은 메시지가 나온다

❯ rake test-integration /Users/wonderer/.rvm/rubies/ruby-2.5.0/bin/ruby -S bundle exec cucumber features -t ~@Ignore Deprecated: Found tags option '~@Ignore'. Support for '~@tag' will be removed from the next release of Cucumber. Please use 'not @tag' instead.

위에서 Task 생성할 때 넣은 cucumber_opts 의 내용중 deprecated 된 것이 있다고 하는데 머리가 아프니 일단 무시했다.

wonderer80 commented 5 years ago

가장 먼저 agent.feature 안에 있는 Feature를 수행하게 된다. 하나의 Scenario 가 존재하고 제일 먼저 Given I have a sample bundle uploaded to s3 가 수행된다. 해당 내용은 common_steps.rb 에 정의되어 있다.

@bucket_creation_count = 0;
Given(/^I have a sample bundle uploaded to s3$/) do
=begin
This fails if the s3 upload is attempted after assume_role is called in the first integration test. 
This is because once you call assume role the next time it instantiates a client it is using different permissions. In my opinion thats a bug because it doesn't match the documentation for the AWS SDK.
https://docs.aws.amazon.com/sdkforruby/api/index.html

Their documentation says an assumed role is the LAST permission it will try to rely on but it looks like its always the first. But the s3 upload is the only place that this mattered so I simply forced this code so it doesn't do it again since the bundle is identical for both tests.
=end
  if @bucket_creation_count == 0
    s3 = Aws::S3::Client.new

    begin
      s3.create_bucket({
        bucket: StepConstants::APP_BUNDLE_BUCKET, # required
        create_bucket_configuration: {
          location_constraint: Aws.config[:region],
        }
      })
    rescue Aws::S3::Errors::BucketAlreadyOwnedByYou
      #Already created the bucket
    end

    Dir.mktmpdir do |temp_directory_to_create_zip_file|
      File.open(zip_app_bundle(temp_directory_to_create_zip_file), 'rb') do |file|
        s3.put_object(bucket: StepConstants::APP_BUNDLE_BUCKET, key: StepConstants::APP_BUNDLE_KEY, body: file)
      end
    end

    @bucket_creation_count += 1
  end

  @bundle_type = 'zip'
  @bundle_location = "s3://#{StepConstants::APP_BUNDLE_BUCKET}/#{StepConstants::APP_BUNDLE_KEY}"
end

이 코드대로라면 S3에 버킷이 생성되어야 하는데 아무 변화가 없다. 디버깅을 해보니 @bucket_creation_count 가 0이 아니라 nil 이라 생성이 되지 않고 있다. @bucket_creation_count = 0; 이라는 코드가 있는데 제대로 동작되지 않는다(뒤에 세미콜론은 왜 붙어있는지 모르겠음)

자세히 코드를 읽다보니(번역기를 돌려보니) 뭔가 문제가 있다는 것 같다. 그냥 이해하기를 포기하고 버그가 있다고만 이해했다.

if 문 위에 다음과 같은 코드를 넣어서 문제를 통과시켰다

@bucket_creation_count = 0 if @bucket_creation_count.nil?

S3 에 codedeploy-agent-integ-test-bucket-771649417534-linux 라는 이름의 버킷이 생성되었다. 버킷안에는 app_bundle.zip 파일이 있었다. 왜인지 모르게 버킷의 리전이 미국 서부(오레곤) 으로 되어 있었다.

wonderer80 commented 5 years ago

다음으로 수행되는 건 I have a CodeDeploy application 인데 agent_steps.rb 에 정의되어 있다.

Given(/^I have a CodeDeploy application$/) do
  @application_name = "codedeploy-integ-testapp-#{SecureRandom.hex(10)}"
  @codedeploy_client.create_application(:application_name => @application_name)
end

codedeploy 에 application 을 생성하는 코드인데 테스트 수행 후에 확인하려고 하면 확인하기가 어렵다 그 이유는 다음과 같이 테스트가 끝난 이후에 다시 삭제해주는 코드가 있기 때문이다.

After("@codedeploy-agent") do
  @thread.kill unless @thread.nil?
  @codedeploy_client.delete_application({:application_name => @application_name}) unless @application_name.nil?
  @codedeploy_client.deregister_on_premises_instance({:instance_name => instance_name})
  FileUtils.rm_rf(@working_directory) unless @working_directory.nil?
end

인위적으로 코드 중간에 sleep 등을 넣어서 테스트가 끝나지 않은 상태로 해두고 확인하면 확인해볼 수 있다. 이 때도 리전이 미국 서부(오레곤) 로 되어 있어서 의문이었는데 step_constants.rb 안에 다음과 같은 코드 때문으로 추정된다.

Aws.config[:region] = 'us-west-2'

저 리전을 다른 리전으로 바꾸면 S3 버킷 생성하다 에러가 난다. 이유는 대충 짐작은 가는데 자세한 설명은 생략한다-0- 어쨋든 현재 코드는 해당 리전에 종속적으로 되어 있다고 생각해야 할 것 같다

wonderer80 commented 5 years ago

참고로 https://docs.aws.amazon.com/sdkforruby/api/Aws/CodeDeploy/Client.html 의 Credentials 에 있는 내용처럼 ~/.aws/credentials 에 정보들을 다 설정해두었는데 Aws.config 를 실행하면 아무것도 들어있지 않았다.

내 환경 상의 문제인지 다른 문제가 있는 것인지 잘 모르겠다

spilist commented 5 years ago

Rakefile에서

  Cucumber::Rake::Task.new('test-integration-aws-codedeploy-agent', desc) do |t|
    t.cucumber_opts = "features -t ~@Ignore"
->
    t.cucumber_opts = "features -t not @Ignore"  
  end

로 하면 deprecation warning은 안나네요.