test/unit/plugins/provisioners/ansible/provisioner_test.rb in vagrant-unbundled-1.8.5.2 vs test/unit/plugins/provisioners/ansible/provisioner_test.rb in vagrant-unbundled-1.9.1.1

- old
+ new

@@ -57,12 +57,10 @@ stubbed_ui = Vagrant::UI::Colored.new stubbed_ui.stub(detail: "") machine.env.stub(ui: stubbed_ui) - subject.stub(:check_path) - config.playbook = 'playbook.yml' end # # Class methods for code reuse across examples @@ -193,11 +191,13 @@ describe "#provision" do before do unless example.metadata[:skip_before] config.finalize! + Vagrant::Util::Subprocess.stub(execute: Vagrant::Util::Subprocess::Result.new(0, "", "")) + subject.stub(:check_path) end end after do unless example.metadata[:skip_after] @@ -205,48 +205,50 @@ end end describe 'checking existence of Ansible configuration files' do - describe 'when the playbook file does not exist' do - it "raises an error", skip_before: true, skip_after: true do + STUBBED_INVALID_PATH = "/test/239nfmd/invalid_path".freeze - subject.stub(:check_path).and_raise(VagrantPlugins::Ansible::Errors::AnsibleError, - _key: :config_file_not_found, - config_option: "playbook", - path: "/home/wip/test/invalid_path.yml", - system: "host") + it 'raises an error when the `playbook` file does not exist', skip_before: true, skip_after: true do + subject.stub(:check_path).and_raise(VagrantPlugins::Ansible::Errors::AnsibleError, + _key: :config_file_not_found, + config_option: "playbook", + path: STUBBED_INVALID_PATH, + system: "host") - config.playbook = "/home/wip/test/invalid_path.yml" - config.finalize! + config.playbook = STUBBED_INVALID_PATH + config.finalize! - expect {subject.provision}.to raise_error(VagrantPlugins::Ansible::Errors::AnsibleError, - "`playbook` does not exist on the host: /home/wip/test/invalid_path.yml") - end + expect {subject.provision}.to raise_error(VagrantPlugins::Ansible::Errors::AnsibleError, + "`playbook` does not exist on the host: #{STUBBED_INVALID_PATH}") end - describe 'when the inventory path does not exist' do - it "raises an error" - end + %w(config_file extra_vars inventory_path galaxy_role_file vault_password_file).each do |option_name| + it "raises an error when the '#{option_name}' does not exist", skip_before: true, skip_after: true do + Vagrant::Util::Subprocess.stub(execute: Vagrant::Util::Subprocess::Result.new(0, "", "")) - describe 'when the extra_vars file does not exist' do - it "raises an error" - end + config.playbook = existing_file + config.send(option_name + '=', STUBBED_INVALID_PATH) + if option_name == 'extra_vars' + # little trick to auto-append the '@' prefix, which is a duty of the config validator... + config.validate(machine) + end + config.finalize! - describe 'when the galaxy_role_file does not exist' do - it "raises an error" + expect {subject.provision}.to raise_error(VagrantPlugins::Ansible::Errors::AnsibleError, + "`#{option_name}` does not exist on the host: #{STUBBED_INVALID_PATH}") + end end - describe 'when the vault_password_file does not exist' do - it "raises an error" - end - end describe 'when ansible-playbook fails' do it "raises an error", skip_before: true, skip_after: true do config.finalize! + + subject.stub(:check_path) Vagrant::Util::Subprocess.stub(execute: Vagrant::Util::Subprocess::Result.new(1, "", "")) expect {subject.provision}.to raise_error(VagrantPlugins::Ansible::Errors::AnsibleCommandFailed) end end @@ -271,10 +273,22 @@ expect(full_command).to include("ansible-playbook") } end end + describe "with playbook_command option" do + before do + config.playbook_command = "custom-ansible-playbook" + end + + it "uses custom playbook_command to run playbooks" do + expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args| + expect(args[0]).to eq("custom-ansible-playbook") + } + end + end + describe "with host_vars option" do it_should_create_and_use_generated_inventory it "adds host variables (given in Hash format) to the generated inventory" do config.host_vars = { @@ -568,10 +582,24 @@ } end end end + context "with config_file option defined" do + before do + config.config_file = existing_file + end + + it "sets ANSIBLE_CONFIG environment variable" do + expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args| + cmd_opts = args.last + expect(cmd_opts[:env]).to include("ANSIBLE_CONFIG") + expect(cmd_opts[:env]['ANSIBLE_CONFIG']).to eql(existing_file) + } + end + end + describe "with ask_vault_pass option" do before do config.ask_vault_pass = true end @@ -663,10 +691,23 @@ expect(cmd_opts[:env]['ANSIBLE_SSH_ARGS']).to include("-o ForwardAgent=yes") } end end + describe "with an ssh proxy command configured" do + before do + ssh_info[:proxy_command] = "ssh -W %h:%p -q user@remote_libvirt_host" + end + + it "sets '-o ProxyCommand' 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 ProxyCommand='ssh -W %h:%p -q user@remote_libvirt_host'") + } + end + end + context "with verbose option defined" do %w(vv vvvv).each do |verbose_option| describe "with a value of '#{verbose_option}'" do before do @@ -750,10 +791,12 @@ config.galaxy_role_file = existing_file end it "raises an error when ansible-galaxy command fails", skip_before: true, skip_after: true do config.finalize! + + subject.stub(:check_path) Vagrant::Util::Subprocess.stub(execute: Vagrant::Util::Subprocess::Result.new(1, "", "")) expect {subject.provision}.to raise_error(VagrantPlugins::Ansible::Errors::AnsibleCommandFailed) end @@ -825,15 +868,16 @@ config.limit = 'machine*:&vagrant:!that_one' config.start_at_task = "joe's awesome task" config.raw_arguments = ["--why-not", "--su-user=foot", "--ask-su-pass", "--limit=all", "--private-key=./myself.key", "--extra-vars='{\"var3\":\"foo\"}'"] # environment variables + config.config_file = existing_file config.host_key_checking = true config.raw_ssh_args = ['-o ControlMaster=no'] end - it_should_set_arguments_and_environment_variables 21, 5, true + it_should_set_arguments_and_environment_variables 21, 6, true it_should_explicitly_enable_ansible_ssh_control_persist_defaults it_should_set_optional_arguments({ "extra_vars" => "--extra-vars={\"var1\":\"string with 'apostrophes', \\\\, \\\" and =\",\"var2\":{\"x\":42}}", "sudo" => "--sudo", "sudo_user" => "--sudo-user=deployer", "verbose" => "-vvv", @@ -856,10 +900,10 @@ } end it "shows the ansible-playbook command, with additional quotes when required" do expect(machine.env.ui).to receive(:detail).with { |full_command| - expect(full_command).to eq(%Q(PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_ROLES_PATH='/up/to the stars' ANSIBLE_HOST_KEY_CHECKING=true ANSIBLE_SSH_ARGS='-o IdentitiesOnly=yes -i '/my/key1' -i '/my/key2' -o ForwardAgent=yes -o ControlMaster=no -o ControlMaster=auto -o ControlPersist=60s' ansible-playbook --connection=ssh --timeout=30 --ask-sudo-pass --ask-vault-pass --limit="machine*:&vagrant:!that_one" --inventory-file=#{generated_inventory_dir} --extra-vars="{\\"var1\\":\\"string with 'apostrophes', \\\\\\\\, \\\\\\" and =\\",\\"var2\\":{\\"x\\":42}}" --sudo --sudo-user=deployer -vvv --vault-password-file=#{File.expand_path(__FILE__)} --tags=db,www --skip-tags=foo,bar --start-at-task="joe's awesome task" --why-not --su-user=foot --ask-su-pass --limit=all --private-key=./myself.key --extra-vars='{\"var3\":\"foo\"}' playbook.yml)) + expect(full_command).to eq(%Q(PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_ROLES_PATH='/up/to the stars' ANSIBLE_CONFIG='#{existing_file}' ANSIBLE_HOST_KEY_CHECKING=true ANSIBLE_SSH_ARGS='-o IdentitiesOnly=yes -i '/my/key1' -i '/my/key2' -o ForwardAgent=yes -o ControlMaster=no -o ControlMaster=auto -o ControlPersist=60s' ansible-playbook --connection=ssh --timeout=30 --ask-sudo-pass --ask-vault-pass --limit="machine*:&vagrant:!that_one" --inventory-file=#{generated_inventory_dir} --extra-vars="{\\"var1\\":\\"string with 'apostrophes', \\\\\\\\, \\\\\\" and =\\",\\"var2\\":{\\"x\\":42}}" --sudo --sudo-user=deployer -vvv --vault-password-file=#{existing_file} --tags=db,www --skip-tags=foo,bar --start-at-task="joe's awesome task" --why-not --su-user=foot --ask-su-pass --limit=all --private-key=./myself.key --extra-vars='{\"var3\":\"foo\"}' playbook.yml)) } end end #