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(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_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_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_run_vault_password_file(self, mock_update, mock_validate, mock_run): parser = argparse.ArgumentParser() ansible.add_args(parser) 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_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" ansible.add_args(parser) kolla_ansible.add_args(parser) vault.add_args(parser) mock_run.assert_called_once_with( ["which", "kayobe-vault-password-helper"], check_output=True, universal_newlines=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_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_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_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_create_kayobe_environment(self, mock_readable_dir, mock_mkdir, mock_copy_dir): mock_readable_dir.return_value = { "result": False, "message": "Path does not exist" } parser = argparse.ArgumentParser() args = [ "--config-path", "/path/to/config", "--source-config-path", "/path/to/foo", "--environment", "foo", ] ansible.add_args(parser) environment.add_args(parser) parsed_args = parser.parse_args(args) environment.create_kayobe_environment(parsed_args) expected_calls = [ mock.call("/path/to/config/environments"), mock.call("/path/to/config/environments/foo"), ] self.assertEqual(expected_calls, mock_mkdir.call_args_list) mock_copy_dir.assert_called_once_with( "/path/to/foo", "/path/to/config/environments/foo", exclude=["environments"])
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_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_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_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_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_run_failure(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([]) mock_run.side_effect = subprocess.CalledProcessError(1, "dummy") self.assertRaises(SystemExit, kolla_ansible.run, parsed_args, "command", "overcloud")
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_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_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_prune_galaxy_roles(self, mock_remove): parser = argparse.ArgumentParser() ansible.add_args(parser) parsed_args = parser.parse_args([]) ansible.prune_galaxy_roles(parsed_args) expected_roles = [ 'stackhpc.os-shade', ] mock_remove.assert_called_once_with(expected_roles, "ansible/roles")
def test_passwords_yml_exists_false(self, mock_is_readable): parser = argparse.ArgumentParser() ansible.add_args(parser) parsed_args = parser.parse_args([]) mock_is_readable.return_value = {"result": False} result = ansible.passwords_yml_exists(parsed_args) self.assertFalse(result) mock_is_readable.assert_called_once_with( "/etc/kayobe/kolla/passwords.yml")
def test_passwords_yml_exists_true(self, mock_is_readable): parser = argparse.ArgumentParser() ansible.add_args(parser) parsed_args = parser.parse_args(["--config-path", "/path/to/config"]) mock_is_readable.return_value = {"result": True} result = ansible.passwords_yml_exists(parsed_args) self.assertTrue(result) mock_is_readable.assert_called_once_with( "/path/to/config/kolla/passwords.yml")
def test_environment_exists(self, mock_readable_dir): mock_readable_dir.side_effect = [{"result": True}, {"result": True}] parser = argparse.ArgumentParser() args = [ "--config-path", "/path/to/config", "--environment", "foo", ] ansible.add_args(parser) environment.add_args(parser) parsed_args = parser.parse_args(args) self.assertRaises(SystemExit, environment.create_kayobe_environment, parsed_args)
def test_prune_galaxy_roles(self, mock_remove): parser = argparse.ArgumentParser() ansible.add_args(parser) parsed_args = parser.parse_args([]) ansible.prune_galaxy_roles(parsed_args) expected_roles = [ 'stackhpc.os-flavors', 'stackhpc.os-projects', 'stackhpc.parted-1-1', 'stackhpc.timezone', ] mock_remove.assert_called_once_with(expected_roles, "ansible/roles")
def test_unreadable_environments_directory(self, mock_readable_dir): mock_readable_dir.return_value = { "result": False, "message": "Directory is not readable" } parser = argparse.ArgumentParser() args = [ "--config-path", "/path/to/config", "--environment", "foo", ] ansible.add_args(parser) environment.add_args(parser) parsed_args = parser.parse_args(args) self.assertRaises(SystemExit, environment.create_kayobe_environment, parsed_args)
def test_install_galaxy_roles(self, mock_mkdirs, mock_is_readable, mock_install): parser = argparse.ArgumentParser() ansible.add_args(parser) parsed_args = parser.parse_args([]) mock_is_readable.return_value = {"result": False} ansible.install_galaxy_roles(parsed_args) mock_install.assert_called_once_with("requirements.yml", "ansible/roles", force=False) mock_is_readable.assert_called_once_with( "/etc/kayobe/ansible/requirements.yml") self.assertFalse(mock_mkdirs.called)
def test_install_galaxy_roles_with_kayobe_config_mkdirs_failure( self, mock_mkdirs, mock_is_readable, mock_install): parser = argparse.ArgumentParser() ansible.add_args(parser) parsed_args = parser.parse_args([]) mock_is_readable.return_value = {"result": True} mock_mkdirs.side_effect = OSError(errno.EPERM) self.assertRaises(exception.Error, ansible.install_galaxy_roles, parsed_args) mock_install.assert_called_once_with("requirements.yml", "ansible/roles", force=False) mock_is_readable.assert_called_once_with( "/etc/kayobe/ansible/requirements.yml") mock_mkdirs.assert_called_once_with("/etc/kayobe/ansible/roles")
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 add_kayobe_ansible_args(self, group): ansible.add_args(group)