Skip to content

Commit 16586a9

Browse files
Merge pull request xenserver#27 from xenserver-next/private/bernhardk/add-sar-system-load-testcase
Add system load testcase to check collecting sar output, improve verification code
2 parents 60ddc57 + 61b87bc commit 16586a9

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""tests/integration/test_system_load.py: Test xen-bugtool --entries=system-load"""
2+
import os
3+
4+
from utils import check_file, run_bugtool_entry, assert_content_from_dom0_template
5+
6+
7+
# In this test case we need to sleep for 1 sec, and it is sufficient
8+
# to test to only with zip archives to keep the test duration short:
9+
def test_system_load(output_archive_type="zip"):
10+
"""Test xen-bugtool --entries=system-load in test jail created by auto-fixtures in conftest.py"""
11+
entry = "system-load"
12+
13+
# Create test input files:
14+
os.mkdir("/var/log")
15+
os.mkdir("/var/log/sa")
16+
with open("/var/log/sa/sa01", "w") as sa01:
17+
sa01.write("sa01 test data")
18+
with open("/var/log/sa/sar31", "w") as sar31:
19+
sar31.write("sar31 test data")
20+
21+
# Create a dummy sar script to assert that xen-bugtool captures its output:
22+
os.environ["PATH"] = "/var:" + os.environ["PATH"]
23+
with open("/var/sar", "w") as sar:
24+
sar.write("#!/bin/sh\nsleep 1;cat /etc/xensource-inventory\n")
25+
os.chmod("/var/sar", 0o777)
26+
27+
run_bugtool_entry(output_archive_type, entry)
28+
29+
assert_content_from_dom0_template("sar-A.out", "etc/xensource-inventory")
30+
assert check_file("var/log/sa/sa01") == "sa01 test data"
31+
assert check_file("var/log/sa/sar31") == "sar31 test data"
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
"""tests/integration/test_xenserver_config.py: Test xen-bugtool --entries=xenserver-config"""
22
import os
33

4-
from utils import assert_cmd, check_file, run_bugtool_entry, verify_content_from_dom0_template
4+
from utils import assert_cmd, check_file, run_bugtool_entry, assert_content_from_dom0_template
55

66

77
def test_xenserver_config(output_archive_type):
88
"""Test xen-bugtool --entries=xenserver-config in test jail created by auto-fixtures in conftest.py"""
99
entry = "xenserver-config"
10+
1011
run_bugtool_entry(output_archive_type, entry)
12+
13+
# Assert that the bugtool output archive of --entries=xenserver-config matches our expectations for it:
1114
assert check_file("ls-lR-%opt%xensource.out").splitlines()[0] == "/opt/xensource:"
1215
assert check_file("ls-lR-%etc%xensource%static-vdis.out") == ""
1316
assert check_file("static-vdis-list.out") == "list"
17+
18+
# Assert the contents of the extracted etc/systemd.tar
1419
os.chdir("..")
20+
# etc/systemd.tar's toplevel directory is os.environ["XENRT_BUGTOOL_BASENAME"] (= entries for the test)
1521
assert_cmd(["tar", "xvf", entry + "/etc/systemd.tar"], entry + "/etc/systemd.tar")
22+
1623
os.chdir(entry)
17-
verify_content_from_dom0_template("etc/xensource-inventory")
18-
verify_content_from_dom0_template("etc/systemd")
24+
assert_content_from_dom0_template("etc/systemd")
25+
assert_content_from_dom0_template("etc/xensource-inventory")

tests/integration/utils.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from subprocess import getoutput
1818

1919
BUGTOOL_OUTPUT_DIR = "/var/opt/xen/bug-report/"
20-
BUGTOOL_DOM0_TEMPL = "tests/integration/dom0-template"
20+
BUGTOOL_DOM0_TEMPL = "tests/integration/dom0-template/"
2121

2222

2323
def run(command):
@@ -49,11 +49,21 @@ def check_file(path):
4949
return contents
5050

5151

52-
def verify_content_from_dom0_template(path):
52+
def assert_content_from_dom0_template(path, control_path=None):
5353
"""Compare the contents of output directories or files with the test's Dom0 template directories"""
5454
assert path[0] != "/"
55-
assert filecmp.dircmp(path, BUGTOOL_DOM0_TEMPL + path)
56-
# After successfuly verficiation of the files, remove the checked output files (missed files remain):
55+
control = BUGTOOL_DOM0_TEMPL + (control_path or path)
56+
print(control)
57+
if os.path.isdir(path):
58+
result = filecmp.dircmp(path, control)
59+
if result.diff_files or result.right_only:
60+
print(result.report)
61+
raise RuntimeError("Missing or Differing files found in " + path)
62+
else:
63+
if not filecmp.cmp(path, control):
64+
os.system("cat " + path)
65+
raise RuntimeError(control)
66+
# Remove verified output files/directories. Untested files will remain and cause the testcase to FAIL:
5767
try:
5868
os.unlink(path)
5969
except OSError:
@@ -99,3 +109,5 @@ def run_bugtool_entry(archive_type, test_entries):
99109
etree.XMLSchema(etree.parse(xmlschema)).assertValid(etree.parse("inventory.xml"))
100110
# After successfuly validation of the inventory.xml, remove it (not removed files make the test fail):
101111
os.unlink("inventory.xml")
112+
# assert_content_from_dom0_template() does not know the srcdir: add a symlink so it can reach the tests
113+
os.symlink(srcdir + "/tests", "tests")

0 commit comments

Comments
 (0)