|
| 1 | +from unittest.mock import Mock, patch, mock_open, PropertyMock |
| 2 | +from lib.api.process import Process |
| 3 | +import base64 |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +proc_status = """Name: test-process |
| 8 | +Umask: 0002 |
| 9 | +State: R (running) |
| 10 | +Tgid: 42 |
| 11 | +Ngid: 0 |
| 12 | +Pid: 42 |
| 13 | +PPid: 24""" |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def os_path_exists(monkeypatch): |
| 17 | + monkeypatch.setattr("os.path.exists", Mock(return_value=True)) |
| 18 | + yield |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def os_path_not_exists(monkeypatch): |
| 22 | + monkeypatch.setattr("os.path.exists", Mock(return_value=False)) |
| 23 | + yield |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def fake_proc_status_file(monkeypatch): |
| 27 | + monkeypatch.setattr("builtins.open", mock_open(read_data=proc_status)) |
| 28 | + yield |
| 29 | + |
| 30 | +ARGS = { |
| 31 | + "pid": 42 |
| 32 | +} |
| 33 | + |
| 34 | +def test_init(): |
| 35 | + """Initialize Process instances using both args and kwargs""" |
| 36 | + kw_args_instance = Process(**ARGS) |
| 37 | + assert kw_args_instance.pid == ARGS["pid"] |
| 38 | + args_instance = Process(*ARGS.values()) |
| 39 | + assert args_instance.pid == ARGS["pid"] |
| 40 | + |
| 41 | +@pytest.mark.usefixtures("os_path_exists") |
| 42 | +def test_proc_alive_states(): |
| 43 | + for state in [ |
| 44 | + "State: R (running)", |
| 45 | + "State: S (sleeping)", |
| 46 | + "State: D (waiting)", |
| 47 | + "State: T (stopped)", |
| 48 | + "State: t (trace stopped)", |
| 49 | + "State: W (paging)", |
| 50 | + "State: W (waking)", |
| 51 | + "State: P (parked)", |
| 52 | + ]: |
| 53 | + state_file_content = proc_status.replace("State: R (running)", state) |
| 54 | + with patch("builtins.open", mock_open(read_data=state_file_content)): |
| 55 | + process = Process(**ARGS) |
| 56 | + assert process.is_alive() |
| 57 | + |
| 58 | +@pytest.mark.usefixtures("os_path_exists") |
| 59 | +def test_proc_dead_states(): |
| 60 | + for state in [ |
| 61 | + "State: Z (zombie)", |
| 62 | + ]: |
| 63 | + state_file_content = proc_status.replace("State: R (running)", state) |
| 64 | + with patch("builtins.open", mock_open(read_data=state_file_content)): |
| 65 | + process = Process(**ARGS) |
| 66 | + alive = process.is_alive() |
| 67 | + assert not alive |
| 68 | + |
| 69 | +@pytest.mark.usefixtures("os_path_not_exists") |
| 70 | +def test_proc_file_not_exists(): |
| 71 | + process = Process(**ARGS) |
| 72 | + assert not process.is_alive() |
| 73 | + |
| 74 | +@pytest.mark.usefixtures("os_path_exists") |
| 75 | +def test_proc_file_corrupt(): |
| 76 | + corrupt_status = base64.b64encode(proc_status.encode("utf-8")).decode("utf-8") |
| 77 | + with patch("builtins.open", mock_open(read_data=corrupt_status)): |
| 78 | + process = Process(**ARGS) |
| 79 | + assert not process.is_alive() |
| 80 | + |
| 81 | +@pytest.mark.usefixtures("os_path_exists", "fake_proc_status_file") |
| 82 | +def test_get_ppid(): |
| 83 | + process = Process(**ARGS) |
| 84 | + assert 24 == process.get_parent_pid() |
| 85 | + |
| 86 | +@patch("builtins.open", side_effect=FileNotFoundError) |
| 87 | +def test_get_ppid_file_not_exists(bopen): |
| 88 | + process = Process(**ARGS) |
| 89 | + assert process.get_parent_pid() is None |
| 90 | + |
| 91 | +@patch("subprocess.Popen") |
| 92 | +def test_execute(popen): |
| 93 | + process = Process(**ARGS) |
| 94 | + type(popen.return_value).pid = PropertyMock(return_value=ARGS["pid"]) |
| 95 | + assert process.execute(["echo", "this is a test message"]) |
| 96 | + assert ARGS["pid"] == process.pid |
| 97 | + assert popen.called |
0 commit comments