Skip to content

Commit

Permalink
tests/unit/test_output.py: Test mock bugtool plugin output files
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhardkaindl committed Dec 20, 2023
1 parent a1cfd0d commit 54e4fad
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 3 deletions.
1 change: 1 addition & 0 deletions tests/integration/dom0-template/usr/sbin/cat
87 changes: 87 additions & 0 deletions tests/unit/test_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""Unit tests for bugtool core functions creating minimal output archives"""
import os
import pytest
import sys
import tarfile
import zipfile

from lxml.etree import XMLSchema, parse # pytype: disable=import-error


def assert_valid_inventory_schema(inventory_tree):
"""Assert that the passed inventory validates against the inventory schema"""

with open(os.getcwd() + "/tests/integration/inventory.xsd") as xmlschema:
XMLSchema(parse(xmlschema)).assertValid(inventory_tree)


def assert_mock_bugtool_plugin_output(extracted):
"""Assertion check of the output files from the mock bugtool plugin"""
assert_valid_inventory_schema(parse(extracted + "inventory.xml"))
if sys.version_info > (3, 0):
return # FIXME: Fix collect_data() for Python3
with open(extracted + "proc_version.out") as proc_version: # type: ignore[unreachable]
assert proc_version.read()[:14] == "Linux version "
with open(extracted + "ls-l-%proc%self%fd.out") as reboot:
assert reboot.read()[:6] == "total "
with open(extracted + "proc/self/status") as status:
assert status.read()[:5] == "Name:"
with open(extracted + "proc/sys/fs/epoll/max_user_watches") as max_user_watches:
assert int(max_user_watches.read()) > 0


def minimal_bugtool(bugtool, dom0_template, archive, subdir):
"""Load the plugins from the template and include the generated inventory"""

# Load the mock plugin from dom0_template and process the plugin's caps:
bugtool.PLUGIN_DIR = dom0_template + "/etc/xensource/bugtool"
bugtool.entries = ["mock"]
bugtool.load_plugins(just_capabilities=False)
# TODO: Fix collect_data() for Python3
if sys.version_info < (3, 0):
bugtool.collect_data(subdir, archive)
bugtool.include_inventory(archive, subdir)
else:
with pytest.raises(TypeError) as e:
bugtool.include_inventory(archive, subdir)
if e: # To be fixed with the conversion to use io.BytesIO:
assert e.match(", not 'str'")
# without data, the exception is: "can't concat str to bytes")
return False
archive.close()
return True


@pytest.mark.skipif(sys.version_info >= (3, 0), reason="TarFile needs BytesIO IO")
def test_tar_output(bugtool, tmp_path, dom0_template):
"""Assert that a bugtool unit test creates a valid minimal tar archive"""

bugtool.BUG_DIR = tmp_path
archive = bugtool.TarOutput("tarball", "tar", -1)
subdir = "tardir"

# Create a minimal bugtool output archive to test core functions:
if not minimal_bugtool(bugtool, dom0_template, archive, subdir):
return # To be fixed with the conversion to use io.BytesIO

# Check the TarFile contents
tmp = str(tmp_path)
tarfile.TarFile(tmp + "/tarball.tar").extractall(tmp)
assert_mock_bugtool_plugin_output(tmp + "/" + subdir + "/")


def test_zip_output(bugtool, tmp_path, dom0_template):
"""Assert that a bugtool unit test creates a valid minimal zip archive"""

bugtool.BUG_DIR = tmp_path
archive = bugtool.ZipOutput("zipfile")
subdir = "zipdir"

# Create a minimal bugtool output archive to test core functions:
if not minimal_bugtool(bugtool, dom0_template, archive, subdir):
return # To be fixed with the conversion to use io.BytesIO

# Check the ZipFile contents
tmp = str(tmp_path)
zipfile.ZipFile(tmp + "/zipfile.zip").extractall(tmp)
assert_mock_bugtool_plugin_output(tmp + "/" + subdir + "/")
11 changes: 8 additions & 3 deletions xen-bugtool
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,13 @@ def get_recent_logs(logs, verbosity):
return [x[1] for x in (logs[-verbosity:] if verbosity < 9 else logs)]


def include_inventory(archive, dir):
"""Add the inventory.xml to the archive, filled from the current data"""

info = StringIOmtime(make_inventory(data, dir))
archive.add_path_with_data(construct_filename(dir, "inventory.xml", {}), info)


def collect_data(subdir, archive):
process_lists = {}

Expand Down Expand Up @@ -1182,9 +1189,7 @@ exclude those logs from the archive.
collect_data(subdir, archive)

# include inventory
inventory = {'cap': None, 'output': StringIOmtime(no_unicode(make_inventory(data, subdir)))}
archive.add_path_with_data(construct_filename(subdir, 'inventory.xml', inventory),
StringIOmtime(no_unicode(make_inventory(data, subdir))))
include_inventory(archive, subdir)

if archive.close():
res = 0
Expand Down

0 comments on commit 54e4fad

Please sign in to comment.