Anna Schumaker
e27bc9f5ae
This command prints out information about the known xunit files. It has extra options for filtering by runid, device, name, and hostname. I also gave it optional Timestamp and Results columns that the user can enable. Implements: #12 (`xfstestsdb xunit list`) Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
192 lines
8.0 KiB
Python
192 lines
8.0 KiB
Python
# Copyright 2023 (c) Anna Schumaker.
|
|
"""Tests the `xfstestsdb xunit list` command."""
|
|
import datetime
|
|
import dateutil.tz
|
|
import io
|
|
import unittest
|
|
import unittest.mock
|
|
import xfstestsdb.xunit.list
|
|
import tests.xunit
|
|
|
|
|
|
class TestXunitList(unittest.TestCase):
|
|
"""Tests the `xfstestsdb xunit list` command."""
|
|
|
|
def setUp(self):
|
|
"""Set up common variables."""
|
|
self.xfstestsdb = xfstestsdb.Command()
|
|
self.xunit = self.xfstestsdb.commands["xunit"]
|
|
self.list = self.xunit.commands["list"]
|
|
|
|
def setup_runs(self, mock_stdout: io.StringIO):
|
|
"""Set up runs in the database and clear stdout."""
|
|
timestamp = datetime.datetime(2023, 1, 29, 14, 14, 14)
|
|
timestamp = timestamp.astimezone(tz=dateutil.tz.tzutc())
|
|
timestamp = timestamp.replace(tzinfo=None)
|
|
|
|
self.xfstestsdb.run(["new", "/dev/vda1"])
|
|
self.xfstestsdb.run(["new", "/dev/vda2"])
|
|
self.xfstestsdb.run(["new", "/dev/vdb1"])
|
|
|
|
self.xfstestsdb.run(["xunit", "read", "1", str(tests.xunit.XUNIT_1)])
|
|
self.xfstestsdb.run(["xunit", "read", "1", str(tests.xunit.XUNIT_1),
|
|
"--name", "test-2"])
|
|
self.xfstestsdb.run(["xunit", "read", "3", str(tests.xunit.XUNIT_1)])
|
|
|
|
self.xfstestsdb.sql.executemany("UPDATE xunits SET timestamp=? "
|
|
"WHERE rowid=?", (timestamp, 1),
|
|
(timestamp.replace(day=30), 2),
|
|
(timestamp.replace(day=31), 3))
|
|
self.xfstestsdb.sql("UPDATE xunits SET hostname=? WHERE rowid=?",
|
|
"myhost2", 3)
|
|
|
|
mock_stdout.seek(0)
|
|
mock_stdout.truncate(0)
|
|
|
|
def test_init(self):
|
|
"""Check that the xunit list command was set up properly."""
|
|
self.assertIsInstance(self.list, xfstestsdb.commands.Command)
|
|
self.assertIsInstance(self.list, xfstestsdb.xunit.list.Command)
|
|
self.assertEqual(self.xunit.subparser.choices["list"],
|
|
self.list.parser)
|
|
|
|
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
|
def test_list_empty(self, mock_stdout: io.StringIO):
|
|
"""Test printing out an empty list."""
|
|
self.xfstestsdb.run(["xunit", "list", "--color", "none"])
|
|
self.assertEqual(mock_stdout.getvalue(), "")
|
|
|
|
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
|
def test_list_xunits(self, mock_stdout: io.StringIO):
|
|
"""Test listing xunits with default options."""
|
|
self.setup_runs(mock_stdout)
|
|
print()
|
|
self.xfstestsdb.run(["xunit", "list", "--color", "none"])
|
|
self.assertEqual(mock_stdout.getvalue(),
|
|
"""
|
|
+-----+-----------+--------+----------+
|
|
| run | device | xunit | hostname |
|
|
+-----+-----------+--------+----------+
|
|
| 1 | /dev/vda1 | test-1 | myhost |
|
|
| 1 | /dev/vda1 | test-2 | myhost |
|
|
| 3 | /dev/vdb1 | test-1 | myhost2 |
|
|
+-----+-----------+--------+----------+
|
|
""")
|
|
|
|
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
|
def test_list_filter_run(self, mock_stdout: io.StringIO):
|
|
"""Test listing xunits beloging to a specific run."""
|
|
self.setup_runs(mock_stdout)
|
|
print()
|
|
self.xfstestsdb.run(["xunit", "list", "--runid", "1",
|
|
"--color", "none"])
|
|
self.assertEqual(mock_stdout.getvalue(),
|
|
"""
|
|
+-----+-----------+--------+----------+
|
|
| run | device | xunit | hostname |
|
|
+-----+-----------+--------+----------+
|
|
| 1 | /dev/vda1 | test-1 | myhost |
|
|
| 1 | /dev/vda1 | test-2 | myhost |
|
|
+-----+-----------+--------+----------+
|
|
""")
|
|
|
|
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
|
def test_list_filter_device(self, mock_stdout: io.StringIO):
|
|
"""Test listing xunits matching a device pattern."""
|
|
self.setup_runs(mock_stdout)
|
|
print()
|
|
self.xfstestsdb.run(["xunit", "list", "--device", "*vdb*",
|
|
"--color", "none"])
|
|
self.assertEqual(mock_stdout.getvalue(),
|
|
"""
|
|
+-----+-----------+--------+----------+
|
|
| run | device | xunit | hostname |
|
|
+-----+-----------+--------+----------+
|
|
| 3 | /dev/vdb1 | test-1 | myhost2 |
|
|
+-----+-----------+--------+----------+
|
|
""")
|
|
|
|
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
|
def test_list_filter_xunit(self, mock_stdout: io.StringIO):
|
|
"""Test listing xunits matching a name pattern."""
|
|
self.setup_runs(mock_stdout)
|
|
print()
|
|
self.xfstestsdb.run(["xunit", "list", "--xunit", "test*1",
|
|
"--color", "none"])
|
|
self.assertEqual(mock_stdout.getvalue(),
|
|
"""
|
|
+-----+-----------+--------+----------+
|
|
| run | device | xunit | hostname |
|
|
+-----+-----------+--------+----------+
|
|
| 1 | /dev/vda1 | test-1 | myhost |
|
|
| 3 | /dev/vdb1 | test-1 | myhost2 |
|
|
+-----+-----------+--------+----------+
|
|
""")
|
|
|
|
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
|
def test_list_filter_hostname(self, mock_stdout: io.StringIO):
|
|
"""Test listing runs with a specific timestamp pattern."""
|
|
self.setup_runs(mock_stdout)
|
|
print()
|
|
self.xfstestsdb.run(["xunit", "list", "--hostname", "my*2",
|
|
"--color", "none"])
|
|
self.assertEqual(mock_stdout.getvalue(),
|
|
"""
|
|
+-----+-----------+--------+----------+
|
|
| run | device | xunit | hostname |
|
|
+-----+-----------+--------+----------+
|
|
| 3 | /dev/vdb1 | test-1 | myhost2 |
|
|
+-----+-----------+--------+----------+
|
|
""")
|
|
|
|
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
|
def test_list_xunit_timestamp(self, mock_stdout: io.StringIO):
|
|
"""Test listing runs with the timestamp column added."""
|
|
self.setup_runs(mock_stdout)
|
|
print()
|
|
self.xfstestsdb.run(["xunit", "list", "--timestamp",
|
|
"--color", "none"])
|
|
self.assertEqual(mock_stdout.getvalue(),
|
|
"""
|
|
+-----+-----------+--------+----------+---------------------+
|
|
| run | device | xunit | hostname | timestamp |
|
|
+-----+-----------+--------+----------+---------------------+
|
|
| 1 | /dev/vda1 | test-1 | myhost | 2023-01-29 14:14:14 |
|
|
| 1 | /dev/vda1 | test-2 | myhost | 2023-01-30 14:14:14 |
|
|
| 3 | /dev/vdb1 | test-1 | myhost2 | 2023-01-31 14:14:14 |
|
|
+-----+-----------+--------+----------+---------------------+
|
|
""")
|
|
|
|
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
|
def test_list_filter_timestamp(self, mock_stdout: io.StringIO):
|
|
"""Test listing runs with a specific timestamp pattern."""
|
|
self.setup_runs(mock_stdout)
|
|
print()
|
|
self.xfstestsdb.run(["xunit", "list", "--timestamp", "*31 14:*",
|
|
"--color", "none"])
|
|
self.assertEqual(mock_stdout.getvalue(),
|
|
"""
|
|
+-----+-----------+--------+----------+---------------------+
|
|
| run | device | xunit | hostname | timestamp |
|
|
+-----+-----------+--------+----------+---------------------+
|
|
| 3 | /dev/vdb1 | test-1 | myhost2 | 2023-01-31 14:14:14 |
|
|
+-----+-----------+--------+----------+---------------------+
|
|
""")
|
|
|
|
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
|
def test_list_xunit_results(self, mock_stdout: io.StringIO):
|
|
"""Test listing xunits with the results columns added."""
|
|
self.setup_runs(mock_stdout)
|
|
print()
|
|
self.xfstestsdb.run(["xunit", "list", "--color", "none", "--results"])
|
|
self.assertEqual(mock_stdout.getvalue(),
|
|
"""
|
|
+-----+-----------+--------+----------+------+------+------+------+
|
|
| run | device | xunit | hostname | pass | fail | skip | time |
|
|
+-----+-----------+--------+----------+------+------+------+------+
|
|
| 1 | /dev/vda1 | test-1 | myhost | 6 | 1 | 3 | 43 s |
|
|
| 1 | /dev/vda1 | test-2 | myhost | 6 | 1 | 3 | 43 s |
|
|
| 3 | /dev/vdb1 | test-1 | myhost2 | 6 | 1 | 3 | 43 s |
|
|
+-----+-----------+--------+----------+------+------+------+------+
|
|
""")
|