[Asterisk-code-review] lib/python/mockd: Remove mid-test checking; add support for ... (testsuite[master])

Anonymous Coward asteriskteam at digium.com
Sun Nov 22 20:23:28 CST 2015


Anonymous Coward #1000019 has submitted this change and it was merged.

Change subject: lib/python/mockd: Remove mid-test checking; add support for regex
......................................................................


lib/python/mockd: Remove mid-test checking; add support for regex

This patch does two things:
(1) It removes mid-test packet checks. This checking was (a) redundant with
    the packet checking that occurs when the test is finished, and (b) can
    cause strange things by attempting to abort the test quickly while other
    pluggable modules are just getting started.
(2) It adds support for matching packets based on a regex. Because the
    configuration was previously reserved only for packets to be matched,
    a new configuration section called 'packets' now lists the packets. The
    test that depended on the previous configuration have been updated
    appropriately.

Change-Id: Ic13927116fe35e5460499f339464bc523178043d
---
M lib/python/mockd.py
M tests/apps/statsd/nominal/test-config.yaml
M tests/apps/statsd/off-nominal/empty_params/test-config.yaml
M tests/apps/statsd/off-nominal/invalid_metric/test-config.yaml
M tests/apps/statsd/off-nominal/invalid_sample_rate/test-config.yaml
M tests/apps/statsd/off-nominal/invalid_value/test-config.yaml
M tests/apps/statsd/off-nominal/invalid_variable/test-config.yaml
M tests/apps/statsd/off-nominal/no_metric/test-config.yaml
M tests/apps/statsd/off-nominal/no_value/test-config.yaml
M tests/apps/statsd/off-nominal/no_variable/test-config.yaml
10 files changed, 44 insertions(+), 57 deletions(-)

Approvals:
  Anonymous Coward #1000019: Verified
  Matt Jordan: Looks good to me, approved
  Joshua Colp: Looks good to me, but someone else must approve



diff --git a/lib/python/mockd.py b/lib/python/mockd.py
index a58cccb..24e76c9 100644
--- a/lib/python/mockd.py
+++ b/lib/python/mockd.py
@@ -11,9 +11,7 @@
 
 from twisted.internet.protocol import DatagramProtocol
 from twisted.internet import reactor
-
-sys.path.append("lib/python")
-sys.path.append("tests/apps/statsd")
+from test_suite_utils import all_match
 
 LOGGER = logging.getLogger(__name__)
 
@@ -61,19 +59,6 @@
         self.test_object.register_stop_observer(self._stop_handler)
         reactor.listenUDP(8125, MockDProtocol(self))
 
-    def check_message(self, message):
-        '''Checks a received message.
-
-        Keyword Arguments:
-        message -- The message to check.
-        '''
-        if message in self.config:
-            LOGGER.debug('%s found in config', message)
-        else:
-            LOGGER.error('%s not specified in configuration', message)
-            self.test_object.set_passed(False)
-            self.test_object.stop_reactor()
-
     def message_handler(self, message):
         '''Datagram message handler
 
@@ -82,15 +67,7 @@
 
         Check the message against the config and pass the test if they match
         '''
-        if self.config[0] == 'ReceiveNothing':
-            LOGGER.error('%s not specified in configuration', message)
-            self.test_object.set_passed(False)
-            self.test_object.stop_reactor()
-            return
-
         self.packets.append(message)
-
-        self.check_message(message)
 
     def _stop_handler(self, result):
         '''A deferred callback called as a result of the test stopping
@@ -100,32 +77,33 @@
         '''
         LOGGER.info('Checking packets received')
 
-        if (self.config[0] == 'ReceiveNothing') and (len(self.packets) == 0):
+        packets = self.config.get('packets')
+
+        if (packets[0] == 'ReceiveNothing') and (len(self.packets) == 0):
             LOGGER.info('Server correctly received nothing')
             self.test_object.set_passed(True)
-            LOGGER.info('Test is stopping')
             return result
 
-        if len(self.packets) != len(self.config):
+        if len(self.packets) != len(packets):
             LOGGER.error('Number of received packets {0} is not equal to '
                 'the number of configured packets '
-                '{1}'.format(len(self.packets),
-                len(self.config)))
+                '{1}'.format(len(self.packets), len(packets)))
             self.test_object.set_passed(False)
-            LOGGER.info('Test is stopping')
             return result
 
+        if self.config.get('regex', False):
+            cmp_fn = all_match
+        else:
+            cmp_fn = lambda expected, actual: expected == actual
         failed_matches = [(actual, expected) for actual, expected in
-            zip(self.packets, self.config) if actual != expected]
+            zip(self.packets, packets) if not cmp_fn(expected, actual)]
 
         if len(failed_matches) != 0:
             LOGGER.error('The following packets failed to match: {0}'
                 .format(failed_matches))
             self.test_object.set_passed(False)
-            LOGGER.info('Test is stopping')
             return result
 
         self.test_object.set_passed(True)
-        LOGGER.info('Test is stopping')
         LOGGER.info('All packets matched')
         return result
diff --git a/tests/apps/statsd/nominal/test-config.yaml b/tests/apps/statsd/nominal/test-config.yaml
index 602c26e..32fa104 100644
--- a/tests/apps/statsd/nominal/test-config.yaml
+++ b/tests/apps/statsd/nominal/test-config.yaml
@@ -24,14 +24,15 @@
     reactor-timeout: 15
 
 statsd-config:
-    -
-        'foo:1|g'
-    -
-        'bar:2|s'
-    -
-        'baz:3|ms'
-    -
-        'last:4|c'
+    packets:
+        -
+            'foo:1|g'
+        -
+            'bar:2|s'
+        -
+            'baz:3|ms'
+        -
+            'last:4|c'
 
 originator-config:
     channel: 'Local/start at default'
diff --git a/tests/apps/statsd/off-nominal/empty_params/test-config.yaml b/tests/apps/statsd/off-nominal/empty_params/test-config.yaml
index 44ee2b0..11309e9 100644
--- a/tests/apps/statsd/off-nominal/empty_params/test-config.yaml
+++ b/tests/apps/statsd/off-nominal/empty_params/test-config.yaml
@@ -23,8 +23,9 @@
     reactor-timeout: 15
 
 statsd-config:
-    -
-        'ReceiveNothing'
+    packets:
+        -
+            'ReceiveNothing'
 
 originator-config:
     channel: 'Local/start at default'
diff --git a/tests/apps/statsd/off-nominal/invalid_metric/test-config.yaml b/tests/apps/statsd/off-nominal/invalid_metric/test-config.yaml
index a1870b3..7fa8582 100644
--- a/tests/apps/statsd/off-nominal/invalid_metric/test-config.yaml
+++ b/tests/apps/statsd/off-nominal/invalid_metric/test-config.yaml
@@ -23,8 +23,9 @@
     reactor-timeout: 15
 
 statsd-config:
-    -
-        'ReceiveNothing'
+    packets:
+        -
+            'ReceiveNothing'
 
 originator-config:
     channel: 'Local/start at default'
diff --git a/tests/apps/statsd/off-nominal/invalid_sample_rate/test-config.yaml b/tests/apps/statsd/off-nominal/invalid_sample_rate/test-config.yaml
index b65a072..a166e5b 100644
--- a/tests/apps/statsd/off-nominal/invalid_sample_rate/test-config.yaml
+++ b/tests/apps/statsd/off-nominal/invalid_sample_rate/test-config.yaml
@@ -23,8 +23,9 @@
     reactor-timeout: 15
 
 statsd-config:
-    -
-        'ReceiveNothing'
+    packets:
+        -
+            'ReceiveNothing'
 
 originator-config:
     channel: 'Local/start at default'
diff --git a/tests/apps/statsd/off-nominal/invalid_value/test-config.yaml b/tests/apps/statsd/off-nominal/invalid_value/test-config.yaml
index a6a088f..0a0c00b 100644
--- a/tests/apps/statsd/off-nominal/invalid_value/test-config.yaml
+++ b/tests/apps/statsd/off-nominal/invalid_value/test-config.yaml
@@ -23,8 +23,9 @@
     reactor-timeout: 15
 
 statsd-config:
-    -
-        'ReceiveNothing'
+    packets:
+        -
+            'ReceiveNothing'
 
 originator-config:
     channel: 'Local/start at default'
diff --git a/tests/apps/statsd/off-nominal/invalid_variable/test-config.yaml b/tests/apps/statsd/off-nominal/invalid_variable/test-config.yaml
index 54a031c..78a138e 100644
--- a/tests/apps/statsd/off-nominal/invalid_variable/test-config.yaml
+++ b/tests/apps/statsd/off-nominal/invalid_variable/test-config.yaml
@@ -23,8 +23,9 @@
     reactor-timeout: 15
 
 statsd-config:
-    -
-        'ReceiveNothing'
+    packets:
+        -
+            'ReceiveNothing'
 
 originator-config:
     channel: 'Local/start at default'
diff --git a/tests/apps/statsd/off-nominal/no_metric/test-config.yaml b/tests/apps/statsd/off-nominal/no_metric/test-config.yaml
index 4b75ea4..375ba66 100644
--- a/tests/apps/statsd/off-nominal/no_metric/test-config.yaml
+++ b/tests/apps/statsd/off-nominal/no_metric/test-config.yaml
@@ -23,8 +23,9 @@
     reactor-timeout: 15
 
 statsd-config:
-    -
-        'ReceiveNothing'
+    packets:
+        -
+            'ReceiveNothing'
 
 originator-config:
     channel: 'Local/start at default'
diff --git a/tests/apps/statsd/off-nominal/no_value/test-config.yaml b/tests/apps/statsd/off-nominal/no_value/test-config.yaml
index 8fbafa1..65adce5 100644
--- a/tests/apps/statsd/off-nominal/no_value/test-config.yaml
+++ b/tests/apps/statsd/off-nominal/no_value/test-config.yaml
@@ -23,8 +23,9 @@
     reactor-timeout: 15
 
 statsd-config:
-    -
-        'ReceiveNothing'
+    packets:
+        -
+            'ReceiveNothing'
 
 originator-config:
     channel: 'Local/start at default'
diff --git a/tests/apps/statsd/off-nominal/no_variable/test-config.yaml b/tests/apps/statsd/off-nominal/no_variable/test-config.yaml
index 43d274f..25ff9ec 100644
--- a/tests/apps/statsd/off-nominal/no_variable/test-config.yaml
+++ b/tests/apps/statsd/off-nominal/no_variable/test-config.yaml
@@ -25,8 +25,9 @@
     reactor-timeout: 15
 
 statsd-config:
-    -
-        'ReceiveNothing'
+    packets:
+        -
+            'ReceiveNothing'
 
 originator-config:
     channel: 'Local/start at default'

-- 
To view, visit https://gerrit.asterisk.org/1657
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ic13927116fe35e5460499f339464bc523178043d
Gerrit-PatchSet: 1
Gerrit-Project: testsuite
Gerrit-Branch: master
Gerrit-Owner: Matt Jordan <mjordan at digium.com>
Gerrit-Reviewer: Anonymous Coward #1000019
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Matt Jordan <mjordan at digium.com>



More information about the asterisk-code-review mailing list