|
6 | 6 | require 'aws-sdk-ec2' |
7 | 7 | require 'aws-sdk-ssm' |
8 | 8 | require 'aws-sdk-cloudformation' |
| 9 | +require 'aws-sdk-iam' |
9 | 10 |
|
10 | 11 | describe EcsDeployCli::Runner do |
11 | 12 | context 'defines task data' do |
12 | 13 | let(:parser) { EcsDeployCli::DSL::Parser.load('spec/support/ECSFile') } |
13 | 14 | subject { described_class.new(parser) } |
| 15 | + let(:mock_iam_client) { Aws::IAM::Client.new(stub_responses: true) } |
14 | 16 | let(:mock_cf_client) { Aws::CloudFormation::Client.new(stub_responses: true) } |
15 | 17 | let(:mock_ssm_client) { Aws::SSM::Client.new(stub_responses: true) } |
16 | 18 | let(:mock_ecs_client) { Aws::ECS::Client.new(stub_responses: true) } |
|
90 | 92 | ENV['AWS_REGION'] = nil |
91 | 93 | end |
92 | 94 |
|
93 | | - it '#setup!' do |
94 | | - mock_ssm_client.stub_responses(:get_parameter, { |
95 | | - parameter: { |
96 | | - name: '/aws/service/ecs/optimized-ami/amazon-linux-2/recommended', |
97 | | - type: 'String', |
98 | | - value: '{"schema_version":1,"image_name":"amzn2-ami-ecs-hvm-2.0.20210331-x86_64-ebs","image_id":"ami-03bbf53329af34379","os":"Amazon Linux 2","ecs_runtime_version":"Docker version 19.03.13-ce","ecs_agent_version":"1.51.0"}' |
99 | | - } |
100 | | - }) |
| 95 | + context '#setup!' do |
| 96 | + it 'setups the cluster correctly' do |
| 97 | + mock_ssm_client.stub_responses(:get_parameter, { |
| 98 | + parameter: { |
| 99 | + name: '/aws/service/ecs/optimized-ami/amazon-linux-2/recommended', |
| 100 | + type: 'String', |
| 101 | + value: '{"schema_version":1,"image_name":"amzn2-ami-ecs-hvm-2.0.20210331-x86_64-ebs","image_id":"ami-03bbf53329af34379","os":"Amazon Linux 2","ecs_runtime_version":"Docker version 19.03.13-ce","ecs_agent_version":"1.51.0"}' |
| 102 | + } |
| 103 | + }) |
| 104 | + |
| 105 | + expect(mock_iam_client).to receive(:get_role).with({ role_name: 'ecsInstanceRole' }).and_return({ role: { arn: 'some' } }) |
| 106 | + expect(mock_cf_client).to receive(:wait_until) |
| 107 | + expect(mock_ecs_client).to receive(:create_service) |
| 108 | + |
| 109 | + expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:iam_client).at_least(:once).and_return(mock_iam_client) |
| 110 | + expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:cwl_client).at_least(:once).and_return(mock_cwl_client) |
| 111 | + expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:ecs_client).at_least(:once).and_return(mock_ecs_client) |
| 112 | + expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:ssm_client).at_least(:once).and_return(mock_ssm_client) |
| 113 | + expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:cf_client).at_least(:once).and_return(mock_cf_client) |
101 | 114 |
|
102 | | - expect(mock_cf_client).to receive(:wait_until) |
103 | | - expect(mock_ecs_client).to receive(:create_service) |
| 115 | + subject.setup! |
| 116 | + end |
104 | 117 |
|
105 | | - expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:cwl_client).at_least(:once).and_return(mock_cwl_client) |
106 | | - expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:ecs_client).at_least(:once).and_return(mock_ecs_client) |
107 | | - expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:ssm_client).at_least(:once).and_return(mock_ssm_client) |
108 | | - expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:cf_client).at_least(:once).and_return(mock_cf_client) |
| 118 | + it 'fails if the IAM role is not setup' do |
| 119 | + expect(EcsDeployCli.logger).to receive(:info).at_least(:once) do |message| |
| 120 | + puts message |
| 121 | + end |
| 122 | + |
| 123 | + expect(mock_iam_client).to receive(:get_role).with({ role_name: 'ecsInstanceRole' }) do |
| 124 | + raise Aws::IAM::Errors::NoSuchEntity.new(nil, 'some') |
| 125 | + end |
| 126 | + |
| 127 | + |
| 128 | + expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:iam_client).at_least(:once).and_return(mock_iam_client) |
| 129 | + expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:ecs_client).at_least(:once).and_return(mock_ecs_client) |
| 130 | + |
| 131 | + expect { subject.setup! }.to output(/IAM Role ecsInstanceRole does not exist./).to_stdout |
| 132 | + end |
109 | 133 |
|
110 | | - subject.setup! |
| 134 | + it 'fails if the cluster is already there' do |
| 135 | + expect(mock_ecs_client).to receive(:describe_clusters).and_return(clusters: [{ }]) |
| 136 | + |
| 137 | + expect(EcsDeployCli.logger).to receive(:info).at_least(:once) do |message| |
| 138 | + puts message |
| 139 | + end |
| 140 | + |
| 141 | + expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:ecs_client).at_least(:once).and_return(mock_ecs_client) |
| 142 | + |
| 143 | + expect { subject.setup! }.to output(/Cluster already created, skipping./).to_stdout |
| 144 | + end |
111 | 145 | end |
112 | 146 |
|
113 | 147 | context '#ssh' do |
|
0 commit comments