def test_order_best_first(): p0 = testutils.create_process(cputime="0:10.00", mempercent="10.0") p1 = testutils.create_process(commandline="awk", cputime="0:11.00", mempercent="1.0") p2 = testutils.create_process(commandline="bash", cputime="0:01.00", mempercent="11.0") # P0 should be first because its score is the highest, then p1 and p2 should # be ordered alphabetically assert px_process.order_best_first([p0, p1, p2]) == [p0, p1, p2] assert px_process.order_best_first([p2, p1, p0]) == [p0, p1, p2]
def test_command_linux_kernelproc(): p = testutils.create_process(commandline="[ksoftirqd/0]") assert p.command == "[ksoftirqd/0]" p = testutils.create_process(commandline="[kworker/0:0H]") assert p.command == "[kworker/0:0H]" p = testutils.create_process(commandline="[rcuob/3]") assert p.command == "[rcuob/3]"
def test_adjust_cpu_times(): now = testutils.now() current = [ px_process.create_kernel_process(now), testutils.create_process(pid=100, cputime="0:10.00", commandline="only in current"), testutils.create_process(pid=200, cputime="0:20.00", commandline="re-used PID baseline", timestring="Mon May 7 09:33:11 2010"), testutils.create_process(pid=300, cputime="0:30.00", commandline="relevant baseline"), ] baseline = [ px_process.create_kernel_process(now), testutils.create_process(pid=200, cputime="0:02.00", commandline="re-used PID baseline", timestring="Mon Apr 7 09:33:11 2010"), testutils.create_process(pid=300, cputime="0:03.00", commandline="relevant baseline"), testutils.create_process(pid=400, cputime="0:03.00", commandline="only in baseline"), ] actual = px_process.order_best_last(px_top.adjust_cpu_times(current, baseline)) expected = px_process.order_best_last([ px_process.create_kernel_process(now), testutils.create_process(pid=100, cputime="0:10.00", commandline="only in current"), testutils.create_process(pid=200, cputime="0:20.00", commandline="re-used PID baseline", timestring="Mon May 7 09:33:11 2010"), testutils.create_process(pid=300, cputime="0:27.00", commandline="relevant baseline"), ]) assert actual == expected
def test_get_command_line_array_space_in_binary(tmpdir): # Create a file name with a space in it spaced_path = tmpdir.join("i contain spaces") spaced_path.write_binary(b"") spaced_name = str(spaced_path) # Verify splitting of the spaced file name p = testutils.create_process(commandline=spaced_name) assert p.get_command_line_array() == [spaced_name] # Verify splitting with more parameters on the line p = testutils.create_process(commandline=spaced_name + " parameter") assert p.get_command_line_array() == [spaced_name, "parameter"]
def test_to_ipc_lines(): ipcmap = { testutils.create_process(commandline="foo"): [ testutils.create_file("PIPE", "[] ->0xAda", "0xE0e", 25), ], testutils.create_process(commandline="bar"): [ testutils.create_file("PIPE", "[] ->0xAda", "0xE0e", 25), ], } lines = px_processinfo.to_ipc_lines(ipcmap) # type: ignore assert lines == [ "bar(47536): [PIPE] ->0xAda", "foo(47536): [PIPE] ->0xAda" ]
def test_get_closest_starts_five_closest(): # Verify that we list the five closest processes even if none of them are # very close all = [] all.append(testutils.create_process(pid=102, timestring="Mon Mar 7 06:33:10 2016")) all.append(testutils.create_process(pid=103, timestring="Mon Mar 7 07:33:10 2016")) all.append(testutils.create_process(pid=104, timestring="Mon Mar 7 08:33:11 2016")) base = testutils.create_process(pid=105, timestring="Mon Mar 7 09:33:11 2016") all.append(base) all.append(testutils.create_process(pid=106, timestring="Mon Mar 7 10:33:11 2016")) all.append(testutils.create_process(pid=107, timestring="Mon Mar 7 11:33:12 2016")) all.append(testutils.create_process(pid=108, timestring="Mon Mar 7 11:43:12 2016")) all.append(testutils.create_process(pid=110, timestring="Tue Mar 7 12:33:13 2016")) close = px_processinfo.get_closest_starts(base, all) assert len(close) == 5 assert all[0] not in close assert all[1] in close assert all[2] in close assert all[3] not in close # This is base, it shouldn't be close to itself assert all[4] in close assert all[5] in close assert all[6] in close assert all[7] not in close
def test_command_dotted_prefix(): # If there's a dot with a lot of text after it we should drop everything # before the dot. p = testutils.create_process( commandline="/.../com.apple.InputMethodKit.TextReplacementService") assert p.command == "TextReplacementService" # If there's a dot with four characters or less after it, assume it's a file # suffix and take the next to last section p = testutils.create_process( commandline="/.../com.apple.InputMethodKit.TextReplacementService.1234") assert p.command == "TextReplacementService" p = testutils.create_process( commandline="/.../com.apple.InputMethodKit.TextReplacementService.12345") assert p.command == "12345"
def test_print_process_subtree(): lines = [] # type: List[str] child_proc = testutils.create_process(pid=2, commandline="child") child_proc.children = [] parent_proc = testutils.create_process(pid=1, commandline="parent") parent_proc.children = [child_proc] px_processinfo.print_process_subtree(parent_proc, 0, lines) assert lines == [ ('' + str(parent_proc), parent_proc), (' ' + str(child_proc), child_proc) ]
def test_ps_line_to_process_2(): process = testutils.create_process(cputime="2:14.15") assert process.pid == 47536 assert process.ppid == 1234 assert process.username == "root" assert process.cpu_time_s == "2m14s" assert process.memory_percent_s == "0%" assert process.cmdline == "/usr/sbin/cupsd -l" assert process.start_time == testutils.TIME assert process.age_seconds > 0
def test_to_screen_lines_unicode(): procs = [testutils.create_process(commandline=u"/usr/bin/😀")] converted = px_terminal.to_screen_lines(procs, None) if sys.version_info.major > 3: assert converted == [ " PID COMMAND USERNAME CPU RAM COMMANDLINE", "47536 😀 root 0.03s 0% /usr/bin/😀" ] else: # Unicode string widths are difficult before Python 3.3, don't test # the actual layout in this case: # https://stackoverflow.com/q/29109944/473672 pass
def test_match(): p = testutils.create_process(username="******", commandline="/usr/libexec/AirPlayXPCHelper") assert p.match(None) assert p.match("root") assert not p.match("roo") assert p.match("Air") assert p.match("Play") assert p.match("air") assert p.match("play")
def test_to_relative_start_string(): base = testutils.create_process(pid=100, timestring="Mon Mar 7 09:33:11 2016") close = testutils.create_process(pid=101, timestring="Mon Mar 7 09:33:12 2016") assert ("cupsd(101) was started 1.0s after cupsd(100)" == px_processinfo.to_relative_start_string(base, close)) base = testutils.create_process(pid=100, timestring="Mon Mar 7 09:33:11 2016") close = testutils.create_process(pid=101, timestring="Mon Mar 7 09:33:10 2016") assert ("cupsd(101) was started 1.0s before cupsd(100)" == px_processinfo.to_relative_start_string(base, close)) base = testutils.create_process(pid=100, timestring="Mon Mar 7 09:33:11 2016") close = testutils.create_process(pid=101, timestring="Mon Mar 7 09:33:11 2016") assert ("cupsd(101) was started just after cupsd(100)" == px_processinfo.to_relative_start_string(base, close))
def test_get_closests_starts_all_within_1s(): # Verify that even if we have a large number of processes created within 1s # of the base one, we get all of those all = [] all.append(testutils.create_process(pid=100, timestring="Mon Mar 7 09:33:09 2016")) all.append(testutils.create_process(pid=101, timestring="Mon Mar 7 09:33:10 2016")) all.append(testutils.create_process(pid=102, timestring="Mon Mar 7 09:33:10 2016")) all.append(testutils.create_process(pid=103, timestring="Mon Mar 7 09:33:10 2016")) all.append(testutils.create_process(pid=104, timestring="Mon Mar 7 09:33:11 2016")) base = testutils.create_process(pid=105, timestring="Mon Mar 7 09:33:11 2016") all.append(base) all.append(testutils.create_process(pid=106, timestring="Mon Mar 7 09:33:11 2016")) all.append(testutils.create_process(pid=107, timestring="Mon Mar 7 09:33:12 2016")) all.append(testutils.create_process(pid=108, timestring="Mon Mar 7 09:33:12 2016")) all.append(testutils.create_process(pid=109, timestring="Mon Mar 7 09:33:12 2016")) all.append(testutils.create_process(pid=110, timestring="Tue Mar 8 09:33:13 2016")) close = px_processinfo.get_closest_starts(base, all) assert len(close) == 8 assert all[0] not in close assert all[1] in close assert all[2] in close assert all[3] in close assert all[4] in close assert all[5] not in close # This is base, it shouldn't be close to itself assert all[6] in close assert all[7] in close assert all[8] in close assert all[9] in close assert all[10] not in close
def test_ps_line_to_process_unicode(): process = testutils.create_process(cputime="2:14.15") assert process.username == u"root" assert process.cmdline == u"/usr/sbin/cupsd -l"
def test_command_in_parentheses(): # Observed on OS X p = testutils.create_process(commandline="(python2.7)") assert p.command == "(python2.7)"
def test_to_screen_lines_unbounded(): procs = [testutils.create_process(commandline="/usr/bin/fluff 1234")] assert px_terminal.to_screen_lines(procs, None) == [ " PID COMMAND USERNAME CPU RAM COMMANDLINE", "47536 fluff root 0.03s 0% /usr/bin/fluff 1234" ]
def test_get_command_line_array(): p = testutils.create_process(commandline="/usr/libexec/AirPlayXPCHelper") assert p.get_command_line_array() == ["/usr/libexec/AirPlayXPCHelper"] p = testutils.create_process(commandline="/usr/sbin/universalaccessd launchd -s") assert p.get_command_line_array() == ["/usr/sbin/universalaccessd", "launchd", "-s"]