[asterisk-commits] StatsD: Create a mock StatsD server in the Testsuite (testsuite[master])

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Oct 6 17:09:15 CDT 2015


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

Change subject: StatsD: Create a mock StatsD server in the Testsuite
......................................................................


StatsD: Create a mock StatsD server in the Testsuite

Created a pluggable module with a mock StatsD server to provide a
foundation for constructing tests for the StatsD dialplan
application.

ASTERISK-25419
Reported By: Ashley Sanders

Change-Id: I4eaf92465b79569fff4db6ada16e8d215f7d214b
---
A tests/apps/statsd/nominal/mockd.py
A tests/apps/statsd/nominal/test-config.yaml
A tests/apps/statsd/tests.yaml
M tests/apps/tests.yaml
4 files changed, 142 insertions(+), 0 deletions(-)

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



diff --git a/tests/apps/statsd/nominal/mockd.py b/tests/apps/statsd/nominal/mockd.py
new file mode 100644
index 0000000..354fae8
--- /dev/null
+++ b/tests/apps/statsd/nominal/mockd.py
@@ -0,0 +1,97 @@
+'''
+Copyright (C) 2015, Digium, Inc.
+Tyler Cambron <tcambron at digium.com>
+
+This program is free software, distributed under the terms of
+the GNU General Public License Version 2.
+'''
+
+import logging
+
+from twisted.internet.protocol import DatagramProtocol
+from twisted.internet import reactor
+
+LOGGER = logging.getLogger(__name__)
+
+
+class MockDProtocol(DatagramProtocol):
+    ''' Protocol for the Mock Server to use for receiving messages.
+    '''
+
+    def __init__(self, mockd_server):
+        ''' Constructor.
+
+        Keyword Arguments:
+        mockd_server -- An instance of the mock StatsD server
+        '''
+        self.mockd_server = mockd_server
+
+    def datagramReceived(self, datagram, address):
+        ''' AMI Newexten event handler
+
+        Keyword Arguments:
+        datagram -- The datagram that was received by the server
+        address -- The address that the datagram came from
+
+        Accept the datagram and send it to be checked against the config
+        '''
+        LOGGER.debug('Server received %s from %s' % (datagram, address))
+        self.mockd_server.message_received(datagram)
+
+
+class MockDServer(object):
+    ''' Pluggable Module that acts as a mock StatsD server
+    '''
+
+    def __init__(self, config, test_object):
+        ''' Constructor
+
+        Keyword Arguments:
+        config -- This object's YAML derived configuration
+        test_object -- The test object it plugs onto
+        '''
+        self.config = config
+        self.test_object = test_object
+        self.packets = []
+        self.test_object.register_stop_observer(self._stop_handler)
+        reactor.listenUDP(8080, MockDProtocol(self))
+
+    def message_received(self, message):
+        ''' Datagram message handler
+
+        Keyword Arguments:
+        message -- The datagram that was received by the server
+
+        Check the message against the config and pass the test if they match
+        '''
+        self.packets.append(message)
+
+        if len(self.packets) == len(self.config):
+            self.test_object.stop_reactor()
+
+    def _stop_handler(self, result):
+        ''' A deferred callback called as a result of the test stopping
+
+        Keyword Arguments:
+        result -- The deferred parameter passed from callback to callback
+        '''
+        LOGGER.info('Checking packets received')
+        if len(self.packets) != len(self.config):
+            LOGGER.error('Number of received packets {0} is not equal to the '
+                'number of configured packets {1}'.format(len(self.packets),
+                len(self.config)))
+            self.test_object.set_passed(False)
+            return
+
+        failed_matches = [(actual, expected) for actual, expected in
+            zip(self.packets, self.config) if actual != expected]
+
+        if len(failed_matches) != 0:
+            LOGGER.error('The following packets failed to match: {0}'
+                .format(failed_matches))
+            self.test_object.set_passed(False)
+            return
+
+        LOGGER.info('All packets matched')
+        self.test_object.set_passed(True)
+        LOGGER.debug('Test is stopping')
diff --git a/tests/apps/statsd/nominal/test-config.yaml b/tests/apps/statsd/nominal/test-config.yaml
new file mode 100644
index 0000000..e4a851c
--- /dev/null
+++ b/tests/apps/statsd/nominal/test-config.yaml
@@ -0,0 +1,41 @@
+testinfo:
+    summary:  |
+        Test sending statistics to a StatsD server for the StatsD Dialplan
+        Application
+    description: |
+        This test verifies that a user will be able to send statistics properly
+        to StatsD through the StatsD Dialplan Application.
+
+test-modules:
+    add-test-to-search-path: 'True'
+    add-relative-to-search-path: ['..']
+    test-object:
+        config-section: 'test-object-config'
+        typename: 'test_case.TestCaseModule'
+    modules:
+        -
+            typename: 'mockd.MockDServer'
+            config-section: 'statsd-config'
+
+test-object-config:
+    asterisk-instances: 1
+    connect-ami: True
+    reactor-timeout: 15
+
+statsd-config:
+    -
+        'foo|val:2'
+    -
+        'bar|val:1'
+    -
+        'last|val:3'
+
+properties:
+    minversion: '13.5.0'
+    dependencies:
+        - python: 'autobahn.websocket'
+        - python: 'starpy'
+        - python: 'twisted'
+    tags:
+        - statsd
+        - apps
\ No newline at end of file
diff --git a/tests/apps/statsd/tests.yaml b/tests/apps/statsd/tests.yaml
new file mode 100644
index 0000000..d39f00d
--- /dev/null
+++ b/tests/apps/statsd/tests.yaml
@@ -0,0 +1,3 @@
+# Enter tests here in the order they should be considered for execution:
+tests:
+    - test: 'nominal'
diff --git a/tests/apps/tests.yaml b/tests/apps/tests.yaml
index 19163d4..3b94a91 100644
--- a/tests/apps/tests.yaml
+++ b/tests/apps/tests.yaml
@@ -22,3 +22,4 @@
     - test: 'channel_redirect'
     - dir: 'disa'
     - dir: 'authenticate'
+    - dir: 'statsd'

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I4eaf92465b79569fff4db6ada16e8d215f7d214b
Gerrit-PatchSet: 6
Gerrit-Project: testsuite
Gerrit-Branch: master
Gerrit-Owner: Tyler Cambron <tcambron at digium.com>
Gerrit-Reviewer: Anonymous Coward #1000019
Gerrit-Reviewer: Ashley Sanders <asanders at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-Reviewer: Matt Jordan <mjordan at digium.com>



More information about the asterisk-commits mailing list