Add base class for triggers
and test the all triggers adhere to the set contract.
Also standardise the trigger class names to NameTrigger.
This will make it easier to do more triggers in the future and also
add the possibility of loading triggers dynamically.
Co-Authored-By: Gregory Haynes <greg@greghaynes.net>
Change-Id: I283bffad97edb91a6a2258163d4162a15b904757
diff --git a/tests/base.py b/tests/base.py
index 67de802..731d842 100755
--- a/tests/base.py
+++ b/tests/base.py
@@ -1027,12 +1027,12 @@
def register_triggers(self):
# Register the available triggers
- self.gerrit_trigger = zuul.trigger.gerrit.Gerrit(
+ self.gerrit_trigger = zuul.trigger.gerrit.GerritTrigger(
self.fake_gerrit, self.config, self.sched, self.gerrit_source)
self.gerrit_trigger.gerrit_connector.delay = 0.0
self.sched.registerTrigger(self.gerrit_trigger)
- self.timer = zuul.trigger.timer.Timer(self.config, self.sched)
+ self.timer = zuul.trigger.timer.TimerTrigger(self.config, self.sched)
self.sched.registerTrigger(self.timer)
self.zuultrigger = zuul.trigger.zuultrigger.ZuulTrigger(self.config,
self.sched)
diff --git a/tests/test_trigger.py b/tests/test_trigger.py
new file mode 100644
index 0000000..a09da21
--- /dev/null
+++ b/tests/test_trigger.py
@@ -0,0 +1,52 @@
+# Copyright 2014 Rackspace Australia
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import logging
+import testtools
+
+import zuul.trigger
+
+
+class TestGerritTrigger(testtools.TestCase):
+ log = logging.getLogger("zuul.test_trigger")
+
+ def test_trigger_abc(self):
+ # We only need to instantiate a class for this
+ trigger = zuul.trigger.gerrit.GerritTrigger(None, None, None, # noqa
+ None)
+
+ def test_trigger_name(self):
+ self.assertEqual('gerrit', zuul.trigger.gerrit.GerritTrigger.name)
+
+
+class TestTimerTrigger(testtools.TestCase):
+ log = logging.getLogger("zuul.test_trigger")
+
+ def test_trigger_abc(self):
+ # We only need to instantiate a class for this
+ trigger = zuul.trigger.timer.TimerTrigger(None, None) # noqa
+
+ def test_trigger_name(self):
+ self.assertEqual('timer', zuul.trigger.timer.TimerTrigger.name)
+
+
+class TestZuulTrigger(testtools.TestCase):
+ log = logging.getLogger("zuul.test_trigger")
+
+ def test_trigger_abc(self):
+ # We only need to instantiate a class for this
+ trigger = zuul.trigger.zuultrigger.ZuulTrigger(None, None) # noqa
+
+ def test_trigger_name(self):
+ self.assertEqual('zuul', zuul.trigger.zuultrigger.ZuulTrigger.name)
diff --git a/zuul/cmd/server.py b/zuul/cmd/server.py
index e0f19a9..684e650 100755
--- a/zuul/cmd/server.py
+++ b/zuul/cmd/server.py
@@ -159,13 +159,11 @@
import zuul.trigger.gerrit
import zuul.trigger.timer
import zuul.trigger.zuultrigger
- self.gerrit_trigger = zuul.trigger.gerrit.Gerrit(self.gerrit,
- self.config,
- self.sched,
- self.gerrit_source)
- timer = zuul.trigger.timer.Timer(self.config, self.sched)
- zuultrigger = zuul.trigger.zuultrigger.ZuulTrigger(self.config,
- self.sched)
+ self.gerrit_trigger = zuul.trigger.gerrit.GerritTrigger(
+ self.gerrit, self.config, self.sched, self.gerrit_source)
+ timer = zuul.trigger.timer.TimerTrigger(self.config, self.sched)
+ zuultrigger = zuul.trigger.zuultrigger.ZuulTrigger(
+ self.config, self.sched)
self.sched.registerTrigger(self.gerrit_trigger)
self.sched.registerTrigger(timer)
diff --git a/zuul/trigger/__init__.py b/zuul/trigger/__init__.py
index e69de29..0d0604f 100644
--- a/zuul/trigger/__init__.py
+++ b/zuul/trigger/__init__.py
@@ -0,0 +1,40 @@
+# Copyright 2014 Rackspace Australia
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import abc
+
+import six
+
+
+@six.add_metaclass(abc.ABCMeta)
+class BaseTrigger(object):
+ """Base class for triggers.
+
+ Defines the exact public methods that must be supplied."""
+
+ @abc.abstractmethod
+ def __init__(self, *args, **kwargs):
+ """Constructor."""
+
+ def stop(self):
+ """Stop the trigger."""
+
+ def postConfig(self):
+ """Called after config is loaded."""
+
+ def onChangeMerged(self, change):
+ """Called when a change has been merged."""
+
+ def onChangeEnqueued(self, change, pipeline):
+ """Called when a change has been enqueued."""
diff --git a/zuul/trigger/gerrit.py b/zuul/trigger/gerrit.py
index 428f67a..edac5ad 100644
--- a/zuul/trigger/gerrit.py
+++ b/zuul/trigger/gerrit.py
@@ -17,6 +17,7 @@
import time
import voluptuous
from zuul.model import TriggerEvent
+from zuul.trigger import BaseTrigger
class GerritEventConnector(threading.Thread):
@@ -114,7 +115,7 @@
self.gerrit.eventDone()
-class Gerrit(object):
+class GerritTrigger(BaseTrigger):
name = 'gerrit'
log = logging.getLogger("zuul.trigger.Gerrit")
@@ -135,9 +136,6 @@
self.gerrit_connector.stop()
self.gerrit_connector.join()
- def postConfig(self):
- pass
-
def validate_trigger(trigger_data):
"""Validates the layout's trigger data."""
diff --git a/zuul/trigger/timer.py b/zuul/trigger/timer.py
index be22a70..d5c1bad 100644
--- a/zuul/trigger/timer.py
+++ b/zuul/trigger/timer.py
@@ -16,9 +16,10 @@
import apscheduler.scheduler
import logging
from zuul.model import TriggerEvent
+from zuul.trigger import BaseTrigger
-class Timer(object):
+class TimerTrigger(BaseTrigger):
name = 'timer'
log = logging.getLogger("zuul.Timer")
diff --git a/zuul/trigger/zuultrigger.py b/zuul/trigger/zuultrigger.py
index 3cc4ff9..c7373e2 100644
--- a/zuul/trigger/zuultrigger.py
+++ b/zuul/trigger/zuultrigger.py
@@ -15,9 +15,10 @@
import logging
from zuul.model import TriggerEvent
+from zuul.trigger import BaseTrigger
-class ZuulTrigger(object):
+class ZuulTrigger(BaseTrigger):
name = 'zuul'
log = logging.getLogger("zuul.ZuulTrigger")
@@ -27,9 +28,6 @@
self._handle_parent_change_enqueued_events = False
self._handle_project_change_merged_events = False
- def stop(self):
- pass
-
def onChangeMerged(self, change):
# Called each time zuul merges a change
if self._handle_project_change_merged_events: