| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  | require 'db/mongo/models/server' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RSpec.describe Devops::Model::Server, type: :model do | 
					
						
							|  |  |  |   let(:server) { build(:server) } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   before do | 
					
						
							|  |  |  |     allow(Provider::ProviderFactory).to receive(:providers).and_return(%w(openstack ec2 static')) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it 'is valid with correct attrs' do | 
					
						
							|  |  |  |     expect(server).to be_valid | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-21 20:46:15 +03:00
										 |  |  |   describe 'validation rules:', stubbed_logger: true do | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |     include_examples 'field type validation', :id, :not_nil, :non_empty_string | 
					
						
							|  |  |  |     include_examples 'field type validation', :provider, :not_nil, :non_empty_string | 
					
						
							|  |  |  |     include_examples 'field type validation', :remote_user, :not_nil, :non_empty_string | 
					
						
							|  |  |  |     include_examples 'field type validation', :project, :not_nil, :non_empty_string | 
					
						
							|  |  |  |     include_examples 'field type validation', :deploy_env, :not_nil, :non_empty_string | 
					
						
							|  |  |  |     include_examples 'field type validation', :private_ip, :not_nil, :non_empty_string | 
					
						
							|  |  |  |     include_examples 'field type validation', :public_ip, :maybe_nil, :maybe_empty_string | 
					
						
							|  |  |  |     include_examples 'field type validation', :key, :not_nil, :non_empty_string | 
					
						
							|  |  |  |     include_examples 'field type validation', :created_by, :not_nil, :non_empty_string | 
					
						
							|  |  |  |     include_examples 'field type validation', :chef_node_name, :not_nil, :maybe_empty_string | 
					
						
							|  |  |  |     include_examples 'field type validation', :reserved_by, :not_nil, :maybe_empty_string | 
					
						
							|  |  |  |     include_examples 'field type validation', :stack, :maybe_nil, :non_empty_string | 
					
						
							| 
									
										
										
										
											2015-11-20 18:31:54 +03:00
										 |  |  |     include_examples 'field type validation', :run_list, :not_nil, :maybe_empty_array, :run_list | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe '#initialize' do | 
					
						
							|  |  |  |     it 'sets run_list to empty array if it is nil' do | 
					
						
							|  |  |  |       server = described_class.new() | 
					
						
							|  |  |  |       expect(server.run_list).to eq [] | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe '.fields' do | 
					
						
							|  |  |  |     it 'is array with several strings' do | 
					
						
							|  |  |  |       expect(described_class.fields).to match_array(%w(
 | 
					
						
							|  |  |  |         chef_node_name project deploy_env provider remote_user | 
					
						
							|  |  |  |         private_ip public_ip created_at created_by key reserved_by | 
					
						
							| 
									
										
										
										
											2015-11-23 17:21:43 +03:00
										 |  |  |         run_list stack last_operation | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |       )) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-19 14:18:50 +03:00
										 |  |  |   describe '.build_from_bson' do | 
					
						
							|  |  |  |     it 'takes a hash and returns instance of Server model' do | 
					
						
							|  |  |  |       model = described_class.build_from_bson('id' => 'foo') | 
					
						
							|  |  |  |       expect(model).to be_an_instance_of(described_class) | 
					
						
							|  |  |  |       expect(model.id).to eq 'foo' | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |   it '#to_hash_without_id returns not nil fields' do | 
					
						
							|  |  |  |     server = described_class.new('run_list' => [], 'project' =>  'asd') | 
					
						
							|  |  |  |     expect(server.to_hash_without_id.keys).to match_array(%w(run_list project)) | 
					
						
							|  |  |  |     server.stack = 'stack_id' | 
					
						
							|  |  |  |     expect(server.to_hash_without_id.keys).to match_array(%w(run_list project stack)) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe '#info' do | 
					
						
							|  |  |  |     it 'returns string' do | 
					
						
							|  |  |  |       expect(server.info).to be_a(String) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe '#static?' do | 
					
						
							|  |  |  |     it 'returns true for servers with provider==static' do | 
					
						
							|  |  |  |       expect(build(:server, provider: 'static').static?).to be true | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'returns false for servers with provider==ec2' do | 
					
						
							|  |  |  |       expect(server.static?).to be false | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe '#set_last_operation' do | 
					
						
							| 
									
										
										
										
											2015-11-23 17:21:43 +03:00
										 |  |  |     it 'sets #last_operation.date to Time.now' do | 
					
						
							|  |  |  |       prev_operation_time = server.last_operation['date'] | 
					
						
							|  |  |  |       server.set_last_operation('deploy', 'root') | 
					
						
							|  |  |  |       expect(server.last_operation['date']).to be > prev_operation_time | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'updates last_operation_type' do | 
					
						
							| 
									
										
										
										
											2015-11-23 17:21:43 +03:00
										 |  |  |       server.set_last_operation('deploy', 'root') | 
					
						
							|  |  |  |       expect(server.last_operation['type']).to eq 'deploy' | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'it raises error if given type is not supported' do | 
					
						
							|  |  |  |       expect { | 
					
						
							| 
									
										
										
										
											2015-11-23 17:21:43 +03:00
										 |  |  |         server.set_last_operation(:wrong, 'root') | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |       }.to raise_error ArgumentError | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'expects string as an argument' do | 
					
						
							|  |  |  |       expect { | 
					
						
							| 
									
										
										
										
											2015-11-23 17:21:43 +03:00
										 |  |  |         server.set_last_operation(:deploy, 'root') | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |       }.to raise_error ArgumentError | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |