Exemplo n.º 1
0
def test_rootbuilddir_for_all_platforms_copy_preserve_permissions(
        build_dir, mock_source):
    root = RootBuildDir(build_dir)
    root.init_build_dirs(["aarch64", "x86_64"], mock_source)

    def create_files(d: BuildDir):
        f1 = d.path / "400.txt"
        f1.write_text("Preserve permissions for a single file")
        f1.chmod(0o400)

        subdir = d.path / "some-dir"
        subdir.mkdir()

        f2 = subdir / "666.txt"
        f2.write_text("Preserve permissions for files in a directory")
        f2.chmod(0o666)

        return [f1, subdir]

    root.for_all_platforms_copy(create_files)

    def check_rwx_perms(filepath: Path, expected_perms: int):
        # Check the 3 rwx bytes (owner, group and others) of file permissions
        actual_perms = filepath.stat().st_mode & 0o777
        assert actual_perms == expected_perms, (
            f"{filepath.relative_to(root.path)}: expected permissions {oct(expected_perms)}, "
            f"actual {oct(actual_perms)}")

    for platform in ["aarch64", "x86_64"]:
        check_rwx_perms(root.path / platform / "400.txt", 0o400)
        check_rwx_perms(root.path / platform / "some-dir" / "666.txt", 0o666)
Exemplo n.º 2
0
def test_rootbuilddir_for_all_platforms_copy(build_dir, mock_source):
    root = RootBuildDir(build_dir)
    root.init_build_dirs(["x86_64", "s390x"], mock_source)
    results = root.for_all_platforms_copy(create_dockerfile)

    build_dir_s390x = build_dir.joinpath("s390x")
    build_dir_x86_64 = build_dir.joinpath("x86_64")
    # created files/dirs by action method
    expected_created = sorted([
        build_dir_s390x / DOCKERFILE_FILENAME,
        build_dir_s390x / "data" / "data.json",
        build_dir_s390x / "cachito-1",
    ])

    assert expected_created == sorted(results)

    # action method creates files for s390x (platforms are sorted)
    # and copies it to x86_64
    expected_all_copied = expected_created + [
        build_dir_s390x / "cachito-1" / "app",
        build_dir_s390x / "cachito-1" / "app" / "main.py",
        build_dir_x86_64 / "cachito-1" / "app",
        build_dir_x86_64 / "cachito-1" / "app" / "main.py",
        build_dir_x86_64 / DOCKERFILE_FILENAME,
        build_dir_x86_64 / "data" / "data.json",
        build_dir_x86_64 / "cachito-1",
    ]

    for f in expected_all_copied:
        assert f.is_absolute()
        assert f.exists()
Exemplo n.º 3
0
def test_rootbuilddir_for_all_platforms_copy_reflink(caplog, build_dir,
                                                     mock_source,
                                                     reflink_support):
    root = RootBuildDir(build_dir)

    # one time in copy_sources, another in for_all_platforms_copy
    flexmock(reflink).should_receive('supported_at').and_return(
        reflink_support).times(2)
    # 2 times in copy_source, it has to copy Dockerfile to both platform dirs
    # 3 times in for_all_platforms, method itself creates all for the first platform s390x
    # and then copies for x86_64 3 files
    flexmock(reflink).should_receive('reflink').and_return(True).times(
        5 if reflink_support else 0)
    flexmock(shutil).should_receive('copy2').and_return(True).times(
        0 if reflink_support else 5)

    root.init_build_dirs(["x86_64", "s390x"], mock_source)
    root.for_all_platforms_copy(create_dockerfile)

    method_name = shutil.copy2.__name__
    if reflink_support:
        method_name = 'reflink_copy'

    log_msg1 = f"copy method used for copy sources: {method_name}"
    log_msg2 = f"copy method used for all platforms copy: {method_name}"
    assert log_msg1 in caplog.text
    assert log_msg2 in caplog.text
Exemplo n.º 4
0
def test_rootbuilddir_get_any_platform(build_dir, mock_source):
    root = RootBuildDir(build_dir)
    root.init_build_dirs(["x86_64", "s390x"], mock_source)
    build_dir_1 = root.any_platform
    build_dir_2 = root.any_platform
    assert build_dir_1.path == build_dir_2.path
    assert build_dir_1.platform == build_dir_2.platform
Exemplo n.º 5
0
def test_rootbuilddir_for_all_platforms_copy_invalid_file_path(
    creation_func: FileCreationFunc, expected_err, build_dir, mock_source
):
    root = RootBuildDir(build_dir)
    root.init_build_dirs(["x86_64"], mock_source)
    with expected_err:
        root.for_all_platforms_copy(creation_func)
Exemplo n.º 6
0
def test_rootbuilddir_for_each_platform(build_dir, mock_source):
    root = RootBuildDir(build_dir)
    root.init_build_dirs(["x86_64", "s390x"], mock_source)
    results = root.for_each_platform(handle_platform)
    expected = {
        "x86_64": "handled x86_64",
        "s390x": {"reserved_build_id": 1000},
    }
    assert expected == results
Exemplo n.º 7
0
def test_rootbuilddir_for_all_platforms_copy(build_dir, mock_source):
    root = RootBuildDir(build_dir)
    root.init_build_dirs(["x86_64", "s390x"], mock_source)
    results = root.for_all_platforms_copy(create_dockerfile)

    build_dir_s390x = build_dir.joinpath("s390x")
    expected_created_files = sorted([
        build_dir_s390x / DOCKERFILE_FILENAME,
        build_dir_s390x / "data" / "data.json",
        build_dir_s390x / "cachito-1",
    ])

    assert expected_created_files == sorted(results)

    expected_all_copied_files = expected_created_files + [
        build_dir_s390x / "cachito-1" / "app",
        build_dir_s390x / "cachito-1" / "app" / "main.py",
    ]

    for f in expected_all_copied_files:
        assert f.is_absolute()
        assert f.exists()
Exemplo n.º 8
0
def test_rootbuilddir_for_each_platform_failure_from_action(
        build_dir, mock_source):
    root = RootBuildDir(build_dir)
    root.init_build_dirs(["x86_64", "s390x"], mock_source)
    with pytest.raises(ValueError, match="Error is raised"):
        root.for_each_platform(failure_action)