Esempio n. 1
0
def test_deploy_file_w_inherited_permissions(
        dummy_src_file: Path, tgt_tmp_dir_w_dummy_files: Path,
        rel_tgt_file: str, inherited_mode: _OptMode,
        inherited_uid: _OptUID, inherited_gid: _OptGID) -> None:
    tgt_tmp_dir = tgt_tmp_dir_w_dummy_files
    tgt_file = tgt_tmp_dir.joinpath(rel_tgt_file)
    LOGGER.info("tgt_file: %s", tgt_file)

    change_file_mode_uid_gid(
        tgt_tmp_dir, inherited_mode, inherited_uid, inherited_gid)

    LOGGER.info("tgt_tmp_dir permissions: {%s}", format_file_permission(tgt_tmp_dir))

    expected_mode = inherited_mode or get_file_mode_simple(tgt_tmp_dir)
    expected_uid = inherited_uid or get_file_uid(tgt_tmp_dir)
    expected_gid = inherited_gid or get_file_gid(tgt_tmp_dir)

    call_shell_program(
        "nsf-file-deploy-w-inherited-permissions", dummy_src_file, tgt_file)

    LOGGER.info("tgt_file permissions: {%s}", format_file_permission(tgt_file))

    assert os.path.exists(tgt_file)
    assert expected_mode == get_file_mode_simple(tgt_file)
    assert expected_uid == get_file_uid(tgt_file)
    assert expected_gid == get_file_gid(tgt_file)
Esempio n. 2
0
def test_mkdir_w_inherited_permissions(
        tgt_tmp_dir_w_dummy_files: Path,
        rel_tgt_dir: str,
        inherited_mode: _OptMode, inherited_uid: _OptUID, inherited_gid: _OptGID
) -> None:
    tgt_tmp_dir = tgt_tmp_dir_w_dummy_files
    tgt_dir = tgt_tmp_dir.joinpath(rel_tgt_dir)
    LOGGER.info("tgt_dir: %s", tgt_dir)

    change_file_mode_uid_gid(
        tgt_tmp_dir, inherited_mode, inherited_uid, inherited_gid)

    LOGGER.info("tgt_tmp_dir permissions: {%s}", format_file_permission(tgt_tmp_dir))

    ref_dir = tgt_tmp_dir
    if os.path.exists(tgt_dir):
        # When the directory already exists, it should be taken as the
        # reference for permissions.
        ref_dir = tgt_dir

    expected_mode = inherited_mode or get_file_mode_simple(ref_dir)
    expected_uid = inherited_uid or get_file_uid(ref_dir)
    expected_gid = inherited_gid or get_file_gid(ref_dir)

    call_shell_program("nsf-dir-mk-w-inherited-permissions", tgt_dir)

    LOGGER.info("tgt_dir permissions: {%s}", format_file_permission(tgt_dir))

    assert os.path.exists(tgt_dir)
    assert expected_mode == get_file_mode_simple(tgt_dir)
    assert expected_uid == get_file_uid(tgt_dir)
    assert expected_gid == get_file_gid(tgt_dir)
Esempio n. 3
0
def test_change_owner(
        dummy_tgt_file: Path, owner_str: str, group_str: str,
        expected_uid: _OptUID, expected_gid: _OptGID):
    tgt_file = dummy_tgt_file
    previous_uid = get_file_uid(tgt_file)
    previous_gid = get_file_gid(tgt_file)
    if expected_uid is None:
        expected_uid = previous_uid
    if expected_gid is None:
        expected_gid = previous_gid

    LOGGER.info("tgt_file: %s", tgt_file)
    LOGGER.info("previous_uid: %s", previous_uid)
    LOGGER.info("previous_gid: %s", previous_gid)

    call_shell_program("nsf-file-chown", tgt_file, owner_str, group_str)

    new_uid = get_file_uid(tgt_file)
    new_gid = get_file_gid(tgt_file)

    LOGGER.info("new_uid: %s", new_uid)
    LOGGER.info("new_gid: %s", new_gid)

    assert expected_uid == new_uid
    assert expected_gid == new_gid
Esempio n. 4
0
def test_deploy_file_w_inherited_permissions_invalid(
        src_tmp_dir_w_dummy_files: Path, tgt_tmp_dir_w_dummy_files: Path,
        rel_src_file: str, rel_tgt_file: str, priviledged_ok: bool,
        inherited_mode: _OptMode,
        inherited_uid: _OptUID, inherited_gid: _OptGID) -> None:

    src_tmp_dir = src_tmp_dir_w_dummy_files
    tgt_tmp_dir = tgt_tmp_dir_w_dummy_files

    src_file = src_tmp_dir.joinpath(rel_src_file)
    tgt_file = tgt_tmp_dir.joinpath(rel_tgt_file)

    LOGGER.info("src_file: %s", src_file)
    LOGGER.info("tgt_file: %s", tgt_file)

    change_file_mode_uid_gid(
        tgt_tmp_dir, inherited_mode, inherited_uid, inherited_gid)

    LOGGER.info("tgt_tmp_dir permissions: {%s}", format_file_permission(tgt_tmp_dir))

    if priviledged_ok and from_nixos_test_machine():
        call_shell_program(
            "nsf-file-deploy-w-inherited-permissions", src_file, tgt_file)
        assert os.path.exists(tgt_file)
    else:
        with pytest.raises(subprocess.CalledProcessError) as e:
            call_shell_program(
                "nsf-file-deploy-w-inherited-permissions", src_file, tgt_file)

        assert 1 == e.value.returncode
Esempio n. 5
0
def test_change_mode(
        dummy_tgt_file: Path, mode_str: str, expected_mode: int) -> None:
    tgt_file = dummy_tgt_file
    LOGGER.info("tgt_file: %s", tgt_file)

    LOGGER.info("previous st_mode: %s", get_file_mode_str(tgt_file))

    call_shell_program("nsf-file-chmod", tgt_file, mode_str)

    assert expected_mode == get_file_mode_simple(tgt_file)

    LOGGER.info("new st_mode: %s", get_file_mode_str(tgt_file))
Esempio n. 6
0
def test_change_mode_invalid(
        dummy_tgt_file: Path, mode_str: str) -> None:
    tgt_file = dummy_tgt_file
    LOGGER.info("tgt_file: %s", tgt_file)

    previous_mode = get_file_mode_simple(tgt_file)
    expected_mode = previous_mode

    with pytest.raises(subprocess.CalledProcessError) as e:
        call_shell_program("nsf-file-chmod", tgt_file, mode_str)

    assert 1 == e.value.returncode
    assert expected_mode == get_file_mode_simple(tgt_file)
Esempio n. 7
0
def test_get_sh_lib_dir() -> None:
    lines = call_shell_program("pkg-nsf-data-deploy-tools-get-sh-lib-dir")
    assert lines
    dir_path = lines[0]
    assert os.path.exists(dir_path)

    sh_module_path = os.path.join(dir_path, "deploy-tools.sh")
    assert os.path.exists(sh_module_path)
Esempio n. 8
0
def test_change_owner_invalid(
        dummy_tgt_file: Path, owner_str: str, group_str: str) -> None:
    tgt_file = dummy_tgt_file
    LOGGER.info("tgt_file: %s", tgt_file)

    previous_uid = get_file_uid(tgt_file)
    previous_gid = get_file_gid(tgt_file)

    with pytest.raises(subprocess.CalledProcessError) as e:
        call_shell_program("nsf-file-chown", tgt_file, owner_str, group_str)

    assert 1 == e.value.returncode

    new_uid = get_file_uid(tgt_file)
    new_gid = get_file_gid(tgt_file)

    assert previous_uid == new_uid
    assert previous_gid == new_gid
def test_get_sh_lib_dir():
    lines = call_shell_program("pkg-nsf-secrets-deploy-tools-get-sh-lib-dir")
    assert lines
    dir_path = Path(lines[0])
    assert dir_path.exists()

    gpg_file_deploy_sh_module_path = dir_path.joinpath("pgp-file-deploy.sh")
    assert gpg_file_deploy_sh_module_path.exists()

    gpgnupg_deploy_sh_module_path = dir_path.joinpath("pgp-gnupg-keyring-deploy.sh")
    assert gpgnupg_deploy_sh_module_path.exists()
Esempio n. 10
0
def test_rm_file(
        tgt_tmp_dir_w_dummy_files: Path,
        rel_tgt_file: str,
        unprivileged_ok: bool, privileged_ok: bool) -> None:
    tgt_tmp_dir = tgt_tmp_dir_w_dummy_files
    tgt_file = tgt_tmp_dir.joinpath(rel_tgt_file)
    LOGGER.info("tgt_file: %s", tgt_file)

    if from_nixos_test_machine():
        expecting_sucess = privileged_ok
    else:
        expecting_sucess = unprivileged_ok

    if expecting_sucess:
        call_shell_program("nsf-file-rm", tgt_file)
        assert not os.path.exists(tgt_file)
    else:
        with pytest.raises(subprocess.CalledProcessError) as e:
            call_shell_program("nsf-file-rm", tgt_file)

        assert 1 == e.value.returncode
        assert os.path.exists(tgt_file)
Esempio n. 11
0
def test_call_shell_program_test_e_failure(temp_dir) -> None:
    with pytest.raises(subprocess.CalledProcessError):
        call_shell_program("test", "-e",
                           os.path.join(temp_dir, "does-not-exist.txt"))
Esempio n. 12
0
def test_call_shell_program_test_e_success(dummy_tmp_file) -> None:
    out = call_shell_program("test", "-e", dummy_tmp_file)
    assert [] == out
Esempio n. 13
0
def test_call_shell_program_echo():
    out = call_shell_program("echo", "Hello", "World")
    assert ["Hello World"] == out
Esempio n. 14
0
def test_call_shell_program_false():
    with pytest.raises(subprocess.CalledProcessError):
        call_shell_program("false")
Esempio n. 15
0
def test_call_shell_program_true():
    out = call_shell_program("true")
    assert [] == out