|
18 | 18 | import pytest
|
19 | 19 |
|
20 | 20 | from pylint import run_pylint, run_pyreverse, run_symilar
|
| 21 | +from pylint.lint.run import _query_cpu |
21 | 22 | from pylint.testutils import GenericTestReporter as Reporter
|
22 | 23 | from pylint.testutils._run import _Run as Run
|
23 | 24 | from pylint.testutils.utils import _test_cwd
|
@@ -100,3 +101,90 @@ def _mock_path(*args: str, **kwargs: Any) -> pathlib.Path:
|
100 | 101 | with patch("pylint.lint.run.Path", _mock_path):
|
101 | 102 | Run(testargs, reporter=Reporter())
|
102 | 103 | assert err.value.code == 0
|
| 104 | + |
| 105 | + |
| 106 | +@pytest.mark.parametrize( |
| 107 | + "contents", |
| 108 | + [ |
| 109 | + "1 2", |
| 110 | + "max 100000", |
| 111 | + ], |
| 112 | +) |
| 113 | +def test_pylint_run_jobs_equal_zero_dont_crash_with_cgroupv2( |
| 114 | + tmp_path: pathlib.Path, |
| 115 | + contents: str, |
| 116 | +) -> None: |
| 117 | + """Check that the pylint runner does not crash if `pylint.lint.run._query_cpu` |
| 118 | + determines only a fraction of a CPU core to be available. |
| 119 | + """ |
| 120 | + builtin_open = open |
| 121 | + |
| 122 | + def _mock_open(*args: Any, **kwargs: Any) -> BufferedReader: |
| 123 | + if args[0] == "/sys/fs/cgroup/cpu.max": |
| 124 | + return mock_open(read_data=contents)(*args, **kwargs) # type: ignore[no-any-return] |
| 125 | + return builtin_open(*args, **kwargs) # type: ignore[no-any-return] |
| 126 | + |
| 127 | + pathlib_path = pathlib.Path |
| 128 | + |
| 129 | + def _mock_path(*args: str, **kwargs: Any) -> pathlib.Path: |
| 130 | + if args[0] == "/sys/fs/cgroup/cpu/cpu.shares": |
| 131 | + return MagicMock(is_file=lambda: False) |
| 132 | + if args[0] == "/sys/fs/cgroup/cpu/cfs_quota_us": |
| 133 | + return MagicMock(is_file=lambda: False) |
| 134 | + if args[0] == "/sys/fs/cgroup/cpu.max": |
| 135 | + return MagicMock(is_file=lambda: True) |
| 136 | + return pathlib_path(*args, **kwargs) |
| 137 | + |
| 138 | + filepath = os.path.abspath(__file__) |
| 139 | + testargs = [filepath, "--jobs=0"] |
| 140 | + with _test_cwd(tmp_path): |
| 141 | + with pytest.raises(SystemExit) as err: |
| 142 | + with patch("builtins.open", _mock_open): |
| 143 | + with patch("pylint.lint.run.Path", _mock_path): |
| 144 | + Run(testargs, reporter=Reporter()) |
| 145 | + assert err.value.code == 0 |
| 146 | + |
| 147 | + |
| 148 | +@pytest.mark.parametrize( |
| 149 | + "contents,expected", |
| 150 | + [ |
| 151 | + ("50000 100000", 1), |
| 152 | + ("100000 100000", 1), |
| 153 | + ("200000 100000", 2), |
| 154 | + ("299999 100000", 2), |
| 155 | + ("300000 100000", 3), |
| 156 | + # Unconstrained cgroup |
| 157 | + ("max 100000", None), |
| 158 | + ], |
| 159 | +) |
| 160 | +def test_query_cpu_cgroupv2( |
| 161 | + tmp_path: pathlib.Path, |
| 162 | + contents: str, |
| 163 | + expected: int, |
| 164 | +) -> None: |
| 165 | + """Check that `pylint.lint.run._query_cpu` generates realistic values in cgroupsv2 |
| 166 | + systems. |
| 167 | + """ |
| 168 | + builtin_open = open |
| 169 | + |
| 170 | + def _mock_open(*args: Any, **kwargs: Any) -> BufferedReader: |
| 171 | + if args[0] == "/sys/fs/cgroup/cpu.max": |
| 172 | + return mock_open(read_data=contents)(*args, **kwargs) # type: ignore[no-any-return] |
| 173 | + return builtin_open(*args, **kwargs) # type: ignore[no-any-return] |
| 174 | + |
| 175 | + pathlib_path = pathlib.Path |
| 176 | + |
| 177 | + def _mock_path(*args: str, **kwargs: Any) -> pathlib.Path: |
| 178 | + if args[0] == "/sys/fs/cgroup/cpu/cpu.shares": |
| 179 | + return MagicMock(is_file=lambda: False) |
| 180 | + if args[0] == "/sys/fs/cgroup/cpu/cfs_quota_us": |
| 181 | + return MagicMock(is_file=lambda: False) |
| 182 | + if args[0] == "/sys/fs/cgroup/cpu.max": |
| 183 | + return MagicMock(is_file=lambda: True) |
| 184 | + return pathlib_path(*args, **kwargs) |
| 185 | + |
| 186 | + with _test_cwd(tmp_path): |
| 187 | + with patch("builtins.open", _mock_open): |
| 188 | + with patch("pylint.lint.run.Path", _mock_path): |
| 189 | + cpus = _query_cpu() |
| 190 | + assert cpus == expected |
0 commit comments