Allow using webapp from connections

Allow connections to register their own handlers for HTTP URIs inside
the zuul's webapp HTTP server. That way, connections can listen for
events comming through HTTP.

Story: 2000774

Change-Id: Ic5887d00ff302f67469df5154e9df10b99f1cfcd
diff --git a/tests/base.py b/tests/base.py
index 9983103..6d3df8b 100755
--- a/tests/base.py
+++ b/tests/base.py
@@ -1367,6 +1367,9 @@
 
         self.sched = zuul.scheduler.Scheduler(self.config)
 
+        self.webapp = zuul.webapp.WebApp(
+            self.sched, port=0, listen_address='127.0.0.1')
+
         self.event_queues = [
             self.sched.result_event_queue,
             self.sched.trigger_event_queue,
@@ -1374,7 +1377,7 @@
         ]
 
         self.configure_connections()
-        self.sched.registerConnections(self.connections)
+        self.sched.registerConnections(self.connections, self.webapp)
 
         def URLOpenerFactory(*args, **kw):
             if isinstance(args[0], urllib.request.Request):
@@ -1414,8 +1417,6 @@
         self.sched.setNodepool(self.nodepool)
         self.sched.setZooKeeper(self.zk)
 
-        self.webapp = zuul.webapp.WebApp(
-            self.sched, port=0, listen_address='127.0.0.1')
         self.rpc = zuul.rpclistener.RPCListener(self.config, self.sched)
 
         self.sched.start()
diff --git a/tests/unit/test_webapp.py b/tests/unit/test_webapp.py
index 8791a25..4511ec7 100644
--- a/tests/unit/test_webapp.py
+++ b/tests/unit/test_webapp.py
@@ -19,6 +19,7 @@
 import json
 
 from six.moves import urllib
+import webob
 
 from tests.base import ZuulTestCase, FIXTURE_DIR
 
@@ -96,3 +97,16 @@
             self.port)
         f = urllib.request.urlopen(req)
         self.assertEqual(f.read(), public_pem)
+
+    def test_webapp_custom_handler(self):
+        def custom_handler(path, tenant_name, request):
+            return webob.Response(body='ok')
+
+        self.webapp.register_path('/custom', custom_handler)
+        req = urllib.request.Request(
+            "http://localhost:%s/custom" % self.port)
+        f = urllib.request.urlopen(req)
+        self.assertEqual('ok', f.read())
+
+        self.webapp.unregister_path('/custom')
+        self.assertRaises(urllib.error.HTTPError, urllib.request.urlopen, req)