add specs for ServerExecutor#deploy

This commit is contained in:
Anton Chuchkalov 2016-01-19 13:36:29 +03:00
parent 170863809f
commit 33b460d50b
5 changed files with 151 additions and 3 deletions

View File

@ -381,7 +381,7 @@ module Devops
f.write json
end
end
@out << "Deploy Input Parameters:\n"
@out.puts "Deploy Input Parameters:"
@out.puts json
@out.flush
cmd << " -j http://#{DevopsConfig.config[:address]}:#{DevopsConfig.config[:port]}/#{DevopsConfig.config[:url_prefix]}/v2.0/deploy/data/#{file}"

View File

@ -420,4 +420,141 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
end
end
describe '#deploy_server', stubbed_knife: true, stubbed_connector: true do
let(:deploy_info) { @deploy_info }
let(:json_file_name) { 'json.json' }
let(:json_file_path) { File.join(SpecSupport.tmp_dir, json_file_name) }
subject { executor.deploy_server(deploy_info) }
before do
allow(executor).to receive(:run_hook).with(:before_deploy, any_args)
allow(executor).to receive(:run_hook).with(:after_deploy, any_args)
allow(stubbed_knife).to receive(:ssh_stream) { 'Chef Client finished'}
allow(stubbed_connector).to receive(:key) { double('Key', path: 'path_to_key') }
allow(stubbed_connector).to receive(:server_update)
@deploy_info = {}
end
it 'runs before_deploy and after_deploy hooks' do
expect(executor).to receive(:run_hook).with(:before_deploy, any_args).ordered
expect(executor).to receive(:run_hook).with(:after_deploy, any_args).ordered
subject
end
context 'when uses json file' do
before(:all) do
@tmp_files_at_start = Dir.entries(SpecSupport.tmp_dir)
end
before do
allow(DevopsConfig).to receive(:config).and_return({
project_info_dir: SpecSupport.tmp_dir,
address: 'host.com',
port: '8080',
url_prefix: 'api'
})
deploy_info['use_json_file'] = true
end
after(:all) do
diff = Dir.entries(SpecSupport.tmp_dir) - @tmp_files_at_start
diff.each do |file|
FileUtils.rm(File.join(SpecSupport.tmp_dir, file))
end
end
it 'writes deploy_info to json file if it not exists' do
expect { subject }.to change { Dir.entries(SpecSupport.tmp_dir)}
end
it "writes deploy_info to given json file name if it doesn't exist" do
FileUtils.rm(json_file_path) if File.exists?(json_file_path)
deploy_info['json_file'] = json_file_name
expect { subject }.to change {
Dir.entries(SpecSupport.tmp_dir)
}
FileUtils.rm(json_file_path)
end
it 'reads json from file if it exists' do
deploy_info['json_file'] = json_file_name
File.open(json_file_path, 'w') { |file| file.puts '{"foo": "bar"'}
expect { subject }.not_to change {
Dir.entries(SpecSupport.tmp_dir)
}
FileUtils.rm(json_file_path)
end
it 'adds link to json to deploy command' do
deploy_info['json_file'] = json_file_name
regexp = %r(-j http://host.com:8080/api/v2.0/deploy/data/#{json_file_name})
expect(stubbed_knife).to receive(:ssh_stream).with(anything, regexp, any_args)
subject
end
end
context "doesn't use json file" do
before do
deploy_info['use_json_file'] = false
deploy_info['run_list'] = %w(foo bar)
end
it "adds run list to command if server's stack is set" do
server.stack = 'stack'
expect(stubbed_knife).to receive(:ssh_stream).with(anything, %r(-r foo,bar), any_args)
subject
end
it "doesn't add run list to command if server's stack is unset" do
expect(stubbed_knife).to receive(:ssh_stream).with(anything, 'chef-client --no-color', any_args)
subject
end
end
it "uses server's key" do
expect(stubbed_connector).to receive(:key).with('key_id')
expect(stubbed_knife).to receive(:ssh_stream).with(any_args, 'path_to_key')
subject
end
it "uses public ip if it's set" do
server.public_ip = '127.0.0.1'
expect(stubbed_knife).to receive(:ssh_stream).with(anything, anything, '127.0.0.1', any_args)
subject
end
it "uses private_ip if public_ip isn't set" do
expect(stubbed_knife).to receive(:ssh_stream).with(anything, anything, server.private_ip, any_args)
subject
end
context 'if deploy was successful' do
it "updates server's last operation" do
expect(server).to receive(:set_last_operation).with('deploy', anything)
expect(stubbed_connector).to receive(:server_update).with(server)
subject
end
it 'returns 0' do
expect(subject).to eq 0
end
end
context "when deploy wasn't successful" do
before { allow(stubbed_knife).to receive(:ssh_stream) { 'fail'} }
it "doesn't run after_deploy hook" do
expect(executor).to receive(:run_hook).with(:before_deploy, any_args)
expect(executor).not_to receive(:run_hook).with(:after_deploy, any_args)
subject
end
it 'returns 1' do
expect(subject).to eq 1
end
end
end
end

View File

@ -3,7 +3,7 @@ require 'db/mongo/models/key'
FactoryGirl.define do
factory :key, class: Devops::Model::Key do
id 'user_key'
path SpecSupport::BLANK_FILE
path SpecSupport.blank_file
scope 'user'
end
end

View File

@ -2,7 +2,14 @@ require 'yaml'
module SpecSupport
ROOT = File.join(__dir__, '../../')
BLANK_FILE = File.join(ROOT, 'spec/support/blank_file')
def self.blank_file
File.join(ROOT, 'spec/support/templates/blank_file')
end
def self.tmp_dir
File.join(ROOT, 'tmp/')
end
def self.db_params
@db_params ||= begin
@ -32,4 +39,8 @@ module SpecSupport
end
end
end
def self.root
File.join(__dir__, '../../')
end
end