diff --git a/CHANGELOG.md b/CHANGELOG.md index a9fe33d..46b7d2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 could not be used as a URL path segment, so such a tunnel could not be edited in netOrk. The key of a peer whose first eight key characters hold `+` or `/` changes once; `public_key` is reported unchanged. +- `get_processes()` reports a process's whole start time in `started` + (`Tue Oct 6 08:47:15 2026`) and only the command line in `command` (#21). `started` + held the weekday alone and `command` began with the rest of the date. The start time + is read in the C locale, so its words are English on every host. ## [0.15.1] - 2026-10-23 diff --git a/napalm_linux/linux.py b/napalm_linux/linux.py index 25a95a1..562138f 100644 --- a/napalm_linux/linux.py +++ b/napalm_linux/linux.py @@ -1852,14 +1852,15 @@ class LinuxDriver( def get_processes(self) -> list[ProcessDict]: """Return running processes via ``ps axo``.""" + # lstart is five words ("Tue Oct 6 08:47:15 2026") -- one word each only in C. out = self._send( - "ps axo pid,ppid,user:20,pcpu,pmem,vsz,rss,tty,stat,lstart,args " + "LC_ALL=C ps axo pid,ppid,user:20,pcpu,pmem,vsz,rss,tty,stat,lstart,args " "--no-headers 2>/dev/null" ) processes: list[ProcessDict] = [] for line in out.splitlines(): - parts = line.split(None, 10) - if len(parts) < 11: + parts = line.split(None, 14) + if len(parts) < 15: continue try: pid = int(parts[0]) @@ -1871,12 +1872,8 @@ class LinuxDriver( rss = int(parts[6]) tty = parts[7] if parts[7] != "?" else "" state = parts[8][0] if parts[8] else "?" - # lstart is 5 tokens: "Mon May 27 12:34:56 2024" → parts[9..13] - # args starts at parts[14] but we merged from 10 onward - # With --no-headers and ps axo, lstart takes 5 parts - # Rebuild: parts[9] is start, args is parts[10] - started = parts[9] - command = parts[10] + started = " ".join(parts[9:14]) + command = parts[14] except (ValueError, IndexError): continue processes.append( diff --git a/tests/quality_baseline.json b/tests/quality_baseline.json index fc08f32..1ce7c65 100644 --- a/tests/quality_baseline.json +++ b/tests/quality_baseline.json @@ -22,7 +22,7 @@ "napalm_linux.linux" ], "python_file_lines": { - "napalm_linux/linux.py": 2260 + "napalm_linux/linux.py": 2257 }, "untyped_public": [] } diff --git a/tests/test_linux.py b/tests/test_linux.py index b68f1da..bf6da05 100644 --- a/tests/test_linux.py +++ b/tests/test_linux.py @@ -2065,3 +2065,61 @@ class TestWireGuardTunnelKeys: (tunnel,) = driver.get_vpn_tunnels().values() assert tunnel["public_key"] == "ab/c+d9EfGhIjKlMnOpQrStUvWxYz0123456789AB=" + + +class TestGetProcesses: + """``lstart`` is five words; the split has to leave them together (#21).""" + + _PS = ( + " 1 0 root 0.0 0.1 23456 12345 ? Ss Tue Oct 6 08:47:15 2026 /sbin/init splash\n" + " 1234 1 chris 1.5 2.3 456789 98765 pts/0 S+ Tue Oct 6 09:01:02 2026 bash -c echo two spaces\n" + ) + + def test_started_is_the_whole_start_time(self, driver): + _mock_send(driver, self._PS) + + init, shell = driver.get_processes() + + assert init["started"] == "Tue Oct 6 08:47:15 2026" + assert shell["started"] == "Tue Oct 6 09:01:02 2026" + + def test_command_is_the_command_line_alone(self, driver): + _mock_send(driver, self._PS) + + init, shell = driver.get_processes() + + assert init["command"] == "/sbin/init splash" + assert shell["command"] == "bash -c echo two spaces" + + def test_the_other_columns(self, driver): + _mock_send(driver, self._PS) + + shell = driver.get_processes()[1] + + assert (shell["pid"], shell["ppid"], shell["user"], shell["tty"]) == ( + 1234, + 1, + "chris", + "pts/0", + ) + assert (shell["cpu"], shell["memory"], shell["vsz"], shell["rss"]) == ( + 1.5, + 2.3, + 456789, + 98765, + ) + assert shell["state"] == "S" + assert driver.get_processes()[0]["tty"] == "" + + def test_the_start_time_is_in_c_locale_words(self, driver): + """A German host says "Di Okt" -- one word each too, but only C promises that.""" + _mock_send(driver, self._PS) + + driver.get_processes() + + assert driver._device.send_command.call_args[0][0].startswith("LC_ALL=C ps ") + + def test_a_line_cut_short_is_skipped(self, driver): + _mock_send(driver, " 1 0 root 0.0 0.1 23456 12345 ? Ss Tue Oct 6\n") + + assert driver.get_processes() == []