summaryrefslogtreecommitdiffstats
path: root/test/utils.py
diff options
context:
space:
mode:
authorOliver Poignant <oliver@poignant.se>2016-05-08 01:19:03 +0200
committerOliver Poignant <oliver@poignant.se>2016-05-08 01:19:03 +0200
commit6e6fd2e4f13cfb827ae1f5a0c3e83f14cc1fa15a (patch)
tree5a8d88d329413a0e9b8f786529c4ee2ce118fbe7 /test/utils.py
parentae3c50f997427bc128ff303b4a3dbaea19c18fde (diff)
downloadGit-Auto-Deploy-6e6fd2e4f13cfb827ae1f5a0c3e83f14cc1fa15a.zip
Git-Auto-Deploy-6e6fd2e4f13cfb827ae1f5a0c3e83f14cc1fa15a.tar.gz
Git-Auto-Deploy-6e6fd2e4f13cfb827ae1f5a0c3e83f14cc1fa15a.tar.bz2
Automated testing of weebhook request handling
Diffstat (limited to 'test/utils.py')
-rw-r--r--test/utils.py108
1 files changed, 108 insertions, 0 deletions
diff --git a/test/utils.py b/test/utils.py
new file mode 100644
index 0000000..a686485
--- /dev/null
+++ b/test/utils.py
@@ -0,0 +1,108 @@
+import unittest
+
+class WebhookTestCaseBase(unittest.TestCase):
+
+ thread = None
+
+ def setUp(self):
+ import sys
+
+ # Use our custom importer to replace certain modules with stub modules to
+ # enable testing of other parts of GAD
+ sys.meta_path.append(StubImporter())
+
+ def start_gad(self, test_config):
+ import sys
+ import time
+ sys.path.insert(1, '..')
+
+ from gitautodeploy.cli.config import get_config_defaults, init_config
+
+ config = get_config_defaults()
+ config.update(test_config)
+
+ init_config(config)
+
+ # Create a GAD instance
+ self.thread = GADRunnerThread(config)
+
+ # Run GAD in a thread
+ self.thread.start()
+
+ def gad_port(self):
+ return self.thread.port
+
+ def await_gad(self):
+ """Waits for GAD and all it's threads to complete."""
+ import threading
+
+ # Wait for GAD runner thread to finish
+ self.thread.join()
+
+ # Wait for all request handler threads to finish
+ main_thread = threading.currentThread()
+ for current_thread in threading.enumerate():
+ if current_thread == main_thread:
+ continue
+ #print "Waiting for thread %s to finish" % current_thread
+ current_thread.join()
+
+ def tearDown(self):
+ pass
+
+
+class StubImporter(object):
+
+ overload_modules = ['gitautodeploy.wrappers.git', 'gitautodeploy.wrappers.process']
+
+ def find_module(self, full_name, package_path):
+
+ # Intervene when any wrapper module is imported
+ if full_name in self.overload_modules:
+
+ # Return a loader
+ return self
+
+ return None
+
+ def load_module(self, full_name):
+ """Load matching module from stubs package (test stub) instead of main gitautodeploy package."""
+ import imp
+ import sys
+
+ module_name = full_name.split('.')[-1]
+ module_info = imp.find_module(module_name, ['stubs'])
+ module = imp.load_module(module_name, *module_info)
+ sys.modules[module_name] = module
+
+# print "returning module %s" % module_name
+
+# module.history.append('WAH')
+
+ # Return a module
+ return module
+
+
+import threading
+
+class GADRunnerThread(threading.Thread):
+ def __init__(self, config):
+ super(GADRunnerThread, self).__init__()
+ import sys
+ import time
+
+ from gitautodeploy import GitAutoDeploy
+
+ self._app = GitAutoDeploy()
+ self._app.setup(config)
+
+ # Store PID and port in thread instance
+ self.pid = self._app._pid
+ self.port = self._app._port
+
+ def run(self):
+ self._app.handle_request()
+
+ def exit(self):
+ self._app.stop()
+ self._app.close()