-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests/unit/test_output.py: Test mock bugtool plugin output files
- Loading branch information
1 parent
a1cfd0d
commit 54e4fad
Showing
5 changed files
with
96 additions
and
3 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/bin/cat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "/") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters