add specs for ServerExecutor#two_phase_bootstrap

This commit is contained in:
Anton Chuchkalov 2015-12-29 16:10:05 +03:00
parent cf19294772
commit ac5b9fd4ac
2 changed files with 124 additions and 11 deletions

View File

@ -243,6 +243,7 @@ module Devops
@out << "Done\n"
end
# essentially, it just bootstrap and then deploy
def two_phase_bootstrap options
prepare_two_phase_bootstrap(options)
# bootstrap phase
@ -253,7 +254,7 @@ module Devops
bootstrap_status = bootstrap(options)
if bootstrap_status == 0
if check_server
if check_server_on_chef_server
@out << "Server #{@server.chef_node_name} is created"
else
@out.puts "Can not find client or node on chef-server"
@ -280,9 +281,8 @@ module Devops
# deploy phase. Assume that all servers are bootstraped successfully here.
begin
@out << "\n"
run_list = compute_run_list
@out << "\nComputed run list: #{run_list.join(", ")}"
@out << "\n\nComputed run list: #{run_list.join(", ")}"
@out.flush
knife_instance.set_run_list(@server.chef_node_name, run_list)
deploy_info = options[:deploy_info] || @project.deploy_info(@deploy_env)
@ -303,7 +303,7 @@ module Devops
end
end
def check_server
def check_server_on_chef_server
knife_instance.chef_node_list.include?(@server.chef_node_name) and knife_instance.chef_client_list.include?(@server.chef_node_name)
end
@ -553,6 +553,7 @@ module Devops
end
# to simplify testing
# :nocov:
def execute_system_command(cmd)
`#{cmd}`
end
@ -564,6 +565,7 @@ module Devops
def knife_instance
@knife_instance ||= KnifeFactory.instance
end
# :nocov:
end
end

View File

@ -1,6 +1,6 @@
require 'lib/executors/server_executor'
RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connector: true do
RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connector: true, stubbed_logger: true do
let(:project) { build(:project) }
let(:deploy_env) { project.deploy_env('foo') }
let(:server) { build(:server, project: project.id, deploy_env: 'foo') }
@ -47,7 +47,7 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
end
end
describe '#create_server', stubbed_connector: true, stubbed_logger: true do
describe '#create_server' do
let!(:without_bootstrap) { @without_bootstrap = true }
let!(:run_list) { @run_list = %w(role[asd]) }
let!(:key) { @key = 'key' }
@ -159,7 +159,7 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
end
context "if creating server in cloud wasn't successful" do
it 'returns proper error code' do
it 'returns creating_server_in_cloud_failed error code' do
allow(provider).to receive(:create_server) { false }
expect(subject).to eq 10
end
@ -168,7 +168,7 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
describe '#bootstrap', stubbed_logger: true, stubbed_knife: true do
describe '#bootstrap', stubbed_knife: true do
subject { executor.bootstrap({}) }
let(:image) { double('Key instance', path: 'path') }
@ -189,7 +189,7 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
end
context "when server's private ip is unset" do
it 'returns proper error code' do
it 'returns server_bootstrap_private_ip_unset error code' do
server.private_ip = nil
expect(subject).to eq 3
end
@ -209,7 +209,7 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
subject
end
it 'returns proper error code' do
it 'returns server_bootstrap_fail error code' do
expect(subject).to eq 2
end
end
@ -244,7 +244,7 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
context "if bootstraping wasn't successful" do
before { allow(stubbed_knife).to receive(:knife_bootstrap).and_return(123) }
it 'returns proper code' do
it 'returns :server_bootstrap_fail code' do
expect(subject).to eq 2
end
@ -255,4 +255,115 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
end
end
end
describe '#two_phase_bootstrap', stubbed_knife: true do
subject { executor.two_phase_bootstrap({}) }
before do
allow(provider).to receive(:run_list) {[]}
allow(stubbed_connector).to receive(:server_delete)
end
context 'when bootstrap was successful' do
before do
allow(executor).to receive(:bootstrap) { 0 }
allow(executor).to receive(:check_server_on_chef_server) { false }
end
context 'if node presents on chef server' do
before do
allow(executor).to receive(:check_server_on_chef_server) { true }
allow(executor).to receive(:deploy_server)
allow(stubbed_knife).to receive(:set_run_list)
end
it 'builds run list' do
expect(executor).to receive(:compute_run_list)
subject
end
it 'sets run list to chef node' do
expect(stubbed_knife).to receive(:set_run_list)
subject
end
it 'deploys server' do
expect(executor).to receive(:deploy_server)
subject
end
context 'if deploy was successful' do
it 'returns 0' do
allow(executor).to receive(:deploy_server) { 0 }
expect(subject).to eq 0
end
end
context "if deploy wasn't successful" do
it 'returns :deploy_failed code' do
allow(executor).to receive(:deploy_server) { 1 }
expect(subject).to eq 8
end
end
context 'when an error occured during deploy' do
it 'returns :deploy_unknown_error code' do
allow(executor).to receive(:deploy_server) { raise }
expect(subject).to eq 6
end
end
end
context "if node doesn't present on chef server" do
it 'roll backs and then deletes server from mongo' do
allow(executor).to receive(:check_server_on_chef_server) { false }
allow(executor).to receive(:roll_back)
allow(stubbed_connector).to receive(:server_delete)
expect(executor).to receive(:roll_back).ordered
expect(stubbed_connector).to receive(:server_delete).ordered
subject
end
end
end
context "when bootstrap wasn't successful" do
it 'returns :server_bootstrap_fail error code' do
allow(executor).to receive(:bootstrap) { 1 }
expect(subject).to eq 2
end
end
context 'when an error occured during bootstrap' do
it 'returns :server_bootstrap_unknown_error error code' do
allow(executor).to receive(:bootstrap) { raise }
expect(subject).to eq 7
end
end
end
describe '#check_server_on_chef_server', stubbed_knife: true do
subject { executor.check_server_on_chef_server }
before do
server.chef_node_name = 'a'
allow(stubbed_knife).to receive(:chef_node_list) { @node_list }
allow(stubbed_knife).to receive(:chef_client_list) { @client_list }
end
it 'returns true when node_name in node list and in client list' do
@node_list = %w(a); @client_list = %w(a)
expect(subject).to be true
end
it "returns false if node name isn't in node list" do
@node_list = []; @client_list = %w(a)
expect(subject).to be false
end
it "returns false if node name isn't in node list" do
@node_list = %w(a); @client_list = []
expect(subject).to be false
end
end
end