def test_run_playbooks_ansible_cfg_env(self, mock_validate, mock_vars, mock_readable, mock_run): mock_vars.return_value = [] mock_readable.return_value = {"result": True} os.environ["ANSIBLE_CONFIG"] = "/path/to/ansible.cfg" parser = argparse.ArgumentParser() ansible.add_args(parser) vault.add_args(parser) parsed_args = parser.parse_args([]) ansible.run_playbooks(parsed_args, ["playbook1.yml"]) expected_cmd = [ "ansible-playbook", "--inventory", "/etc/kayobe/inventory", "playbook1.yml", ] expected_env = { "ANSIBLE_CONFIG": "/path/to/ansible.cfg", "KAYOBE_CONFIG_PATH": "/etc/kayobe" } mock_run.assert_called_once_with(expected_cmd, check_output=False, quiet=False, env=expected_env) mock_vars.assert_called_once_with(["/etc/kayobe"]) mock_readable.assert_called_once_with("/etc/kayobe/ansible.cfg")
def test_run_custom_ansible_cfg_2(self, mock_validate, mock_readable, mock_run): mock_readable.side_effect = [{"result": False}, {"result": True}] parser = argparse.ArgumentParser() ansible.add_args(parser) kolla_ansible.add_args(parser) vault.add_args(parser) parsed_args = parser.parse_args([]) kolla_ansible.run(parsed_args, "command", "overcloud") expected_cmd = [ ".", "/path/to/cwd/venvs/kolla-ansible/bin/activate", "&&", "kolla-ansible", "command", "--inventory", "/etc/kolla/inventory/overcloud", ] expected_cmd = " ".join(expected_cmd) expected_env = {"ANSIBLE_CONFIG": "/etc/kayobe/ansible.cfg"} mock_run.assert_called_once_with(expected_cmd, shell=True, quiet=False, env=expected_env) expected_calls = [ mock.call("/etc/kayobe/kolla/ansible.cfg"), mock.call("/etc/kayobe/ansible.cfg"), ] self.assertEqual(mock_readable.call_args_list, expected_calls)
def test_run_vault_password_helper(self, mock_update, mock_vars, mock_run): mock_vars.return_value = [] parser = argparse.ArgumentParser() mock_run.return_value = "/path/to/kayobe-vault-password-helper" kolla_ansible.add_args(parser) vault.add_args(parser) mock_run.assert_called_once_with( ["which", "kayobe-vault-password-helper"], check_output=True) mock_run.reset_mock() parsed_args = parser.parse_args([]) kolla_ansible.run(parsed_args, "command", "overcloud") expected_cmd = [ ".", "/path/to/cwd/venvs/kolla-ansible/bin/activate", "&&", "kolla-ansible", "command", "--key", "/path/to/kayobe-vault-password-helper", "--inventory", "/etc/kolla/inventory/overcloud", ] expected_cmd = " ".join(expected_cmd) expected_env = {"KAYOBE_VAULT_PASSWORD": "******"} mock_run.assert_called_once_with(expected_cmd, shell=True, quiet=False, env=expected_env) mock_update.assert_called_once_with(mock.ANY, expected_env)
def test_run_vault_password_file(self, mock_update, mock_validate, mock_run): parser = argparse.ArgumentParser() kolla_ansible.add_args(parser) vault.add_args(parser) args = [ "--vault-password-file", "/path/to/vault/pw", ] parsed_args = parser.parse_args(args) kolla_ansible.run(parsed_args, "command", "overcloud") expected_cmd = [ ".", "/path/to/cwd/venvs/kolla-ansible/bin/activate", "&&", "kolla-ansible", "command", "--key", "/path/to/vault/pw", "--inventory", "/etc/kolla/inventory/overcloud", ] expected_cmd = " ".join(expected_cmd) mock_run.assert_called_once_with(expected_cmd, shell=True, quiet=False, env={}) mock_update.assert_called_once_with(mock.ANY, {})
def test_run_func_args(self, mock_validate, mock_run): parser = argparse.ArgumentParser() ansible.add_args(parser) kolla_ansible.add_args(parser) vault.add_args(parser) args = [ "--kolla-extra-vars", "ev_name1=ev_value1", "--kolla-tags", "tag1,tag2", ] parsed_args = parser.parse_args(args) kwargs = { "extra_vars": {"ev_name2": "ev_value2"}, "tags": "tag3,tag4", "verbose_level": 1, "extra_args": ["--arg1", "--arg2"], } kolla_ansible.run(parsed_args, "command", "overcloud", **kwargs) expected_cmd = [ ".", "/path/to/cwd/venvs/kolla-ansible/bin/activate", "&&", "kolla-ansible", "command", "-v", "--inventory", "/etc/kolla/inventory/overcloud", "-e", "ev_name1=ev_value1", "-e", "ev_name2='ev_value2'", "--tags", "tag1,tag2,tag3,tag4", "--arg1", "--arg2", ] expected_cmd = " ".join(expected_cmd) mock_run.assert_called_once_with(expected_cmd, shell=True, quiet=False, env={})
def test_update_environment_no_vault(self): parser = argparse.ArgumentParser() vault.add_args(parser) parsed_args = parser.parse_args([]) env = {} vault.update_environment(parsed_args, env) self.assertEqual({}, env)
def test_run_all_the_args(self, mock_validate, mock_run): parser = argparse.ArgumentParser() ansible.add_args(parser) kolla_ansible.add_args(parser) vault.add_args(parser) args = [ "-C", "-D", "--kolla-config-path", "/path/to/config", "-ke", "ev_name1=ev_value1", "-ki", "/path/to/inventory", "-kl", "host1:host2", "-kt", "tag1,tag2", ] parsed_args = parser.parse_args(args) kolla_ansible.run(parsed_args, "command", "overcloud") expected_cmd = [ ".", "/path/to/cwd/venvs/kolla-ansible/bin/activate", "&&", "kolla-ansible", "command", "--inventory", "/path/to/inventory", "--configdir", "/path/to/config", "--passwords", "/path/to/config/passwords.yml", "-e", "ev_name1=ev_value1", "--limit", "'host1:host2'", "--tags", "tag1,tag2", ] expected_cmd = " ".join(expected_cmd) expected_env = {"EXTRA_OPTS": " --check --diff"} mock_run.assert_called_once_with(expected_cmd, shell=True, quiet=False, env=expected_env)
def test_run_all_the_long_args(self, mock_validate, mock_run): parser = argparse.ArgumentParser() kolla_ansible.add_args(parser) vault.add_args(parser) args = [ "--ask-vault-pass", "--kolla-config-path", "/path/to/config", "--kolla-extra-vars", "ev_name1=ev_value1", "--kolla-inventory", "/path/to/inventory", "--kolla-limit", "host1:host2", "--kolla-skip-tags", "tag3,tag4", "--kolla-tags", "tag1,tag2", ] parsed_args = parser.parse_args(args) kolla_ansible.run(parsed_args, "command", "overcloud") expected_cmd = [ ".", "/path/to/cwd/venvs/kolla-ansible/bin/activate", "&&", "kolla-ansible", "command", "--inventory", "/path/to/inventory", "--configdir", "/path/to/config", "--passwords", "/path/to/config/passwords.yml", "-e", "ev_name1=ev_value1", "--limit", "host1:host2", "--skip-tags", "tag3,tag4", "--tags", "tag1,tag2", ] expected_cmd = " ".join(expected_cmd) mock_run.assert_called_once_with(expected_cmd, shell=True, quiet=False)
def test_run_func_args(self, mock_validate, mock_run): parser = argparse.ArgumentParser() kolla_ansible.add_args(parser) vault.add_args(parser) args = [ "--kolla-extra-vars", "ev_name1=ev_value1", "--kolla-tags", "tag1,tag2", ] parsed_args = parser.parse_args(args) kwargs = { "extra_vars": {"ev_name2": "ev_value2"}, "tags": "tag3,tag4", "verbose_level": 1, "extra_args": ["--arg1", "--arg2"], } kolla_ansible.run(parsed_args, "command", "overcloud", **kwargs) expected_cmd = [ ".", "/path/to/cwd/venvs/kolla-ansible/bin/activate", "&&", "kolla-ansible", "command", "-v", "--inventory", "/etc/kolla/inventory/overcloud", "-e", "ev_name1=ev_value1", "-e", "ev_name2=ev_value2", "--tags", "tag1,tag2,tag3,tag4", "--arg1", "--arg2", ] expected_cmd = " ".join(expected_cmd) mock_run.assert_called_once_with(expected_cmd, shell=True, quiet=False)
def test_run_playbooks_vault_password_file(self, mock_update, mock_validate, mock_vars, mock_run): mock_vars.return_value = [] parser = argparse.ArgumentParser() ansible.add_args(parser) vault.add_args(parser) args = [ "--vault-password-file", "/path/to/vault/pw", ] parsed_args = parser.parse_args(args) ansible.run_playbooks(parsed_args, ["playbook1.yml"]) expected_cmd = [ "ansible-playbook", "--vault-password-file", "/path/to/vault/pw", "--inventory", "/etc/kayobe/inventory", "playbook1.yml", ] expected_env = {"KAYOBE_CONFIG_PATH": "/etc/kayobe"} mock_run.assert_called_once_with(expected_cmd, check_output=False, quiet=False, env=expected_env) mock_update.assert_called_once_with(mock.ANY, expected_env)
def test_run_playbooks_vault_password_helper(self, mock_validate, mock_vars, mock_run): mock_vars.return_value = [] parser = argparse.ArgumentParser() mock_run.return_value = "/path/to/kayobe-vault-password-helper" ansible.add_args(parser) vault.add_args(parser) mock_run.assert_called_once_with( ["which", "kayobe-vault-password-helper"], check_output=True) mock_run.reset_mock() parsed_args = parser.parse_args([]) ansible.run_playbooks(parsed_args, ["playbook1.yml"]) expected_cmd = [ "ansible-playbook", "--vault-password-file", "/path/to/kayobe-vault-password-helper", "--inventory", "/etc/kayobe/inventory", "playbook1.yml", ] expected_env = { "KAYOBE_CONFIG_PATH": "/etc/kayobe", "KAYOBE_VAULT_PASSWORD": "******" } mock_run.assert_called_once_with(expected_cmd, check_output=False, quiet=False, env=expected_env)
def test_run_playbooks(self, mock_validate, mock_vars, mock_run): mock_vars.return_value = [ "/etc/kayobe/vars-file1.yml", "/etc/kayobe/vars-file2.yaml" ] parser = argparse.ArgumentParser() ansible.add_args(parser) vault.add_args(parser) parsed_args = parser.parse_args([]) ansible.run_playbooks(parsed_args, ["playbook1.yml", "playbook2.yml"]) expected_cmd = [ "ansible-playbook", "--inventory", "/etc/kayobe/inventory", "-e", "@/etc/kayobe/vars-file1.yml", "-e", "@/etc/kayobe/vars-file2.yaml", "playbook1.yml", "playbook2.yml", ] expected_env = {"KAYOBE_CONFIG_PATH": "/etc/kayobe"} mock_run.assert_called_once_with(expected_cmd, check_output=False, quiet=False, env=expected_env) mock_vars.assert_called_once_with("/etc/kayobe")
def test_run_playbooks_list_tasks_arg(self, mock_validate, mock_vars, mock_run): mock_vars.return_value = [] parser = argparse.ArgumentParser() ansible.add_args(parser) vault.add_args(parser) args = [ "--list-tasks", ] parsed_args = parser.parse_args(args) kwargs = {"list_tasks": False} ansible.run_playbooks(parsed_args, ["playbook1.yml", "playbook2.yml"], **kwargs) expected_cmd = [ "ansible-playbook", "--inventory", "/etc/kayobe/inventory", "playbook1.yml", "playbook2.yml", ] expected_env = {"KAYOBE_CONFIG_PATH": "/etc/kayobe"} mock_run.assert_called_once_with(expected_cmd, check_output=False, quiet=False, env=expected_env) mock_vars.assert_called_once_with("/etc/kayobe")
def test_run_custom_ansible_cfg_env(self, mock_validate, mock_readable, mock_run): mock_readable.return_value = {"result": True} os.environ["ANSIBLE_CONFIG"] = "/path/to/ansible.cfg" parser = argparse.ArgumentParser() ansible.add_args(parser) kolla_ansible.add_args(parser) vault.add_args(parser) parsed_args = parser.parse_args([]) kolla_ansible.run(parsed_args, "command", "overcloud") expected_cmd = [ ".", "/path/to/cwd/venvs/kolla-ansible/bin/activate", "&&", "kolla-ansible", "command", "--inventory", "/etc/kolla/inventory/overcloud", ] expected_cmd = " ".join(expected_cmd) expected_env = {"ANSIBLE_CONFIG": "/path/to/ansible.cfg"} mock_run.assert_called_once_with(expected_cmd, shell=True, quiet=False, env=expected_env) mock_readable.assert_called_once_with("/etc/kayobe/kolla/ansible.cfg")
def test_multiple_inventory_args(self, mock_validate, mock_vars, mock_run): mock_vars.return_value = [] parser = argparse.ArgumentParser() ansible.add_args(parser) vault.add_args(parser) args = [ "--inventory", "/etc/kayobe/inventory", "--inventory", "/etc/kayobe/environments/foobar/inventory", ] parsed_args = parser.parse_args(args) ansible.run_playbooks(parsed_args, ["playbook1.yml", "playbook2.yml"]) expected_cmd = [ "ansible-playbook", "--inventory", "/etc/kayobe/inventory", "--inventory", "/etc/kayobe/environments/foobar/inventory", "playbook1.yml", "playbook2.yml", ] expected_env = {"KAYOBE_CONFIG_PATH": "/etc/kayobe"} mock_run.assert_called_once_with(expected_cmd, check_output=False, quiet=False, env=expected_env) mock_vars.assert_called_once_with(["/etc/kayobe"])
def test_run_playbooks_all_the_args(self, mock_validate, mock_vars, mock_run): mock_vars.return_value = [ "/path/to/config/vars-file1.yml", "/path/to/config/vars-file2.yaml" ] parser = argparse.ArgumentParser() ansible.add_args(parser) vault.add_args(parser) args = [ "-b", "-C", "--config-path", "/path/to/config", "--environment", "test-env", "-e", "ev_name1=ev_value1", "-i", "/path/to/inventory", "-l", "group1:host", "-t", "tag1,tag2", "-lt", ] parsed_args = parser.parse_args(args) ansible.run_playbooks(parsed_args, ["playbook1.yml", "playbook2.yml"], verbose_level=2) expected_cmd = [ "ansible-playbook", "-vv", "--list-tasks", "--inventory", "/path/to/inventory", "-e", "@/path/to/config/vars-file1.yml", "-e", "@/path/to/config/vars-file2.yaml", "-e", "ev_name1=ev_value1", "--become", "--check", "--limit", "group1:host", "--tags", "tag1,tag2", "playbook1.yml", "playbook2.yml", ] expected_env = { "KAYOBE_CONFIG_PATH": "/path/to/config", "KAYOBE_ENVIRONMENT": "test-env" } mock_run.assert_called_once_with(expected_cmd, check_output=False, quiet=False, env=expected_env) mock_vars.assert_called_once_with( ["/path/to/config", "/path/to/config/environments/test-env"])
def test_run_failure(self, mock_validate, mock_run): parser = argparse.ArgumentParser() kolla_ansible.add_args(parser) vault.add_args(parser) parsed_args = parser.parse_args([]) mock_run.side_effect = subprocess.CalledProcessError(1, "dummy") self.assertRaises(SystemExit, kolla_ansible.run, parsed_args, "command", "overcloud")
def test_run_playbooks_failure(self, mock_validate, mock_vars, mock_run): parser = argparse.ArgumentParser() ansible.add_args(parser) vault.add_args(parser) parsed_args = parser.parse_args([]) mock_run.side_effect = subprocess.CalledProcessError(1, "dummy") self.assertRaises(SystemExit, ansible.run_playbooks, parsed_args, ["command"])
def test_update_environment_prompt(self, mock_ask): mock_ask.return_value = "test-pass" parser = argparse.ArgumentParser() vault.add_args(parser) parsed_args = parser.parse_args(["--ask-vault-pass"]) env = {} vault.update_environment(parsed_args, env) self.assertEqual({"KAYOBE_VAULT_PASSWORD": "******"}, env) mock_ask.assert_called_once_with()
def test_run_playbooks_all_the_long_args(self, mock_validate, mock_vars, mock_run): mock_vars.return_value = [ "/path/to/config/vars-file1.yml", "/path/to/config/vars-file2.yaml" ] parser = argparse.ArgumentParser() ansible.add_args(parser) vault.add_args(parser) args = [ "--ask-vault-pass", "--become", "--check", "--config-path", "/path/to/config", "--extra-vars", "ev_name1=ev_value1", "--inventory", "/path/to/inventory", "--limit", "group1:host1", "--skip-tags", "tag3,tag4", "--tags", "tag1,tag2", "--list-tasks", ] parsed_args = parser.parse_args(args) ansible.run_playbooks(parsed_args, ["playbook1.yml", "playbook2.yml"]) expected_cmd = [ "ansible-playbook", "--list-tasks", "--ask-vault-pass", "--inventory", "/path/to/inventory", "-e", "@/path/to/config/vars-file1.yml", "-e", "@/path/to/config/vars-file2.yaml", "-e", "ev_name1=ev_value1", "--become", "--check", "--limit", "group1:host1", "--skip-tags", "tag3,tag4", "--tags", "tag1,tag2", "playbook1.yml", "playbook2.yml", ] expected_env = {"KAYOBE_CONFIG_PATH": "/path/to/config"} mock_run.assert_called_once_with(expected_cmd, quiet=False, env=expected_env) mock_vars.assert_called_once_with("/path/to/config")
def test_run_all_the_long_args(self, mock_ask, mock_validate, mock_run): parser = argparse.ArgumentParser() ansible.add_args(parser) kolla_ansible.add_args(parser) vault.add_args(parser) mock_ask.return_value = "test-pass" args = [ "--ask-vault-pass", "--kolla-config-path", "/path/to/config", "--kolla-extra-vars", "ev_name1=ev_value1", "--kolla-inventory", "/path/to/inventory", "--kolla-limit", "host1:host2", "--kolla-skip-tags", "tag3,tag4", "--kolla-tags", "tag1,tag2", ] parsed_args = parser.parse_args(args) mock_run.return_value = "/path/to/kayobe-vault-password-helper" kolla_ansible.run(parsed_args, "command", "overcloud") expected_cmd = [ ".", "/path/to/cwd/venvs/kolla-ansible/bin/activate", "&&", "kolla-ansible", "command", "--key", "/path/to/kayobe-vault-password-helper", "--inventory", "/path/to/inventory", "--configdir", "/path/to/config", "--passwords", "/path/to/config/passwords.yml", "-e", "ev_name1=ev_value1", "--limit", "'host1:host2'", "--skip-tags", "tag3,tag4", "--tags", "tag1,tag2", ] expected_cmd = " ".join(expected_cmd) expected_env = {"KAYOBE_VAULT_PASSWORD": "******"} expected_calls = [ mock.call(["which", "kayobe-vault-password-helper"], check_output=True, universal_newlines=True), mock.call(expected_cmd, shell=True, quiet=False, env=expected_env) ] self.assertEqual(expected_calls, mock_run.mock_calls)
def test_update_environment_file(self, mock_read): mock_read.return_value = "test-pass" parser = argparse.ArgumentParser() vault.add_args(parser) args = ["--vault-password-file", "/path/to/file"] parsed_args = parser.parse_args(args) env = {} vault.update_environment(parsed_args, env) self.assertEqual({"KAYOBE_VAULT_PASSWORD": "******"}, env) mock_read.assert_called_once_with("/path/to/file")
def test_run_playbooks_func_args(self, mock_validate, mock_vars, mock_run): mock_vars.return_value = [ "/etc/kayobe/vars-file1.yml", "/etc/kayobe/vars-file2.yaml" ] parser = argparse.ArgumentParser() ansible.add_args(parser) vault.add_args(parser) args = [ "--extra-vars", "ev_name1=ev_value1", "--limit", "group1:host1", "--tags", "tag1,tag2", ] parsed_args = parser.parse_args(args) kwargs = { "extra_vars": { "ev_name2": "ev_value2" }, "limit": "group2:host2", "tags": "tag3,tag4", "verbose_level": 0, "check": True, "diff": True, } ansible.run_playbooks(parsed_args, ["playbook1.yml", "playbook2.yml"], **kwargs) expected_cmd = [ "ansible-playbook", "--inventory", "/etc/kayobe/inventory", "-e", "@/etc/kayobe/vars-file1.yml", "-e", "@/etc/kayobe/vars-file2.yaml", "-e", "ev_name1=ev_value1", "-e", "ev_name2='ev_value2'", "--check", "--diff", "--limit", "group1:host1:&group2:host2", "--tags", "tag1,tag2,tag3,tag4", "playbook1.yml", "playbook2.yml", ] expected_env = {"KAYOBE_CONFIG_PATH": "/etc/kayobe"} mock_run.assert_called_once_with(expected_cmd, check_output=False, quiet=False, env=expected_env) mock_vars.assert_called_once_with(["/etc/kayobe"])
def test_run_playbooks_vault_ask_and_file(self, mock_validate, mock_vars, mock_run): mock_vars.return_value = [] parser = argparse.ArgumentParser() ansible.add_args(parser) vault.add_args(parser) args = [ "--ask-vault-pass", "--vault-password-file", "/path/to/vault/pw", ] self.assertRaises(SystemExit, parser.parse_args, args)
def test_run(self, mock_validate, mock_run): parser = argparse.ArgumentParser() kolla_ansible.add_args(parser) vault.add_args(parser) parsed_args = parser.parse_args([]) kolla_ansible.run(parsed_args, "command", "overcloud") expected_cmd = [ ".", "/path/to/cwd/venvs/kolla-ansible/bin/activate", "&&", "kolla-ansible", "command", "--inventory", "/etc/kolla/inventory/overcloud", ] expected_cmd = " ".join(expected_cmd) mock_run.assert_called_once_with(expected_cmd, shell=True, quiet=False)
def test_run(self, mock_validate, mock_run): parser = argparse.ArgumentParser() ansible.add_args(parser) kolla_ansible.add_args(parser) vault.add_args(parser) parsed_args = parser.parse_args([]) kolla_ansible.run(parsed_args, "command", "overcloud") expected_cmd = [ ".", "/path/to/cwd/venvs/kolla-ansible/bin/activate", "&&", "kolla-ansible", "command", "--inventory", "/etc/kolla/inventory/overcloud", ] expected_cmd = " ".join(expected_cmd) mock_run.assert_called_once_with(expected_cmd, shell=True, quiet=False, env={})
def test_run_playbooks_all_the_args(self, mock_validate, mock_vars, mock_run): mock_vars.return_value = [ "/path/to/config/vars-file1.yml", "/path/to/config/vars-file2.yaml" ] parser = argparse.ArgumentParser() ansible.add_args(parser) vault.add_args(parser) args = [ "-b", "-C", "--config-path", "/path/to/config", "-e", "ev_name1=ev_value1", "-i", "/path/to/inventory", "-l", "group1:host", "-t", "tag1,tag2", ] parsed_args = parser.parse_args(args) ansible.run_playbooks(parsed_args, ["playbook1.yml", "playbook2.yml"]) expected_cmd = [ "ansible-playbook", "--inventory", "/path/to/inventory", "-e", "@/path/to/config/vars-file1.yml", "-e", "@/path/to/config/vars-file2.yaml", "-e", "ev_name1=ev_value1", "--become", "--check", "--limit", "group1:host", "--tags", "tag1,tag2", "playbook1.yml", "playbook2.yml", ] mock_run.assert_called_once_with(expected_cmd, quiet=False) mock_vars.assert_called_once_with("/path/to/config")
def test_run_playbooks_vault_password_file(self, mock_validate, mock_vars, mock_run): mock_vars.return_value = [] parser = argparse.ArgumentParser() ansible.add_args(parser) vault.add_args(parser) args = [ "--vault-password-file", "/path/to/vault/pw", ] parsed_args = parser.parse_args(args) ansible.run_playbooks(parsed_args, ["playbook1.yml"]) expected_cmd = [ "ansible-playbook", "--vault-password-file", "/path/to/vault/pw", "--inventory", "/etc/kayobe/inventory", "playbook1.yml", ] mock_run.assert_called_once_with(expected_cmd, quiet=False)
def test_run_vault_password_helper(self, mock_vars, mock_run): mock_vars.return_value = [] parser = argparse.ArgumentParser() mock_run.return_value = "/path/to/kayobe-vault-password-helper" kolla_ansible.add_args(parser) vault.add_args(parser) mock_run.assert_called_once_with( ["which", "kayobe-vault-password-helper"], check_output=True) mock_run.reset_mock() parsed_args = parser.parse_args([]) kolla_ansible.run(parsed_args, "command", "overcloud") expected_cmd = [ ".", "/path/to/cwd/venvs/kolla-ansible/bin/activate", "&&", "kolla-ansible", "command", "--key", "/path/to/kayobe-vault-password-helper", "--inventory", "/etc/kolla/inventory/overcloud", ] expected_cmd = " ".join(expected_cmd) mock_run.assert_called_once_with(expected_cmd, shell=True, quiet=False)
def test_multiple_inventories(self, mock_validate, mock_vars, mock_run, mock_exists): mock_vars.return_value = [] # os.path.exists gets called three times: # 1) shared inventory # 2) environment inventory # 3) ansible.cfg mock_exists.side_effect = [True, True, False] parser = argparse.ArgumentParser() ansible.add_args(parser) vault.add_args(parser) args = [ "--environment", "test-env", ] parsed_args = parser.parse_args(args) ansible.run_playbooks(parsed_args, ["playbook1.yml", "playbook2.yml"]) expected_cmd = [ "ansible-playbook", "--inventory", "/etc/kayobe/inventory", "--inventory", "/etc/kayobe/environments/test-env/inventory", "playbook1.yml", "playbook2.yml", ] expected_env = { "KAYOBE_CONFIG_PATH": "/etc/kayobe", "KAYOBE_ENVIRONMENT": "test-env" } expected_calls = [ mock.call("/etc/kayobe/inventory"), mock.call("/etc/kayobe/environments/test-env/inventory"), mock.call("/etc/kayobe/ansible.cfg"), ] self.assertEqual(expected_calls, mock_exists.mock_calls) mock_run.assert_called_once_with(expected_cmd, check_output=False, quiet=False, env=expected_env) mock_vars.assert_called_once_with( ["/etc/kayobe", "/etc/kayobe/environments/test-env"])
def test_validate_args_vault_password_file(self): parser = argparse.ArgumentParser() vault.add_args(parser) parsed_args = parser.parse_args( ["--vault-password-file", "/path/to/file"]) self.assertRaises(SystemExit, vault.validate_args, parsed_args)
def test_run_playbooks_all_the_long_args(self, mock_ask, mock_validate, mock_vars, mock_run): mock_vars.return_value = [ "/path/to/config/vars-file1.yml", "/path/to/config/vars-file2.yaml" ] parser = argparse.ArgumentParser() ansible.add_args(parser) vault.add_args(parser) mock_ask.return_value = "test-pass" args = [ "--ask-vault-pass", "--become", "--check", "--config-path", "/path/to/config", "--extra-vars", "ev_name1=ev_value1", "--inventory", "/path/to/inventory", "--limit", "group1:host1", "--skip-tags", "tag3,tag4", "--tags", "tag1,tag2", "--list-tasks", ] parsed_args = parser.parse_args(args) mock_run.return_value = "/path/to/kayobe-vault-password-helper" ansible.run_playbooks(parsed_args, ["playbook1.yml", "playbook2.yml"]) expected_cmd = [ "ansible-playbook", "--list-tasks", "--vault-password-file", "/path/to/kayobe-vault-password-helper", "--inventory", "/path/to/inventory", "-e", "@/path/to/config/vars-file1.yml", "-e", "@/path/to/config/vars-file2.yaml", "-e", "ev_name1=ev_value1", "--become", "--check", "--limit", "group1:host1", "--skip-tags", "tag3,tag4", "--tags", "tag1,tag2", "playbook1.yml", "playbook2.yml", ] expected_env = { "KAYOBE_CONFIG_PATH": "/path/to/config", "KAYOBE_VAULT_PASSWORD": "******" } expected_calls = [ mock.call(["which", "kayobe-vault-password-helper"], check_output=True), mock.call(expected_cmd, check_output=False, quiet=False, env=expected_env) ] self.assertEqual(expected_calls, mock_run.mock_calls) mock_vars.assert_called_once_with("/path/to/config")
def get_parser(self, prog_name): parser = super(VaultMixin, self).get_parser(prog_name) group = parser.add_argument_group("Ansible vault") vault.add_args(group) return parser