コード例 #1
0
def test_set_all_params():
    """ test that we can set all the parameters """
    p = PbVarsParser(full_conf_pb_path)
    b, m = p.get_build_and_metadata()
    b.playbook_path = "/somewhere.yaml"

    b.validate()
    m.validate()

    assert isinstance(b, Build)
    assert isinstance(m, ImageMetadata)

    assert b.base_image == "mona_lisa"
    assert b.layering
    assert not b.cache_tasks
    assert b.ansible_extra_args == "--some --args"
    assert b.build_volumes == ["/c:/d"]
    assert b.target_image == "funky-mona-lisa"

    assert m.env_vars == {"z": "value"}
    assert m.volumes == ["/a"]
    assert m.working_dir == "/workshop"
    assert m.labels == {"x": "y"}
    assert m.annotations == {"bohemian": "rhapsody"}
    assert m.cmd == "command -x -y z"
    assert m.user == "leonardo"
コード例 #2
0
def test_validation_err_ux2(path, message):
    """ Test that validation errors are useful """
    p = PbVarsParser(path)
    with pytest.raises(ABValidationError) as ex:
        p.get_build_and_metadata()
    s = str(ex.value)
    assert message in s
コード例 #3
0
def test_multiplay(caplog):
    set_logging()
    p = PbVarsParser(multiplay_path)
    b, m = p.get_build_and_metadata()

    assert b.target_image != "nope"
    assert "Variables are loaded only from the first play." == caplog.records[0].msg
    assert "no bender data found in the playbook" == caplog.records[1].msg
コード例 #4
0
ファイル: cli.py プロジェクト: tabulon-ext/ansible-bender
    def _build(self):
        pb_vars_p = PbVarsParser(self.args.playbook_path)
        build, metadata = pb_vars_p.get_build_and_metadata()
        build.metadata = metadata
        if self.args.workdir:
            metadata.working_dir = self.args.workdir
        if self.args.labels:
            for label in self.args.labels:
                err_msg = "Label variable {} doesn't seem to be " + \
                          "specified in format 'KEY=VALUE'.".format(label)
                k, v = split_once_or_fail_with(label, "=", err_msg)
                metadata.labels[k] = v
        if self.args.annotations:
            for ann in self.args.annotations:
                err_msg = "Annotation {} doesn't seem to be " + \
                          "specified in format 'KEY=VALUE'.".format(ann)
                k, v = split_once_or_fail_with(ann, "=", err_msg)
                metadata.annotations[k] = v
        if self.args.env_vars:
            for e_v in self.args.env_vars:
                err_msg = "Environment variable {} doesn't seem to be " + \
                    "specified in format 'KEY=VALUE'.".format(e_v)
                k, v = split_once_or_fail_with(e_v, "=", err_msg)
                metadata.env_vars[k] = v
        if self.args.cmd:
            metadata.cmd = self.args.cmd
        if self.args.entrypoint:
            metadata.entrypoint = self.args.entrypoint
        if self.args.user:
            metadata.user = self.args.user
        if self.args.ports:
            metadata.ports = self.args.ports
        if self.args.runtime_volumes:
            metadata.volumes = self.args.runtime_volumes

        build.playbook_path = self.args.playbook_path
        if self.args.build_volumes:
            build.build_volumes = self.args.build_volumes
        if self.args.base_image:
            build.base_image = self.args.base_image
        if self.args.target_image:
            build.target_image = self.args.target_image
        if self.args.builder:
            build.builder_name = self.args.builder
        if self.args.no_cache is not None:
            build.cache_tasks = not self.args.no_cache
        if self.args.squash:
            build.squash = self.args.squash
        if self.args.extra_buildah_from_args:
            build.buildah_from_extra_args = self.args.extra_buildah_from_args
        if self.args.extra_ansible_args:
            build.ansible_extra_args = self.args.extra_ansible_args
        if self.args.python_interpreter:
            build.python_interpreter = self.args.python_interpreter
        if self.args.build_entrypoint:
            build.build_entrypoint = self.args.build_entrypoint

        self.app.build(build)
コード例 #5
0
def test_expand_pb_vars():
    p = PbVarsParser(b_p_w_vars_path)
    data = p.expand_pb_vars()
    assert data["base_image"] == "docker.io/library/python:3-alpine"
    assert data["verbose_layer_names"]
    playbook_dir = os.path.dirname(b_p_w_vars_path)
    assert data["working_container"]["volumes"] == [f"{playbook_dir}:/src:Z"]
    assert data["target_image"]["name"] == "challet"
    assert data["target_image"]["labels"] == {"x": "y"}
    assert data["target_image"]["environment"] == {"asd": playbook_dir}
コード例 #6
0
def test_validation_err_ux():
    """ Test that validation errors are useful """
    p = PbVarsParser(basic_playbook_path)
    b, m = p.get_build_and_metadata()

    with pytest.raises(jsonschema.exceptions.ValidationError) as ex:
        b.validate()

    s = str(ex.value)

    assert "is not of type" in s
    assert "Failed validating 'type' in schema" in s
コード例 #7
0
def test_b_m_empty():
    """ test that build and metadata are 'empty' when there are no vars """
    p = PbVarsParser(basic_playbook_path)
    b, m = p.get_build_and_metadata()
    b.playbook_path = "/somewhere.yaml"

    b.base_image = "fedora:29"
    b.playbook_path = "/asd.yaml"
    b.target_image = "lolz"

    b.validate()
    m.validate()
    assert isinstance(b, Build)
    assert isinstance(m, ImageMetadata)
    assert b.cache_tasks is True
    assert b.layering is True
コード例 #8
0
def test_ansible_selinux_workaround(mock_r_t, mock_i_m, should_raise):
    mock_r_t()
    mock_i_m()
    p = PbVarsParser("")
    if should_raise:
        with pytest.raises(RuntimeError) as ex:
            p._check_selinux_iz_gud()
        assert "libselinux" in str(ex.value)
    else:
        p._check_selinux_iz_gud()
コード例 #9
0
def test_validation(di, error_message):
    p = PbVarsParser("")
    with pytest.raises(ABValidationError) as ex:
        p.process_pb_vars(di)
    assert error_message in str(ex)