jenkins-bot has submitted this change and it was merged.

Change subject: Tests for `pybal.monitor`
......................................................................


Tests for `pybal.monitor`

Change-Id: I3ba6384157e6b39293ca47910fe5c2168eb05bf5
---
M tests/__init__.py
A tests/test_monitor.py
2 files changed, 125 insertions(+), 0 deletions(-)

Approvals:
  Ori.livneh: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/tests/__init__.py b/tests/__init__.py
index d0f1ca3..6bbaea9 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -10,3 +10,4 @@
 
 from .test_util import *
 from .test_ipvs import *
+from .test_monitor import *
diff --git a/tests/test_monitor.py b/tests/test_monitor.py
new file mode 100644
index 0000000..144a423
--- /dev/null
+++ b/tests/test_monitor.py
@@ -0,0 +1,124 @@
+# -*- coding: utf-8 -*-
+"""
+  PyBal unit tests
+  ~~~~~~~~~~~~~~~~
+
+  This module contains tests for `pybal.monitor`.
+
+"""
+import unittest
+
+import pybal.monitor
+import pybal.util
+import twisted.internet
+
+
+class StubCoordinator(object):
+    """Test stub for `pybal.pybal.Coordinator`."""
+
+    def __init__(self):
+        self.up = None
+        self.reason = None
+
+    def resultUp(self, monitor):
+        self.up = True
+
+    def resultDown(self, monitor, reason=None):
+        self.up = False
+        self.reason = reason
+
+
+class MonitoringProtocolTestCase(unittest.TestCase):
+    """Test case for `pybal.monitor.MonitoringProtocol`."""
+
+    def setUp(self):
+        self.coordinator = StubCoordinator()
+        self.config = pybal.util.ConfigDict()
+        self.monitor = pybal.monitor.MonitoringProtocol(
+            self.coordinator, None, self.config)
+        self.monitor.__name__ = 'TestMonitor'
+        self.reactor = twisted.internet.reactor
+
+    def testRun(self):
+        """Test `MonitoringProtocol.run`."""
+        self.monitor.run()
+        self.assertTrue(self.monitor.active)
+        with self.assertRaises(AssertionError):
+            self.monitor.run()
+
+    def testStop(self):
+        """Test `MonitoringProtocol.stop`."""
+        self.monitor.run()
+        self.monitor.stop()
+        self.assertFalse(self.monitor.active)
+
+    def testStopBeforeShutdown(self):
+        """`MonitoringProtocol` stops on system shutdown."""
+        self.monitor.run()
+        self.reactor.fireSystemEvent('shutdown')
+        self.assertFalse(self.monitor.active)
+
+    def testName(self):
+        """Test `MonitoringProtocol._resultUp`."""
+        self.assertEquals(self.monitor.name(), 'TestMonitor')
+
+    def testResultUp(self):
+        """Test `MonitoringProtocol._resultUp`."""
+        self.monitor._resultUp()
+        self.assertTrue(self.coordinator.up)
+
+    def testResultUpInactive(self):
+        """`MonitoringProtocol._resultUp` should not change state when monitor
+        is inactive, unless this is the first check."""
+        self.monitor.firstCheck = False
+        self.monitor._resultUp()
+        self.assertIsNone(self.coordinator.up)
+
+    def testResultDown(self):
+        """Test `MonitoringProtocol._resultDown`."""
+        self.monitor._resultDown()
+        self.assertFalse(self.monitor.up)
+
+    def testResultDownInactive(self):
+        """`MonitoringProtocol._resultDown` should not change state when
+        monitor is inactive, unless this is the first check."""
+        self.monitor.firstCheck = False
+        self.monitor._resultDown()
+        self.assertIsNone(self.coordinator.up)
+
+    def testGetConfigString(self):
+        """Test `MonitoringProtocol._getConfigString`."""
+        self.config['testmonitor.strValue'] = 'abc'
+        self.assertEquals(self.monitor._getConfigString('strValue'), 'abc')
+
+        self.config['testmonitor.badStrValue'] = 123
+        with self.assertRaises(ValueError):
+            self.monitor._getConfigString('badStrValue')
+
+    def testGetConfigInt(self):
+        """Test `MonitoringProtocol._getConfigInt`."""
+        self.config['testmonitor.intValue'] = 123
+        self.assertEquals(self.monitor._getConfigInt('intValue'), 123)
+
+    def testGetConfigBool(self):
+        """Test `MonitoringProtocol._getConfigBool`."""
+        self.config['testmonitor.boolValue'] = 'false'
+        self.assertFalse(self.monitor._getConfigBool('boolValue'))
+
+    def testGetConfigStringList(self):
+        """Test `MonitoringProtocol._getConfigStringList`."""
+        self.config['testmonitor.strListValue'] = '"abc"'
+        self.assertEquals(
+            self.monitor._getConfigStringList('strListValue'), 'abc')
+
+        self.config['testmonitor.strListValue'] = '["abc", "def"]'
+        self.assertEquals(
+            self.monitor._getConfigStringList('strListValue'), ['abc', 'def'])
+
+        self.config['testmonitor.badStrListValue'] = '["abc", 123]'
+        with self.assertRaises(ValueError):
+            self.monitor._getConfigStringList('badStrListValue')
+
+        self.config['testmonitor.emptyStrListValue'] = '[]'
+        with self.assertRaises(ValueError):
+            self.monitor._getConfigStringList('emptyStrListValue')

-- 
To view, visit https://gerrit.wikimedia.org/r/172805
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I3ba6384157e6b39293ca47910fe5c2168eb05bf5
Gerrit-PatchSet: 4
Gerrit-Project: operations/debs/pybal
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <[email protected]>
Gerrit-Reviewer: Mark Bergsma <[email protected]>
Gerrit-Reviewer: Ori.livneh <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to