Skip to content

Commit

Permalink
ensure user exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Tianhao-Gu committed Sep 8, 2024
1 parent a03fabb commit 7e65823
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/jupyterhub_config/custom_spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ def _ensure_system_user(self, username: str, group: str = None):

if group:
# Check if the group exists, create if necessary
group_check = subprocess.run(['getent', 'group', group], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
group_check = subprocess.run(['getent', 'group', group],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if group_check.returncode != 0:
self.log.info(f'Group {group} does not exist, creating it.')
subprocess.run(['sudo', 'groupadd', group], check=True)
Expand All @@ -84,14 +85,19 @@ def _ensure_user_directory(self, user_dir: Path, username: str):
Ensure the user's home directory exists and is correctly owned and permissioned.
"""
if not user_dir.exists():
self.log.info(f'Creating user directory for {username}')
user_dir.mkdir(parents=True)

self.log.info(f'Getting user info for {username}')
try:
user_info = pwd.getpwnam(username)
except KeyError:
raise ValueError(f'System user {username} does not exist')
# Get the Jupyter user's UID and GID
user_info = pwd.getpwnam(username)
uid = user_info.pw_uid
gid = user_info.pw_gid

self.log.info(f'Creating user directory for {username}')
user_dir.mkdir(parents=True)

# Change the directory's ownership to the user
os.chown(user_dir, uid, gid)

Expand Down
26 changes: 25 additions & 1 deletion test/src/jupyterhub_config/custom_spawner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,34 @@ def test_ensure_user_directory_with_logging(mock_chmod, mock_chown, mock_mkdir,
mock_chown.assert_called_once_with(user_dir, 1000, 1000)
mock_chmod.assert_called_once_with(user_dir, 0o700)

assert f'Getting user info for {username}' in caplog.text
assert f'Creating user directory for {username}' in caplog.text


@patch('pwd.getpwnam')
@patch('pathlib.Path.exists')
@patch('pathlib.Path.mkdir')
def test_ensure_user_directory_user_not_found(mock_mkdir, mock_exists, mock_getpwnam, caplog):
username = 'nonexistentuser'
user_dir = Path('/home/nonexistentuser')

# Mock directory existence check (directory does not exist)
mock_exists.return_value = False

# Mock pwd.getpwnam to raise KeyError
mock_getpwnam.side_effect = KeyError

with caplog.at_level(logging.INFO):
with pytest.raises(ValueError, match=f'System user {username} does not exist'):
spawner = VirtualEnvSpawner()
spawner._ensure_user_directory(user_dir, username)

# Ensure that mkdir was not called because the user does not exist
mock_mkdir.assert_not_called()

assert f'Getting user info for {username}' in caplog.text


@patch('os.chown')
@patch('os.chmod')
@patch('pathlib.Path.mkdir')
Expand All @@ -154,7 +179,6 @@ def test_ensure_user_directory_reuse_existing(mock_exists, mock_mkdir, mock_chow
mock_chown.assert_not_called()
mock_chmod.assert_not_called()

# Assert that the correct log message was created
assert f'Reusing user directory for {username}' in caplog.text


Expand Down

0 comments on commit 7e65823

Please sign in to comment.