Skip to content

Commit

Permalink
Improves robustness of some tests that were too environment-specific (g…
Browse files Browse the repository at this point in the history
…iampaolo#2080)

Signed-off-by: Steve Dower <[email protected]>
  • Loading branch information
zooba authored Feb 25, 2022
1 parent 4225d79 commit 8091fa5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
28 changes: 22 additions & 6 deletions psutil/tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,19 +799,29 @@ def test_nice(self):
init = p.nice()
try:
if WINDOWS:
for prio in [psutil.NORMAL_PRIORITY_CLASS,
psutil.IDLE_PRIORITY_CLASS,
# 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,
psutil.REALTIME_PRIORITY_CLASS,
psutil.NORMAL_PRIORITY_CLASS,
psutil.ABOVE_NORMAL_PRIORITY_CLASS,
psutil.HIGH_PRIORITY_CLASS,
psutil.ABOVE_NORMAL_PRIORITY_CLASS]:
psutil.REALTIME_PRIORITY_CLASS]:
with self.subTest(prio=prio):
try:
p.nice(prio)
except psutil.AccessDenied:
pass
else:
self.assertEqual(p.nice(), prio)
new_prio = p.nice()
if CI_TESTING:
if new_prio == prio or highest_prio is None:
highest_prio = prio
self.assertEqual(new_prio, highest_prio)
else:
self.assertEqual(new_prio, prio)
else:
try:
if hasattr(os, "getpriority"):
Expand Down Expand Up @@ -846,7 +856,13 @@ def test_username(self):
username = p.username()
if WINDOWS:
domain, username = username.split('\\')
self.assertEqual(username, getpass.getuser())
getpass_user = getpass.getuser()
if getpass_user.endswith('$'):
# When running as a service account (most likely to be
# NetworkService), these user name calculations don't produce
# the same result, causing the test to fail.
raise unittest.SkipTest('running as service account')
self.assertEqual(username, getpass_user)
if 'USERDOMAIN' in os.environ:
self.assertEqual(domain, os.environ['USERDOMAIN'])
else:
Expand Down
28 changes: 20 additions & 8 deletions psutil/tests/test_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,14 @@ def test_cpu_count_vs_GetSystemInfo(self):

def test_cpu_count_logical_vs_wmi(self):
w = wmi.WMI()
proc = w.Win32_Processor()[0]
self.assertEqual(psutil.cpu_count(), proc.NumberOfLogicalProcessors)
procs = sum(proc.NumberOfLogicalProcessors
for proc in w.Win32_Processor())
self.assertEqual(psutil.cpu_count(), procs)

def test_cpu_count_cores_vs_wmi(self):
w = wmi.WMI()
proc = w.Win32_Processor()[0]
self.assertEqual(psutil.cpu_count(logical=False), proc.NumberOfCores)
cores = sum(proc.NumberOfCores for proc in w.Win32_Processor())
self.assertEqual(psutil.cpu_count(logical=False), cores)

def test_cpu_count_vs_cpu_times(self):
self.assertEqual(psutil.cpu_count(),
Expand Down Expand Up @@ -213,7 +214,7 @@ def test_boot_time(self):
wmi_btime_str, "%Y%m%d%H%M%S")
psutil_dt = datetime.datetime.fromtimestamp(psutil.boot_time())
diff = abs((wmi_btime_dt - psutil_dt).total_seconds())
self.assertLessEqual(diff, 3)
self.assertLessEqual(diff, 5)

def test_boot_time_fluctuation(self):
# https://github.com/giampaolo/psutil/issues/1007
Expand Down Expand Up @@ -351,12 +352,23 @@ def test_ctrl_signals(self):
p.send_signal, signal.CTRL_BREAK_EVENT)

def test_username(self):
self.assertEqual(psutil.Process().username(),
win32api.GetUserNameEx(win32con.NameSamCompatible))
name = win32api.GetUserNameEx(win32con.NameSamCompatible)
if name.endswith('$'):
# When running as a service account (most likely to be
# NetworkService), these user name calculations don't produce the
# same result, causing the test to fail.
raise unittest.SkipTest('running as service account')
self.assertEqual(psutil.Process().username(), name)

def test_cmdline(self):
sys_value = re.sub(' +', ' ', win32api.GetCommandLine()).strip()
sys_value = re.sub('[ ]+', ' ', win32api.GetCommandLine()).strip()
psutil_value = ' '.join(psutil.Process().cmdline())
if sys_value[0] == '"' != psutil_value[0]:
# The PyWin32 command line may retain quotes around argv[0] if they
# were used unnecessarily, while psutil will omit them. So remove
# the first 2 quotes from sys_value if not in psutil_value.
# A path to an executable will not contain quotes, so this is safe.
sys_value = sys_value.replace('"', '', 2)
self.assertEqual(sys_value, psutil_value)

# XXX - occasional failures
Expand Down

0 comments on commit 8091fa5

Please sign in to comment.