|
| 1 | +"""`pytest` tests for `benchcab.py`.""" |
| 2 | +import re |
| 3 | +from contextlib import nullcontext as does_not_raise |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from benchcab.benchcab import Benchcab |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture(scope="module", autouse=True) |
| 12 | +def _set_user_projects(): |
| 13 | + with mock.patch("grp.getgrgid") as mocked_getgrid, mock.patch( |
| 14 | + "os.getgroups" |
| 15 | + ) as mocked_groups: |
| 16 | + type(mocked_getgrid.return_value).gr_name = mock.PropertyMock( |
| 17 | + return_value="hh5" |
| 18 | + ) |
| 19 | + mocked_groups.return_value = [1] |
| 20 | + yield |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture(scope="module", params=["hh5", "invalid_project_name"]) |
| 24 | +def config_project(request): |
| 25 | + """Get config project name.""" |
| 26 | + return request.param |
| 27 | + |
| 28 | + |
| 29 | +# Error message if config project name cannot be resolved. |
| 30 | +no_project_name_msg = re.escape( |
| 31 | + """Couldn't resolve project: check 'project' in config.yaml |
| 32 | + and/or $PROJECT set in ~/.config/gadi-login.conf |
| 33 | + """ |
| 34 | +) |
| 35 | + |
| 36 | +# For testing whether user is member of necessary projects to run benchcab, we need to simulate the environment of Gadi |
| 37 | +# TODO: Simulate Gadi environment for running tests for validating environment |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.skip() |
| 41 | +@pytest.mark.parametrize( |
| 42 | + ("config_project", "pytest_error"), |
| 43 | + [ |
| 44 | + ("hh5", does_not_raise()), |
| 45 | + (None, pytest.raises(AttributeError, match=no_project_name_msg)), |
| 46 | + ], |
| 47 | +) |
| 48 | +def test_project_name(config_project, pytest_error): |
| 49 | + """Tests whether config project name is suitable to run in Gadi environment.""" |
| 50 | + app = Benchcab(benchcab_exe_path=None) |
| 51 | + with pytest_error: |
| 52 | + app._validate_environment(project=config_project, modules=[]) |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.skip() |
| 56 | +@pytest.mark.parametrize( |
| 57 | + ("config_project", "pytest_error"), |
| 58 | + [ |
| 59 | + ("hh5", does_not_raise()), |
| 60 | + ("invalid_project_name", pytest.raises(PermissionError)), |
| 61 | + ], |
| 62 | +) |
| 63 | +def test_user_project_group(config_project, pytest_error): |
| 64 | + """Test _validate_environment for if current user's groups does not contain the project name.""" |
| 65 | + app = Benchcab(benchcab_exe_path=None) |
| 66 | + with pytest_error: |
| 67 | + app._validate_environment(project=config_project, modules=[]) |
0 commit comments