예제 #1
0
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)
예제 #2
0
def test_deprecation(
        spec_fixture,
        workspace_manager_fixture,  # noqa
        test_workspace,
        tmpdir):
    """Verify execution runs with deprecated option """

    my_temp_dir = tmpdir.mkdir("tmp")
    deprecated_output = my_temp_dir.join("deprecated_output.yml")

    deprecated_input_string = \
        ['example', '--deprecated-way', 'TestingValue', '--dry-run',
         '-o', str(deprecated_output)]

    output = my_temp_dir.join("output.yml")

    input_string = \
        ['example', '--new-way', 'TestingValue', '--dry-run',
         '-o', str(output)]

    spec_manager = api.SpecManager()
    spec_manager.register_spec(spec_fixture)

    workspace_manager_fixture.activate(test_workspace.name)
    spec_manager.run_specs(args=deprecated_input_string)
    spec_manager.run_specs(args=input_string)

    with open(deprecated_output.strpath) as fp:
        deprecated_yml = yaml.safe_load(fp)["provision"]

    with open(output.strpath) as fp:
        new_yml = yaml.safe_load(fp)["provision"]

    assert deprecated_yml.get('new', None).get('way', None) == 'TestingValue'
    assert new_yml.get('new', None).get('way', None) == 'TestingValue'
예제 #3
0
def test_output_with_NestedList_app(spec_fixture, tmpdir,
                                    workspace_manager_fixture, test_workspace,
                                    cli_args, from_file, expected_output):
    """Verifies the output file with NestedList complex type args
       from CLI & file
    """
    my_temp_dir = tmpdir.mkdir("tmp")
    dry_output = my_temp_dir.join("dry_output.yml")

    input_string = ['example', "--dry-run", "-o", str(dry_output)]

    if from_file:
        input_string += ['--from-file', from_file]

    if cli_args:
        input_string += cli_args.split()

    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']['nestedlist']['app'] == expected_output
예제 #4
0
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), '--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)
    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))
예제 #5
0
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), 'w+') 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)
예제 #6
0
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
예제 #7
0
def test_execute_main(
        spec_fixture,
        workspace_manager_fixture,  # noqa
        test_workspace):
    """Verify execution runs the main.yml playbook.

    Implicitly covers that vars dict is passed, since we know it will fail
    on task "fail if no vars dict" because test_test_execute_fail verifies
    failure is respected and output file isn't generated.

    Verifies that plugin roles are invoked properly.
    """

    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))
    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)

    assert return_value == 0
    assert path.exists(path.join(inventory_dir, output_file))
    assert path.exists(path.join(inventory_dir, "role_" +
                                 output_file)), "Plugin role not invoked"
예제 #8
0
def test_nested_KeyValueList_CLI(spec_fixture, workspace_manager_fixture,
                                 test_workspace, tmpdir, input_value,
                                 expected_output_dict):
    """Tests that CLI input of Complex type KeyValueList is nested in vars dict.

    Use "-o output_file" and evaluate output YAML file.
    """

    dry_output = tmpdir.mkdir("tmp").join("dry_output.yml")

    input_string = ['example'] + input_value

    input_string.extend(["-o", str(dry_output)])

    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)
예제 #9
0
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))
예제 #10
0
def test_nested_KeyValueList_CLI(spec_fixture,
                                 workspace_manager_fixture,
                                 test_workspace, tmpdir,
                                 input_value, expected_output_dict):
    """Tests that CLI input of Complex type KeyValueList is nested in vars dict.

    Use "-o output_file" and evaluate output YAML file.
    """

    dry_output = tmpdir.mkdir("tmp").join("dry_output.yml")

    input_string = ['example'] + input_value

    input_string.extend(["-o", str(dry_output)])

    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)
예제 #11
0
def test_min_max_corrupted_spec(spec_fixture, workspace_manager_fixture,
                                test_workspace, test_key, override_param,
                                override_value):
    """Tests the 'minimum' and 'maximum' support

    :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: test_key: key in group to test
    :param override_param: Spec param to override
    :param override_value: Spec param override value
    :return:
    """
    spec_manager = api.SpecManager()
    spec_manager.register_spec(spec_fixture)

    workspace_manager_fixture.activate(test_workspace.name)

    spec_groups = spec_fixture.specification.parser['example']['groups']
    for group in spec_groups:
        if group['title'] == 'Group F':
            options = group['options']
            # edit value-minmax
            options[test_key][override_param] = override_value

    with pytest.raises(IRInvalidMinMaxRangeException):
        spec_manager.run_specs(args=['example', '--' + test_key + '=150'])
예제 #12
0
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))
예제 #13
0
def test_output_with_IniType(spec_fixture, tmpdir,
                             workspace_manager_fixture, test_workspace,
                             cli_args, from_file, expected_output):
    """Verifies the output file with IniType complex type args from CLI & file
    """
    my_temp_dir = tmpdir.mkdir("tmp")
    dry_output = my_temp_dir.join("dry_output.yml")

    input_string = ['example', "--dry-run", "-o", str(dry_output)]

    if from_file:
        input_string += ['--from-file', from_file]

    if cli_args:
        input_string += cli_args.split()

    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']['iniopt'] == expected_output
예제 #14
0
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)
예제 #15
0
def test_deprecation(spec_fixture, workspace_manager_fixture,  # noqa
                               test_workspace, tmpdir):
    """Verify execution runs with deprecated option """

    my_temp_dir = tmpdir.mkdir("tmp")
    deprecated_output = my_temp_dir.join("deprecated_output.yml")

    deprecated_input_string = \
        ['example', '--deprecated-way', 'TestingValue', '--dry-run',
         '-o', str(deprecated_output)]

    output = my_temp_dir.join("output.yml")

    input_string = \
        ['example', '--new-way', 'TestingValue', '--dry-run',
         '-o', str(output)]

    spec_manager = api.SpecManager()
    spec_manager.register_spec(spec_fixture)

    workspace_manager_fixture.activate(test_workspace.name)
    spec_manager.run_specs(args=deprecated_input_string)
    spec_manager.run_specs(args=input_string)

    with open(deprecated_output.strpath) as fp:
        deprecated_yml = yaml.safe_load(fp)["provision"]

    with open(output.strpath) as fp:
        new_yml = yaml.safe_load(fp)["provision"]

    assert deprecated_yml.get('new', None).get('way', None) == 'TestingValue'
    assert new_yml.get('new', None).get('way', None) == 'TestingValue'
예제 #16
0
def test_execute_main(spec_fixture, workspace_manager_fixture,          # noqa
                      test_workspace):
    """Verify execution runs the main.yml playbook.

    Implicitly covers that vars dict is passed, since we know it will fail
    on task "fail if no vars dict" because test_test_execute_fail verifies
    failure is respected and output file isn't generated.

    Verifies that plugin roles are invoked properly.
    """

    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))
    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)

    assert return_value == 0
    assert path.exists(path.join(inventory_dir, output_file))
    assert path.exists(path.join(
        inventory_dir,
        "role_" + output_file)), "Plugin role not invoked"
예제 #17
0
def test_extra_vars(spec_fixture, workspace_manager_fixture, test_workspace):
    """ Verify the --extra-vars parameter is validated"""
    spec_manager = api.SpecManager()
    spec_manager.register_spec(spec_fixture)

    workspace_manager_fixture.activate(test_workspace.name)
    assert spec_manager.run_specs(args=['example', '-e', 'key=value']) == 0

    with pytest.raises(IRExtraVarsException):
        spec_manager.run_specs(args=['example', '-e', 'key'])
예제 #18
0
def test_extra_vars(spec_fixture, workspace_manager_fixture, test_workspace):
    """ Verify the --extra-vars parameter is validated"""
    spec_manager = api.SpecManager()
    spec_manager.register_spec(spec_fixture)

    workspace_manager_fixture.activate(test_workspace.name)
    assert spec_manager.run_specs(args=['example', '-e', 'key=value']) == 0

    with pytest.raises(IRExtraVarsException):
        spec_manager.run_specs(args=['example', '-e', 'key'])
예제 #19
0
def test_nested_value_CLI_with_answers_file(spec_fixture, tmpdir,
                                            workspace_manager_fixture,
                                            test_workspace, input_value):
    """Verfies answers file is loaded and that CLI overrides it.

    Use "-o output_file" and evaluate output YAML file.
    """
    mytempdir = tmpdir.mkdir("tmp")
    dry_output = mytempdir.join("dry_output.yml")

    config = configparser.ConfigParser()
    config.add_section('example')
    config.set('example', 'foo-bar', 'from_answers_file')

    answers_file = mytempdir.join("answers_file")

    with open(str(answers_file), 'wb') as configfile:
        config.write(configfile)

    input_string = ['example', '--from-file', str(answers_file)]
    if input_value:
        input_string += ['--foo-bar', input_value]
    input_string.extend(["-o", str(dry_output)])

    # if no input, check that default value is loaded
    expected_output_dict = {"foo": {"bar": input_value or 'from_answers_file'}}

    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)
예제 #20
0
def test_env_answer_file_negative(spec_fixture, tmpdir,  # noqa
                                  workspace_manager_fixture, test_workspace):
    """Verifies functionality of answer file containing environment variables
       Negative testing - expects IRAnswersFileEnvVarNotDefined to be raised
    """
    # Environment variable may be set due to previous tests, undefine it
    del environ['MOCK_ENV_VAR']
    input_string = ['example', "--dry-run", '--from-file',
                    'tests/example/files/answers_file_env_vars.ini']

    spec_manager = api.SpecManager()
    spec_manager.register_spec(spec_fixture)

    workspace_manager_fixture.activate(test_workspace.name)
    from infrared.core.utils import exceptions
    with pytest.raises(exceptions.IRAnswersFileEnvVarNotDefined):
        spec_manager.run_specs(args=input_string)
예제 #21
0
def test_fake_inventory(spec_fixture, workspace_manager_fixture,          # noqa
                        test_workspace):
    """Verify "--inventory" updates workspace's inventory. """

    input_string = ['example', '--inventory', 'fake']

    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)
    with pytest.raises(IOError) as exc:
        spec_manager.run_specs(args=input_string)
    assert exc.value.message == "File not found: fake"
예제 #22
0
def test_nested_value_CLI_with_answers_file(spec_fixture, tmpdir,
                                            workspace_manager_fixture,
                                            test_workspace, input_value):
    """Verfies answers file is loaded and that CLI overrides it.

    Use "-o output_file" and evaluate output YAML file.
    """
    mytempdir = tmpdir.mkdir("tmp")
    dry_output = mytempdir.join("dry_output.yml")

    config = ConfigParser.ConfigParser()
    config.add_section('example')
    config.set('example', 'foo-bar', 'from_answers_file')

    answers_file = mytempdir.join("answers_file")

    with open(str(answers_file), 'wb') as configfile:
        config.write(configfile)

    input_string = ['example', '--from-file', str(answers_file)]
    if input_value:
        input_string += ['--foo-bar', input_value]
    input_string.extend(["-o", str(dry_output)])

    # if no input, check that default value is loaded
    expected_output_dict = {"foo": {"bar": input_value or 'from_answers_file'}}

    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)
예제 #23
0
def test_fake_inventory(spec_fixture, workspace_manager_fixture,          # noqa
                        test_workspace):
    """Verify "--inventory" updates workspace's inventory. """

    input_string = ['example', '--inventory', 'fake']

    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)
    with pytest.raises(IOError) as exc:
        spec_manager.run_specs(args=input_string)
    assert exc.value.message == "File not found: fake"
예제 #24
0
def test_fake_inventory(spec_fixture, workspace_manager_fixture,          # noqa
                        test_workspace):
    """Verify "--inventory" updates workspace's inventory. """

    input_string = ['example', '--inventory', 'fake']

    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)
    from infrared.core.utils import exceptions
    with pytest.raises(exceptions.IRFileNotFoundException) as exc:
        spec_manager.run_specs(args=input_string)
    assert "fake" in exc.value.message
예제 #25
0
def test_nested_KeyValueList_negative(
        spec_fixture, workspace_manager_fixture, test_workspace, bad_input):
    """Tests that bad input for KeyValueList raises exception. """

    input_string = list(('example', "--dry-run", "--dictionary-val"))
    input_string.append(bad_input)

    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)

    from infrared.core.utils import exceptions
    with pytest.raises(exceptions.IRKeyValueListException):
        spec_manager.run_specs(args=input_string)
예제 #26
0
def test_nested_KeyValueList_negative(spec_fixture, workspace_manager_fixture,
                                      test_workspace, bad_input):
    """Tests that bad input for KeyValueList raises exception. """

    input_string = list(('example', "--dry-run", "--dictionary-val"))
    input_string.append(bad_input)

    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)

    from infrared.core.utils import exceptions
    with pytest.raises(exceptions.IRKeyValueListException):
        spec_manager.run_specs(args=input_string)
예제 #27
0
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))
예제 #28
0
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))
예제 #29
0
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))
예제 #30
0
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))
예제 #31
0
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
예제 #32
0
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.safe_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)
예제 #33
0
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))
예제 #34
0
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())
예제 #35
0
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())
예제 #36
0
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)
예제 #37
0
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
예제 #38
0
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