xunit: Create the `xfstestsdb xunit properties` command

This command prints out (key, value) pairs for each xunit property in
the database. It has options to filter by runid, xunit name, property
name, and value.

Implements: #14 (`xfstestsdb xunit properties`)
Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
This commit is contained in:
Anna Schumaker 2023-02-07 14:51:59 -05:00
parent 29065d0f8f
commit 84a7507998
3 changed files with 258 additions and 0 deletions

View File

@ -0,0 +1,185 @@
# Copyright 2023 (c) Anna Schumaker.
"""Tests the `xfstestsdb xunit properties` command."""
import io
import unittest
import unittest.mock
import xfstestsdb.xunit.properties
import tests.xunit
class TestXunitProperties(unittest.TestCase):
"""Tests the `xfstestsdb xunit properties` command."""
def setUp(self):
"""Set up common variables."""
self.xfstestsdb = xfstestsdb.Command()
self.xunit = self.xfstestsdb.commands["xunit"]
self.properties = self.xunit.commands["properties"]
def setup_runs(self, mock_stdout: io.StringIO):
"""Set up runs in the database and clear stdout."""
self.xfstestsdb.run(["new", "/dev/vda1"])
self.xfstestsdb.run(["new", "/dev/vda2"])
self.xfstestsdb.run(["xunit", "read", "1", str(tests.xunit.XUNIT_1)])
self.xfstestsdb.run(["xunit", "read", "2", str(tests.xunit.XUNIT_1),
"--name", "test-2"])
mock_stdout.seek(0)
mock_stdout.truncate(0)
def test_init(self):
"""Check that the xunit properties command was set up properly."""
self.assertIsInstance(self.properties, xfstestsdb.commands.Command)
self.assertIsInstance(self.properties,
xfstestsdb.xunit.properties.Command)
self.assertEqual(self.xunit.subparser.choices["properties"],
self.properties.parser)
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_list_empty(self, mock_stdout: io.StringIO):
"""Test printing out an empty property list."""
self.xfstestsdb.run(["xunit", "properties", "--color", "none"])
self.assertEqual(mock_stdout.getvalue(), "")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_list_properties(self, mock_stdout: io.StringIO):
"""Test listing properties with default options."""
self.setup_runs(mock_stdout)
print()
self.xfstestsdb.run(["xunit", "properties", "--color", "none"])
self.assertEqual(mock_stdout.getvalue(),
"""
+-----+--------+---------------+---------------------------------+
| run | xunit | property | value |
+-----+--------+---------------+---------------------------------+
| 1 | test-1 | CHECK_OPTIONS | -r -R xunit -g quick |
| 1 | test-1 | FSTYP | myfs |
| 1 | test-1 | HOST_OPTIONS | local.config |
| 1 | test-1 | LOAD_FACTOR | 1 |
| 1 | test-1 | MOUNT_OPTIONS | -o mountopt1,mountopt2 |
| 1 | test-1 | OVL_LOWER | ovl-lower |
| 1 | test-1 | OVL_UPPER | ovl-upper |
| 1 | test-1 | OVL_WORK | ovl-work |
| 1 | test-1 | PLATFORM | Linux/x86_64 myhost 6.1.8-arch1 |
| 1 | test-1 | SCRATCH_DEV | /dev/vdb2 |
| 1 | test-1 | SCRATCH_MNT | /mnt/scratch |
| 1 | test-1 | SECTION | -no-sections- |
| 1 | test-1 | TEST_DEV | /dev/vdb1 |
| 1 | test-1 | TEST_DIR | /mnt/test |
| 1 | test-1 | TIME_FACTOR | 1 |
| 2 | test-2 | CHECK_OPTIONS | -r -R xunit -g quick |
| 2 | test-2 | FSTYP | myfs |
| 2 | test-2 | HOST_OPTIONS | local.config |
| 2 | test-2 | LOAD_FACTOR | 1 |
| 2 | test-2 | MOUNT_OPTIONS | -o mountopt1,mountopt2 |
| 2 | test-2 | OVL_LOWER | ovl-lower |
| 2 | test-2 | OVL_UPPER | ovl-upper |
| 2 | test-2 | OVL_WORK | ovl-work |
| 2 | test-2 | PLATFORM | Linux/x86_64 myhost 6.1.8-arch1 |
| 2 | test-2 | SCRATCH_DEV | /dev/vdb2 |
| 2 | test-2 | SCRATCH_MNT | /mnt/scratch |
| 2 | test-2 | SECTION | -no-sections- |
| 2 | test-2 | TEST_DEV | /dev/vdb1 |
| 2 | test-2 | TEST_DIR | /mnt/test |
| 2 | test-2 | TIME_FACTOR | 1 |
+-----+--------+---------------+---------------------------------+
""")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_list_filter_run(self, mock_stdout: io.StringIO):
"""Test listing properties beloging to a specific run."""
self.setup_runs(mock_stdout)
print()
self.xfstestsdb.run(["xunit", "properties", "--runid", "1",
"--color", "none"])
self.assertEqual(mock_stdout.getvalue(),
"""
+-----+--------+---------------+---------------------------------+
| run | xunit | property | value |
+-----+--------+---------------+---------------------------------+
| 1 | test-1 | CHECK_OPTIONS | -r -R xunit -g quick |
| 1 | test-1 | FSTYP | myfs |
| 1 | test-1 | HOST_OPTIONS | local.config |
| 1 | test-1 | LOAD_FACTOR | 1 |
| 1 | test-1 | MOUNT_OPTIONS | -o mountopt1,mountopt2 |
| 1 | test-1 | OVL_LOWER | ovl-lower |
| 1 | test-1 | OVL_UPPER | ovl-upper |
| 1 | test-1 | OVL_WORK | ovl-work |
| 1 | test-1 | PLATFORM | Linux/x86_64 myhost 6.1.8-arch1 |
| 1 | test-1 | SCRATCH_DEV | /dev/vdb2 |
| 1 | test-1 | SCRATCH_MNT | /mnt/scratch |
| 1 | test-1 | SECTION | -no-sections- |
| 1 | test-1 | TEST_DEV | /dev/vdb1 |
| 1 | test-1 | TEST_DIR | /mnt/test |
| 1 | test-1 | TIME_FACTOR | 1 |
+-----+--------+---------------+---------------------------------+
""")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_list_filter_xunit(self, mock_stdout: io.StringIO):
"""Test listing properties matching an xunit pattern."""
self.setup_runs(mock_stdout)
print()
self.xfstestsdb.run(["xunit", "properties", "--xunit", "*-2",
"--color", "none"])
self.assertEqual(mock_stdout.getvalue(),
"""
+-----+--------+---------------+---------------------------------+
| run | xunit | property | value |
+-----+--------+---------------+---------------------------------+
| 2 | test-2 | CHECK_OPTIONS | -r -R xunit -g quick |
| 2 | test-2 | FSTYP | myfs |
| 2 | test-2 | HOST_OPTIONS | local.config |
| 2 | test-2 | LOAD_FACTOR | 1 |
| 2 | test-2 | MOUNT_OPTIONS | -o mountopt1,mountopt2 |
| 2 | test-2 | OVL_LOWER | ovl-lower |
| 2 | test-2 | OVL_UPPER | ovl-upper |
| 2 | test-2 | OVL_WORK | ovl-work |
| 2 | test-2 | PLATFORM | Linux/x86_64 myhost 6.1.8-arch1 |
| 2 | test-2 | SCRATCH_DEV | /dev/vdb2 |
| 2 | test-2 | SCRATCH_MNT | /mnt/scratch |
| 2 | test-2 | SECTION | -no-sections- |
| 2 | test-2 | TEST_DEV | /dev/vdb1 |
| 2 | test-2 | TEST_DIR | /mnt/test |
| 2 | test-2 | TIME_FACTOR | 1 |
+-----+--------+---------------+---------------------------------+
""")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_list_filter_properties(self, mock_stdout: io.StringIO):
"""Test listing properties matching a property key pattern."""
self.setup_runs(mock_stdout)
print()
self.xfstestsdb.run(["xunit", "properties", "--property", "TEST_*",
"--color", "none"])
self.assertEqual(mock_stdout.getvalue(),
"""
+-----+--------+----------+-----------+
| run | xunit | property | value |
+-----+--------+----------+-----------+
| 1 | test-1 | TEST_DEV | /dev/vdb1 |
| 1 | test-1 | TEST_DIR | /mnt/test |
| 2 | test-2 | TEST_DEV | /dev/vdb1 |
| 2 | test-2 | TEST_DIR | /mnt/test |
+-----+--------+----------+-----------+
""")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_list_filter_values(self, mock_stdout: io.StringIO):
"""Test listing properties matching a value pattern."""
self.setup_runs(mock_stdout)
print()
self.xfstestsdb.run(["xunit", "properties", "--value", "*vdb*",
"--color", "none"])
self.assertEqual(mock_stdout.getvalue(),
"""
+-----+--------+-------------+-----------+
| run | xunit | property | value |
+-----+--------+-------------+-----------+
| 1 | test-1 | SCRATCH_DEV | /dev/vdb2 |
| 1 | test-1 | TEST_DEV | /dev/vdb1 |
| 2 | test-2 | SCRATCH_DEV | /dev/vdb2 |
| 2 | test-2 | TEST_DEV | /dev/vdb1 |
+-----+--------+-------------+-----------+
""")

View File

@ -5,6 +5,7 @@ from .. import commands
from .. import sqlite
from . import delete
from . import list
from . import properties
from . import read
from . import rename
@ -20,6 +21,7 @@ class Command(commands.Command):
self.subparser = self.parser.add_subparsers(title="xunit commands")
self.commands = {"delete": delete.Command(self.subparser, sql),
"list": list.Command(self.subparser, sql),
"properties": properties.Command(self.subparser, sql),
"read": read.Command(self.subparser, sql),
"rename": rename.Command(self.subparser, sql)}

View File

@ -0,0 +1,71 @@
# Copyright 2023 (c) Anna Schumaker.
"""The `xfstestsdb xunit properties` command."""
import argparse
from .. import colors
from .. import commands
from .. import sqlite
from .. import table
class Command(commands.Command):
"""The `xfstestsdb xunit properties` command."""
def __init__(self, subparser: argparse.Action,
sql: sqlite.Connection) -> None:
"""Set up the xunit properties command."""
super().__init__(subparser, sql, "properties",
help="list the xfstest xunit properties")
self.parser.add_argument("--color", metavar="color", nargs="?",
choices=["light", "dark", "none"],
const=colors.get_default_colors(),
default=colors.get_default_colors(),
help="show properties with color output "
f"[default={colors.get_default_colors()}]")
self.parser.add_argument("--property", metavar="property", nargs=1,
help="show properties matching "
"the given pattern")
self.parser.add_argument("--runid", metavar="runid", nargs=1, type=int,
help="show properties belonging "
"to the given run")
self.parser.add_argument("--value", metavar="value", nargs=1,
help="show properties with a value "
"matching the given pattern")
self.parser.add_argument("--xunit", metavar="xunit", nargs=1,
help="show properties with an xunit "
"matching the given pattern")
def do_command(self, args: argparse.Namespace) -> None:
"""Print out the xfstestsdb xunits table."""
tbl = table.Table(["run", "xunit", "property", "value"],
["r", "l", "l", "l"], args.color)
sql_where = ""
sql_args = []
filter = []
if args.property:
filter.append("key GLOB ?")
sql_args.append(args.property[0])
if args.runid:
filter.append("runid=?")
sql_args.append(args.runid[0])
if args.value:
filter.append("value GLOB ?")
sql_args.append(args.value[0])
if args.xunit:
filter.append("xunit GLOB ?")
sql_args.append(args.xunit[0])
if len(filter) > 0:
sql_where = " WHERE " + " AND ".join(filter) + " "
cur = self.sql("""SELECT runid, xunit, key, value
FROM xunit_properties_view""" + sql_where + """
ORDER BY runid, xunit, key""", *sql_args)
for row in cur.fetchall():
tbl.add_row(row["runid"], row["xunit"], row["key"], row["value"])
#
if len(tbl.rows) > 0:
print(tbl)