|
| 1 | +"""Unit tests for SystemAdministratorHandler version normalisation logic |
| 2 | +and the SystemAdministratorClientCLI do_update input validation. |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +from unittest.mock import MagicMock, patch |
| 7 | + |
| 8 | + |
| 9 | +# --------------------------------------------------------------------------- |
| 10 | +# Helpers – replicate the version-normalisation logic from |
| 11 | +# SystemAdministratorHandler.export_updateSoftware so we can test it |
| 12 | +# without standing up a full DIRAC service. |
| 13 | +# --------------------------------------------------------------------------- |
| 14 | + |
| 15 | + |
| 16 | +def _normalise_version(version): |
| 17 | + """Mirror the normalisation applied at the top of export_updateSoftware. |
| 18 | +
|
| 19 | + Returns the normalised version string, or raises ValueError if invalid. |
| 20 | + Mirrors the side-effects relevant to the directory-name derivation and |
| 21 | + the pip command construction. |
| 22 | + """ |
| 23 | + from packaging.version import Version, InvalidVersion |
| 24 | + |
| 25 | + version = version.strip() |
| 26 | + if not version: |
| 27 | + raise ValueError("No version specified") |
| 28 | + |
| 29 | + released_version = True |
| 30 | + isPrerelease = False |
| 31 | + |
| 32 | + if version.lower() in ["integration", "devel", "master", "main"]: |
| 33 | + released_version = False |
| 34 | + version = "DIRAC[server] @ git+https://github.com/DIRACGrid/DIRAC.git@integration" |
| 35 | + |
| 36 | + if released_version: |
| 37 | + try: |
| 38 | + parsed = Version(version) |
| 39 | + isPrerelease = parsed.is_prerelease |
| 40 | + version = f"v{parsed}" |
| 41 | + except InvalidVersion: |
| 42 | + if "https://" in version: |
| 43 | + released_version = False |
| 44 | + else: |
| 45 | + raise ValueError(f"Invalid version passed {version!r}") |
| 46 | + |
| 47 | + return version, released_version, isPrerelease |
| 48 | + |
| 49 | + |
| 50 | +def _directory_from_version(version, released_version): |
| 51 | + """Mirror the directory-name derivation in export_updateSoftware. |
| 52 | +
|
| 53 | + Split on the *first* "@" only (the pip package @ URL separator), strip |
| 54 | + whitespace, then drop any "#egg=..." fragment. |
| 55 | + """ |
| 56 | + if released_version: |
| 57 | + return version |
| 58 | + return version.split("@", 1)[1].strip().split("#")[0] |
| 59 | + |
| 60 | + |
| 61 | +# --------------------------------------------------------------------------- |
| 62 | +# Tests: version normalisation |
| 63 | +# --------------------------------------------------------------------------- |
| 64 | + |
| 65 | + |
| 66 | +class TestNormaliseVersion: |
| 67 | + def test_empty_string_raises(self): |
| 68 | + with pytest.raises(ValueError, match="No version specified"): |
| 69 | + _normalise_version("") |
| 70 | + |
| 71 | + def test_whitespace_only_raises(self): |
| 72 | + with pytest.raises(ValueError, match="No version specified"): |
| 73 | + _normalise_version(" ") |
| 74 | + |
| 75 | + def test_released_version(self): |
| 76 | + version, released, pre = _normalise_version("9.0.18") |
| 77 | + assert released is True |
| 78 | + assert pre is False |
| 79 | + assert version == "v9.0.18" |
| 80 | + |
| 81 | + def test_released_prerelease_version(self): |
| 82 | + version, released, pre = _normalise_version("9.0.18a1") |
| 83 | + assert released is True |
| 84 | + assert pre is True |
| 85 | + assert version == "v9.0.18a1" |
| 86 | + |
| 87 | + @pytest.mark.parametrize("keyword", ["integration", "devel", "master", "main"]) |
| 88 | + def test_special_keywords(self, keyword): |
| 89 | + version, released, pre = _normalise_version(keyword) |
| 90 | + assert released is False |
| 91 | + assert "DIRACGrid/DIRAC" in version |
| 92 | + assert "@integration" in version |
| 93 | + |
| 94 | + def test_git_url_without_spaces(self): |
| 95 | + raw = "DIRAC[server]@git+https://github.com/fstagni/DIRAC.git@test_branch" |
| 96 | + version, released, pre = _normalise_version(raw) |
| 97 | + assert released is False |
| 98 | + assert version == raw |
| 99 | + |
| 100 | + def test_git_url_with_spaces_around_at(self): |
| 101 | + """The CLI now sends the raw user input; leading/trailing spaces must be stripped.""" |
| 102 | + raw = " DIRAC[server] @ git+https://github.com/fstagni/DIRAC.git@test_branch " |
| 103 | + version, released, pre = _normalise_version(raw) |
| 104 | + assert released is False |
| 105 | + # Internal spaces around "@" are preserved (pip accepts them) |
| 106 | + assert version.strip() == raw.strip() |
| 107 | + |
| 108 | + def test_invalid_version_no_url_raises(self): |
| 109 | + with pytest.raises(ValueError, match="Invalid version passed"): |
| 110 | + _normalise_version("not-a-valid-version") |
| 111 | + |
| 112 | + |
| 113 | +# --------------------------------------------------------------------------- |
| 114 | +# Tests: directory derivation from version string |
| 115 | +# --------------------------------------------------------------------------- |
| 116 | + |
| 117 | + |
| 118 | +class TestDirectoryFromVersion: |
| 119 | + def test_released_version_uses_version_directly(self): |
| 120 | + d = _directory_from_version("v9.0.18", released_version=True) |
| 121 | + assert d == "v9.0.18" |
| 122 | + |
| 123 | + def test_git_url_without_spaces(self): |
| 124 | + """No spaces around '@' separator — branch name must be included.""" |
| 125 | + version = "DIRAC[server]@git+https://github.com/fstagni/DIRAC.git@test_branch" |
| 126 | + d = _directory_from_version(version, released_version=False) |
| 127 | + # Split on first "@" → "git+https://github.com/fstagni/DIRAC.git@test_branch" |
| 128 | + assert d == "git+https://github.com/fstagni/DIRAC.git@test_branch" |
| 129 | + |
| 130 | + def test_git_url_with_spaces_around_at_separator(self): |
| 131 | + """Spaces around the pip '@' separator must be stripped; branch part kept.""" |
| 132 | + # Simulate what the handler receives after version.strip() |
| 133 | + version = "DIRAC[server] @ git+https://github.com/fstagni/DIRAC.git@test_branch" |
| 134 | + d = _directory_from_version(version, released_version=False) |
| 135 | + assert d == "git+https://github.com/fstagni/DIRAC.git@test_branch" |
| 136 | + |
| 137 | + def test_git_url_with_hash_fragment(self): |
| 138 | + """#egg= fragment must be stripped from directory name.""" |
| 139 | + version = "DIRAC[server]@git+https://github.com/DIRACGrid/DIRAC.git@integration#egg=DIRAC" |
| 140 | + d = _directory_from_version(version, released_version=False) |
| 141 | + assert d == "git+https://github.com/DIRACGrid/DIRAC.git@integration" |
| 142 | + |
| 143 | + |
| 144 | +# --------------------------------------------------------------------------- |
| 145 | +# Tests: CLI do_update input validation |
| 146 | +# --------------------------------------------------------------------------- |
| 147 | + |
| 148 | + |
| 149 | +class TestDoUpdate: |
| 150 | + """Test SystemAdministratorClientCLI.do_update input validation.""" |
| 151 | + |
| 152 | + def _make_cli(self): |
| 153 | + from DIRAC.FrameworkSystem.Client.SystemAdministratorClientCLI import ( |
| 154 | + SystemAdministratorClientCLI, |
| 155 | + ) |
| 156 | + |
| 157 | + with patch("DIRAC.FrameworkSystem.Client.SystemAdministratorClientCLI.SystemAdministratorClient"): |
| 158 | + cli = SystemAdministratorClientCLI.__new__(SystemAdministratorClientCLI) |
| 159 | + cli.host = "localhost" |
| 160 | + cli.port = 9162 |
| 161 | + return cli |
| 162 | + |
| 163 | + def test_empty_args_prints_usage_and_returns(self): |
| 164 | + cli = self._make_cli() |
| 165 | + with ( |
| 166 | + patch( |
| 167 | + "DIRAC.FrameworkSystem.Client.SystemAdministratorClientCLI.SystemAdministratorClient" |
| 168 | + ) as mock_client_cls, |
| 169 | + patch("DIRAC.FrameworkSystem.Client.SystemAdministratorClientCLI.gLogger") as mock_logger, |
| 170 | + ): |
| 171 | + cli.do_update("") |
| 172 | + # Client must NOT be contacted |
| 173 | + mock_client_cls.assert_not_called() |
| 174 | + # Usage should be printed |
| 175 | + assert mock_logger.notice.called |
| 176 | + |
| 177 | + def test_whitespace_only_args_prints_usage_and_returns(self): |
| 178 | + cli = self._make_cli() |
| 179 | + with ( |
| 180 | + patch( |
| 181 | + "DIRAC.FrameworkSystem.Client.SystemAdministratorClientCLI.SystemAdministratorClient" |
| 182 | + ) as mock_client_cls, |
| 183 | + patch("DIRAC.FrameworkSystem.Client.SystemAdministratorClientCLI.gLogger"), |
| 184 | + ): |
| 185 | + cli.do_update(" ") |
| 186 | + mock_client_cls.assert_not_called() |
| 187 | + |
| 188 | + def test_valid_version_calls_client(self): |
| 189 | + cli = self._make_cli() |
| 190 | + with ( |
| 191 | + patch( |
| 192 | + "DIRAC.FrameworkSystem.Client.SystemAdministratorClientCLI.SystemAdministratorClient" |
| 193 | + ) as mock_client_cls, |
| 194 | + patch("DIRAC.FrameworkSystem.Client.SystemAdministratorClientCLI.gLogger"), |
| 195 | + ): |
| 196 | + mock_instance = MagicMock() |
| 197 | + mock_instance.updateSoftware.return_value = {"OK": True, "Value": None} |
| 198 | + mock_client_cls.return_value = mock_instance |
| 199 | + |
| 200 | + cli.do_update("9.0.18") |
| 201 | + |
| 202 | + mock_client_cls.assert_called_once_with(cli.host, cli.port) |
| 203 | + mock_instance.updateSoftware.assert_called_once_with("9.0.18", timeout=600) |
| 204 | + |
| 205 | + def test_git_url_with_spaces_passes_stripped_version(self): |
| 206 | + """Spaces are stripped from the outer edges but the version body is preserved.""" |
| 207 | + cli = self._make_cli() |
| 208 | + with ( |
| 209 | + patch( |
| 210 | + "DIRAC.FrameworkSystem.Client.SystemAdministratorClientCLI.SystemAdministratorClient" |
| 211 | + ) as mock_client_cls, |
| 212 | + patch("DIRAC.FrameworkSystem.Client.SystemAdministratorClientCLI.gLogger"), |
| 213 | + ): |
| 214 | + mock_instance = MagicMock() |
| 215 | + mock_instance.updateSoftware.return_value = {"OK": True, "Value": None} |
| 216 | + mock_client_cls.return_value = mock_instance |
| 217 | + |
| 218 | + raw = "DIRAC[server] @ git+https://github.com/fstagni/DIRAC.git@test_branch" |
| 219 | + cli.do_update(raw) |
| 220 | + |
| 221 | + mock_instance.updateSoftware.assert_called_once_with(raw, timeout=600) |
0 commit comments