From 2f4d6b60f5c5581a0e53ffa77b747485b1e64d7b Mon Sep 17 00:00:00 2001 From: Micah Anderson Date: Thu, 30 May 2013 14:45:06 -0400 Subject: initial debian packaging --- .../share/pyshared/leap/common/testing/__init__.py | 0 .../share/pyshared/leap/common/testing/basetest.py | 140 +++++++++++++++++++++ .../pyshared/leap/common/testing/https_server.py | 87 +++++++++++++ .../pyshared/leap/common/testing/test_basetest.py | 140 +++++++++++++++++++++ 4 files changed, 367 insertions(+) create mode 100644 debian/python-leap.common/usr/share/pyshared/leap/common/testing/__init__.py create mode 100644 debian/python-leap.common/usr/share/pyshared/leap/common/testing/basetest.py create mode 100644 debian/python-leap.common/usr/share/pyshared/leap/common/testing/https_server.py create mode 100644 debian/python-leap.common/usr/share/pyshared/leap/common/testing/test_basetest.py (limited to 'debian/python-leap.common/usr/share/pyshared/leap/common/testing') diff --git a/debian/python-leap.common/usr/share/pyshared/leap/common/testing/__init__.py b/debian/python-leap.common/usr/share/pyshared/leap/common/testing/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/debian/python-leap.common/usr/share/pyshared/leap/common/testing/basetest.py b/debian/python-leap.common/usr/share/pyshared/leap/common/testing/basetest.py new file mode 100644 index 0000000..65e23a9 --- /dev/null +++ b/debian/python-leap.common/usr/share/pyshared/leap/common/testing/basetest.py @@ -0,0 +1,140 @@ +# -*- coding: utf-8 -*- +# basetest.py +# Copyright (C) 2013 LEAP +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +""" +Common testing facilities +""" +import os +import platform +import shutil +import tempfile + +try: + import unittest2 as unittest +except ImportError: + import unittest + +from leap.common.check import leap_assert +from leap.common.files import mkdir_p, check_and_fix_urw_only + + +class BaseLeapTest(unittest.TestCase): + """ + Base Leap TestCase + """ + __name__ = "leap_test" + _system = platform.system() + + @classmethod + def setUpClass(cls): + """ + Sets up common facilities for testing this TestCase: + - custom PATH and HOME environmental variables + - creates a temporal folder to which those point. + It saves the old path and home vars so they can be restored later. + """ + cls.old_path = os.environ['PATH'] + cls.old_home = os.environ['HOME'] + cls.tempdir = tempfile.mkdtemp(prefix="leap_tests-") + cls.home = cls.tempdir + bin_tdir = os.path.join( + cls.tempdir, + 'bin') + os.environ["PATH"] = bin_tdir + os.environ["HOME"] = cls.tempdir + + @classmethod + def tearDownClass(cls): + """ + Cleanup common facilities used for testing this TestCase: + - restores the default PATH and HOME variables + - removes the temporal folder + """ + os.environ["PATH"] = cls.old_path + os.environ["HOME"] = cls.old_home + # safety check! please do not wipe my home... + # XXX needs to adapt to non-linuces + leap_assert( + cls.tempdir.startswith('/tmp/leap_tests-'), + "beware! tried to remove a dir which does not " + "live in temporal folder!") + shutil.rmtree(cls.tempdir) + + # you have to override these methods + # this way we ensure we did not put anything + # here that you can forget to call. + + def setUp(self): + """not implemented""" + raise NotImplementedError("abstract base class") + + def tearDown(self): + """not implemented""" + raise NotImplementedError("abstract base class") + + # + # helper methods + # + + def _missing_test_for_plat(self, do_raise=False): + """ + Raises NotImplementedError for this platform + if do_raise is True + + @param do_raise: flag to actually raise exception + @type do_raise: bool + """ + if do_raise: + raise NotImplementedError( + "This test is not implemented " + "for the running platform: %s" % + self._system) + + def get_tempfile(self, filename): + """ + Returns the path of a given filename + prepending the temporal dir associated with this + TestCase + + @param filename: the filename + @type filename: str + """ + return os.path.join(self.tempdir, filename) + + def touch(self, filepath): + """ + Touches a filepath, creating folders along + the way if needed. + + @param filepath: path to be touched + @type filepath: str + """ + folder, filename = os.path.split(filepath) + if not os.path.isdir(folder): + mkdir_p(folder) + self.assertTrue(os.path.isdir(folder)) + with open(filepath, 'w') as fp: + fp.write(' ') + self.assertTrue(os.path.isfile(filepath)) + + def chmod600(self, filepath): + """ + Chmods 600 a file + + @param filepath: filepath to be chmodded + @type filepath: str + """ + check_and_fix_urw_only(filepath) diff --git a/debian/python-leap.common/usr/share/pyshared/leap/common/testing/https_server.py b/debian/python-leap.common/usr/share/pyshared/leap/common/testing/https_server.py new file mode 100644 index 0000000..08d5089 --- /dev/null +++ b/debian/python-leap.common/usr/share/pyshared/leap/common/testing/https_server.py @@ -0,0 +1,87 @@ +# -*- coding: utf-8 -*- +# leap.common.testing.https_server.py +# Copyright (C) 2013 LEAP +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +""" +A simple HTTPS server to be used in tests +""" +from BaseHTTPServer import HTTPServer +import os +import ssl +import SocketServer +import threading +import unittest + +_where = os.path.split(__file__)[0] + + +def where(filename): + return os.path.join(_where, filename) + + +class HTTPSServer(HTTPServer): + def server_bind(self): + SocketServer.TCPServer.server_bind(self) + self.socket = ssl.wrap_socket( + self.socket, server_side=True, + certfile=where("leaptestscert.pem"), + keyfile=where("leaptestskey.pem"), + ca_certs=where("cacert.pem"), + ssl_version=ssl.PROTOCOL_SSLv23) + + +class TestServerThread(threading.Thread): + def __init__(self, test_object, request_handler): + threading.Thread.__init__(self) + self.request_handler = request_handler + self.test_object = test_object + + def run(self): + self.server = HTTPSServer(('localhost', 0), self.request_handler) + host, port = self.server.socket.getsockname() + self.test_object.HOST, self.test_object.PORT = host, port + self.test_object.server_started.set() + self.test_object = None + try: + self.server.serve_forever(0.05) + finally: + self.server.server_close() + + def stop(self): + self.server.shutdown() + + +class BaseHTTPSServerTestCase(unittest.TestCase): + """ + derived classes need to implement a request_handler + """ + def setUp(self): + self.server_started = threading.Event() + self.thread = TestServerThread(self, self.request_handler) + self.thread.start() + self.server_started.wait() + + def tearDown(self): + self.thread.stop() + + def get_server(self): + host, port = self.HOST, self.PORT + if host == "127.0.0.1": + host = "localhost" + return "%s:%s" % (host, port) + + +if __name__ == "__main__": + unittest.main() diff --git a/debian/python-leap.common/usr/share/pyshared/leap/common/testing/test_basetest.py b/debian/python-leap.common/usr/share/pyshared/leap/common/testing/test_basetest.py new file mode 100644 index 0000000..220e28d --- /dev/null +++ b/debian/python-leap.common/usr/share/pyshared/leap/common/testing/test_basetest.py @@ -0,0 +1,140 @@ +# -*- coding: utf-8 -*- +# test_basetest.py +# Copyright (C) 2013 LEAP +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +""" +Unittests for BaseLeapTest ...becase it's oh so meta +""" +try: + import unittest2 as unittest +except ImportError: + import unittest + +import os +import StringIO + +from leap.common.testing.basetest import BaseLeapTest + +_tempdir = None # global for tempdir checking + + +class _TestCaseRunner(object): + """ + TestCaseRunner used to run BaseLeapTest + """ + def run_testcase(self, testcase=None): + """ + Runs a given TestCase + + @param testcase: the testcase + @type testcase: unittest.TestCase + """ + if not testcase: + return None + loader = unittest.TestLoader() + suite = loader.loadTestsFromTestCase(testcase) + + # Create runner, and run testcase + io = StringIO.StringIO() + runner = unittest.TextTestRunner(stream=io) + results = runner.run(suite) + return results + + +class TestAbstractBaseLeapTest(unittest.TestCase, _TestCaseRunner): + """ + TestCase for BaseLeapTest abs + """ + def test_abstract_base_class(self): + """ + Test errors raised when setup/teardown not overloaded + """ + class _BaseTest(BaseLeapTest): + def test_dummy_method(self): + pass + + def test_tautology(self): + assert True + + results = self.run_testcase(_BaseTest) + + # should be 2 errors: NotImplemented + # raised for setUp/tearDown + self.assertEquals(results.testsRun, 2) + self.assertEquals(len(results.failures), 0) + self.assertEquals(len(results.errors), 2) + + +class TestInitBaseLeapTest(BaseLeapTest): + """ + TestCase for testing initialization of BaseLeapTest + """ + + def setUp(self): + """nuke it""" + pass + + def tearDown(self): + """nuke it""" + pass + + def test_path_is_changed(self): + """tests whether we have changed the PATH env var""" + os_path = os.environ['PATH'] + self.assertTrue(os_path.startswith(self.tempdir)) + + def test_old_path_is_saved(self): + """tests whether we restore the PATH env var""" + self.assertTrue(len(self.old_path) > 1) + + +class TestCleanedBaseLeapTest(unittest.TestCase, _TestCaseRunner): + """ + TestCase for testing tempdir creation and cleanup + """ + + def test_tempdir_is_cleaned_after_tests(self): + """ + test if a TestCase derived from BaseLeapTest creates and cleans the + temporal dir + """ + class _BaseTest(BaseLeapTest): + def setUp(self): + """set global _tempdir to this instance tempdir""" + global _tempdir + _tempdir = self.tempdir + + def tearDown(self): + """nothing""" + pass + + def test_tempdir_created(self): + """test if tempdir was created""" + self.assertTrue(os.path.isdir(self.tempdir)) + + def test_tempdir_created_on_setupclass(self): + """test if tempdir is the one created by setupclass""" + self.assertEqual(_tempdir, self.tempdir) + + results = self.run_testcase(_BaseTest) + self.assertEquals(results.testsRun, 2) + self.assertEquals(len(results.failures), 0) + self.assertEquals(len(results.errors), 0) + + # did we cleaned the tempdir? + self.assertFalse(os.path.isdir(_tempdir)) + +if __name__ == "__main__": + unittest.main() -- cgit v1.2.3