| 
									
										
										
										
											2016-01-31 17:52:11 +03:00
										 |  |  | require 'workers/stack_bootstrap/stack_synchronizer' | 
					
						
							|  |  |  | RSpec.describe StackSynchronizer, stubbed_connector: true do | 
					
						
							|  |  |  |   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) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     lots_of_statuses = ['CREATE_IN_PROGRESS'] * 10 + ['CREATE_COMPLETE'] | 
					
						
							|  |  |  |     allow(stack).to receive(:stack_status).and_return(*lots_of_statuses) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe '#sync' do | 
					
						
							|  |  |  |     it 'waits for stack creating to be finished' do | 
					
						
							|  |  |  |       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]) | 
					
						
							|  |  |  |       syncer.sync | 
					
						
							|  |  |  |       expect(out).to have_received(:puts).with(/t1/).once.ordered | 
					
						
							|  |  |  |       expect(out).to have_received(:puts).with(/t2/).once.ordered | 
					
						
							|  |  |  |       expect(out).to have_received(:puts).with(/t3/).once.ordered | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context 'when stack creating was successful' do | 
					
						
							|  |  |  |       it 'updates stack in DB when stack creating is finished and returns 0' do | 
					
						
							|  |  |  |         expect(stubbed_connector).to receive(:stack_update).with(stack) | 
					
						
							|  |  |  |         expect(syncer.sync).to eq 0
 | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context 'when stack was rollbacked' do | 
					
						
							|  |  |  |       it 'returns 1 (:stack_rolled_back)' do | 
					
						
							|  |  |  |         allow(stack).to receive(:stack_status).and_return('CREATE_IN_PROGRESS', 'ROLLBACK_IN_PROGRESS', 'ROLLBACK_COMPLETE') | 
					
						
							|  |  |  |         expect(syncer.sync).to eq 1
 | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context 'when unkown stack status was found' do | 
					
						
							|  |  |  |       it 'returns 2 (:unkown_status)' do | 
					
						
							|  |  |  |         allow(stack).to receive(:stack_status).and_return('CREATE_IN_PROGRESS', 'unknown') | 
					
						
							|  |  |  |         expect(syncer.sync).to eq 2
 | 
					
						
							|  |  |  |       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'} | 
					
						
							|  |  |  |         expect(syncer.sync).to eq 3
 | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context 'when an error occured during syncing', stubbed_logger: true do | 
					
						
							|  |  |  |       it 'returns 5 (:error)' do | 
					
						
							|  |  |  |         allow(stack).to receive(:stack_status).and_return('CREATE_IN_PROGRESS', 'CREATE_COMPLETE') | 
					
						
							|  |  |  |         allow(stubbed_connector).to receive(:stack_update) { raise } | 
					
						
							|  |  |  |         expect(syncer.sync).to eq 5
 | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe '#reason_from_error_code' do | 
					
						
							|  |  |  |     it 'returns reason as symbol for integer error_code' do | 
					
						
							|  |  |  |       expect(syncer.reason_from_error_code(1)).to eq :stack_rolled_back | 
					
						
							|  |  |  |       expect(syncer.reason_from_error_code(2)).to eq :unkown_status | 
					
						
							|  |  |  |       expect(syncer.reason_from_error_code(3)).to eq :timeout | 
					
						
							|  |  |  |       expect(syncer.reason_from_error_code(5)).to eq :error | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | end |