2014-03-21 11:02:23 +00:00
|
|
|
require_relative "../../../base"
|
|
|
|
|
2014-03-22 16:18:00 +00:00
|
|
|
require Vagrant.source_root.join("plugins/provisioners/ansible/config")
|
2014-03-21 11:02:23 +00:00
|
|
|
require Vagrant.source_root.join("plugins/provisioners/ansible/provisioner")
|
|
|
|
|
|
|
|
describe VagrantPlugins::Ansible::Provisioner do
|
|
|
|
include_context "unit"
|
|
|
|
|
|
|
|
subject { described_class.new(machine, config) }
|
|
|
|
|
|
|
|
let(:iso_env) do
|
|
|
|
# We have to create a Vagrantfile so there is a Vagrant Environment
|
|
|
|
env = isolated_environment
|
|
|
|
env.vagrantfile("")
|
|
|
|
env.create_vagrant_env
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:machine) { iso_env.machine(iso_env.machine_names[0], :dummy) }
|
2014-03-22 16:18:00 +00:00
|
|
|
let(:config) { VagrantPlugins::Ansible::Config.new }
|
|
|
|
let(:file_that_exists) { File.expand_path(__FILE__) }
|
2014-03-21 11:02:23 +00:00
|
|
|
|
|
|
|
let(:ssh_info) {{
|
|
|
|
private_key_path: ['/path/to/my/key'],
|
|
|
|
username: 'testuser'
|
|
|
|
}}
|
|
|
|
|
|
|
|
before do
|
|
|
|
machine.stub(ssh_info: ssh_info)
|
|
|
|
|
2014-03-22 16:18:00 +00:00
|
|
|
config.playbook = 'playbook.yml'
|
|
|
|
|
|
|
|
$generated_inventory_file = File.join(machine.env.local_data_path, %w(provisioners ansible inventory vagrant_ansible_inventory))
|
2014-03-21 11:02:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for "an ansible-playbook subprocess" do
|
|
|
|
it "sets default arguments" do
|
|
|
|
expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args|
|
|
|
|
|
2014-03-22 16:18:00 +00:00
|
|
|
expect(args[0]).to eq("ansible-playbook")
|
|
|
|
expect(args[1]).to eq("--private-key=/path/to/my/key")
|
|
|
|
expect(args[2]).to eq("--user=testuser")
|
2014-03-21 11:02:23 +00:00
|
|
|
|
2014-03-22 16:18:00 +00:00
|
|
|
index = args.index("--limit=#{machine.name}")
|
2014-03-21 11:02:23 +00:00
|
|
|
expect(index).to be > 0
|
|
|
|
|
2014-03-22 16:18:00 +00:00
|
|
|
expect(args[args.length-2]).to eq("playbook.yml")
|
2014-03-21 11:02:23 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it "exports default environment variables" do
|
|
|
|
expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args|
|
|
|
|
cmd_opts = args.last
|
|
|
|
expect(cmd_opts[:env]['ANSIBLE_FORCE_COLOR']).to eql("true")
|
|
|
|
expect(cmd_opts[:env]['ANSIBLE_HOST_KEY_CHECKING']).to eql("false")
|
|
|
|
expect(cmd_opts[:env]['PYTHONUNBUFFERED']).to eql(1)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for "SSH transport mode is not forced" do
|
|
|
|
it "does not export ANSIBLE_SSH_ARGS" do
|
|
|
|
expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args|
|
|
|
|
cmd_opts = args.last
|
|
|
|
expect(cmd_opts[:env]['ANSIBLE_SSH_ARGS']).to be_nil
|
|
|
|
}
|
|
|
|
end
|
|
|
|
it "does not force SSH transport mode" do
|
|
|
|
expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args|
|
2014-03-22 16:18:00 +00:00
|
|
|
index = args.index("--connection=ssh")
|
2014-03-21 11:02:23 +00:00
|
|
|
expect(index).to be_nil
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for "SSH transport mode is forced" do
|
|
|
|
it "sets --connection=ssh argument" do
|
|
|
|
expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args|
|
2014-03-22 16:18:00 +00:00
|
|
|
index = args.index("--connection=ssh")
|
2014-03-21 11:02:23 +00:00
|
|
|
expect(index).to be > 0
|
|
|
|
}
|
|
|
|
end
|
|
|
|
it "enables ControlPersist (like Ansible defaults) via ANSIBLE_SSH_ARGS" do
|
|
|
|
expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args|
|
|
|
|
cmd_opts = args.last
|
|
|
|
expect(cmd_opts[:env]['ANSIBLE_SSH_ARGS']).to include("-o ControlMaster=auto")
|
|
|
|
expect(cmd_opts[:env]['ANSIBLE_SSH_ARGS']).to include("-o ControlPersist=60s")
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#provision" do
|
|
|
|
|
|
|
|
before do
|
2014-03-22 16:18:00 +00:00
|
|
|
unless example.metadata[:skip_before]
|
|
|
|
config.finalize!
|
|
|
|
Vagrant::Util::Subprocess.stub(execute: Vagrant::Util::Subprocess::Result.new(0, "", ""))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
unless example.metadata[:skip_after]
|
|
|
|
subject.provision
|
|
|
|
end
|
2014-03-21 11:02:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't raise an error if it succeeds" do
|
|
|
|
end
|
|
|
|
|
2014-03-22 16:18:00 +00:00
|
|
|
it "raises an error if the exit code is non-zero", skip_before: true, skip_after: true do
|
|
|
|
config.finalize!
|
|
|
|
Vagrant::Util::Subprocess.stub(execute: Vagrant::Util::Subprocess::Result.new(1, "", ""))
|
2014-03-21 11:02:23 +00:00
|
|
|
|
2014-03-22 16:18:00 +00:00
|
|
|
expect {subject.provision}.to raise_error(Vagrant::Errors::AnsibleFailed)
|
2014-03-21 11:02:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "with default options" do
|
2014-03-22 16:18:00 +00:00
|
|
|
|
2014-03-21 11:02:23 +00:00
|
|
|
it_behaves_like 'an ansible-playbook subprocess'
|
|
|
|
it_behaves_like 'SSH transport mode is not forced'
|
|
|
|
|
2014-03-22 16:18:00 +00:00
|
|
|
it "generates an inventory file and uses it" do
|
2014-03-21 11:02:23 +00:00
|
|
|
expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args|
|
2014-03-22 16:18:00 +00:00
|
|
|
expect(File.exists?($generated_inventory_file)).to be_true
|
|
|
|
expect(args.include?("--inventory-file=#{$generated_inventory_file}")).to be_true
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with inventory_path option" do
|
|
|
|
before do
|
|
|
|
config.inventory_path = file_that_exists
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'an ansible-playbook subprocess'
|
|
|
|
it_behaves_like 'SSH transport mode is not forced'
|
|
|
|
|
|
|
|
it "uses given inventory path" do
|
|
|
|
expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args|
|
|
|
|
expect(args.include?("--inventory-file=#{file_that_exists}")).to be_true
|
|
|
|
expect(args.include?("--inventory-file=#{$generated_inventory_file}")).to be_false
|
2014-03-21 11:02:23 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with multiple SSH identities" do
|
|
|
|
before do
|
|
|
|
ssh_info[:private_key_path] = ['/path/to/my/key', '/an/other/identity', '/yet/an/other/key']
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'an ansible-playbook subprocess'
|
|
|
|
it_behaves_like 'SSH transport mode is forced'
|
|
|
|
|
|
|
|
it "passes additional Identity Files via ANSIBLE_SSH_ARGS" do
|
|
|
|
expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args|
|
|
|
|
cmd_opts = args.last
|
|
|
|
expect(cmd_opts[:env]['ANSIBLE_SSH_ARGS']).to include("-o IdentityFile=/an/other/identity")
|
|
|
|
expect(cmd_opts[:env]['ANSIBLE_SSH_ARGS']).to include("-o IdentityFile=/yet/an/other/key")
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with ssh forwarding enabled" do
|
|
|
|
before do
|
|
|
|
ssh_info[:forward_agent] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'an ansible-playbook subprocess'
|
|
|
|
it_behaves_like 'SSH transport mode is forced'
|
|
|
|
|
|
|
|
it "enables SSH-Forwarding via ANSIBLE_SSH_ARGS" do
|
|
|
|
expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args|
|
|
|
|
cmd_opts = args.last
|
|
|
|
expect(cmd_opts[:env]['ANSIBLE_SSH_ARGS']).to include("-o ForwardAgent=yes")
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|