2016-01-31 17:52:11 +03:00
|
|
|
require 'workers/stack_bootstrap/stack_synchronizer'
|
2016-03-28 00:16:11 +03:00
|
|
|
RSpec.describe StackSynchronizer, stubbed_connector: true, init_messages: true do
|
2016-01-31 17:52:11 +03:00
|
|
|
let(:out) { double(:out, puts: nil, flush: nil) }
|
|
|
|
|
let(:stack) { build(:stack) }
|
|
|
|
|
let(:syncer) { described_class.new(stack, out) }
|
|
|
|
|
|
|
|
|
|
before do
|
2016-02-24 21:26:44 +03:00
|
|
|
allow(stack).to receive(:sync!)
|
2016-01-31 17:52:11 +03:00
|
|
|
allow(stack).to receive(:events).and_return( [{'event_id' => 1}] )
|
|
|
|
|
allow(syncer).to receive(:sleep)
|
|
|
|
|
allow(stubbed_connector).to receive(:stack_update)
|
|
|
|
|
end
|
|
|
|
|
|
2016-03-23 18:35:05 +03:00
|
|
|
def setup_statuses(statuses_array)
|
|
|
|
|
statuses = statuses_array.to_enum
|
|
|
|
|
allow(stack).to receive(:sync!) {
|
|
|
|
|
stack.stack_status = statuses.next
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe '#sync', stubbed_logger: true do
|
2016-01-31 17:52:11 +03:00
|
|
|
it 'waits for stack creating to be finished' do
|
2016-03-23 18:35:05 +03:00
|
|
|
setup_statuses(['CREATE_IN_PROGRESS'] * 10 + ['CREATE_COMPLETE'])
|
2016-01-31 17:52:11 +03:00
|
|
|
expect(syncer).to receive(:sleep).at_least(10).times
|
2016-02-24 21:26:44 +03:00
|
|
|
expect(stack).to receive(:sync!).at_least(10).times
|
2016-01-31 17:52:11 +03:00
|
|
|
syncer.sync
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'prints each message only once' do
|
|
|
|
|
event1 = {'event_id' => 1, 'timestamp' => 't1'}
|
|
|
|
|
event2 = {'event_id' => 2, 'timestamp' => 't2'}
|
|
|
|
|
event3 = {'event_id' => 3, 'timestamp' => 't3'}
|
|
|
|
|
|
|
|
|
|
allow(stack).to receive(:events).and_return([event1], [event1, event2], [event1, event2, event3])
|
2016-03-23 18:35:05 +03:00
|
|
|
setup_statuses(['CREATE_IN_PROGRESS', 'CREATE_IN_PROGRESS', 'CREATE_IN_PROGRESS', 'CREATE_COMPLETE'])
|
2016-03-28 00:16:11 +03:00
|
|
|
expect(syncer).to receive(:sleep).exactly(4).times
|
|
|
|
|
expect(out).to receive(:puts).with(/t1/).once.ordered
|
|
|
|
|
expect(out).to receive(:puts).with(/t2/).once.ordered
|
|
|
|
|
expect(out).to receive(:puts).with(/t3/).once.ordered
|
2016-01-31 17:52:11 +03:00
|
|
|
syncer.sync
|
|
|
|
|
end
|
|
|
|
|
|
2016-03-23 18:35:05 +03:00
|
|
|
it 'updates stack when status is changed' do
|
|
|
|
|
setup_statuses(['CREATE_IN_PROGRESS', 'CREATE_IN_PROGRESS', 'ROLLBACK_IN_PROGRESS', 'ROLLBACK_COMPLETE'])
|
|
|
|
|
expect(stubbed_connector).to receive(:stack_update).exactly(3).times
|
|
|
|
|
syncer.sync
|
|
|
|
|
end
|
|
|
|
|
|
2016-01-31 17:52:11 +03:00
|
|
|
context 'when stack creating was successful' do
|
|
|
|
|
it 'updates stack in DB when stack creating is finished and returns 0' do
|
2016-03-23 18:35:05 +03:00
|
|
|
setup_statuses(['CREATE_COMPLETE'])
|
2016-01-31 17:52:11 +03:00
|
|
|
expect(stubbed_connector).to receive(:stack_update).with(stack)
|
2016-03-28 00:16:11 +03:00
|
|
|
expect(out).to receive(:puts).with(/CREATE_COMPLETE/)
|
2016-03-27 23:37:57 +03:00
|
|
|
expect(syncer.sync).to be_ok
|
2016-01-31 17:52:11 +03:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when stack was rollbacked' do
|
|
|
|
|
it 'returns 1 (:stack_rolled_back)' do
|
2016-03-23 18:35:05 +03:00
|
|
|
setup_statuses(['CREATE_IN_PROGRESS', 'ROLLBACK_IN_PROGRESS', 'ROLLBACK_COMPLETE'])
|
2016-03-28 00:16:11 +03:00
|
|
|
expect(out).to receive(:puts).with(/ROLLBACK_COMPLETE/)
|
2016-03-27 23:37:57 +03:00
|
|
|
expect(syncer.sync).to be_stack_rolled_back
|
2016-01-31 17:52:11 +03:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when unkown stack status was found' do
|
|
|
|
|
it 'returns 2 (:unkown_status)' do
|
2016-03-23 18:35:05 +03:00
|
|
|
setup_statuses(['CREATE_IN_PROGRESS', 'unknown'])
|
2016-03-28 00:16:11 +03:00
|
|
|
expect(out).to receive(:puts).with(/unknown/)
|
2016-03-27 23:37:57 +03:00
|
|
|
expect(syncer.sync).to be_unkown_status
|
2016-01-31 17:52:11 +03:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "when stack hasn't been synced in an hour" do
|
|
|
|
|
it 'returns 3 (:timeout)' do
|
|
|
|
|
allow(stack).to receive(:stack_status) {'CREATE_IN_PROGRESS'}
|
2016-03-28 00:16:11 +03:00
|
|
|
expect(out).to receive(:puts).with(/hasn't been synced/)
|
2016-03-27 23:37:57 +03:00
|
|
|
expect(syncer.sync).to be_timeout
|
2016-01-31 17:52:11 +03:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when an error occured during syncing', stubbed_logger: true do
|
|
|
|
|
it 'returns 5 (:error)' do
|
2016-03-23 18:35:05 +03:00
|
|
|
setup_statuses(['CREATE_IN_PROGRESS', 'CREATE_COMPLETE'])
|
2016-01-31 17:52:11 +03:00
|
|
|
allow(stubbed_connector).to receive(:stack_update) { raise }
|
2016-03-25 14:50:22 +03:00
|
|
|
expect(syncer.sync.code).to eq 5
|
2016-01-31 17:52:11 +03:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|