From 8e574ff2611094a54c6f3bceede1b6c17b8c18d7 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Mon, 18 Dec 2023 14:28:38 +0100 Subject: [PATCH] fix some win tests + upgrade BSD* vmactions --- .github/workflows/bsd.yml | 14 +++++++------- .github/workflows/build.yml | 1 - psutil/tests/__init__.py | 26 ++++++++++++++++++++++++++ psutil/tests/test_contracts.py | 14 ++++++++------ psutil/tests/test_process.py | 13 ++++++++----- scripts/internal/winmake.py | 5 +++-- 6 files changed, 52 insertions(+), 21 deletions(-) diff --git a/.github/workflows/bsd.yml b/.github/workflows/bsd.yml index efae0fc9b..3af8d2371 100644 --- a/.github/workflows/bsd.yml +++ b/.github/workflows/bsd.yml @@ -11,11 +11,11 @@ concurrency: cancel-in-progress: true jobs: freebsd: - runs-on: macos-12 + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v4 - name: Run tests - uses: vmactions/freebsd-vm@v0 + uses: vmactions/freebsd-vm@v1 with: usesh: true prepare: | @@ -28,11 +28,11 @@ jobs: make test make test-memleaks openbsd: - runs-on: macos-12 + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v4 - name: Run tests - uses: vmactions/openbsd-vm@v0 + uses: vmactions/openbsd-vm@v1 with: usesh: true prepare: | @@ -46,16 +46,16 @@ jobs: make test make test-memleaks netbsd: - runs-on: macos-12 + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v4 - name: Run tests - uses: vmactions/netbsd-vm@v0 + uses: vmactions/netbsd-vm@v1 with: usesh: true prepare: | set -e - pkg_add -v pkgin + /usr/sbin/pkg_add -v pkgin pkgin update pkgin -y install python311-* py311-setuptools-* gcc12-* run: | diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index df248a630..f600b9d16 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -33,7 +33,6 @@ jobs: archs: "AMD64" - os: windows-2019 archs: "x86" - steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v4 diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index 8e552e9ab..f28e13bc4 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -96,6 +96,7 @@ 'unittest', 'skip_on_access_denied', 'skip_on_not_implemented', 'retry_on_failure', 'TestMemoryLeak', 'PsutilTestCase', 'process_namespace', 'system_namespace', 'print_sysinfo', + 'is_win_secure_system_proc', # fs utils 'chdir', 'safe_rmpath', 'create_exe', 'get_testfn', # os @@ -1295,6 +1296,31 @@ def print_sysinfo(): print("=" * 70, file=sys.stderr) # NOQA sys.stdout.flush() + if WINDOWS: + os.system("tasklist") + elif which("ps"): + os.system("ps aux") + print("=" * 70, file=sys.stderr) # NOQA + sys.stdout.flush() + + +def is_win_secure_system_proc(pid): + # see: https://github.com/giampaolo/psutil/issues/2338 + @memoize + def get_procs(): + ret = {} + out = sh("tasklist.exe /NH /FO csv") + for line in out.splitlines()[1:]: + bits = [x.replace('"', "") for x in line.split(",")] + name, pid = bits[0], int(bits[1]) + ret[pid] = name + return ret + + try: + return get_procs()[pid] == "Secure System" + except KeyError: + return False + def _get_eligible_cpu(): p = psutil.Process() diff --git a/psutil/tests/test_contracts.py b/psutil/tests/test_contracts.py index 2874c0d90..0bf03b53d 100755 --- a/psutil/tests/test_contracts.py +++ b/psutil/tests/test_contracts.py @@ -36,7 +36,6 @@ from psutil._compat import long from psutil._compat import range from psutil._compat import unicode -from psutil.tests import APPVEYOR from psutil.tests import CI_TESTING from psutil.tests import GITHUB_ACTIONS from psutil.tests import HAS_CPU_FREQ @@ -51,6 +50,7 @@ from psutil.tests import create_sockets from psutil.tests import enum from psutil.tests import is_namedtuple +from psutil.tests import is_win_secure_system_proc from psutil.tests import kernel_version from psutil.tests import process_namespace from psutil.tests import serialrun @@ -442,8 +442,8 @@ def test_all(self): meth(value, info) except Exception: s = '\n' + '=' * 70 + '\n' - s += "FAIL: name=test_%s, pid=%s, ret=%s\n" % ( - name, info['pid'], repr(value)) + s += "FAIL: name=test_%s, pid=%s, ret=%s\ninfo=%s\n" % ( + name, info['pid'], repr(value), info) s += '-' * 70 s += "\n%s" % traceback.format_exc() s = "\n".join((" " * 4) + i for i in s.splitlines()) + "\n" @@ -489,11 +489,12 @@ def ppid(self, ret, info): def name(self, ret, info): self.assertIsInstance(ret, (str, unicode)) - if APPVEYOR and not ret and info['status'] == 'stopped': + if WINDOWS and not ret and is_win_secure_system_proc(info['pid']): + # https://github.com/giampaolo/psutil/issues/2338 return # on AIX, "" processes don't have names if not AIX: - assert ret + assert ret, repr(ret) def create_time(self, ret, info): self.assertIsInstance(ret, float) @@ -562,7 +563,8 @@ def ionice(self, ret, info): def num_threads(self, ret, info): self.assertIsInstance(ret, int) - if APPVEYOR and not ret and info['status'] == 'stopped': + if WINDOWS and ret == 0 and is_win_secure_system_proc(info['pid']): + # https://github.com/giampaolo/psutil/issues/2338 return self.assertGreaterEqual(ret, 1) diff --git a/psutil/tests/test_process.py b/psutil/tests/test_process.py index 6a90c5b93..63705f9aa 100755 --- a/psutil/tests/test_process.py +++ b/psutil/tests/test_process.py @@ -825,9 +825,6 @@ def test_nice(self): init = p.nice() try: if WINDOWS: - # A CI runner may limit our maximum priority, which will break - # this test. Instead, we test in order of increasing priority, - # and match either the expected value or the highest so far. highest_prio = None for prio in [psutil.IDLE_PRIORITY_CLASS, psutil.BELOW_NORMAL_PRIORITY_CLASS, @@ -842,10 +839,16 @@ def test_nice(self): pass else: new_prio = p.nice() - if CI_TESTING: + # The OS may limit our maximum priority, + # even if the function succeeds. For higher + # priorities, we match either the expected + # value or the highest so far. + if prio in (psutil.ABOVE_NORMAL_PRIORITY_CLASS, + psutil.HIGH_PRIORITY_CLASS, + psutil.REALTIME_PRIORITY_CLASS): if new_prio == prio or highest_prio is None: highest_prio = prio - self.assertEqual(new_prio, highest_prio) + self.assertEqual(new_prio, highest_prio) else: self.assertEqual(new_prio, prio) else: diff --git a/scripts/internal/winmake.py b/scripts/internal/winmake.py index 931a2c001..890a3a415 100755 --- a/scripts/internal/winmake.py +++ b/scripts/internal/winmake.py @@ -442,7 +442,7 @@ def test_by_name(name): sh("%s -m unittest -v %s" % (PYTHON, name)) -def test_failed(): +def test_last_failed(): """Re-run tests which failed on last run.""" build() sh("%s %s --last-failed" % (PYTHON, RUNNER_PY)) @@ -559,7 +559,8 @@ def parse_args(): test_by_name = sp.add_parser('test-by-name', help=" run test by name") sp.add_parser('test-connections', help="run connections tests") sp.add_parser('test-contracts', help="run contracts tests") - sp.add_parser('test-failed', help="re-run tests which failed on last run") + sp.add_parser('test-last-failed', + help="re-run tests which failed on last run") sp.add_parser('test-memleaks', help="run memory leaks tests") sp.add_parser('test-misc', help="run misc tests") sp.add_parser('test-platform', help="run windows only tests")