コード例 #1
0
ファイル: test_api.py プロジェクト: sthagen/ansible-bender
def test_multiplay(build, application):
    im = "multiplay"
    build.playbook_path = multiplay_path
    build.target_image = im
    application.build(build)
    try:
        build = application.db.get_build(build.build_id)
        podman_run_cmd(im, ["ls", "/queen"])  # the file has to be in there
        assert len(build.layers) == 3
    finally:
        run_cmd(["buildah", "rmi", im], ignore_status=True, print_output=True)
コード例 #2
0
ファイル: test_api.py プロジェクト: sthagen/ansible-bender
def test_pb_with_role(build, application):
    im = "image-built-with-role"
    build.playbook_path = role_pb_path
    build.target_image = im
    os.environ["ANSIBLE_ROLES_PATH"] = roles_dir
    application.build(build)
    try:
        build = application.db.get_build(build.build_id)
        podman_run_cmd(im, ["ls", "/officer"])  # the file has to be in there
        # base image + 2 from roles: [] + 2 from import_role
        # + 3 from include_role (include_role is a task)
        assert len(build.layers) == 8
    finally:
        run_cmd(["buildah", "rmi", im], ignore_status=True, print_output=True)
コード例 #3
0
def test_tback_in_callback(tmpdir):
    # this code may look really crazy: we test a real-world scenario here
    # it may happen that something goes wrong while the callback plugin is running
    # in such case, the build should be aborted
    # that's exactly what we do here: we simulate buildah failure
    im = "registry.example.com/ab-will-fail-" + random_word(12) + ":oldest"
    new_path = f"{tmpdir}:" + os.environ["PATH"]
    new_buildah_path = os.path.join(str(tmpdir), "buildah")
    with open(new_buildah_path, "w") as fd:
        fd.write(MAGIKO.format(tmpdir=str(tmpdir)))
    os.chmod(new_buildah_path, 0o755)

    # make sure our new buildah gets called
    good_env = os.environ
    new_env = os.environ.copy()
    new_env["PATH"] = new_path
    new_env["ANSIBLE_STDOUT_CALLBACK"] = "debug"
    os.environ = new_env
    # assert subprocess.call(["env", f"PATH={new_path}", "buildah", "run"]) == 42

    try:
        cmd = [
            "--debug", "build", "--extra-ansible-args=-vvvvvv",
            basic_playbook_path, base_image, im
        ]
        with pytest.raises(subprocess.CalledProcessError):
            ab(cmd, str(tmpdir), env=new_env)

        image_name_regex = "%s+[-]+[0-9]+[-]+[0-9]+-failed" % (im)
        try:
            cmd = ["inspect", "--json"]
            ab_inspect_data = json.loads(
                ab(cmd, str(tmpdir), return_output=True))
            assert ab_inspect_data["state"] == "failed"
            assert re.match(image_name_regex, ab_inspect_data["target_image"])
            assert len(ab_inspect_data["layers"]) == 2
            with pytest.raises(subprocess.CalledProcessError) as ex:
                podman_run_cmd(ab_inspect_data["target_image"], ["ls", "/fun"],
                               return_output=True)
            assert "No such file or directory" in ex.value.output
        finally:
            subprocess.call(
                ["buildah", "rmi", ab_inspect_data["target_image"]])
    finally:
        os.environ = good_env
コード例 #4
0
def test_build_basic_image_with_env_vars(tmpdir, target_image):
    a_b = "A=B"
    x_y = "X=Y"
    cmd = ["build", "-e", a_b, x_y, "--",
           basic_playbook_path, base_image, target_image]
    ab(cmd, str(tmpdir))
    out = inspect_buildah_resource("image", target_image)
    assert a_b in out["OCIv1"]["config"]["Env"]
    assert x_y in out["OCIv1"]["config"]["Env"]
    e = podman_run_cmd(target_image, ["env"], return_output=True)
    assert a_b in e
    assert x_y in e