|
| 1 | +RSpec.shared_examples 'create binding orphan mitigation' do |
| 2 | + let(:job) { VCAP::CloudController::PollableJobModel.last } |
| 3 | + |
| 4 | + before do |
| 5 | + stub_request(:delete, bind_url). |
| 6 | + with(query: { |
| 7 | + accepts_incomplete: true, |
| 8 | + plan_id: plan_id, |
| 9 | + service_id: offering_id, |
| 10 | + }).to_return(status: 200, body: {}.to_json) |
| 11 | + end |
| 12 | + |
| 13 | + context 'broker returns a success code' do |
| 14 | + codes = [200, 201] |
| 15 | + codes.each do |code| |
| 16 | + context "response is #{code}" do |
| 17 | + before do |
| 18 | + stub_request(:put, bind_url). |
| 19 | + with(query: { |
| 20 | + accepts_incomplete: true, |
| 21 | + }, |
| 22 | + body: client_body).to_return(status: code, body: {}.to_json) |
| 23 | + end |
| 24 | + |
| 25 | + it 'updates the binding and job' do |
| 26 | + execute_all_jobs(expected_successes: 1, expected_failures: 0) |
| 27 | + |
| 28 | + expect(binding.last_operation.type).to eq('create') |
| 29 | + expect(binding.last_operation.state).to eq('succeeded') |
| 30 | + |
| 31 | + expect(job.state).to eq(VCAP::CloudController::PollableJobModel::COMPLETE_STATE) |
| 32 | + |
| 33 | + expect( |
| 34 | + a_request(:delete, bind_url). |
| 35 | + with( |
| 36 | + query: { |
| 37 | + accepts_incomplete: true, |
| 38 | + plan_id: plan_id, |
| 39 | + service_id: offering_id, |
| 40 | + }, |
| 41 | + ) |
| 42 | + ).not_to have_been_made |
| 43 | + end |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + context 'response is 201 with malformed response' do |
| 48 | + before do |
| 49 | + stub_request(:put, bind_url). |
| 50 | + with(query: { |
| 51 | + accepts_incomplete: true, |
| 52 | + }, |
| 53 | + body: client_body).to_return(status: 201, body: nil) |
| 54 | + end |
| 55 | + |
| 56 | + it 'updates the binding and performs orphan mitigation' do |
| 57 | + execute_all_jobs(expected_successes: 1, expected_failures: 1) |
| 58 | + |
| 59 | + assert_failed_job(binding, job) |
| 60 | + assert_orphan_mitigation_performed(plan_id, offering_id) |
| 61 | + end |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + context 'broker does not process the request' do |
| 66 | + [400, 401, 408, 409, *411..431].each do |code| |
| 67 | + context "response is #{code}" do |
| 68 | + before do |
| 69 | + stub_request(:put, bind_url). |
| 70 | + with(query: { |
| 71 | + accepts_incomplete: true, |
| 72 | + }, |
| 73 | + body: client_body).to_return(status: code, body: { error: 'ConcurrencyError', description: 'some description' }.to_json) |
| 74 | + end |
| 75 | + |
| 76 | + it 'updates the binding and job' do |
| 77 | + execute_all_jobs(expected_successes: 0, expected_failures: 1) |
| 78 | + |
| 79 | + assert_failed_job(binding, job) |
| 80 | + |
| 81 | + expect( |
| 82 | + a_request(:delete, bind_url). |
| 83 | + with( |
| 84 | + query: { |
| 85 | + accepts_incomplete: true, |
| 86 | + plan_id: plan_id, |
| 87 | + service_id: offering_id, |
| 88 | + }, |
| 89 | + ) |
| 90 | + ).not_to have_been_made |
| 91 | + end |
| 92 | + end |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + context 'broker fails to bind with an error not specified in osbapi' do |
| 97 | + [*500..511, *203..208].sample(4).each do |code| |
| 98 | + context "response is #{code}" do |
| 99 | + before do |
| 100 | + stub_request(:put, bind_url). |
| 101 | + with(query: { |
| 102 | + accepts_incomplete: true, |
| 103 | + }, |
| 104 | + body: client_body).to_return(status: code, body: {}.to_json) |
| 105 | + end |
| 106 | + |
| 107 | + it 'does orphan mitigation and fails the job' do |
| 108 | + execute_all_jobs(expected_successes: 1, expected_failures: 1) |
| 109 | + |
| 110 | + assert_failed_job(binding, job) |
| 111 | + assert_orphan_mitigation_performed(plan_id, offering_id) |
| 112 | + end |
| 113 | + end |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + context 'timeout' do |
| 118 | + before do |
| 119 | + stub_request(:put, bind_url). |
| 120 | + with(query: { |
| 121 | + accepts_incomplete: true, |
| 122 | + }, |
| 123 | + body: client_body).to_timeout |
| 124 | + |
| 125 | + stub_request(:delete, bind_url). |
| 126 | + with(query: { |
| 127 | + accepts_incomplete: true, |
| 128 | + plan_id: plan_id, |
| 129 | + service_id: offering_id, |
| 130 | + }).to_return(status: 200, body: {}.to_json) |
| 131 | + end |
| 132 | + |
| 133 | + it 'does orphan mitigation and fails the job' do |
| 134 | + execute_all_jobs(expected_successes: 1, expected_failures: 1) |
| 135 | + |
| 136 | + assert_failed_job(binding, job) |
| 137 | + assert_orphan_mitigation_performed(plan_id, offering_id) |
| 138 | + end |
| 139 | + end |
| 140 | +end |
| 141 | + |
| 142 | +def assert_failed_job(binding, job) |
| 143 | + binding.reload |
| 144 | + expect(binding.last_operation.type).to eq('create') |
| 145 | + expect(binding.last_operation.state).to eq('failed') |
| 146 | + expect(job.state).to eq(VCAP::CloudController::PollableJobModel::FAILED_STATE) |
| 147 | +end |
| 148 | + |
| 149 | +def assert_orphan_mitigation_performed(plan_id, offering_id) |
| 150 | + expect( |
| 151 | + a_request(:delete, bind_url). |
| 152 | + with( |
| 153 | + query: { |
| 154 | + accepts_incomplete: true, |
| 155 | + plan_id: plan_id, |
| 156 | + service_id: offering_id, |
| 157 | + }, |
| 158 | + ) |
| 159 | + ).to have_been_made.once |
| 160 | +end |
0 commit comments