From 339ab935cdb413fa811efacf6363c0cbc1998c55 Mon Sep 17 00:00:00 2001 From: Jake Hunsaker Date: Fri, 19 Aug 2022 13:23:59 -0400 Subject: [Plugin] Allow for manual collections during collect phase Historically, sos has limited collections to existing files and command output. While there have been many reasons for this, there have also been several exceptions made to current plugins as well as ongoing requests for data that is not currently provided via existing commands. As sos evolves, it should in turn be more capable to provide diagnostic data beyond what is strictly available via command outputs. As such, add a new step to the collection phase that allows plugins to perform these manual data collections. Plugins may now define their own `collect()` method to do so, thus moving the existing exceptions out of `setup()` phase execution. To aide in writing these collections to the plugin directory, a new `collection_file()` generator has been added which will handle creating, managing, and closing the new file so that plugins only need to be concerned with the content being written to such files. Plugin contributors should note that these manual collections are executed at the end of the collection phase - meaning they are more likely to be skipped or interrupted due to plugin timeouts. Closes: #2992 Signed-off-by: Jake Hunsaker --- .../plugin_tests/collet_manual_tests.py | 37 ++++++++++++++++++++++ .../plugin_tests/string_collection_tests.py | 37 ---------------------- tests/unittests/plugin_tests.py | 19 ++++++----- 3 files changed, 46 insertions(+), 47 deletions(-) create mode 100644 tests/report_tests/plugin_tests/collet_manual_tests.py delete mode 100644 tests/report_tests/plugin_tests/string_collection_tests.py (limited to 'tests') diff --git a/tests/report_tests/plugin_tests/collet_manual_tests.py b/tests/report_tests/plugin_tests/collet_manual_tests.py new file mode 100644 index 00000000..fdcda526 --- /dev/null +++ b/tests/report_tests/plugin_tests/collet_manual_tests.py @@ -0,0 +1,37 @@ +# This file is part of the sos project: https://github.com/sosreport/sos +# +# This copyrighted material is made available to anyone wishing to use, +# modify, copy, or redistribute it subject to the terms and conditions of +# version 2 of the GNU General Public License. +# +# See the LICENSE file in the source distribution for further information. + + +from sos_tests import StageOneReportTest + + +class CollectManualTest(StageOneReportTest): + """Test to ensure that collect() is working for plugins that + directly call it as part of their collections + + :avocado: tags=stageone + """ + + sos_cmd = '-o unpackaged,python -k python.hashes' + # unpackaged is only a RedHatPlugin + redhat_only = True + + def test_unpackaged_list_collected(self): + self.assertFileCollected('sos_commands/unpackaged/unpackaged') + + def test_python_hashes_collected(self): + self.assertFileCollected('sos_commands/python/digests.json') + + def test_no_strings_dir(self): + self.assertFileNotCollected('sos_strings/') + + def test_manifest_collections_correct(self): + pkgman = self.get_plugin_manifest('unpackaged') + self.assertTrue(pkgman['collections']['unpackaged']) + pyman = self.get_plugin_manifest('python') + self.assertTrue(pyman['collections']['digests']) diff --git a/tests/report_tests/plugin_tests/string_collection_tests.py b/tests/report_tests/plugin_tests/string_collection_tests.py deleted file mode 100644 index 05091a2e..00000000 --- a/tests/report_tests/plugin_tests/string_collection_tests.py +++ /dev/null @@ -1,37 +0,0 @@ -# This file is part of the sos project: https://github.com/sosreport/sos -# -# This copyrighted material is made available to anyone wishing to use, -# modify, copy, or redistribute it subject to the terms and conditions of -# version 2 of the GNU General Public License. -# -# See the LICENSE file in the source distribution for further information. - - -from sos_tests import StageOneReportTest - - -class CollectStringTest(StageOneReportTest): - """Test to ensure that add_string_as_file() is working for plugins that - directly call it as part of their collections - - :avocado: tags=stageone - """ - - sos_cmd = '-o unpackaged,python -k python.hashes' - # unpackaged is only a RedHatPlugin - redhat_only = True - - def test_unpackaged_list_collected(self): - self.assertFileCollected('sos_commands/unpackaged/unpackaged') - - def test_python_hashes_collected(self): - self.assertFileCollected('sos_commands/python/digests.json') - - def test_no_strings_dir(self): - self.assertFileNotCollected('sos_strings/') - - def test_manifest_strings_correct(self): - pkgman = self.get_plugin_manifest('unpackaged') - self.assertTrue(pkgman['strings']['unpackaged']) - pyman = self.get_plugin_manifest('python') - self.assertTrue(pyman['strings']['digests_json']) diff --git a/tests/unittests/plugin_tests.py b/tests/unittests/plugin_tests.py index 1d7cc96d..8170a1dd 100644 --- a/tests/unittests/plugin_tests.py +++ b/tests/unittests/plugin_tests.py @@ -296,7 +296,7 @@ class PluginTests(unittest.TestCase): }) p.archive = MockArchive() p.setup() - p.collect() + p.collect_plugin() self.assertEquals(p.archive.m, {}) def test_postproc_default_on(self): @@ -358,10 +358,10 @@ class AddCopySpecTests(unittest.TestCase): self.mp.sysroot = '/' fn = create_file(2) # create 2MB file, consider a context manager self.mp.add_copy_spec(fn, 1) - content, fname, _tags = self.mp.copy_strings[0] - self.assertTrue("tailed" in fname) + fname, _size = self.mp._tail_files_list[0] + self.assertTrue(fname == fn) self.assertTrue("tmp" in fname) - self.assertEquals(1024 * 1024, len(content)) + self.assertEquals(1024 * 1024, _size) os.unlink(fn) def test_bad_filename(self): @@ -388,10 +388,9 @@ class AddCopySpecTests(unittest.TestCase): create_file(2, dir=tmpdir) create_file(2, dir=tmpdir) self.mp.add_copy_spec(tmpdir + "/*", 1) - self.assertEquals(len(self.mp.copy_strings), 1) - content, fname, _tags = self.mp.copy_strings[0] - self.assertTrue("tailed" in fname) - self.assertEquals(1024 * 1024, len(content)) + self.assertEquals(len(self.mp._tail_files_list), 1) + fname, _size = self.mp._tail_files_list[0] + self.assertEquals(1024 * 1024, _size) shutil.rmtree(tmpdir) def test_multiple_files_no_limit(self): @@ -450,7 +449,7 @@ class RegexSubTests(unittest.TestCase): def test_no_replacements(self): self.mp.sysroot = '/' self.mp.add_copy_spec(j("tail_test.txt")) - self.mp.collect() + self.mp.collect_plugin() replacements = self.mp.do_file_sub( j("tail_test.txt"), r"wont_match", "foobar") self.assertEquals(0, replacements) @@ -459,7 +458,7 @@ class RegexSubTests(unittest.TestCase): # test uses absolute paths self.mp.sysroot = '/' self.mp.add_copy_spec(j("tail_test.txt")) - self.mp.collect() + self.mp.collect_plugin() replacements = self.mp.do_file_sub( j("tail_test.txt"), r"(tail)", "foobar") self.assertEquals(1, replacements) -- cgit