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