[Asterisk-code-review] StatsD: Create a mock StatsD server in the Testsuite (testsuite[master])

Tyler Cambron asteriskteam at digium.com
Fri Sep 25 11:50:38 CDT 2015


Tyler Cambron has uploaded a new change for review.

  https://gerrit.asterisk.org/1313

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

StatsD: Create a mock StatsD server in the Testsuite

Create a pluggable module that accepts configuration from a test-config.yaml
to indicate what messages are to be received from Asterisk. The mock server
is placed inside of the pluggable module and receives incoming messages.
The messages are then checked against the expected values.

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, 126 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/testsuite refs/changes/13/1313/2

diff --git a/tests/apps/statsd/nominal/mockd.py b/tests/apps/statsd/nominal/mockd.py
new file mode 100644
index 0000000..d243ac4
--- /dev/null
+++ b/tests/apps/statsd/nominal/mockd.py
@@ -0,0 +1,85 @@
+'''
+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 sys
+import logging
+
+from twisted.internet.protocol import DatagramProtocol
+from twisted.internet import reactor
+
+sys.path.append("tests/apps/statsd/nominal")
+
+LOGGER = logging.getLogger(__name__)
+
+
+class MockDProtocol(DatagramProtocol):
+    ''' Protocol for the Mock Server to receive messages through
+    '''
+
+    def __init__(self, mockd_server):
+        ''' Constructor
+
+        :param mockd_server An instance of the mock StatsD server
+        '''
+        self.mockd_server = mockd_server
+
+    def datagramReceived(self, datagram, address):
+        ''' AMI Newexten event handler
+
+        :param datagram The datagram that was received by the server
+        :param address The address that the datagram came from
+
+        Accept the datagram and send it to be checked against the config
+        '''
+        self.mockd_server.message_received(datagram)
+
+
+class MockDServer(object):
+    ''' Pluggable Module object that acts as a StatsD server to examine
+    UDP messages to ensure that messages are being sent correctly
+    '''
+
+    def __init__(self, config, test_object):
+        ''' Constructor
+
+        :param config This object's YAML derived configuration
+        :param test_object The test object it plugs onto
+        '''
+        self.config = config
+        self.test_object = test_object
+        self.test_object.register_ami_observer(self.__on_ami_connect)
+        self.test_object.register_stop_observer(self._stop_handler)
+        reactor.listenUDP(8080, MockDProtocol(self))
+
+    def message_received(self, message):
+        ''' Datagram message handler
+
+        :param message The datagram that was received by the server
+
+        Check the message against the config and pass the test if they match
+        '''
+        self.message = message
+        if self.config[0] == self.message:
+            self.test_object.set_passed(True)
+            LOGGER.debug('Test passed')
+        else:
+            LOGGER.debug('Message of %s does not match config' % message)
+
+    def __on_ami_connect(self, ami):
+        ''' AMI connected handler
+
+        :param The AMI instance that just connected
+        '''
+        LOGGER.debug('AMI instance %s Connected' % ami)
+
+    def _stop_handler(self, result):
+        ''' A deferred callback called as a result of the test stopping
+
+        :param result The deferred parameter passed from callback to callback
+        '''
+        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..2c9b603
--- /dev/null
+++ b/tests/apps/statsd/nominal/test-config.yaml
@@ -0,0 +1,37 @@
+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: 10
+
+statsd-config:
+    -
+        'foo|val:2'
+
+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: newchange
Gerrit-Change-Id: I4eaf92465b79569fff4db6ada16e8d215f7d214b
Gerrit-PatchSet: 2
Gerrit-Project: testsuite
Gerrit-Branch: master
Gerrit-Owner: Tyler Cambron <tcambron at digium.com>



More information about the asterisk-code-review mailing list