|
22 | 22 | ENV.stubs(:[]).returns(nil) |
23 | 23 | end |
24 | 24 |
|
| 25 | + after do |
| 26 | + lock_path = File.expand_path(File.join(__dir__, 'fixtures', 'aws-google.lock')) |
| 27 | + File.delete(lock_path) if File.exist?(lock_path) |
| 28 | + end |
| 29 | + |
25 | 30 | describe 'not configured' do |
26 | 31 | it 'does nothing' do |
27 | 32 | Aws::Google.expects(:new).never |
28 | | - Aws::STS::Client.new |
| 33 | + Aws::STS::Client.new(region: 'us-east-1') |
29 | 34 | end |
30 | 35 | end |
31 | 36 |
|
|
36 | 41 | client_id: 'client_id', |
37 | 42 | client_secret: 'client_secret', |
38 | 43 | profile: 'cdo', |
39 | | - client: Aws::STS::Client.new(stub_responses: true) |
| 44 | + client: Aws::STS::Client.new(region: 'us-east-1', stub_responses: true) |
40 | 45 | } |
41 | 46 | end |
42 | 47 |
|
|
130 | 135 | Aws::Google.stubs(:config).returns(config) |
131 | 136 | @oauth_default.once |
132 | 137 | system.times(5) |
133 | | - c = Aws::STS::Client.new.config.credentials |
| 138 | + c = Aws::STS::Client.new(region: 'us-east-1').config.credentials |
134 | 139 | _(c.credentials.access_key_id).must_equal credentials[:access_key_id] |
135 | 140 | _(c.credentials.secret_access_key).must_equal credentials[:secret_access_key] |
136 | 141 | _(c.credentials.session_token).must_equal credentials[:session_token] |
137 | 142 | end |
138 | 143 |
|
| 144 | + describe 'write lock' do |
| 145 | + let :provider do |
| 146 | + Aws::Google.allocate.tap do |google| |
| 147 | + google.instance_variable_set(:@credentials, Aws::Credentials.new('x', 'y', 'z')) |
| 148 | + google.instance_variable_set(:@expiration, 123) |
| 149 | + google.instance_variable_set(:@session_profile, 'cdo_session') |
| 150 | + end |
| 151 | + end |
| 152 | + |
| 153 | + let(:lock_path) { File.expand_path(File.join(__dir__, 'fixtures', 'aws-google.lock')) } |
| 154 | + let(:lock) { mock } |
| 155 | + |
| 156 | + it 'writes credentials while holding the lock' do |
| 157 | + File.expects(:open).with(lock_path, File::RDWR | File::CREAT).yields(lock) |
| 158 | + lock.expects(:flock).with(File::LOCK_EX | File::LOCK_NB).returns(true) |
| 159 | + system.times(5) |
| 160 | + |
| 161 | + provider.send(:with_write_lock) { provider.send(:write_credentials) } |
| 162 | + end |
| 163 | + |
| 164 | + it 'retries until the lock is available' do |
| 165 | + File.expects(:open).with(lock_path, File::RDWR | File::CREAT).yields(lock) |
| 166 | + lock.expects(:flock).with(File::LOCK_EX | File::LOCK_NB).times(3).returns(false, false, true) |
| 167 | + Process.stubs(:clock_gettime).with(Process::CLOCK_MONOTONIC).returns(0, 10, 20) |
| 168 | + provider.expects(:sleep).with(0.1).twice |
| 169 | + system.times(5) |
| 170 | + |
| 171 | + provider.send(:with_write_lock) { provider.send(:write_credentials) } |
| 172 | + end |
| 173 | + |
| 174 | + it 'raises when the lock times out' do |
| 175 | + File.expects(:open).with(lock_path, File::RDWR | File::CREAT).yields(lock) |
| 176 | + lock.expects(:flock).with(File::LOCK_EX | File::LOCK_NB).returns(false) |
| 177 | + Process.stubs(:clock_gettime).with(Process::CLOCK_MONOTONIC).returns(0, 60) |
| 178 | + provider.expects(:system).never |
| 179 | + |
| 180 | + err = assert_raises(RuntimeError) do |
| 181 | + provider.send(:with_write_lock) { provider.send(:write_credentials) } |
| 182 | + end |
| 183 | + |
| 184 | + _(err.message).must_equal "Timed out after 60s waiting for: #{lock_path}" |
| 185 | + end |
| 186 | + end |
| 187 | + |
139 | 188 | describe 'valid Google auth, no AWS permissions' do |
140 | 189 | before do |
141 | 190 | config[:client].stub_responses( |
|
210 | 259 | before do |
211 | 260 | Aws.shared_config.fresh( |
212 | 261 | config_enabled: true, |
213 | | - credentials_path: nil, |
214 | | - config_path: nil |
| 262 | + credentials_path: File.expand_path(File.join(__dir__, 'fixtures', 'missing_aws_credentials')), |
| 263 | + config_path: File.expand_path(File.join(__dir__, 'fixtures', 'missing_aws_config')) |
215 | 264 | ) |
216 | 265 | end |
217 | 266 |
|
|
0 commit comments