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 describe 'validation rules:' do 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 include_examples 'field type validation', :run_list, :not_nil, :maybe_empty_array include_examples 'field type validation', :run_list, :run_list 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 run_list stack last_operation )) end end 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 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 end it 'updates last_operation_type' do server.set_last_operation('deploy', 'root') expect(server.last_operation['type']).to eq 'deploy' end it 'it raises error if given type is not supported' do expect { server.set_last_operation(:wrong, 'root') }.to raise_error ArgumentError end it 'expects string as an argument' do expect { server.set_last_operation(:deploy, 'root') }.to raise_error ArgumentError end end end