From 6ea48cbbc85d007dfefd1f254db66ff2e0a9cec5 Mon Sep 17 00:00:00 2001 From: Jesse Jaggars Date: Wed, 14 Dec 2011 16:05:59 -0600 Subject: Major updates to most of SoSReport Code reorganization Cross platform support for Windows, OS X and Linux Dynamically loaded policies Support for loading plugins from multiple locations via __path__ modification of sos.plugins Support for running via Jython Support for executing from a jarfile Support for json based reporting infrastructure - Previous reporting methods still exist Support for other checksum algorithms (determined by policy) Support for other compression algorithms (determined by policy) New plugin API for writing arbitrary information in a new file inside the report archive. New plugin API for modifying files that have been added to the archive. Added API for global plugin options - external interface is unavailable at this time Many small bugfixes --- tests/archive_tests.py | 130 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/report_tests.py | 118 ++++++++++++++++++++++++++++++++++++++++++++ tests/worker_link | 1 + 3 files changed, 249 insertions(+) create mode 100755 tests/archive_tests.py create mode 100644 tests/report_tests.py create mode 120000 tests/worker_link (limited to 'tests') diff --git a/tests/archive_tests.py b/tests/archive_tests.py new file mode 100755 index 00000000..01c46395 --- /dev/null +++ b/tests/archive_tests.py @@ -0,0 +1,130 @@ +#!/usr/bin/env python + +import unittest +import os +import tarfile +import zipfile + +from sos.utilities import TarFileArchive, ZipFileArchive + +class ZipFileArchiveTest(unittest.TestCase): + + def setUp(self): + self.zf = ZipFileArchive('test') + + def tearDown(self): + os.unlink('test.zip') + + def check_for_file(self, filename): + zf = zipfile.ZipFile('test.zip', 'r') + zf.getinfo(filename) + zf.close() + + def test_create(self): + self.zf.close() + + def test_add_file(self): + self.zf.add_file('tests/worker.py') + self.zf.close() + + self.check_for_file('test/tests/worker.py') + + def test_add_dir(self): + self.zf.add_file('tests/') + self.zf.close() + + self.check_for_file('test/tests/worker.py') + + def test_add_renamed(self): + self.zf.add_file('tests/worker.py', dest='tests/worker_renamed.py') + self.zf.close() + + self.check_for_file('test/tests/worker_renamed.py') + + def test_add_renamed_dir(self): + self.zf.add_file('tests/', 'tests_renamed/') + self.zf.close() + + self.check_for_file('test/tests_renamed/worker.py') + + def test_add_string(self): + self.zf.add_string('this is content', 'tests/string_test.txt') + self.zf.close() + + self.check_for_file('test/tests/string_test.txt') + + def test_get_file(self): + self.zf.add_string('this is my content', 'tests/string_test.txt') + + afp = self.zf.open_file('tests/string_test.txt') + self.assertEquals('this is my content', afp.read()) + + def test_overwrite_file(self): + self.zf.add_string('this is my content', 'tests/string_test.txt') + self.zf.add_string('this is my new content', 'tests/string_test.txt') + + afp = self.zf.open_file('tests/string_test.txt') + self.assertEquals('this is my new content', afp.read()) + +class TarFileArchiveTest(unittest.TestCase): + + def setUp(self): + self.tf = TarFileArchive('test') + + def tearDown(self): + os.unlink('test.tar') + + def check_for_file(self, filename): + rtf = tarfile.open('test.tar') + rtf.getmember(filename) + rtf.close() + + def test_create(self): + self.tf.close() + self.assertTrue(os.path.exists('test.tar')) + + def test_add_file(self): + self.tf.add_file('tests/worker.py') + self.tf.close() + + self.check_for_file('test/tests/worker.py') + + def test_add_dir(self): + self.tf.add_file('tests/') + self.tf.close() + + self.check_for_file('test/tests/worker.py') + + def test_add_renamed(self): + self.tf.add_file('tests/worker.py', dest='tests/worker_renamed.py') + self.tf.close() + + self.check_for_file('test/tests/worker_renamed.py') + + def test_add_renamed_dir(self): + self.tf.add_file('tests/', 'tests_renamed/') + self.tf.close() + + self.check_for_file('test/tests_renamed/worker.py') + + def test_add_string(self): + self.tf.add_string('this is content', 'tests/string_test.txt') + self.tf.close() + + self.check_for_file('test/tests/string_test.txt') + + def test_get_file(self): + self.tf.add_string('this is my content', 'tests/string_test.txt') + + afp = self.tf.open_file('tests/string_test.txt') + self.assertEquals('this is my content', afp.read()) + + def test_overwrite_file(self): + self.tf.add_string('this is my content', 'tests/string_test.txt') + self.tf.add_string('this is my new content', 'tests/string_test.txt') + + afp = self.tf.open_file('tests/string_test.txt') + self.assertEquals('this is my new content', afp.read()) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/report_tests.py b/tests/report_tests.py new file mode 100644 index 00000000..a08f0aec --- /dev/null +++ b/tests/report_tests.py @@ -0,0 +1,118 @@ +#!/usr/bin/env python + +import unittest +import os + +try: + import json +except ImportError: + import simplejson as json + +from sos.reporting import Report, Section, Command, CopiedFile, CreatedFile, Alert +from sos.reporting import PlainTextReport + +class ReportTest(unittest.TestCase): + + def test_empty(self): + report = Report() + + expected = json.dumps({}) + + self.assertEquals(expected, str(report)) + + def test_nested_section(self): + report = Report() + section = Section(name="section") + report.add(section) + + expected = json.dumps({"section": {}}) + + self.assertEquals(expected, str(report)) + + def test_multiple_sections(self): + report = Report() + section = Section(name="section") + report.add(section) + + section2 = Section(name="section2") + report.add(section2) + + expected = json.dumps({"section": {}, + "section2": {},}) + + self.assertEquals(expected, str(report)) + + + def test_deeply_nested(self): + report = Report() + section = Section(name="section") + command = Command(name="a command", return_code=0, href="does/not/matter") + + section.add(command) + report.add(section) + + expected = json.dumps({"section": {"commands": [{"name": "a command", + "return_code": 0, + "href": "does/not/matter"}]}}) + + self.assertEquals(expected, str(report)) + + +class TestPlainReport(unittest.TestCase): + + def setUp(self): + self.report = Report() + self.section = Section(name="plugin") + self.div = PlainTextReport.DIVIDER + + def test_basic(self): + self.assertEquals("", str(PlainTextReport(self.report))) + + def test_one_section(self): + self.report.add(self.section) + + self.assertEquals("plugin\n" + self.div, str(PlainTextReport(self.report))) + + def test_two_sections(self): + section1 = Section(name="first") + section2 = Section(name="second") + self.report.add(section1, section2) + + self.assertEquals("first\n" + self.div + "\nsecond\n" + self.div, str(PlainTextReport(self.report))) + + def test_command(self): + cmd = Command(name="ls -al /foo/bar/baz", + return_code=0, + href="sos_commands/plugin/ls_-al_foo.bar.baz") + self.section.add(cmd) + self.report.add(self.section) + + self.assertEquals("plugin\n" + self.div + "\n- commands executed:\n * ls -al /foo/bar/baz", + str(PlainTextReport(self.report))) + + def test_copied_file(self): + cf = CopiedFile(name="/etc/hosts", href="etc/hosts") + self.section.add(cf) + self.report.add(self.section) + + self.assertEquals("plugin\n" + self.div + "\n- files copied:\n * /etc/hosts", + str(PlainTextReport(self.report))) + + def test_created_file(self): + crf = CreatedFile(name="sample.txt") + self.section.add(crf) + self.report.add(self.section) + + self.assertEquals("plugin\n" + self.div + "\n- files created:\n * sample.txt", + str(PlainTextReport(self.report))) + + def test_alert(self): + alrt = Alert("this is an alert") + self.section.add(alrt) + self.report.add(self.section) + + self.assertEquals("plugin\n" + self.div + "\n- alerts:\n ! this is an alert", + str(PlainTextReport(self.report))) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/worker_link b/tests/worker_link new file mode 120000 index 00000000..20be81a3 --- /dev/null +++ b/tests/worker_link @@ -0,0 +1 @@ +worker.py \ No newline at end of file -- cgit