add specs for ServerExecutor#create_server

This commit is contained in:
Anton Chuchkalov 2015-12-24 19:51:52 +04:00
parent 799961b361
commit 7273228833
3 changed files with 130 additions and 18 deletions

View File

@ -13,7 +13,8 @@ module Devops
server_not_in_chef_nodes: 5,
server_bootstrap_unknown_error: 7,
deploy_unknown_error: 6,
deploy_failed: 8
deploy_failed: 8,
creating_server_unknown_error: 9
}
# waiting for 5*60 seconds (5 min)
@ -35,6 +36,9 @@ module Devops
before_deploy :create_run_list
attr_accessor :server, :deploy_env
def initialize server, out, options={}
if server
@project = Devops::Db.connector.project(server.project)
@ -67,14 +71,6 @@ module Devops
@project = p
end
def deploy_env= e
@deploy_env = e
end
def server
@server
end
def create_server_object options
Devops::Model::Server.new({
"project" => @project.id,
@ -133,8 +129,7 @@ module Devops
DevopsLogger.logger.error e.message
roll_back
mongo.server_delete @server.id
# return 5
return result_code(:server_not_in_chef_nodes)
result_code(:creating_server_unknown_error)
end
end
@ -277,7 +272,6 @@ module Devops
# deploy phase. Assume that all servers are bootstraped successfully here.
begin
#raise "hello"
@out << "\n"
run_list = compute_run_list
@out << "\nComputed run list: #{run_list.join(", ")}"

View File

@ -4,19 +4,19 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
let(:project) { build(:project) }
let(:deploy_env) { project.deploy_env('foo') }
let(:server) { build(:server, project: project.id, deploy_env: 'foo') }
let(:executor) { described_class.new(server, '') }
let(:output) { File.open(File::NULL, "w") }
let(:executor) { described_class.new(server, output) }
before do
allow(stubbed_connector).to receive(:project) { project }
end
describe '#initialize' do
it 'sets server, project, deploy_env, out instance variables' do
expect(executor).to have_instance_variable_value(:server, server)
expect(executor.server).to eq server
expect(executor.deploy_env).to eq deploy_env
expect(executor).to have_instance_variable_value(:project, project)
expect(executor).to have_instance_variable_value(:deploy_env, deploy_env)
expect(executor).to have_instance_variable_value(:out, '')
expect(executor).to have_instance_variable_value(:out, output)
end
it 'set knife_instance instance variable' do
@ -47,4 +47,122 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
expect(server.created_by).to eq 'me'
end
end
describe '#create_server', stubbed_connector: true, stubbed_logger: true do
let!(:without_bootstrap) { @without_bootstrap = true }
let!(:run_list) { @run_list = %w(role[asd]) }
let!(:key) { @key = 'key' }
subject {
executor.create_server(
'created_by' => 'user',
'run_list' => @run_list,
'name' => 'node_name',
'key' => @key,
'without_bootstrap' => @without_bootstrap
)
}
before do
@provider = double('Provider instance')
allow(executor.deploy_env).to receive(:provider_instance) { @provider }
allow(@provider).to receive(:create_server) { true }
@image = double('Image instance')
allow(@image).to receive(:remote_user) { 'remote_user' }
allow(stubbed_connector).to receive(:image) { @image}
allow(stubbed_connector).to receive(:server_insert)
end
it 'builds server model from given options' do
subject
expect(executor.server.created_by).to eq 'user'
expect(executor.server.chef_node_name).to eq 'node_name'
expect(executor.server.key).to eq @key
expect(executor.server.run_list).to eq @run_list
end
it 'sets run list to an empty array by default' do
@run_list = nil
subject
expect(executor.server.run_list).to eq []
end
it 'sets key to default provider ssh key by default' do
@key = nil
allow(@provider).to receive(:ssh_key) { 'default_key' }
subject
expect(executor.server.key).to eq 'default_key'
end
it 'runs hooks' do
expect(executor).to receive(:run_hook).with(:before_create).ordered
expect(executor).to receive(:run_hook).with(:after_create).ordered
subject
end
it 'creates server in cloud' do
expect(@provider).to receive(:create_server).with(
an_instance_of(Devops::Model::Server), deploy_env.image, deploy_env.flavor, deploy_env.subnets, deploy_env.groups, output
)
subject
end
it 'inserts built server into mongo' do
expect(stubbed_connector).to receive(:server_insert)
subject
end
it 'schedules expiration for server' do
expect(executor).to receive(:schedule_expiration).with(an_instance_of(Devops::Model::Server))
subject
end
context 'without_bootstrap option is false' do
it 'launches bootstrap' do
@without_bootstrap = false
allow(@image).to receive(:bootstrap_template) { 'template' }
allow(executor).to receive(:two_phase_bootstrap)
expect(executor).to receive(:two_phase_bootstrap)
subject
end
end
context 'without_bootstrap option is nil' do
it 'launches bootstrap' do
@without_bootstrap = nil
allow(@image).to receive(:bootstrap_template) { 'template' }
allow(executor).to receive(:two_phase_bootstrap)
expect(executor).to receive(:two_phase_bootstrap)
subject
end
end
context 'without_bootstrap option is true' do
it "doesn't launch bootstrap" do
@without_bootstrap = true
expect(executor).not_to receive(:two_phase_bootstrap)
subject
end
end
context 'if error has been raised during execution' do
before do
allow(stubbed_connector).to receive(:server_delete)
allow(@provider).to receive(:create_server) { raise }
end
it 'rollbacks server creating' do
expect(executor).to receive(:roll_back)
subject
end
it 'deletes server from mongo' do
expect(stubbed_connector).to receive(:server_delete)
subject
end
end
end
end

View File

@ -1,5 +1,5 @@
RSpec.shared_context 'stubbed calls to connector', stubbed_connector: true do
let(:stubbed_connector) { double() }
let(:stubbed_connector) { double('Connector') }
before do
allow(Devops::Db).to receive(:connector) { stubbed_connector }
end