change subject with named let)
This commit is contained in:
parent
f4d13ce19f
commit
a96ea68922
@ -78,12 +78,8 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
|
||||
|
||||
describe '#create_server' do
|
||||
let!(:without_bootstrap) { @without_bootstrap = true }
|
||||
let!(:run_list) { @run_list = %w(role[asd]) }
|
||||
let!(:key) { @key = 'key' }
|
||||
let!(:image) { double('Image instance', remote_user: 'remote_user') }
|
||||
|
||||
subject {
|
||||
let(:image) { double('Image instance', remote_user: 'remote_user') }
|
||||
let(:create_server) {
|
||||
executor.create_server(
|
||||
'created_by' => 'user',
|
||||
'run_list' => @run_list,
|
||||
@ -97,10 +93,13 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
allow(provider).to receive(:create_server) { true }
|
||||
allow(stubbed_connector).to receive(:image) { image }
|
||||
allow(stubbed_connector).to receive(:server_insert)
|
||||
@without_bootstrap = true
|
||||
@run_list = %w(role[asd])
|
||||
@key = 'key'
|
||||
end
|
||||
|
||||
it 'builds server model from given options' do
|
||||
subject
|
||||
create_server
|
||||
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
|
||||
@ -109,46 +108,46 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
|
||||
it 'sets run list to an empty array by default' do
|
||||
@run_list = nil
|
||||
subject
|
||||
create_server
|
||||
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
|
||||
create_server
|
||||
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
|
||||
create_server
|
||||
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
|
||||
create_server
|
||||
end
|
||||
|
||||
it 'inserts built server into mongo' do
|
||||
expect(stubbed_connector).to receive(:server_insert)
|
||||
subject
|
||||
create_server
|
||||
end
|
||||
|
||||
it 'schedules expiration for server' do
|
||||
deploy_env.expires = '2m'
|
||||
allow(DeleteServerWorker).to receive(:perform_in)
|
||||
expect(DeleteServerWorker).to receive(:perform_in).with(120, {server_chef_node_name: 'node_name'})
|
||||
subject
|
||||
create_server
|
||||
end
|
||||
|
||||
it "doesn't schedule expiration if deploy_env.expires is nil" do
|
||||
deploy_env.expires = nil
|
||||
expect(DeleteServerWorker).not_to receive(:perform_in)
|
||||
subject
|
||||
create_server
|
||||
end
|
||||
|
||||
context 'without_bootstrap option is false' do
|
||||
@ -157,7 +156,7 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
allow(image).to receive(:bootstrap_template) { 'template' }
|
||||
allow(executor).to receive(:two_phase_bootstrap)
|
||||
expect(executor).to receive(:two_phase_bootstrap)
|
||||
subject
|
||||
create_server
|
||||
end
|
||||
end
|
||||
|
||||
@ -167,7 +166,7 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
allow(image).to receive(:bootstrap_template) { 'template' }
|
||||
allow(executor).to receive(:two_phase_bootstrap)
|
||||
expect(executor).to receive(:two_phase_bootstrap)
|
||||
subject
|
||||
create_server
|
||||
end
|
||||
end
|
||||
|
||||
@ -175,7 +174,7 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
it "doesn't launch bootstrap" do
|
||||
@without_bootstrap = true
|
||||
expect(executor).not_to receive(:two_phase_bootstrap)
|
||||
subject
|
||||
create_server
|
||||
end
|
||||
end
|
||||
|
||||
@ -187,19 +186,19 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
|
||||
it 'rollbacks server creating' do
|
||||
expect(executor).to receive(:roll_back)
|
||||
subject
|
||||
create_server
|
||||
end
|
||||
|
||||
it 'deletes server from mongo' do
|
||||
expect(stubbed_connector).to receive(:server_delete)
|
||||
subject
|
||||
create_server
|
||||
end
|
||||
end
|
||||
|
||||
context "if creating server in cloud wasn't successful" do
|
||||
it 'returns creating_server_in_cloud_failed error code' do
|
||||
allow(provider).to receive(:create_server) { false }
|
||||
expect(subject).to eq 10
|
||||
expect(create_server).to eq 10
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -207,8 +206,8 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
|
||||
|
||||
describe '#bootstrap', stubbed_knife: true do
|
||||
subject { executor.bootstrap({}) }
|
||||
let(:image) { double('Key instance', path: 'path') }
|
||||
let(:bootstrap) { executor.bootstrap({}) }
|
||||
|
||||
before do
|
||||
allow(executor).to receive(:sleep)
|
||||
@ -223,19 +222,19 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
it 'run before hook' do
|
||||
expect(executor).to receive(:run_hook).with(:before_bootstrap, output).ordered
|
||||
expect(executor).to receive(:run_hook).with(:after_bootstrap, output).ordered
|
||||
subject
|
||||
bootstrap
|
||||
end
|
||||
|
||||
context "when server's private ip is unset" do
|
||||
it 'returns server_bootstrap_private_ip_unset error code' do
|
||||
server.private_ip = nil
|
||||
expect(subject).to eq 4
|
||||
expect(bootstrap).to eq 4
|
||||
end
|
||||
end
|
||||
|
||||
it 'tries to ssh to server' do
|
||||
expect(executor).to receive(:execute_system_command).with(/ssh/)
|
||||
subject
|
||||
bootstrap
|
||||
end
|
||||
|
||||
context "couldn't ssh to server" do
|
||||
@ -244,11 +243,11 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
it 'tries to ssh to server maximum MAX_SSH_RETRIES_AMOUNT times' do
|
||||
max_retries = Devops::Executor::ServerExecutor::MAX_SSH_RETRIES_AMOUNT
|
||||
expect(executor).to receive(:execute_system_command).exactly(max_retries).times
|
||||
subject
|
||||
bootstrap
|
||||
end
|
||||
|
||||
it 'returns server_bootstrap_fail error code' do
|
||||
expect(subject).to eq 2
|
||||
expect(bootstrap).to eq 2
|
||||
end
|
||||
end
|
||||
|
||||
@ -258,24 +257,24 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
|
||||
it "sets default chef node name if it's nil" do
|
||||
executor.server.chef_node_name = nil
|
||||
expect {subject}.to change {executor.server.chef_node_name}.to 'chef_node'
|
||||
expect {bootstrap}.to change {executor.server.chef_node_name}.to 'chef_node'
|
||||
end
|
||||
|
||||
it 'executes knife bootstrap' do
|
||||
expect(stubbed_knife).to receive(:knife_bootstrap).with(output, server.private_ip, instance_of(Array))
|
||||
subject
|
||||
bootstrap
|
||||
end
|
||||
|
||||
it "bootstraps to public ip if it's set" do
|
||||
server.public_ip = '8.8.8.8'
|
||||
expect(stubbed_knife).to receive(:knife_bootstrap).with(output, '8.8.8.8', instance_of(Array))
|
||||
subject
|
||||
bootstrap
|
||||
end
|
||||
|
||||
context 'after successful bootstrap' do
|
||||
it "updates server's chef node name in db" do
|
||||
expect(stubbed_connector).to receive(:server_set_chef_node_name).with(instance_of(Devops::Model::Server))
|
||||
subject
|
||||
bootstrap
|
||||
end
|
||||
end
|
||||
|
||||
@ -283,12 +282,12 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
before { allow(stubbed_knife).to receive(:knife_bootstrap).and_return(123) }
|
||||
|
||||
it 'returns :server_bootstrap_fail code' do
|
||||
expect(subject).to eq 2
|
||||
expect(bootstrap).to eq 2
|
||||
end
|
||||
|
||||
it "doesn't run after hook" do
|
||||
expect(executor).to receive(:run_hook).with(:before_bootstrap, output)
|
||||
subject
|
||||
bootstrap
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -297,7 +296,7 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
|
||||
|
||||
describe '#two_phase_bootstrap', stubbed_knife: true do
|
||||
subject { executor.two_phase_bootstrap({}) }
|
||||
let(:two_phase_bootstrap) { executor.two_phase_bootstrap({}) }
|
||||
|
||||
before do
|
||||
allow(provider).to receive(:run_list) {[]}
|
||||
@ -319,37 +318,37 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
|
||||
it 'builds run list' do
|
||||
expect(executor).to receive(:compute_run_list)
|
||||
subject
|
||||
two_phase_bootstrap
|
||||
end
|
||||
|
||||
it 'sets run list to chef node' do
|
||||
expect(stubbed_knife).to receive(:set_run_list)
|
||||
subject
|
||||
two_phase_bootstrap
|
||||
end
|
||||
|
||||
it 'deploys server' do
|
||||
expect(executor).to receive(:deploy_server)
|
||||
subject
|
||||
two_phase_bootstrap
|
||||
end
|
||||
|
||||
context 'if deploy was successful' do
|
||||
it 'returns 0' do
|
||||
allow(executor).to receive(:deploy_server) { 0 }
|
||||
expect(subject).to eq 0
|
||||
expect(two_phase_bootstrap).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
|
||||
expect(two_phase_bootstrap).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
|
||||
expect(two_phase_bootstrap).to eq 6
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -361,7 +360,7 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
allow(stubbed_connector).to receive(:server_delete)
|
||||
expect(executor).to receive(:roll_back).ordered
|
||||
expect(stubbed_connector).to receive(:server_delete).ordered
|
||||
subject
|
||||
two_phase_bootstrap
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -369,21 +368,19 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
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
|
||||
expect(two_phase_bootstrap).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
|
||||
expect(two_phase_bootstrap).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 }
|
||||
@ -392,17 +389,17 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
|
||||
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
|
||||
expect(executor.check_server_on_chef_server).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
|
||||
expect(executor.check_server_on_chef_server).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
|
||||
expect(executor.check_server_on_chef_server).to be false
|
||||
end
|
||||
end
|
||||
|
||||
@ -464,7 +461,7 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
let(:joined_initial_tags) { initial_tags.join(' ') }
|
||||
let(:given_tags) { %w(c d) }
|
||||
let(:joined_given_tags) { given_tags.join(' ') }
|
||||
subject { executor.deploy_server_with_tags(given_tags, {}) }
|
||||
let(:deploy_server_with_tags) { executor.deploy_server_with_tags(given_tags, {}) }
|
||||
|
||||
before do
|
||||
@current_tags = initial_tags.dup
|
||||
@ -490,7 +487,7 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
expect(stubbed_knife).to receive(:swap_tags).with(instance_of(String), joined_initial_tags, joined_given_tags).ordered
|
||||
expect(executor).to receive(:deploy_server).ordered
|
||||
expect(stubbed_knife).to receive(:swap_tags).with(instance_of(String), joined_given_tags, joined_initial_tags).ordered
|
||||
subject
|
||||
deploy_server_with_tags
|
||||
end
|
||||
end
|
||||
|
||||
@ -498,7 +495,7 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
it 'restores tags anyway' do
|
||||
allow(executor).to receive(:deploy_server) { raise }
|
||||
expect {
|
||||
subject
|
||||
deploy_server_with_tags
|
||||
}.to raise_error StandardError
|
||||
expect(current_tags).to eq initial_tags
|
||||
end
|
||||
@ -507,7 +504,7 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
context 'if cannot add tags to server' do
|
||||
it 'returns :server_cannot_update_tags code' do
|
||||
allow(stubbed_knife).to receive(:swap_tags) { false }
|
||||
expect(subject).to eq 3
|
||||
expect(deploy_server_with_tags).to eq 3
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -519,7 +516,7 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
let(:json_file_name) { 'json.json' }
|
||||
let(:json_file_path) { File.join(SpecSupport.tmp_dir, json_file_name) }
|
||||
|
||||
subject { executor.deploy_server(deploy_info) }
|
||||
let(:deploy_server) { executor.deploy_server(deploy_info) }
|
||||
|
||||
before do
|
||||
allow(executor).to receive(:run_hook).with(:before_deploy, any_args)
|
||||
@ -534,7 +531,7 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
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
|
||||
deploy_server
|
||||
end
|
||||
|
||||
context 'when uses json file' do
|
||||
@ -560,13 +557,13 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
|
||||
|
||||
it 'writes deploy_info to json file if it not exists' do
|
||||
expect { subject }.to change { Dir.entries(SpecSupport.tmp_dir)}
|
||||
expect { deploy_server }.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 {
|
||||
expect { deploy_server }.to change {
|
||||
Dir.entries(SpecSupport.tmp_dir)
|
||||
}
|
||||
FileUtils.rm(json_file_path)
|
||||
@ -575,7 +572,7 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
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 {
|
||||
expect { deploy_server }.not_to change {
|
||||
Dir.entries(SpecSupport.tmp_dir)
|
||||
}
|
||||
FileUtils.rm(json_file_path)
|
||||
@ -585,7 +582,7 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
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
|
||||
deploy_server
|
||||
end
|
||||
end
|
||||
|
||||
@ -598,41 +595,41 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
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
|
||||
deploy_server
|
||||
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
|
||||
deploy_server
|
||||
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
|
||||
deploy_server
|
||||
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
|
||||
deploy_server
|
||||
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
|
||||
deploy_server
|
||||
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
|
||||
deploy_server
|
||||
end
|
||||
|
||||
it 'returns 0' do
|
||||
expect(subject).to eq 0
|
||||
expect(deploy_server).to eq 0
|
||||
end
|
||||
end
|
||||
|
||||
@ -641,26 +638,26 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
|
||||
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
|
||||
deploy_server
|
||||
end
|
||||
|
||||
it 'returns 1' do
|
||||
expect(subject).to eq 1
|
||||
expect(deploy_server).to eq 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
describe '#delete_from_chef_server', stubbed_knife: true do
|
||||
subject { executor.delete_from_chef_server('foo') }
|
||||
let(:delete_from_chef_server) { executor.delete_from_chef_server('foo') }
|
||||
before do
|
||||
allow(stubbed_knife).to receive(:chef_client_delete)
|
||||
allow(stubbed_knife).to receive(:chef_node_delete)
|
||||
subject
|
||||
delete_from_chef_server
|
||||
end
|
||||
|
||||
it 'returns hash with :chef_node and :chef_client keys' do
|
||||
expect(subject).to be_a(Hash).and include(:chef_node, :chef_client)
|
||||
expect(delete_from_chef_server).to be_a(Hash).and include(:chef_node, :chef_client)
|
||||
end
|
||||
|
||||
it 'calls to :chef_node_delete and :chef_client_delete' do
|
||||
|
||||
Loading…
Reference in New Issue
Block a user