| 
									
										
										
										
											2016-01-31 17:52:11 +03:00
										 |  |  | require 'workers/stack_bootstrap_worker' | 
					
						
							| 
									
										
										
										
											2016-03-30 12:05:59 +03:00
										 |  |  | require 'lib/executors/stack_executor' | 
					
						
							| 
									
										
										
										
											2016-01-31 17:52:11 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-27 23:37:57 +03:00
										 |  |  | RSpec.describe StackBootstrapWorker, type: :worker, stubbed_connector: true, init_messages: true do | 
					
						
							| 
									
										
										
										
											2016-01-31 17:52:11 +03:00
										 |  |  |   let(:stack_attrs) { attributes_for(:stack_ec2).stringify_keys } | 
					
						
							|  |  |  |   let(:perform_with_bootstrap) { worker.perform('stack_attributes' => stack_attrs) } | 
					
						
							|  |  |  |   let(:perform_without_bootstrap) { worker.perform('stack_attributes' => stack_attrs.merge('without_bootstrap' => true)) } | 
					
						
							|  |  |  |   let(:worker) { described_class.new } | 
					
						
							| 
									
										
										
										
											2016-03-30 12:05:59 +03:00
										 |  |  |   let(:executor) { | 
					
						
							|  |  |  |     instance_double(Devops::Executor::StackExecutor, | 
					
						
							|  |  |  |       wait_till_stack_is_created: true, | 
					
						
							|  |  |  |       create_stack: Devops::Model::StackEc2.new(stack_attrs), | 
					
						
							|  |  |  |       persist_stack_servers: nil, | 
					
						
							|  |  |  |       delete_stack: nil | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def bootstrap_result(reason) | 
					
						
							| 
									
										
										
										
											2016-03-30 12:57:24 +03:00
										 |  |  |     Devops::Executor::StackExecutor::ServersBootstrapper::Result.from_reason(reason) | 
					
						
							| 
									
										
										
										
											2016-03-30 12:05:59 +03:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2016-01-31 17:52:11 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |   before do | 
					
						
							| 
									
										
										
										
											2016-03-17 13:49:19 +03:00
										 |  |  |     allow(worker).to receive(:update_report) | 
					
						
							| 
									
										
										
										
											2016-03-30 12:05:59 +03:00
										 |  |  |     allow(worker).to receive(:executor) { executor } | 
					
						
							| 
									
										
										
										
											2016-03-27 23:37:57 +03:00
										 |  |  |     allow(worker).to receive(:persist_stack_servers) { {1 => build_list(:server, 2)} } | 
					
						
							| 
									
										
										
										
											2016-03-30 12:05:59 +03:00
										 |  |  |     allow(worker).to receive(:bootstrap_servers_by_priority) { bootstrap_result(:ok) } | 
					
						
							| 
									
										
										
										
											2016-01-31 17:52:11 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it 'requires "stack_attributes" in options' do | 
					
						
							| 
									
										
										
										
											2016-03-17 13:49:19 +03:00
										 |  |  |     expect{ worker.perform({}) }.to raise_error KeyError | 
					
						
							| 
									
										
										
										
											2016-01-31 17:52:11 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-10 20:39:49 +03:00
										 |  |  |   it 'updates report about operation' do | 
					
						
							| 
									
										
										
										
											2016-03-17 13:49:19 +03:00
										 |  |  |     expect(worker).to receive(:update_report) | 
					
						
							| 
									
										
										
										
											2016-01-31 17:52:11 +03:00
										 |  |  |     perform_without_bootstrap | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-10 20:39:49 +03:00
										 |  |  |   it 'updates report about operation, creates stack and persists stack servers' do | 
					
						
							| 
									
										
										
										
											2016-03-17 13:49:19 +03:00
										 |  |  |     expect(worker).to receive(:update_report).ordered | 
					
						
							| 
									
										
										
										
											2016-03-30 12:05:59 +03:00
										 |  |  |     expect(executor).to receive(:create_stack).ordered | 
					
						
							| 
									
										
										
										
											2016-03-27 23:37:57 +03:00
										 |  |  |     expect(worker).to receive(:persist_stack_servers).ordered | 
					
						
							| 
									
										
										
										
											2016-01-31 17:52:11 +03:00
										 |  |  |     perform_without_bootstrap | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-30 12:05:59 +03:00
										 |  |  |   it "waits for stack creation to be completed" do | 
					
						
							|  |  |  |     expect(executor).to receive(:wait_till_stack_is_created) | 
					
						
							|  |  |  |     perform_with_bootstrap | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it "returns 1 if waiting wasn't successful" do | 
					
						
							|  |  |  |     allow(executor).to receive(:wait_till_stack_is_created) { false } | 
					
						
							|  |  |  |     expect(perform_with_bootstrap).to eq 1
 | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-31 17:52:11 +03:00
										 |  |  |   context 'if without_bootstrap is true' do | 
					
						
							|  |  |  |     it "doesn't bootstrap servers" do | 
					
						
							| 
									
										
										
										
											2016-03-27 23:37:57 +03:00
										 |  |  |       expect(worker).not_to receive(:bootstrap_servers_by_priority) | 
					
						
							| 
									
										
										
										
											2016-01-31 17:52:11 +03:00
										 |  |  |       perform_without_bootstrap | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'returns 0' do | 
					
						
							|  |  |  |       expect(perform_without_bootstrap).to eq 0
 | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   context 'if without_bootstrap is false or not set' do | 
					
						
							| 
									
										
										
										
											2016-03-27 23:37:57 +03:00
										 |  |  |     it 'returns 0 when bootstraping servers was successful' do | 
					
						
							|  |  |  |       expect(perform_with_bootstrap).to eq 0
 | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'rollbacks stack and returns 2 when a known error occured during servers bootstrap' do | 
					
						
							| 
									
										
										
										
											2016-03-30 12:05:59 +03:00
										 |  |  |       allow(worker).to receive(:bootstrap_servers_by_priority) { bootstrap_result(:bootstrap_error) } | 
					
						
							|  |  |  |       expect(executor).to receive(:delete_stack) | 
					
						
							| 
									
										
										
										
											2016-01-31 17:52:11 +03:00
										 |  |  |       perform_with_bootstrap | 
					
						
							| 
									
										
										
										
											2016-03-27 23:37:57 +03:00
										 |  |  |       expect(perform_with_bootstrap).to eq 2
 | 
					
						
							| 
									
										
										
										
											2016-01-31 17:52:11 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-27 23:37:57 +03:00
										 |  |  |     it "doesn't rollback stack and returns 3 when a known error occured during servers deploy" do | 
					
						
							| 
									
										
										
										
											2016-03-30 12:05:59 +03:00
										 |  |  |       allow(worker).to receive(:bootstrap_servers_by_priority) { bootstrap_result(:deploy_error) } | 
					
						
							| 
									
										
										
										
											2016-03-27 23:37:57 +03:00
										 |  |  |       expect(worker).not_to receive(:rollback_stack!) | 
					
						
							|  |  |  |       expect(perform_with_bootstrap).to eq 3
 | 
					
						
							| 
									
										
										
										
											2016-01-31 17:52:11 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-27 23:37:57 +03:00
										 |  |  |     it "doesn't rollback stack and returns 3 when a servers bootstrap & deploy haven't been finished due to timeout" do | 
					
						
							| 
									
										
										
										
											2016-03-30 12:05:59 +03:00
										 |  |  |       allow(worker).to receive(:bootstrap_servers_by_priority) { bootstrap_result(:timeout_reached) } | 
					
						
							| 
									
										
										
										
											2016-03-27 23:37:57 +03:00
										 |  |  |       expect(worker).not_to receive(:rollback_stack!) | 
					
						
							|  |  |  |       expect(perform_with_bootstrap).to eq 4
 | 
					
						
							| 
									
										
										
										
											2016-01-31 17:52:11 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-27 23:37:57 +03:00
										 |  |  |     it 'rollbacks stack and reraises that error when an unknown error occured during servers bootsrap and deploy' do | 
					
						
							|  |  |  |       error = StandardError.new | 
					
						
							|  |  |  |       allow(worker).to receive(:bootstrap_servers_by_priority) { raise error } | 
					
						
							|  |  |  |       expect(worker).to receive(:rollback_stack!) | 
					
						
							|  |  |  |       expect{perform_with_bootstrap}.to raise_error(error) | 
					
						
							| 
									
										
										
										
											2016-01-31 17:52:11 +03:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-03-27 23:37:57 +03:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2016-01-31 17:52:11 +03:00
										 |  |  | end |