| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  | require 'db/mongo/models/environment/environment' | 
					
						
							|  |  |  | require 'exceptions/non_existing_user' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RSpec.describe Devops::Model::Environment, type: :model do | 
					
						
							|  |  |  |   let(:env) { build(:environment) } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   before do | 
					
						
							|  |  |  |     create(:user, id: 'root') | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   after do | 
					
						
							|  |  |  |     Devops::Model::User.delete_all | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |   it 'is valid with correct attrs' do | 
					
						
							|  |  |  |     expect(env).to be_valid | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe 'validation rules:' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     include_examples 'field type validation', :id, :not_nil, :non_empty_string, :only_word_symbols | 
					
						
							|  |  |  |     include_examples 'field type validation', :run_list, :maybe_empty_array, :run_list | 
					
						
							|  |  |  |     include_examples 'field type validation', :users, :maybe_empty_array | 
					
						
							|  |  |  |     include_examples 'field type validation', :expires, :maybe_nil, :non_empty_string | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it 'should be valid only with all users available' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       expect(build(:environment, users: ['root'])).to be_valid | 
					
						
							|  |  |  |       expect(build(:environment, users: ['wrong'])).not_to be_valid | 
					
						
							|  |  |  |       expect(build(:environment, users: ['wrong', 'root'])).not_to be_valid | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it 'expires should be smth like "2d" or "15h"' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       expect(build(:environment, expires: '2d')).to be_valid | 
					
						
							|  |  |  |       expect(build(:environment, expires: 'wrong')).not_to be_valid | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe '#initialize' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     it 'sets id from given hash' do | 
					
						
							|  |  |  |       model = build(:environment, 'id' => 'a') | 
					
						
							|  |  |  |       expect(model.id).to eq 'a' | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |     end | 
					
						
							|  |  |  |     it 'sets uniq elements from run list from given hash' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       model = build(:environment, 'run_list' => ['role[a]', 'role[a]', 'recipe[e]']) | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |       expect(model.run_list).to match_array ['role[a]', 'recipe[e]'] | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     it 'sets empty array if run list is empty' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       model = build(:environment, run_list: nil) | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |       expect(model.run_list).to match_array [] | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     it 'sets expires from given hash' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       model = build(:environment, 'expires' => '3d') | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |       expect(model.expires).to eq '3d' | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     it 'sets uniq users from given hash' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       model = build(:environment, 'users' => ['root', 'root', 'user']) | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |       expect(model.users).to match_array ['root', 'user'] | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe '#to_hash' do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     it 'includes id, run_list, expires, users in keys' do | 
					
						
							|  |  |  |       expect(build(:environment).to_hash).to include('id', 'run_list', 'expires', 'users') | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe '#add_users' do | 
					
						
							|  |  |  |     it "doesn't add duplications of already added users" do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       env = build(:environment, 'users' => %w(root user)) | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |       env.add_users %w(root user) | 
					
						
							|  |  |  |       expect(env.users).to match_array %w(root user) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "adds new users" do | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       env = build(:environment, 'users' => %w(root)) | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |       env.add_users ['user1', 'user2'] | 
					
						
							|  |  |  |       expect(env.users).to match_array %w(root user1 user2) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |   describe '#update_field', clean_db_after_example: true do | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |     subject { env.update_field('project_name', 'run_list', ['role[asd]']) } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     pending 'validate it' do | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |       expect(env).to receive(:validate_run_list!) | 
					
						
							|  |  |  |       subject | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     pending 'updates attribute value' do | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |       expect { subject }.to change {env.run_list}.to(['role[asd]']) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     pending 'saves it' do | 
					
						
							|  |  |  |       expected_args = ['project_name', env.id, {'run_list' => ['role[asd]']}] | 
					
						
							|  |  |  |       expect { subject }.to change { described_class.count } | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |   # should be moved to categories specs | 
					
						
							| 
									
										
										
										
											2016-01-27 15:44:59 +03:00
										 |  |  |   describe '#create_role', stubbed_logger: true do | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |     subject { env.create_role('project_name') } | 
					
						
							|  |  |  |     before do | 
					
						
							|  |  |  |       allow(env).to receive_message_chain('knife_instance.role_name') { 'role_name' } | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     pending 'returns nil if knife_instance is nil' do | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |       allow(env).to receive(:knife_instance) | 
					
						
							|  |  |  |       expect(subject).to be nil | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     pending 'returns hash with :error if knife.roles is nil' do | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |       allow(env).to receive_message_chain('knife_instance.roles') | 
					
						
							|  |  |  |       expect(subject).to be_a(Hash).and include(:error) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     pending 'returns hash with :error when creating a role raises an error' do | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |       allow(env).to receive_message_chain('knife_instance.roles') { [] } | 
					
						
							|  |  |  |       allow(env).to receive_message_chain('knife_instance.create_role') { raise StandardError } | 
					
						
							|  |  |  |       expect(subject).to be_a(Hash).and include(error: ['role_name']) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context 'when a role already exists in chef' do | 
					
						
							|  |  |  |       before { allow(env).to receive_message_chain('knife_instance.roles') { ['role_name'] } } | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       pending 'returns hash with :exist' do | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |         expect(subject).to be_a(Hash).and include(exist: ['role_name']) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       pending "adds the role to env's run_list" do | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |         subject | 
					
						
							|  |  |  |         expect(env.run_list).to include('role[role_name]') | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context 'when a role is missing in chef' do | 
					
						
							|  |  |  |       before { allow(env).to receive_message_chain('knife_instance.roles') { [] } } | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       pending 'returns hash with :new when a role has been created' do | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |         expect(env).to receive_message_chain('knife_instance.create_role') | 
					
						
							|  |  |  |         expect(subject).to be_a(Hash).and include(new: ['role_name']) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       pending "adds the role to env's run_list" do | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |         allow(env).to receive_message_chain('knife_instance.create_role') | 
					
						
							|  |  |  |         subject | 
					
						
							|  |  |  |         expect(env.run_list).to include('role[role_name]') | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-27 15:44:59 +03:00
										 |  |  |   describe '#rename', stubbed_logger: true do | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |     subject { env.rename('project_id', 'new_name') } | 
					
						
							|  |  |  |     let(:old_role_name) {'project_id_name'} | 
					
						
							|  |  |  |     let(:new_role_name) {'project_id_new_name'} | 
					
						
							|  |  |  |     let(:suggested_old_roles) {["role[#{old_role_name}]"]} | 
					
						
							|  |  |  |     let(:suggested_new_roles) {["role[#{new_role_name}]"]} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     before do | 
					
						
							|  |  |  |       # simulate correct start conditions | 
					
						
							|  |  |  |       env.run_list = suggested_old_roles | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # stub calls to knife | 
					
						
							|  |  |  |       allow(env).to receive(:create_role) { {exist: [new_role_name]} } | 
					
						
							|  |  |  |       allow(env).to receive_message_chain('knife_instance.role_name') { old_role_name } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # there is a call to Project.create_roles_response, stub it | 
					
						
							|  |  |  |       allow_message_expectations_on_nil | 
					
						
							|  |  |  |       project_class_double = double() | 
					
						
							|  |  |  |       allow(project_class_double).to receive(:create_roles_response) { ''} | 
					
						
							|  |  |  |       stub_const('Devops::Model::Project', project_class_double) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     pending 'raises InvalidRecord if env with such name already exists in project' do | 
					
						
							|  |  |  |       project = build(:project, with_env_identifiers: %w(new_name)) | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |       expect{subject}.to raise_error(InvalidRecord) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |     context 'when there is no env with such name already' do | 
					
						
							|  |  |  |       pending 'validates new id' do | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |         expect(env).to receive(:validate_identifier!) | 
					
						
							|  |  |  |         subject | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |       pending 'removes old role and adds new one' do | 
					
						
							| 
									
										
										
										
											2015-11-03 11:46:54 +03:00
										 |  |  |         # somewhy 'expect to change' did't work | 
					
						
							|  |  |  |         expect(env.run_list).to match_array(suggested_old_roles) | 
					
						
							|  |  |  |         subject | 
					
						
							|  |  |  |         expect(env.run_list).to match_array(suggested_new_roles) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |