Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python-waf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
--cgroupns host
steps:
- name: Install dependencies
run: apt update && apt install -y sudo iproute2 iputils-ping
run: apt update && apt install -y sudo iproute2 iputils-ping stress
- name: Checkout
uses: actions/checkout@v4
- name: Configure
Expand Down
4 changes: 3 additions & 1 deletion NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ every change, see the Git log.

Latest
------
* tbd
* Major: `RunInfo` now takes a `subprocess.Popen` object instead of a `pid`.
`RunInfo.pid` is still accessible and points to `RunInfo.popen.pid` for
backwards compatability.

10.0.0
------
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ classifiers = [
]
dependencies = [
"psutil==7.0.0",
"subprocess4==0.1.1",
]

[project.urls]
Expand Down
12 changes: 6 additions & 6 deletions src/dummynet/process_monitor.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import select
import logging
import os
import subprocess
import signal
import getpass
import time

from subprocess4 import Popen as Popen4
import subprocess

from functools import lru_cache
from typing import Optional

Expand Down Expand Up @@ -192,7 +194,7 @@ def __init__(
# Run inside wrapped /bin/sh environment when cmd is string.
shell = isinstance(cmd, str)

self.popen = subprocess.Popen(
self.popen = Popen4(
cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
Expand All @@ -210,16 +212,14 @@ def __init__(

# Pipe possible sudo password to the process
if sudo and (cached_sudo_password is not None):
assert cached_sudo_password.endswith(
"\n"
) # Ensure the password ends with a newline as otherwise sudo will hang
assert cached_sudo_password.endswith("\n")
self.popen.stdin.write(cached_sudo_password)
self.popen.stdin.flush()

self.info = run_info.RunInfo(
cmd=cmd,
cwd=cwd,
pid=self.popen.pid,
popen=self.popen,
stdout="",
stderr="",
returncode=None,
Expand Down
74 changes: 47 additions & 27 deletions src/dummynet/run_info.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fnmatch
import subprocess
from typing import Callable, Optional

from . import errors
Expand Down Expand Up @@ -27,13 +28,22 @@ class RunInfo:
"""

def __init__(
self, cmd, cwd, pid, stdout, stderr, returncode, is_async, is_daemon, timeout
self,
cmd,
cwd,
popen,
stdout,
stderr,
returncode,
is_async,
is_daemon,
timeout,
):
"""Create a new object

:param cmd: The command that was executed
:param cwd: Current working directory i.e. path where the command was executed
:param pid: The process ID of the command
:param popen: The subprocess instance for the command
:param stdout: The standard output stream generated by the command
:param stderr: The standard error stream generated by the command
:param returncode: The return code set after invoking the command
Expand All @@ -44,7 +54,7 @@ def __init__(

self.cmd: str | list[str] = cmd
self.cwd: Optional[str] = cwd
self.pid: int = pid
self.popen: subprocess.Popen = popen
self.stdout: str = stdout
self.stderr: str = stderr
self.returncode: Optional[int] = returncode
Expand All @@ -54,6 +64,14 @@ def __init__(
self.stderr_callback: Optional[Callable] = None
self.timeout: Optional[int | float] = timeout

@property
def pid(self):
return self.popen.pid

@property
def rusage(self):
return self.popen.rusage

def match(self, stdout=None, stderr=None):
"""Matches the lines in the output with the pattern. The match
pattern can contain basic wildcards, see
Expand Down Expand Up @@ -108,29 +126,31 @@ def _match(self, pattern, stream_name, output):
pattern=pattern, stream_name=stream_name, output=output
)

def __str__(self):
"""Print the RunInfo object as a string"""
run_string = (
"RunInfo\n"
"command: {command}\n"
"cwd: {cwd}\n"
"pid: {pid}\n"
"returncode: {returncode}\n"
"stdout: \n{stdout}"
"stderr: \n{stderr}"
"is_async: {is_async}\n"
"is_daemon: {is_daemon}\n"
"timeout: {timeout}\n"
def __repr__(self):
"""Return a detailed representation of the object for debugging"""
modes = []

if self.is_async:
modes.append("async")
if self.is_daemon:
modes.append("daemon")

return (
f"<{self.__class__.__name__}:"
+ f" cmd: {self.cmd!r}"
+ (f" cwd: {self.cwd!r}" if self.cwd is not None else "")
+ (
f" returncode: {self.returncode!r}"
if self.returncode is not None
else ""
)
+ (f" timeout: {self.timeout!r} " if self.timeout is not None else "")
+ (f" stdout: {len(self.stdout)} chars" if self.stdout else "")
+ (f" stderr: {len(self.stderr)} chars" if self.stderr else "")
+ (f" modes: {modes!r}" if modes else "")
+ ">"
)

return run_string.format(
command=self.cmd,
cwd=self.cwd,
pid=self.pid,
returncode=self.returncode,
stdout=self.stdout,
stderr=self.stderr,
is_async=self.is_async,
is_daemon=self.is_daemon,
timeout=self.timeout,
)
def __str__(self):
"""Print the RunInfo object as a string"""
return self.__repr__()
29 changes: 26 additions & 3 deletions test/test_dummynet.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,12 +647,17 @@ def test_stop_process_async(process_monitor: ProcessMonitor):
process_monitor.stop_process_async(process)
assert process.returncode is not None

class FakePopen:
@property
def pid(self):
return None

fake_process = RunInfo(
cmd="echo",
cwd=None,
pid=None,
stdout=None,
stderr=None,
popen=FakePopen(),
stdout="",
stderr="",
returncode=None,
is_async=False,
is_daemon=False,
Expand Down Expand Up @@ -697,3 +702,21 @@ def test_stop_process_async_kill(process_monitor: ProcessMonitor):
daemon = process_monitor.run_process_async("sleep 10", sudo=False, daemon=False)
process_monitor.stop_process_async(daemon)
assert signal.Signals(-daemon.returncode).name == "SIGTERM" # type: ignore


def test_cpu_usage_statistics(process_monitor: ProcessMonitor):
def run_task_async(task, sudo, utime):
process = process_monitor.run_process_async(task, sudo=sudo)

while process_monitor.keep_running():
pass

# Allow a 5% margin of the given utime value
margin = utime * 0.05
actual_utime = process.rusage.ru_utime # type: ignore
assert (utime - margin) < actual_utime < (utime + margin)

run_task_async("stress --cpu 1 --timeout 1", sudo=False, utime=1.0)
run_task_async(["stress", "--cpu", "2", "--timeout", "1"], sudo=False, utime=2.0)
run_task_async("stress --cpu 1 --timeout 2", sudo=True, utime=2.0)
run_task_async(["stress", "--cpu", "2", "--timeout", "2"], sudo=True, utime=4.0)