Anna Schumaker
68a00ea94d
This command is used to garbage collect the xunit table by removing xunits that have no testcases. This could be expanded on later to add more removal conditions, such as xunits older than some age. Implements: #15 (`xfstestsdb xunit gc`) Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
71 lines
2.9 KiB
Python
71 lines
2.9 KiB
Python
# Copyright 2023 (c) Anna Schumaker.
|
|
"""Tests the `xfstestsdb xunit gc` command."""
|
|
import io
|
|
import unittest
|
|
import unittest.mock
|
|
import xfstestsdb.xunit.gc
|
|
import tests.xunit
|
|
|
|
|
|
class TestXunitGC(unittest.TestCase):
|
|
"""Tests the `xfstestsdb xunit gc` command."""
|
|
|
|
def setUp(self):
|
|
"""Set up common variables."""
|
|
self.xfstestsdb = xfstestsdb.Command()
|
|
self.xunit = self.xfstestsdb.commands["xunit"]
|
|
self.gc = self.xunit.commands["gc"]
|
|
|
|
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/vda1"])
|
|
|
|
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", "2", str(tests.xunit.XUNIT_1)])
|
|
|
|
self.xfstestsdb.sql("DELETE FROM testcases WHERE xunitid=?", 2)
|
|
self.xfstestsdb.sql("DELETE FROM testcases WHERE xunitid=?", 3)
|
|
|
|
mock_stdout.seek(0)
|
|
mock_stdout.truncate(0)
|
|
|
|
def test_init(self):
|
|
"""Check that the xunit gc command was set up properly."""
|
|
self.assertIsInstance(self.gc, xfstestsdb.commands.Command)
|
|
self.assertIsInstance(self.gc, xfstestsdb.xunit.gc.Command)
|
|
self.assertEqual(self.xunit.subparser.choices["gc"],
|
|
self.gc.parser)
|
|
|
|
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
|
def test_gc_empty(self, mock_stdout: io.StringIO):
|
|
"""Test garbage collecting an empty database."""
|
|
self.xfstestsdb.run(["xunit", "gc"])
|
|
self.assertEqual(mock_stdout.getvalue(), "")
|
|
|
|
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
|
def test_gc_xunits(self, mock_stdout: io.StringIO):
|
|
"""Test garbage collecting xunits with default options."""
|
|
self.setup_runs(mock_stdout)
|
|
self.xfstestsdb.run(["xunit", "gc"])
|
|
self.assertRegex(mock_stdout.getvalue(),
|
|
"run #1 xunit 'test-2' has been deleted\n"
|
|
"run #2 xunit 'test-1' has been deleted")
|
|
|
|
cur = self.xfstestsdb.sql("SELECT COUNT(*) FROM xunits")
|
|
self.assertEqual(cur.fetchone()["COUNT(*)"], 1)
|
|
|
|
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
|
def test_gc_xunits_dry_run(self, mock_stdout: io.StringIO):
|
|
"""Test garbage collecting xunits with the --dry-run option."""
|
|
self.setup_runs(mock_stdout)
|
|
self.xfstestsdb.run(["xunit", "gc", "--dry-run"])
|
|
self.assertRegex(mock_stdout.getvalue(),
|
|
"run #1 xunit 'test-2' would be deleted\n"
|
|
"run #2 xunit 'test-1' would be deleted")
|
|
|
|
cur = self.xfstestsdb.sql("SELECT COUNT(*) FROM xunits")
|
|
self.assertEqual(cur.fetchone()["COUNT(*)"], 3)
|