Skip to content

Commit 6106a0e

Browse files
authored
Add an explicit system test for processes on unix systems (elastic#20320)
Some fields are only available on some operating systems, and for processes Metricbeat has permissions to read. Check these fields only on these operating systems and on processes owned by the same user running the test. Fix also check on current working directory.
1 parent 7a34167 commit 6106a0e

File tree

1 file changed

+63
-19
lines changed

1 file changed

+63
-19
lines changed

metricbeat/module/system/test_system.py

+63-19
Original file line numberDiff line numberDiff line change
@@ -392,41 +392,85 @@ def test_process(self):
392392
self.assertGreater(len(output), 0)
393393

394394
found_cmdline = False
395-
found_env = False
395+
for evt in output:
396+
process = evt["system"]["process"]
397+
found_cmdline |= "cmdline" in process
398+
399+
# Remove 'env' prior to checking documented fields because its keys are dynamic.
400+
process.pop("env", None)
401+
self.assert_fields_are_documented(evt)
402+
403+
# Remove optional keys.
404+
process.pop("cgroup", None)
405+
process.pop("fd", None)
406+
process.pop("cmdline", None)
407+
408+
self.assertCountEqual(SYSTEM_PROCESS_FIELDS, process.keys())
409+
410+
self.assertTrue(found_cmdline, "cmdline not found in any process events")
411+
412+
@unittest.skipUnless(re.match("(?i)linux|darwin|freebsd", sys.platform), "os")
413+
def test_process_unix(self):
414+
"""
415+
Test system/process output for fields specific of unix systems.
416+
"""
417+
import getpass
418+
419+
self.render_config_template(
420+
modules=[{
421+
"name": "system",
422+
"metricsets": ["process"],
423+
"period": "5s",
424+
"extras": {
425+
"process.env.whitelist": ["PATH"],
426+
"process.include_cpu_ticks": True,
427+
428+
# Remove 'percpu' prior to checking documented fields because its keys are dynamic.
429+
"process.include_per_cpu": False,
430+
},
431+
}],
432+
# Some info is only guaranteed in processes with permissions, check
433+
# only on own processes.
434+
processors=[{
435+
"drop_event": {
436+
"when": "not.equals.user.name: " + getpass.getuser(),
437+
},
438+
}],
439+
)
440+
proc = self.start_beat()
441+
self.wait_until(lambda: self.output_lines() > 0)
442+
proc.check_kill_and_wait()
443+
self.assert_no_logged_warnings()
444+
445+
output = self.read_output_json()
446+
self.assertGreater(len(output), 0)
447+
396448
found_fd = False
449+
found_env = False
397450
found_cwd = not sys.platform.startswith("linux")
398451
for evt in output:
452+
found_cwd |= "working_directory" in evt["process"]
453+
399454
process = evt["system"]["process"]
455+
found_fd |= "fd" in process
456+
found_env |= "env" in process
400457

401458
# Remove 'env' prior to checking documented fields because its keys are dynamic.
402459
env = process.pop("env", None)
403-
if env is not None:
404-
found_env = True
405-
406460
self.assert_fields_are_documented(evt)
407461

408462
# Remove optional keys.
409463
process.pop("cgroup", None)
410-
cmdline = process.pop("cmdline", None)
411-
if cmdline is not None:
412-
found_cmdline = True
413-
fd = process.pop("fd", None)
414-
if fd is not None:
415-
found_fd = True
416-
cwd = process.pop("cwd", None)
417-
if cwd is not None:
418-
found_cwd = True
464+
process.pop("cmdline", None)
465+
process.pop("fd", None)
419466

420467
self.assertCountEqual(SYSTEM_PROCESS_FIELDS, process.keys())
421468

422-
self.assertTrue(found_cmdline, "cmdline not found in any process events")
423-
424-
if sys.platform.startswith("linux") or sys.platform.startswith("freebsd"):
469+
if not sys.platform.startswith("darwin"):
425470
self.assertTrue(found_fd, "fd not found in any process events")
426471

427-
if sys.platform.startswith("linux") or sys.platform.startswith("freebsd")\
428-
or sys.platform.startswith("darwin"):
429-
self.assertTrue(found_env, "env not found in any process events")
472+
self.assertTrue(found_env, "env not found in any process events")
473+
self.assertTrue(found_cwd, "working_directory not found in any process events")
430474

431475
@unittest.skipUnless(re.match("(?i)win|linux|darwin|freebsd", sys.platform), "os")
432476
def test_process_metricbeat(self):

0 commit comments

Comments
 (0)