Skip to content

Commit

Permalink
tests/unit/test_dump_xapi_procs.py: Test dump_xapi_subprocess_info() (#…
Browse files Browse the repository at this point in the history
…85)

Signed-off-by: Bernhard Kaindl <[email protected]>
  • Loading branch information
bernhardkaindl authored Mar 11, 2024
1 parent 493e0a2 commit bd7e5df
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ repos:
- defusedxml
- lxml
- mock
- pyfakefs
- pytest-coverage
- pytest-mock
- XenAPI
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
lxml
mock
pyfakefs
pytest-coverage
pytest-mock
types-mock
10 changes: 10 additions & 0 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,19 @@
import os
import shutil
import sys
from types import ModuleType

import pytest

#
# Prevent the import of the pandas module: It is not needed for unit tests:
# But pyfakefs tries to import it for patching it. This is not needed and
# causes a DeprecationWarning when Pandas is imported: It warns that the
# pyarrow backend is deprecated and will be removed in a future version. Also,
# the time to import pandas is very noticeable(0.3s) when starting tests:
#
sys.modules["pandas"] = ModuleType("pandas")


@pytest.fixture(scope="function")
def builtins():
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/test_dump_xapi_procs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""This module contains the unit tests for the dump_xapi_procs function"""

import sys


def test_dump_xapi_subprocess_info(bugtool, fs):
"""Test the dump_xapi_subprocess_info() function to perform as expected"""

# Prepare the virtual pyfakefs filesystem for the test

fs.create_file("/proc/2/status", contents="PPid: 1")
fs.create_file("/proc/3/status", contents="PPid: 2")
fs.create_file("/proc/4/status", contents="")
fs.create_file("/proc/2/cmdline", contents="/opt/xensource/bin/xapi")
fs.create_file("/proc/3/cmdline", contents="/bin/sh")
fs.create_file("/proc/4/cmdline", contents="")
fs.create_symlink("/proc/2/fd/1", "/dev/null")
fs.create_symlink("/proc/3/fd/0", "/dev/zero")

# Prepare the expected result

expected_result = """{ '2': { 'children': { '3': { 'children': {},
'cmdline': '/bin/sh',
'fds': {'0': '/dev/zero'}}},
'cmdline': '/opt/xensource/bin/xapi',
'fds': {'1': '/dev/null'}}}"""

if sys.version_info.major == 2: # pragma: no cover
expected_result = expected_result.replace("{}", "{ }")
expected_result = expected_result.replace(": {'", ": { '")

# Assert the expected result

assert bugtool.dump_xapi_subprocess_info("cap") == expected_result

0 comments on commit bd7e5df

Please sign in to comment.