|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +module VCAP::CloudController |
| 4 | + module Jobs::Runtime |
| 5 | + RSpec.describe ServiceOperationsInitialCleanup, job_context: :worker do |
| 6 | + subject(:job) { ServiceOperationsInitialCleanup.new } |
| 7 | + |
| 8 | + let(:fake_orphan_mitigator) { double(VCAP::Services::ServiceBrokers::V2::OrphanMitigator) } |
| 9 | + let(:fake_logger) { instance_double(Steno::Logger, info: nil) } |
| 10 | + |
| 11 | + it { is_expected.to be_a_valid_job } |
| 12 | + |
| 13 | + it 'knows its job name' do |
| 14 | + expect(job.job_name_in_configuration).to equal(:service_operations_initial_cleanup) |
| 15 | + end |
| 16 | + |
| 17 | + it 'adds 10% to the broker_client_timeout' do |
| 18 | + expect(job.broker_client_timeout_plus_margin).to equal(66) |
| 19 | + end |
| 20 | + |
| 21 | + def expect_no_orphan_mitigator_calls(orphan_mitigator) |
| 22 | + %i[cleanup_failed_provision cleanup_failed_bind cleanup_failed_key].each do |call| |
| 23 | + expect(orphan_mitigator).not_to have_received(call) |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + describe 'perform' do |
| 28 | + let!(:service_instance) { ManagedServiceInstance.make } |
| 29 | + let!(:service_binding) { ServiceBinding.make } |
| 30 | + let!(:service_key) { ServiceKey.make } |
| 31 | + let!(:route_binding) { RouteBinding.make } |
| 32 | + |
| 33 | + before do |
| 34 | + allow(Steno).to receive(:logger).and_return(fake_logger) |
| 35 | + |
| 36 | + allow(VCAP::Services::ServiceBrokers::V2::OrphanMitigator).to receive(:new).and_return(fake_orphan_mitigator) |
| 37 | + allow(fake_orphan_mitigator).to receive(:cleanup_failed_provision).with(service_instance) |
| 38 | + allow(fake_orphan_mitigator).to receive(:cleanup_failed_bind).with(service_binding) |
| 39 | + allow(fake_orphan_mitigator).to receive(:cleanup_failed_key).with(service_key) |
| 40 | + end |
| 41 | + |
| 42 | + context 'when there are no service instance operations in state create/initial' do |
| 43 | + let!(:service_instance_operation) { ServiceInstanceOperation.make(service_instance_id: service_instance.id, type: 'create', state: 'succeeded') } |
| 44 | + |
| 45 | + it 'does nothing' do |
| 46 | + job.perform |
| 47 | + |
| 48 | + expect(service_instance_operation.reload.type).to eq('create') |
| 49 | + expect(service_instance_operation.reload.state).to eq('succeeded') |
| 50 | + expect_no_orphan_mitigator_calls(fake_orphan_mitigator) |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + context 'when there is one service instance operation in state create/initial' do |
| 55 | + let!(:service_instance_operation) { ServiceInstanceOperation.make(service_instance_id: service_instance.id, type: 'create', state: 'initial') } |
| 56 | + |
| 57 | + context 'and the service broker connection timeout has not yet passed' do |
| 58 | + it 'does nothing' do |
| 59 | + job.perform |
| 60 | + |
| 61 | + expect(service_instance_operation.reload.type).to eq('create') |
| 62 | + expect(service_instance_operation.reload.state).to eq('initial') |
| 63 | + expect_no_orphan_mitigator_calls(fake_orphan_mitigator) |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + context 'and the service broker connection timeout has passed' do |
| 68 | + let(:expired_time) { Time.now.utc - 2.seconds } |
| 69 | + |
| 70 | + before do |
| 71 | + TestConfig.override(broker_client_timeout_seconds: 1) |
| 72 | + service_instance_operation.this.update(updated_at: expired_time) |
| 73 | + end |
| 74 | + |
| 75 | + it 'sets the operation state to create/failed and triggers the orphan mitigation' do |
| 76 | + job.perform |
| 77 | + |
| 78 | + expect(service_instance_operation.reload.type).to eq('create') |
| 79 | + expect(service_instance_operation.reload.state).to eq('failed') |
| 80 | + expect(service_instance_operation.reload.description).to eq('Operation was stuck in "initial" state. Set to "failed" by cleanup job.') |
| 81 | + expect(fake_logger).to have_received(:info).with("ServiceInstance #{service_instance[:guid]} is stuck in state 'create'/'initial'. " \ |
| 82 | + "Setting state to 'failed' and triggering orphan mitigation.") |
| 83 | + expect(fake_orphan_mitigator).to have_received(:cleanup_failed_provision).with(service_instance) |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + context 'when there are no service instance binding operations in state create/initial' do |
| 89 | + let!(:service_binding_operation) { ServiceBindingOperation.make(service_binding_id: service_binding.id, type: 'create', state: 'succeeded') } |
| 90 | + |
| 91 | + it 'does nothing' do |
| 92 | + job.perform |
| 93 | + |
| 94 | + expect(service_binding_operation.reload.type).to eq('create') |
| 95 | + expect(service_binding_operation.reload.state).to eq('succeeded') |
| 96 | + expect_no_orphan_mitigator_calls(fake_orphan_mitigator) |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + context 'when there is one service instance binding operation in state create/initial' do |
| 101 | + let!(:service_binding_operation) { ServiceBindingOperation.make(service_binding_id: service_binding.id, type: 'create', state: 'initial') } |
| 102 | + |
| 103 | + context 'and the service broker connection timeout has not yet passed' do |
| 104 | + it 'does nothing' do |
| 105 | + job.perform |
| 106 | + |
| 107 | + expect(service_binding_operation.reload.type).to eq('create') |
| 108 | + expect(service_binding_operation.reload.state).to eq('initial') |
| 109 | + expect_no_orphan_mitigator_calls(fake_orphan_mitigator) |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + context 'and the service broker connection timeout has passed' do |
| 114 | + let(:expired_time) { Time.now.utc - 2.seconds } |
| 115 | + |
| 116 | + before do |
| 117 | + TestConfig.override(broker_client_timeout_seconds: 1) |
| 118 | + service_binding_operation.this.update(updated_at: expired_time) |
| 119 | + end |
| 120 | + |
| 121 | + it 'sets the operation state to create/failed and triggers the orphan mitigation' do |
| 122 | + job.perform |
| 123 | + |
| 124 | + expect(service_binding_operation.reload.type).to eq('create') |
| 125 | + expect(service_binding_operation.reload.state).to eq('failed') |
| 126 | + expect(service_binding_operation.reload.description).to eq('Operation was stuck in "initial" state. Set to "failed" by cleanup job.') |
| 127 | + expect(fake_logger).to have_received(:info).with("ServiceBinding #{service_binding[:guid]} is stuck in state 'create'/'initial'. " \ |
| 128 | + "Setting state to 'failed' and triggering orphan mitigation.") |
| 129 | + expect(fake_orphan_mitigator).to have_received(:cleanup_failed_bind).with(service_binding) |
| 130 | + end |
| 131 | + end |
| 132 | + end |
| 133 | + |
| 134 | + context 'when there are no service key operations in state create/initial' do |
| 135 | + let!(:service_key_operation) { ServiceKeyOperation.make(service_key_id: service_key.id, type: 'create', state: 'succeeded') } |
| 136 | + |
| 137 | + it 'does nothing' do |
| 138 | + job.perform |
| 139 | + |
| 140 | + expect(service_key_operation.reload.type).to eq('create') |
| 141 | + expect(service_key_operation.reload.state).to eq('succeeded') |
| 142 | + expect_no_orphan_mitigator_calls(fake_orphan_mitigator) |
| 143 | + end |
| 144 | + end |
| 145 | + |
| 146 | + context 'when there is one service key operation in state create/initial' do |
| 147 | + let!(:service_key_operation) { ServiceKeyOperation.make(service_key_id: service_key.id, type: 'create', state: 'initial') } |
| 148 | + |
| 149 | + context 'and the service broker connection timeout has not yet passed' do |
| 150 | + it 'does nothing' do |
| 151 | + job.perform |
| 152 | + |
| 153 | + expect(service_key_operation.reload.type).to eq('create') |
| 154 | + expect(service_key_operation.reload.state).to eq('initial') |
| 155 | + expect_no_orphan_mitigator_calls(fake_orphan_mitigator) |
| 156 | + end |
| 157 | + end |
| 158 | + |
| 159 | + context 'and the service broker connection timeout has passed' do |
| 160 | + let(:expired_time) { Time.now.utc - 2.seconds } |
| 161 | + |
| 162 | + before do |
| 163 | + TestConfig.override(broker_client_timeout_seconds: 1) |
| 164 | + service_key_operation.this.update(updated_at: expired_time) |
| 165 | + end |
| 166 | + |
| 167 | + it 'sets the operation state to create/failed and triggers the orphan mitigation' do |
| 168 | + job.perform |
| 169 | + |
| 170 | + expect(service_key_operation.reload.type).to eq('create') |
| 171 | + expect(service_key_operation.reload.state).to eq('failed') |
| 172 | + expect(service_key_operation.reload.description).to eq('Operation was stuck in "initial" state. Set to "failed" by cleanup job.') |
| 173 | + expect(fake_logger).to have_received(:info).with("ServiceKey #{service_key[:guid]} is stuck in state 'create'/'initial'. " \ |
| 174 | + "Setting state to 'failed' and triggering orphan mitigation.") |
| 175 | + expect(fake_orphan_mitigator).to have_received(:cleanup_failed_key).with(service_key) |
| 176 | + end |
| 177 | + end |
| 178 | + end |
| 179 | + |
| 180 | + context 'when there are no route binding operations in state create/initial' do |
| 181 | + let!(:route_binding_operation) { RouteBindingOperation.make(route_binding_id: route_binding.id, type: 'create', state: 'succeeded') } |
| 182 | + |
| 183 | + it 'does nothing' do |
| 184 | + job.perform |
| 185 | + |
| 186 | + expect(route_binding_operation.reload.type).to eq('create') |
| 187 | + expect(route_binding_operation.reload.state).to eq('succeeded') |
| 188 | + expect_no_orphan_mitigator_calls(fake_orphan_mitigator) |
| 189 | + end |
| 190 | + end |
| 191 | + |
| 192 | + context 'when there is one route binding operation in state create/initial' do |
| 193 | + let!(:route_binding_operation) { RouteBindingOperation.make(route_binding_id: route_binding.id, type: 'create', state: 'initial') } |
| 194 | + |
| 195 | + context 'and the service broker connection timeout has not yet passed' do |
| 196 | + it 'does nothing' do |
| 197 | + job.perform |
| 198 | + |
| 199 | + expect(route_binding_operation.reload.type).to eq('create') |
| 200 | + expect(route_binding_operation.reload.state).to eq('initial') |
| 201 | + expect_no_orphan_mitigator_calls(fake_orphan_mitigator) |
| 202 | + end |
| 203 | + end |
| 204 | + |
| 205 | + context 'and the service broker connection timeout has passed' do |
| 206 | + let(:expired_time) { Time.now.utc - 2.seconds } |
| 207 | + |
| 208 | + before do |
| 209 | + TestConfig.override(broker_client_timeout_seconds: 1) |
| 210 | + route_binding_operation.this.update(updated_at: expired_time) |
| 211 | + end |
| 212 | + |
| 213 | + it 'sets the operation state to create/failed and triggers the orphan mitigation' do |
| 214 | + job.perform |
| 215 | + |
| 216 | + expect(route_binding_operation.reload.type).to eq('create') |
| 217 | + expect(route_binding_operation.reload.state).to eq('failed') |
| 218 | + expect(route_binding_operation.reload.description).to eq('Operation was stuck in "initial" state. Set to "failed" by cleanup job.') |
| 219 | + expect(fake_logger).to have_received(:info).with("RouteBinding #{route_binding[:guid]} is stuck in state 'create'/'initial'. " \ |
| 220 | + "Setting state to 'failed' and triggering orphan mitigation.") |
| 221 | + expect_no_orphan_mitigator_calls(fake_orphan_mitigator) |
| 222 | + end |
| 223 | + end |
| 224 | + end |
| 225 | + end |
| 226 | + end |
| 227 | + end |
| 228 | +end |
0 commit comments