generated from kbase/kbase-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from kbase/dev_jupyterhub
add create system user
- Loading branch information
Showing
2 changed files
with
155 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,101 @@ | ||
from jupyterhub_config.custom_spawner import * | ||
import logging | ||
import subprocess | ||
import unittest | ||
from subprocess import CalledProcessError | ||
from unittest.mock import patch, MagicMock | ||
|
||
import pytest | ||
|
||
from jupyterhub_config.custom_spawner import VirtualEnvSpawner | ||
|
||
|
||
# Test when the user already exists | ||
@patch('subprocess.run') | ||
def test_ensure_system_user_already_exists(mock_run, caplog): | ||
with caplog.at_level(logging.INFO): | ||
# Mock 'id' command to simulate user already exists | ||
mock_run.return_value.returncode = 0 | ||
|
||
spawner = VirtualEnvSpawner() | ||
username = 'testuser' | ||
spawner._ensure_system_user(username) | ||
|
||
mock_run.assert_called_once_with(['id', username], stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
|
||
assert f'User {username} already exists' in caplog.text | ||
|
||
|
||
# Test when the group and user need to be created | ||
@patch('subprocess.run') | ||
def test_ensure_system_user_create_group_and_user(mock_run, caplog): | ||
with caplog.at_level(logging.INFO): | ||
# Define side_effect to simulate user does not exist, group does not exist, and then successful creation | ||
mock_run.side_effect = [ | ||
MagicMock(returncode=1), # 'id' command: User does not exist | ||
MagicMock(returncode=2), # 'getent' command: Group does not exist | ||
MagicMock(returncode=0), # 'groupadd' command: Group created successfully | ||
MagicMock(returncode=0) # 'useradd' command: User created successfully | ||
] | ||
|
||
spawner = VirtualEnvSpawner() | ||
username = 'testuser' | ||
group = 'testgroup' | ||
spawner._ensure_system_user(username, group) | ||
|
||
expected_calls = [ | ||
unittest.mock.call(['id', username], stdout=subprocess.PIPE, stderr=subprocess.PIPE), | ||
unittest.mock.call(['getent', 'group', group], stdout=subprocess.PIPE, stderr=subprocess.PIPE), | ||
unittest.mock.call(['sudo', 'groupadd', group], check=True), | ||
unittest.mock.call(['sudo', 'useradd', '-r', '-g', group, username], check=True) | ||
] | ||
|
||
# Check that the expected calls were made in order | ||
mock_run.assert_has_calls(expected_calls, any_order=False) | ||
|
||
assert f'Creating system user: {username}' in caplog.text | ||
assert f'Group {group} does not exist, creating it.' in caplog.text | ||
|
||
|
||
# Test when the user is created without a group | ||
@patch('subprocess.run') | ||
def test_ensure_system_user_create_user_without_group(mock_run, caplog): | ||
with caplog.at_level(logging.INFO): | ||
# Mock the 'id' command to simulate user does not exist | ||
mock_run.side_effect = [ | ||
MagicMock(returncode=1), # User does not exist | ||
MagicMock(returncode=0) # User created successfully | ||
] | ||
|
||
spawner = VirtualEnvSpawner() | ||
username = 'testuser' | ||
spawner._ensure_system_user(username) | ||
|
||
assert f'Creating system user: {username}' in caplog.text | ||
expected_calls = [ | ||
unittest.mock.call(['id', username], stdout=subprocess.PIPE, stderr=subprocess.PIPE), | ||
unittest.mock.call(['sudo', 'useradd', '-r', username], check=True) | ||
] | ||
|
||
mock_run.assert_has_calls(expected_calls, any_order=False) | ||
|
||
|
||
# Test subprocess.CalledProcessError is handled correctly | ||
@patch('subprocess.run') | ||
def test_ensure_system_user_error(mock_run): | ||
# Mock the 'id' command to simulate user does not exist | ||
# Mock 'useradd' command to raise CalledProcessError | ||
mock_run.side_effect = [ | ||
MagicMock(returncode=1), | ||
CalledProcessError(1, 'useradd') | ||
] | ||
|
||
spawner = VirtualEnvSpawner() | ||
with pytest.raises(ValueError, match="Failed to create system user"): | ||
spawner._ensure_system_user('testuser') | ||
|
||
expected_calls = [ | ||
unittest.mock.call(['id', 'testuser'], stdout=subprocess.PIPE, stderr=subprocess.PIPE), | ||
unittest.mock.call(['sudo', 'useradd', '-r', 'testuser'], check=True) | ||
] | ||
|
||
mock_run.assert_has_calls(expected_calls, any_order=False) |