add specs for ServerExecutor#unbootstrap

This commit is contained in:
Anton Chuchkalov 2016-01-20 10:23:34 +03:00
parent ee9605fc4d
commit f4d13ce19f
2 changed files with 57 additions and 19 deletions

View File

@ -177,7 +177,7 @@ module Devops
DevopsLogger.logger.error "Can not connect with command '#{cmd}':\n#{res}"
return error_code(:server_bootstrap_fail)
end
raise ArgumentError.new("Can not connect with command '#{cmd}' ") unless connected_successfully?
raise ArgumentError.new("Can not connect with command '#{cmd}' ") unless last_command_successful?
rescue ArgumentError => e
@out.puts "SSH command failed, retry (#{retries_amount}/#{MAX_SSH_RETRIES_AMOUNT})"
@out.flush
@ -299,7 +299,6 @@ module Devops
knife_instance.chef_node_list.include?(@server.chef_node_name) and knife_instance.chef_client_list.include?(@server.chef_node_name)
end
# there were changes in unbootstrap in other branches; leave it for now
def unbootstrap
k = Devops::Db.connector.key(@server.key)
cert_path = k.path
@ -307,14 +306,13 @@ module Devops
res = delete_from_chef_server(@server.chef_node_name)
begin
new_name = "/etc/chef.backup_#{Time.now.strftime("%d-%m-%Y_%H.%M.%S")}"
# r = `ssh -i #{cert_path} -q #{@server.remote_user}@#{@server.private_ip} rm -Rf /etc/chef`
cmd = "ssh -i #{cert_path} -q #{@server.remote_user}@#{@server.private_ip} \"/bin/sh -c 'if [[ -d /etc/chef ]]; then mv /etc/chef #{new_name}; else echo not found; fi'\""
DevopsLogger.logger.info("Trying to run command '#{cmd}'")
r = `#{cmd}`.strip
r = execute_system_command(cmd).strip
if r == 'not found'
res[:server] = "Directory '/etc/chef' does not exists"
else
raise(r) unless $?.success?
raise(r) unless last_command_successful?
res[:server] = "'/etc/chef' renamed to '#{new_name}'"
end
rescue => e
@ -412,16 +410,6 @@ module Devops
}
end
=begin
def delete_etc_chef s, cert_path
cmd = "ssh -i #{cert_path} -t -q #{s.remote_user}@#{s.private_ip}"
cmd += " sudo " unless s.remote_user == "root"
cmd += "rm -Rf /etc/chef"
r = `#{cmd}`
raise(r) unless $?.success?
end
=end
def delete_server
mongo = ::Devops::Db.connector
if @server.static?
@ -514,7 +502,7 @@ module Devops
`#{cmd}`
end
def connected_successfully?
def last_command_successful?
$?.success?
end

View File

@ -212,7 +212,7 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
before do
allow(executor).to receive(:sleep)
allow(executor).to receive(:connected_successfully?).and_return(true)
allow(executor).to receive(:last_command_successful?).and_return(true)
allow(executor).to receive(:execute_system_command)
allow(provider).to receive(:create_default_chef_node_name).and_return('chef_node')
allow(stubbed_connector).to receive(:key).and_return(image)
@ -239,7 +239,7 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
end
context "couldn't ssh to server" do
before { allow(executor).to receive(:connected_successfully?) { false } }
before { allow(executor).to receive(:last_command_successful?) { false } }
it 'tries to ssh to server maximum MAX_SSH_RETRIES_AMOUNT times' do
max_retries = Devops::Executor::ServerExecutor::MAX_SSH_RETRIES_AMOUNT
@ -254,7 +254,7 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
context 'after successful ssh check' do
before { allow(executor).to receive(:connected_successfully?).and_return(false, true) }
before { allow(executor).to receive(:last_command_successful?).and_return(false, true) }
it "sets default chef node name if it's nil" do
executor.server.chef_node_name = nil
@ -407,6 +407,56 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
end
describe '#unbootstrap', stubbed_knife: true do
before do
allow(stubbed_connector).to receive_message_chain('key.path') { 'path_to_key' }
allow(stubbed_knife).to receive(:chef_node_delete)
allow(stubbed_knife).to receive(:chef_client_delete)
allow(executor).to receive(:execute_system_command) { '' }
allow(executor).to receive(:last_command_successful?) { true }
allow(executor).to receive(:sleep)
end
it 'deletes node from chef server' do
allow(executor).to receive(:delete_from_chef_server).and_call_original
expect(executor).to receive(:delete_from_chef_server)
executor.unbootstrap
end
it "uses server's key" do
expect(executor).to receive(:execute_system_command).with(%r(ssh -i path_to_key))
executor.unbootstrap
end
it 'backups /etc/chef' do
expect(executor).to receive(:execute_system_command).with(%r(mv /etc/chef ))
executor.unbootstrap
end
it 'returns a hash with :chef_node, :chef_client and :server keys' do
expect(executor.unbootstrap).to be_a(Hash).and include(:chef_node, :chef_client, :server)
end
it "writes successful message into result's :server key" do
expect(executor.unbootstrap[:server]).to include('renamed')
end
context "when command returned 'not found'" do
it "writes error message into result's :server key" do
allow(executor).to receive(:execute_system_command) { 'not found' }
expect(executor.unbootstrap[:server]).to eq("Directory '/etc/chef' does not exists")
end
end
context "if command wasn't successful" do
it 'returns hash with error after 5.retries' do
allow(executor).to receive(:last_command_successful?) { false }
expect(executor).to receive(:sleep).with(1).exactly(5).times
expect(executor.unbootstrap).to be_a(Hash).and include(:error)
end
end
end
describe '#deploy_server_with_tags', stubbed_knife: true do
let(:current_tags) { @current_tags }