Skip to content

Commit

Permalink
[not4review] test
Browse files Browse the repository at this point in the history
  • Loading branch information
FengPan-Frank committed Oct 22, 2024
1 parent 246f2d2 commit b038037
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
18 changes: 16 additions & 2 deletions scripts/procdockerstatsd
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ REDIS_HOSTIP = "127.0.0.1"


class ProcDockerStats(daemon_base.DaemonBase):
all_process_obj = {}

def __init__(self, log_identifier):
super(ProcDockerStats, self).__init__(log_identifier)
Expand Down Expand Up @@ -138,10 +139,18 @@ class ProcDockerStats(daemon_base.DaemonBase):

def update_processstats_command(self):
processdata = []
for process in psutil.process_iter(['pid', 'ppid', 'memory_percent', 'cpu_percent', 'create_time', 'cmdline']):
pid_set = set()
for process_obj in psutil.process_iter(['pid', 'ppid', 'memory_percent', 'cpu_percent', 'create_time', 'cmdline']):
try:
pid = process_obj.pid
pid_set.add(pid)
# store process object and reuse for CPU utilization
if pid in self.all_process_obj:
process = self.all_process_obj[pid]
else:
self.all_process_obj[pid] = process_obj
process = process_obj
uid = process.uids()[0]
pid = process.pid
ppid = process.ppid()
mem = process.memory_percent()
cpu = process.cpu_percent()
Expand All @@ -157,6 +166,11 @@ class ProcDockerStats(daemon_base.DaemonBase):
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass

# erase dead process
for id in list(self.all_process_obj.keys()):
if id not in pid_set:
del self.all_process_obj[id]

# wipe out all data before updating with new values
self.state_db.delete_all_by_pattern('STATE_DB', 'PROCESS_STATS|*')

Expand Down
5 changes: 5 additions & 0 deletions tests/procdockerstatsd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,13 @@ def test_update_processstats_command(self):
]

with patch("procdockerstatsd.psutil.process_iter", return_value=mocked_processes) as mock_process_iter:
pdstatsd.all_process_obj = {5678: MockProcess(uids=[1000], pid=5678, ppid=0, memory_percent=5.5, cpu_percent=15.5, create_time=valid_create_time2, cmdline=['bash', 'script.sh'], user_time=3.5, system_time=4.0)}
pdstatsd.update_processstats_command()
mock_process_iter.assert_called_once()
assert pdstatsd.all_process_obj.get(1234).uids() == mocked_processes[0].uids() # Compare the uids attribute
assert pdstatsd.all_process_obj.get(1234).pid() == mocked_processes[0].pid() # Compare the pid attribute
assert pdstatsd.all_process_obj.get(1234).ppid() == mocked_processes[0].ppid() # Compare the ppid attribute
assert pdstatsd.all_process_obj.get(5678) is None # Verify process object is deleted from all_process_obj

@patch('procdockerstatsd.getstatusoutput_noshell_pipe', return_value=([0, 0], ''))
def test_update_fipsstats_command(self, mock_cmd):
Expand Down

0 comments on commit b038037

Please sign in to comment.