|
1 | | -RSpec.shared_examples 'binding last operation response handling' do |type| |
2 | | - context 'valid http codes' do |
| 1 | +RSpec.shared_examples 'binding last operation response handling' do |operation_type| |
| 2 | + context 'failure http codes' do |
3 | 3 | valid_responses = [ |
4 | | - { code: 400, body: { description: 'helpful message' }, expected_description: 'helpful message' }, |
5 | | - { code: 200, body: { state: 'failed', description: 'something went wrong' }, expected_description: 'something went wrong' } |
| 4 | + { code: 400, body: { description: 'helpful message' } }, |
| 5 | + { code: 200, body: { state: 'failed', description: 'something went wrong' } } |
6 | 6 | ] |
7 | 7 |
|
8 | 8 | valid_responses.each do |response| |
|
11 | 11 | let(:last_operation_status_code) { response[:code] } |
12 | 12 | let(:last_operation_body) { response[:body] } |
13 | 13 |
|
14 | | - it 'updates the binding and job' do |
15 | | - execute_all_jobs(expected_successes: 1, expected_failures: 0) |
| 14 | + it 'updates the binding and job to failed' do |
| 15 | + execute_all_jobs(expected_successes: 0, expected_failures: 1) |
16 | 16 |
|
17 | 17 | binding.reload |
18 | | - expect(binding.last_operation.type).to eq(type) |
| 18 | + expect(binding.last_operation.type).to eq(operation_type) |
19 | 19 | expect(binding.last_operation.state).to eq('failed') |
20 | 20 | expect(binding.last_operation.description).to eq(response[:body][:description]) |
21 | 21 |
|
22 | | - expect(job.state).to eq(VCAP::CloudController::PollableJobModel::COMPLETE_STATE) |
| 22 | + expect(job.state).to eq(VCAP::CloudController::PollableJobModel::FAILED_STATE) |
23 | 23 | end |
24 | 24 | end |
25 | 25 | end |
26 | 26 | end |
27 | 27 |
|
28 | | - context 'invalid http codes' do |
| 28 | + context 'keep polling http codes' do |
29 | 29 | [404, 500].each do |code| |
30 | 30 | context "last operation response is #{code}" do |
31 | 31 | let(:last_operation_status_code) { code } |
|
35 | 35 | execute_all_jobs(expected_successes: 1, expected_failures: 0) |
36 | 36 |
|
37 | 37 | binding.reload |
38 | | - expect(binding.last_operation.type).to eq(type) |
| 38 | + expect(binding.last_operation.type).to eq(operation_type) |
39 | 39 | expect(binding.last_operation.state).to eq('in progress') |
40 | 40 | expect(binding.last_operation.description).to include("Status Code: #{code}") |
41 | 41 |
|
|
57 | 57 | execute_all_jobs(expected_successes: 1, expected_failures: 0) |
58 | 58 |
|
59 | 59 | binding.reload |
60 | | - expect(binding.last_operation.type).to eq(type) |
| 60 | + expect(binding.last_operation.type).to eq(operation_type) |
61 | 61 | expect(binding.last_operation.state).to eq('in progress') |
62 | 62 |
|
63 | 63 | expect(job.state).to eq(VCAP::CloudController::PollableJobModel::POLLING_STATE) |
|
77 | 77 | execute_all_jobs(expected_successes: 1, expected_failures: 0) |
78 | 78 |
|
79 | 79 | binding.reload |
80 | | - expect(binding.last_operation.type).to eq(type) |
| 80 | + expect(binding.last_operation.type).to eq(operation_type) |
| 81 | + expect(binding.last_operation.state).to eq('in progress') |
| 82 | + |
| 83 | + expect(job.state).to eq(VCAP::CloudController::PollableJobModel::POLLING_STATE) |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | +end |
| 88 | + |
| 89 | +# TODO: Merge these two shared examples into one in https://www.pivotaltracker.com/story/show/174748133 |
| 90 | +RSpec.shared_examples 'unbinding last operation response handling' do |operation_type| |
| 91 | + context 'failure http codes' do |
| 92 | + valid_responses = [ |
| 93 | + { code: 400, body: { description: 'helpful message' } }, |
| 94 | + { code: 200, body: { state: 'failed', description: 'something went wrong' } } |
| 95 | + ] |
| 96 | + |
| 97 | + valid_responses.each do |response| |
| 98 | + context "last operation response is #{response[:code]}" do |
| 99 | + let(:state) { response[:state] } |
| 100 | + let(:last_operation_status_code) { response[:code] } |
| 101 | + let(:last_operation_body) { response[:body] } |
| 102 | + |
| 103 | + it 'updates the binding and job to failed' do |
| 104 | + execute_all_jobs(expected_successes: 1, expected_failures: 0) |
| 105 | + |
| 106 | + binding.reload |
| 107 | + expect(binding.last_operation.type).to eq(operation_type) |
| 108 | + expect(binding.last_operation.state).to eq('failed') |
| 109 | + expect(binding.last_operation.description).to eq(response[:body][:description]) |
| 110 | + |
| 111 | + expect(job.state).to eq(VCAP::CloudController::PollableJobModel::COMPLETE_STATE) |
| 112 | + end |
| 113 | + end |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + context 'keep polling http codes' do |
| 118 | + [404, 500].each do |code| |
| 119 | + context "last operation response is #{code}" do |
| 120 | + let(:last_operation_status_code) { code } |
| 121 | + let(:last_operation_body) { 'something awful' } |
| 122 | + |
| 123 | + it 'continues polling' do |
| 124 | + execute_all_jobs(expected_successes: 1, expected_failures: 0) |
| 125 | + |
| 126 | + binding.reload |
| 127 | + expect(binding.last_operation.type).to eq(operation_type) |
| 128 | + expect(binding.last_operation.state).to eq('in progress') |
| 129 | + expect(binding.last_operation.description).to include("Status Code: #{code}") |
| 130 | + |
| 131 | + expect(job.state).to eq(VCAP::CloudController::PollableJobModel::POLLING_STATE) |
| 132 | + end |
| 133 | + end |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + context 'timeout' do |
| 138 | + before do |
| 139 | + stub_request(:get, broker_binding_last_operation_url). |
| 140 | + with(query: hash_including({ |
| 141 | + operation: operation |
| 142 | + })).to_timeout |
| 143 | + end |
| 144 | + |
| 145 | + it 'continues polling' do |
| 146 | + execute_all_jobs(expected_successes: 1, expected_failures: 0) |
| 147 | + |
| 148 | + binding.reload |
| 149 | + expect(binding.last_operation.type).to eq(operation_type) |
| 150 | + expect(binding.last_operation.state).to eq('in progress') |
| 151 | + |
| 152 | + expect(job.state).to eq(VCAP::CloudController::PollableJobModel::POLLING_STATE) |
| 153 | + end |
| 154 | + end |
| 155 | + |
| 156 | + context 'connection errors' do |
| 157 | + [SocketError, Errno::ECONNREFUSED, RuntimeError].each do |error| |
| 158 | + before do |
| 159 | + stub_request(:get, broker_binding_last_operation_url). |
| 160 | + with(query: hash_including({ |
| 161 | + operation: operation |
| 162 | + })).to_raise(error) |
| 163 | + end |
| 164 | + |
| 165 | + it 'continues polling' do |
| 166 | + execute_all_jobs(expected_successes: 1, expected_failures: 0) |
| 167 | + |
| 168 | + binding.reload |
| 169 | + expect(binding.last_operation.type).to eq(operation_type) |
81 | 170 | expect(binding.last_operation.state).to eq('in progress') |
82 | 171 |
|
83 | 172 | expect(job.state).to eq(VCAP::CloudController::PollableJobModel::POLLING_STATE) |
|
0 commit comments