Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ckunki committed Mar 26, 2024
1 parent a308d09 commit 130308c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,17 @@ def id(self):
self._id = pwd.getpwnam(self.name).pw_uid
return self._id

def chown_rec(self, path: Path):
uid = self.id
gid = self.group.id
os.chown(path, uid, gid)
for root, dirs, files in os.walk(path):
root = Path(root)
for name in files:
os.chown(root / name, uid, gid)
for name in dirs:
os.chown(root / name, uid, gid)

def enable_group_access(self, path: Path):
file = FileInspector(path)
if file.is_group_accessible():
Expand Down Expand Up @@ -310,6 +321,8 @@ def main():
args = arg_parser().parse_args()
user = User(args.user, Group(args.group), Group(args.docker_group))
if user.is_specified:
if args.notebooks:
user.chown_rec(args.notebooks)
user.enable_group_access(Path("/var/run/docker.sock")).switch_to()
if args.notebook_defaults and args.notebooks:
copy_rec(
Expand Down
25 changes: 25 additions & 0 deletions test/unit/entrypoint/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@
def test_no_args(mocker):
mocker.patch("sys.argv", ["app"])
mocker.patch(entrypoint_method("sleep_infinity"))
mocker.patch(entrypoint_method("start_jupyter_server"))
mocker.patch(entrypoint_method("copy_rec"))
user = create_autospec(entrypoint.User, is_specified=False)
mocker.patch(entrypoint_method("User"), return_value=user)
entrypoint.main()
assert entrypoint.sleep_infinity.called
assert not user.enable_group_access.called
assert not user.chown_rec.called
assert not entrypoint.copy_rec.called
assert not entrypoint.start_jupyter_server.called


def test_user_arg(mocker):
Expand All @@ -37,6 +45,23 @@ def test_user_arg(mocker):
assert user.switch_to.called


def test_chown_rec_args(mocker):
dir = "/path/to/final/notebooks"
mocker.patch("sys.argv", [
"app",
"--user", "jennifer",
"--group", "users",
"--docker-group", "docker",
"--notebooks", dir,
])
user = create_autospec(entrypoint.User)
mocker.patch(entrypoint_method("User"), return_value=user)
mocker.patch(entrypoint_method("sleep_infinity"))
entrypoint.main()
assert user.chown_rec.called
assert user.chown_rec.call_args == mocker.call(Path(dir))


@pytest.mark.parametrize("warning_as_error", [True, False])
def test_copy_args_valid(mocker, warning_as_error ):
extra_args = ["--warning-as-error"] if warning_as_error else []
Expand Down
17 changes: 17 additions & 0 deletions test/unit/entrypoint/test_user_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ def test_uid(mocker, user):
and pwd.getpwnam.call_args == mocker.call("jennifer")


def test_chown_rec(mocker, user, tmp_path):
child = tmp_path / "child"
sub = tmp_path / "sub"
grand_child = sub / "grand_child"
child.touch()
sub.mkdir()
grand_child.touch()
mocker.patch("os.chown")
passwd_struct = MagicMock(pw_uid=444)
mocker.patch("pwd.getpwnam", return_value=passwd_struct)
user.chown_rec(tmp_path)
expected = [ mocker.call(f, user.id, user.group.id) for f in (
tmp_path, child, sub, grand_child,
)]
assert expected == os.chown.call_args_list


def test_enable_file_absent(mocker, user):
mocker.patch(entrypoint_method("GroupAccess"))
user.enable_group_access(Path("/non/existing/path"))
Expand Down

0 comments on commit 130308c

Please sign in to comment.