|
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 |
|
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( |
|
0 commit comments