2015-11-03 11:46:54 +03:00
|
|
|
RSpec.shared_examples 'deploy env' do
|
|
|
|
|
validated_model_name = described_class.name.demodulize.underscore
|
|
|
|
|
|
|
|
|
|
it 'is valid with correct attrs' do
|
|
|
|
|
expect(env).to be_valid
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe 'validation rules:' do
|
|
|
|
|
include_examples 'field type validation', :identifier, :not_nil, :non_empty_string, :only_word_symbols, :field_validator
|
|
|
|
|
include_examples 'field type validation', :run_list, :not_nil, :maybe_empty_array, :run_list, :field_validator
|
|
|
|
|
include_examples 'field type validation', :users, :not_nil, :maybe_empty_array, :field_validator
|
|
|
|
|
include_examples 'field type validation', :expires, :maybe_nil, :non_empty_string, :field_validator
|
|
|
|
|
|
|
|
|
|
it 'should be valid only with all users available' do
|
|
|
|
|
expect(build(validated_model_name, users: ['root'])).to be_valid
|
|
|
|
|
expect(build(validated_model_name, users: ['wrong'])).not_to be_valid
|
|
|
|
|
expect(build(validated_model_name, users: ['wrong', 'root'])).not_to be_valid
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'expires should be smth like "2d" or "15h"' do
|
|
|
|
|
expect(build(validated_model_name, expires: '2d')).to be_valid
|
|
|
|
|
expect(build(validated_model_name, expires: 'wrong')).not_to be_valid
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe '#validate_provider!' do
|
|
|
|
|
it 'raises InvalidRecord if provider is nil' do
|
|
|
|
|
env.provider = nil
|
|
|
|
|
expect {env.validate_provider!}.to raise_error InvalidRecord
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe '#initialize' do
|
|
|
|
|
it 'sets identifier from given hash' do
|
|
|
|
|
model = described_class.new('identifier' => 'a')
|
|
|
|
|
expect(model.identifier).to eq 'a'
|
|
|
|
|
end
|
|
|
|
|
it 'sets provider from given hash' do
|
|
|
|
|
model = described_class.new('provider' => 'ec2')
|
|
|
|
|
expect(model.provider).to eq 'ec2'
|
|
|
|
|
end
|
|
|
|
|
it 'sets uniq elements from run list from given hash' do
|
|
|
|
|
model = described_class.new('run_list' => ['role[a]', 'role[a]', 'recipe[e]'])
|
|
|
|
|
expect(model.run_list).to match_array ['role[a]', 'recipe[e]']
|
|
|
|
|
end
|
|
|
|
|
it 'sets empty array if run list is empty' do
|
|
|
|
|
model = described_class.new
|
|
|
|
|
expect(model.run_list).to match_array []
|
|
|
|
|
end
|
|
|
|
|
it 'sets expires from given hash' do
|
|
|
|
|
model = described_class.new('expires' => '3d')
|
|
|
|
|
expect(model.expires).to eq '3d'
|
|
|
|
|
end
|
|
|
|
|
it 'sets uniq users from given hash' do
|
|
|
|
|
model = described_class.new('users' => ['root', 'root', 'user'])
|
|
|
|
|
expect(model.users).to match_array ['root', 'user']
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe '#to_hash' do
|
|
|
|
|
it 'includes identifier, run_list, expires, users and provider in keys' do
|
|
|
|
|
expect(described_class.new.to_hash).to include('identifier', 'run_list', 'expires', 'users', 'provider')
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe '#add_users' do
|
|
|
|
|
it "doesn't add duplications of already added users" do
|
|
|
|
|
env = described_class.new('users' => %w(root user))
|
|
|
|
|
env.add_users %w(root user)
|
|
|
|
|
expect(env.users).to match_array %w(root user)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "adds new users" do
|
|
|
|
|
env = described_class.new('users' => %w(root))
|
|
|
|
|
env.add_users ['user1', 'user2']
|
|
|
|
|
expect(env.users).to match_array %w(root user1 user2)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-12-10 01:24:23 +03:00
|
|
|
describe '#update_field', stubbed_connector: true do
|
2015-11-03 11:46:54 +03:00
|
|
|
subject { env.update_field('project_name', 'run_list', ['role[asd]']) }
|
|
|
|
|
before do
|
2015-12-10 01:24:23 +03:00
|
|
|
allow(stubbed_connector).to receive(:set_project_deploy_env_field)
|
2015-11-03 11:46:54 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'validate it' do
|
|
|
|
|
expect(env).to receive(:validate_run_list!)
|
|
|
|
|
subject
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'updates attribute value' do
|
|
|
|
|
expect { subject }.to change {env.run_list}.to(['role[asd]'])
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'saves it' do
|
2015-12-10 01:24:23 +03:00
|
|
|
expected_args = ['project_name', env.identifier, {'run_list' => ['role[asd]']}]
|
|
|
|
|
expect(stubbed_connector).to receive(:set_project_deploy_env_field).with(*expected_args)
|
2015-11-03 11:46:54 +03:00
|
|
|
subject
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-12-10 01:24:23 +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
|
|
|
|
|
|
|
|
|
|
it 'returns nil if knife_instance is nil' do
|
|
|
|
|
allow(env).to receive(:knife_instance)
|
|
|
|
|
expect(subject).to be nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns hash with :error if knife.roles is nil' do
|
|
|
|
|
allow(env).to receive_message_chain('knife_instance.roles')
|
|
|
|
|
expect(subject).to be_a(Hash).and include(:error)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns hash with :error when creating a role raises an error' do
|
|
|
|
|
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'] } }
|
|
|
|
|
it 'returns hash with :exist' do
|
|
|
|
|
expect(subject).to be_a(Hash).and include(exist: ['role_name'])
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "adds the role to env's run_list" do
|
|
|
|
|
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') { [] } }
|
|
|
|
|
it 'returns hash with :new when a role has been created' do
|
|
|
|
|
expect(env).to receive_message_chain('knife_instance.create_role')
|
|
|
|
|
expect(subject).to be_a(Hash).and include(new: ['role_name'])
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "adds the role to env's run_list" do
|
|
|
|
|
allow(env).to receive_message_chain('knife_instance.create_role')
|
|
|
|
|
subject
|
|
|
|
|
expect(env.run_list).to include('role[role_name]')
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-12-10 01:24:23 +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
|
|
|
|
|
|
2015-12-10 01:24:23 +03:00
|
|
|
it 'raises InvalidRecord if env with such name already exists in project', stubbed_connector: true do
|
|
|
|
|
project = build(:project, with_deploy_env_identifiers: %w(new_name))
|
|
|
|
|
allow(stubbed_connector).to receive(:project) { project }
|
2015-11-03 11:46:54 +03:00
|
|
|
expect{subject}.to raise_error(InvalidRecord)
|
|
|
|
|
end
|
|
|
|
|
|
2015-12-10 01:24:23 +03:00
|
|
|
context 'when there is no env with such name already', stubbed_connector: true do
|
2015-11-03 11:46:54 +03:00
|
|
|
before do
|
2015-12-10 01:24:23 +03:00
|
|
|
allow(stubbed_connector).to receive(:project) { build(:project) }
|
|
|
|
|
allow(stubbed_connector).to receive(:set_project_deploy_env_field)
|
2015-11-03 11:46:54 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'validates new identifier' do
|
|
|
|
|
expect(env).to receive(:validate_identifier!)
|
|
|
|
|
subject
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'removes old role and adds new one' do
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
|
|
describe '#knife_instance' do
|
|
|
|
|
it 'calls KnifeFactory.instance' do
|
|
|
|
|
expect(KnifeFactory).to receive(:instance)
|
|
|
|
|
env.knife_instance
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|