def test_ansible_args(spec_fixture, workspace_manager_fixture, # noqa test_workspace): """Verify execution runs with --ansible-args. """ input_string = ['example', '--ansible-args', 'start-at-task="Test output";tags=only_this'] spec_manager = api.SpecManager() spec_manager.register_spec(spec_fixture) inventory_dir = test_workspace.path output_file = "output.example" assert not path.exists(path.join(inventory_dir, output_file)) workspace_manager_fixture.activate(test_workspace.name) return_value = spec_manager.run_specs(args=input_string) assert return_value == 0 # Combination of tags and start-at-task should avoid the file creation assert not path.exists(path.join(inventory_dir, output_file))
def test_execute_fail(spec_fixture, workspace_manager_fixture, # noqa test_workspace): """Verify execution fails as expected with CLI input. """ input_string = ['example', "--foo-bar", "fail"] spec_manager = api.SpecManager() spec_manager.register_spec(spec_fixture) inventory_dir = test_workspace.path output_file = "output.example" assert not path.exists(path.join(inventory_dir, output_file)) workspace_manager_fixture.activate(test_workspace.name) return_value = spec_manager.run_specs(args=input_string) # Assert return code != 0 assert return_value assert not path.exists(path.join(inventory_dir, output_file))
def test_execute_main_role_path(spec_fixture, workspace_manager_fixture, # noqa test_workspace, input_value, input_roles): """Verify execution runs the main.yml playbook when roles_path is set. Workflow is the same as in the test_execute_main test, however, the plugin used here has config.roles_path set. Verifies that ANSIBLE_ROLES_PATH is set before plugin's main.yml execution and it's restored to the original value after the plugin execution is over. """ input_string = ['example'] # get the plugin with role_path defined role_path_plugin = 'example/plugins/plugin_with_role_path/infrared/plugin' plugin_dir = path.join(path.abspath(path.dirname(tests.__file__)), role_path_plugin) test_plugin = plugins.InfraredPlugin(plugin_dir=plugin_dir) from infrared.api import InfraredPluginsSpec spec = InfraredPluginsSpec(test_plugin) spec_manager = api.SpecManager() spec_manager.register_spec(spec) inventory_dir = test_workspace.path output_file = "output.example" environ['ANSIBLE_ROLES_PATH'] = input_value assert not path.exists(path.join(inventory_dir, output_file)) assert not path.exists(path.join(inventory_dir, "role_" + output_file)) workspace_manager_fixture.activate(test_workspace.name) return_value = spec_manager.run_specs(args=input_string) out_file = open(path.join(inventory_dir, output_file), "r") expected_resp = 'ANSIBLE_ROLES_PATH=' + input_roles expected_resp += path.join(plugin_dir, test_plugin.roles_path + '../') assert return_value == 0 assert environ.get('ANSIBLE_ROLES_PATH', '') == input_value assert path.exists(path.join(inventory_dir, output_file)) assert out_file.read() == expected_resp
def test_nested_value_CLI(spec_fixture, workspace_manager_fixture, test_workspace, input_value, tmpdir): """Tests that CLI input of Complex type Value is nested in vars dict. Use "-o output_file" and evaluate output YAML file. """ dry_output = tmpdir.mkdir("tmp").join("dry_output.yml") if input_value: input_string = ['example', '--foo-bar', input_value] else: input_string = ['example'] input_string.extend(["-o", str(dry_output)]) # if no input, check that default value is loaded expected_output_dict = {"foo": {"bar": input_value or "default string"}} spec_manager = api.SpecManager() spec_manager.register_spec(spec_fixture) inventory_dir = test_workspace.path output_file = "output.example" assert not path.exists(path.join(inventory_dir, output_file)) workspace_manager_fixture.activate(test_workspace.name) return_value = spec_manager.run_specs(args=input_string) assert return_value == 0 assert path.exists(path.join(inventory_dir, output_file)) output_dict = yaml.load(dry_output.read())["provision"] # asserts expected_output_dict is subset of output assert subdict_in_dict( expected_output_dict, output_dict), "expected:{} actual:{}".format(expected_output_dict, output_dict)
def test_generate_answers_file(spec_fixture, workspace_manager_fixture, # noqa test_workspace, tmpdir): """Verify answers-file is generated to destination. """ answers_file = tmpdir.mkdir("tmp").join("answers_file") input_string = ['example', '--generate-answers-file', str(answers_file)] spec_manager = api.SpecManager() spec_manager.register_spec(spec_fixture) workspace_manager_fixture.activate(test_workspace.name) return_value = spec_manager.run_specs(args=input_string) assert return_value is None config = ConfigParser.ConfigParser() config.read(str(answers_file)) assert config.get("example", "foo-bar") == "default string" # verify playbook didn't run output_file = "output.example" inventory_dir = test_workspace.path assert not path.exists(path.join(inventory_dir, output_file))
def test_extra_vars(spec_fixture, workspace_manager_fixture, test_workspace, input_args, expected_output_dict, tmpdir): """Tests that "--extra-vars" are inserted to vars_dict. """ dry_output = tmpdir.mkdir("tmp").join("dry_output.yml") input_string = ['example' ] + input_args + ["-o", str(dry_output), "--dry-run"] spec_manager = api.SpecManager() spec_manager.register_spec(spec_fixture) workspace_manager_fixture.activate(test_workspace.name) return_value = spec_manager.run_specs(args=input_string) # dry run returns None assert return_value is None output_dict = yaml.load(dry_output.read()) # asserts expected_output_dict is subset of output assert subdict_in_dict(expected_output_dict, output_dict), "expected:{} actual:{}".format( expected_output_dict, output_dict)
def test_required_when(spec_fixture, workspace_manager_fixture, test_workspace, cli_args, should_pass): """Tests the 'required_when' mechanism :param spec_fixture: Fixtures which creates 'testing spec' (tests/example) :param workspace_manager_fixture: Fixture which sets the default workspace directory :param test_workspace: Fixture which creates temporary workspace directory :param cli_args: CLI arguments :param should_pass: Boolean value tells whether the test should pass or not :return: """ spec_manager = api.SpecManager() spec_manager.register_spec(spec_fixture) workspace_manager_fixture.activate(test_workspace.name) if should_pass: rc = spec_manager.run_specs(args=['example'] + cli_args.split()) assert rc == 0, "Execution failed, return code is: {}".format(rc) else: with pytest.raises(IRRequiredArgsMissingException): spec_manager.run_specs(args=['example'] + cli_args.split())
def test_extra_vars_with_file(spec_fixture, workspace_manager_fixture, test_workspace, input_args, file_dicts, expected_output_dict, tmpdir): """Tests that extra-vars supports yaml file with "@". """ tmp_dir = tmpdir.mkdir("tmp") dry_output = tmp_dir.join("dry_output.yml") for file_dict in file_dicts: tmp_file = tmp_dir.join(file_dict["filename"]) # write dict to tmp yaml file with open(str(tmp_file), 'wb') as yaml_file: yaml_file.write(yaml.safe_dump(file_dict["content"], default_flow_style=False)) # Inject full file path to command for i, arg in enumerate(input_args): input_args[i] = arg.replace(file_dict["filename"], str(tmp_file)) input_string = ['example'] + input_args + ["-o", str(dry_output), "--dry-run"] spec_manager = api.SpecManager() spec_manager.register_spec(spec_fixture) workspace_manager_fixture.activate(test_workspace.name) return_value = spec_manager.run_specs(args=input_string) # dry run returns None assert return_value is None output_dict = yaml.load(dry_output.read()) # asserts expected_output_dict is subset of output assert subdict_in_dict( expected_output_dict, output_dict), "expected:{} actual:{}".format(expected_output_dict, output_dict)
def test_nested_value_dry_run(spec_fixture, workspace_manager_fixture, test_workspace, input_value): """Verifies that --dry-run doesn't run playbook. """ dry = "--dry-run" in input_value if input_value: input_string = ['example', '--foo-bar'] + input_value else: input_string = ['example'] spec_manager = api.SpecManager() spec_manager.register_spec(spec_fixture) inventory_dir = test_workspace.path output_file = "output.example" assert not path.exists(path.join(inventory_dir, output_file)) workspace_manager_fixture.activate(test_workspace.name) return_value = spec_manager.run_specs(args=input_string) assert return_value is None if dry else return_value == 0 # assert that playbook didn't run if "--dry-run" requested assert not dry == path.exists(path.join(inventory_dir, output_file))
def test_bad_user_inventory(spec_fixture, workspace_manager_fixture, # noqa test_workspace, tmpdir): """Verify user-inventory is loaded and not default inventory. tests/example/main.yml playbook runs on all hosts. New inventory defines unreachable node. """ fake_inventory = tmpdir.mkdir("ir_dir").join("fake_hosts_file") fake_inventory.write("host2") test_workspace.inventory = str(fake_inventory) input_string = ['example', '--inventory', str(fake_inventory)] spec_manager = api.SpecManager() spec_manager.register_spec(spec_fixture) inventory_dir = test_workspace.path output_file = "output.example" assert not path.exists(path.join(inventory_dir, output_file)) workspace_manager_fixture.activate(test_workspace.name) return_value = spec_manager.run_specs(args=input_string) assert return_value
def test_env_answer_file(spec_fixture, tmpdir, workspace_manager_fixture, test_workspace, from_file, expected_output): """Verifies functionality of answer file containing environment variables Complex type parsing are out of scope of this test """ my_temp_dir = tmpdir.mkdir("tmp") dry_output = my_temp_dir.join("dry_output.yml") environ['MOCK_ENV_VAR'] = 'expected_value' input_string = ['example', "--dry-run", "-o", str(dry_output), '--from-file', from_file] spec_manager = api.SpecManager() spec_manager.register_spec(spec_fixture) workspace_manager_fixture.activate(test_workspace.name) return_value = spec_manager.run_specs(args=input_string) assert return_value is None assert path.exists(dry_output.strpath),\ "Output file doesn't exit: {}".format(dry_output.strpath) with open(dry_output.strpath) as fp: loaded_yml = yaml.safe_load(fp) assert loaded_yml['provision']['mock_key'] == expected_output
def main(): specs_manager = api.SpecManager() plugin = InfraredPlugin.from_config_file(os.getcwd(), 'infrared.cfg') specs_manager.register_spec(api.DefaultInfraredPluginSpec(plugin)) specs_manager.run_specs()