[asterisk-commits] dlee: branch dlee/ari-tests r3846 - in /asterisk/team/dlee/ari-tests: lib/pyt...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Jun 12 15:21:31 CDT 2013


Author: dlee
Date: Wed Jun 12 15:21:29 2013
New Revision: 3846

URL: http://svnview.digium.com/svn/testsuite?view=rev&rev=3846
Log:
Docs and final cleanup

Added:
    asterisk/team/dlee/ari-tests/sample-yaml/ari-config.yaml.sample   (with props)
Modified:
    asterisk/team/dlee/ari-tests/lib/python/asterisk/ari.py
    asterisk/team/dlee/ari-tests/tests/rest_api/continue/configs/ast1/extensions.conf
    asterisk/team/dlee/ari-tests/tests/rest_api/continue/rest_continue.py
    asterisk/team/dlee/ari-tests/tests/rest_api/continue/test-config.yaml

Modified: asterisk/team/dlee/ari-tests/lib/python/asterisk/ari.py
URL: http://svnview.digium.com/svn/testsuite/asterisk/team/dlee/ari-tests/lib/python/asterisk/ari.py?view=diff&rev=3846&r1=3845&r2=3846
==============================================================================
--- asterisk/team/dlee/ari-tests/lib/python/asterisk/ari.py (original)
+++ asterisk/team/dlee/ari-tests/lib/python/asterisk/ari.py Wed Jun 12 15:21:29 2013
@@ -64,7 +64,7 @@
     '''Twisted protocol factory for building ARI WebSocket clients.
     '''
     def __init__(self, host, apps, on_event, port=DEFAULT_PORT,
-                 timeout_secs=15):
+                 timeout_secs=60):
         '''Constructor
 
         :param host: Hostname of Asterisk.
@@ -156,6 +156,7 @@
         '''
         self.base_url = "http://%s:%d/stasis" % (host, port)
 
+
     def build_url(self, *args):
         '''Build a URL from the given path.
 
@@ -173,30 +174,46 @@
 
         :param args: Path segements.
         :param kwargs: Query parameters.
+        :returns: requests.models.Response
+        :throws: requests.exceptions.HTTPError
         '''
         url = self.build_url(*args, **kwargs)
         logger.info("GET %s %r" % (url, kwargs))
-        return requests.get(url, params=kwargs)
+        return raise_on_err(requests.get(url, params=kwargs))
 
     def post(self, *args, **kwargs):
         '''Send a POST request to ARI.
 
         :param args: Path segements.
         :param kwargs: Query parameters.
+        :returns: requests.models.Response
+        :throws: requests.exceptions.HTTPError
         '''
         url = self.build_url(*args, **kwargs)
         logger.info("POST %s %r" % (url, kwargs))
-        return requests.post(url, params=kwargs)
+        return raise_on_err(requests.post(url, params=kwargs))
 
     def delete(self, *args, **kwargs):
         '''Send a DELETE request to ARI.
 
         :param args: Path segements.
         :param kwargs: Query parameters.
+        :returns: requests.models.Response
+        :throws: requests.exceptions.HTTPError
         '''
         url = self.build_url(*args, **kwargs)
         logger.info("DELETE %s %r" % (url, kwargs))
-        return requests.delete(url, params=kwargs)
+        return raise_on_err(requests.delete(url, params=kwargs))
+
+
+def raise_on_err(resp):
+    '''Helper to raise an exception when a response is a 4xx or 5xx error.
+
+    :param resp: requests.models.Response object
+    :returns: resp
+    '''
+    resp.raise_for_status()
+    return resp
 
 
 class EventMatcher(object):
@@ -233,7 +250,7 @@
                 if not res:
                     logger.error("Callback failed: %r" %
                                  self.instance_config)
-                self.passed = self.passed and res
+                    self.passed = False
             except:
                 logger.error("Exception in callback: %s" %
                              traceback.format_exc())
@@ -275,6 +292,8 @@
     :returns: True if message matches pattern; False otherwise.
     '''
     #logger.debug("%r ?= %r" % (pattern, message))
+    #logger.debug("  %r" % type(pattern))
+    #logger.debug("  %r" % type(message))
     if pattern is None:
         # Empty pattern always matches
         return True
@@ -284,6 +303,7 @@
         i = 0
         while res and i < len(pattern):
             res = all_match(pattern[i], message[i])
+            i += 1
         return res
     elif isinstance(pattern, dict):
         # Dict should match for every field in the pattern.
@@ -293,7 +313,7 @@
             if to_check is None or not all_match(value, to_check):
                 return False
         return True
-    elif isinstance(pattern, str):
+    elif isinstance(pattern, str) or isinstance(pattern, unicode):
         # Pattern strings are considered to be regexes
         return re.match(pattern, message) is not None
     elif isinstance(pattern, int):

Added: asterisk/team/dlee/ari-tests/sample-yaml/ari-config.yaml.sample
URL: http://svnview.digium.com/svn/testsuite/asterisk/team/dlee/ari-tests/sample-yaml/ari-config.yaml.sample?view=auto&rev=3846
==============================================================================
--- asterisk/team/dlee/ari-tests/sample-yaml/ari-config.yaml.sample (added)
+++ asterisk/team/dlee/ari-tests/sample-yaml/ari-config.yaml.sample Wed Jun 12 15:21:29 2013
@@ -1,0 +1,70 @@
+# -*- yaml -*-
+# Configuration sample for the Generic ARI module
+
+ari-config:
+    # ARI always attempts to connect to the /ari/events WebSocket.
+    # This is a comma seperated list of applications to connect for.
+    apps: foo-test
+    # List of events to monitor for. Every event received on the WebSocket
+    # is compared against the 'conditions' clause. If the conditions match,
+    # then the further specified processing is evaluated.
+    events:
+        -
+            # Conditions that must be met in order to consider performing the
+            # match.
+            conditions:
+                # Specifies a pattern that must match the received JSON object
+                # for performing the match.
+                #
+                #  * object - Specifies fields must match JSON. JSON is allowed
+                #             to have unspecified fields.
+                #  * string - A regular expression that must match JSON.
+                #  * int    - Literal integer that must match.
+                #  * array  - Contents must match JSON
+                match:
+                    application: foo-test
+                    stasis_start:
+                        args: ['bar.*']
+                # Specified a pattern that must NOT match the received JSON
+                # object. The pattern is evaluated identically to a match
+                # clause
+                nomatch:
+                    stasis_start:
+                        args: ['barman.*']
+
+            # The above example would match the following:
+            #  { 'application': 'foo-test', 'args': [ 'bar' ] }
+            #   * simple match
+            #  { 'application': 'foo-test', 'args': [ 'bardoll' ] }
+            #   * bardoll matches the bar.* regex
+            #  { 'application': 'foo-test', 'args': [ 'bar' ], 'whatever': 1 }
+            #   * extra fields in the JSON are ignored
+            #
+            # But not these:
+            #  { 'application': 'foo-test', 'args': [] }
+            #   * array contents do not match
+            #  { 'application': 'foo-test', 'args': [ 'bar', 'bam' ] }
+            #   * array contents do not match
+            #  { 'application': 'foo-test', 'args': [ 'barman' ] }
+            #   * matches the nomatch clause
+
+            # Specifies how many times the event is expected to occur. If
+            # unspecified, will match for at least one.
+            #  * Bare number - exact count
+            #  * >number - at least count
+            #  * <number - at most count
+            count: 1
+
+            # If specified, lists the module and callback function to which the
+            # event is passed. The event is passed in as a parsed JSON object
+            # (so it's a dict with string keys that contains strings, ints,
+            # arrays and other dictionaries). The callback is also given an
+            # ARI object, on which it may invoke ARI HTTP calls.
+            # If the callback returns a falsey value, or raises an exception,
+            # the test will fail.
+            #
+            # In this example, the rest_foo.py file will contain:
+            # def on_start(ari, event):
+            callback:
+                module: rest_foo
+                method: on_start

Propchange: asterisk/team/dlee/ari-tests/sample-yaml/ari-config.yaml.sample
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: asterisk/team/dlee/ari-tests/sample-yaml/ari-config.yaml.sample
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: asterisk/team/dlee/ari-tests/sample-yaml/ari-config.yaml.sample
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: asterisk/team/dlee/ari-tests/tests/rest_api/continue/configs/ast1/extensions.conf
URL: http://svnview.digium.com/svn/testsuite/asterisk/team/dlee/ari-tests/tests/rest_api/continue/configs/ast1/extensions.conf?view=diff&rev=3846&r1=3845&r2=3846
==============================================================================
--- asterisk/team/dlee/ari-tests/tests/rest_api/continue/configs/ast1/extensions.conf (original)
+++ asterisk/team/dlee/ari-tests/tests/rest_api/continue/configs/ast1/extensions.conf Wed Jun 12 15:21:29 2013
@@ -3,5 +3,5 @@
 exten => s,1,NoOp()
 	same => n,Answer()
 	same => n,Stasis(continue-test)
-	same => n,UserEvent(TestResult,Result: pass,Status: successfully broke out of Stasis)
+	same => n,Stasis(continue-test,fin)
 	same => n,Hangup()

Modified: asterisk/team/dlee/ari-tests/tests/rest_api/continue/rest_continue.py
URL: http://svnview.digium.com/svn/testsuite/asterisk/team/dlee/ari-tests/tests/rest_api/continue/rest_continue.py?view=diff&rev=3846&r1=3845&r2=3846
==============================================================================
--- asterisk/team/dlee/ari-tests/tests/rest_api/continue/rest_continue.py (original)
+++ asterisk/team/dlee/ari-tests/tests/rest_api/continue/rest_continue.py Wed Jun 12 15:21:29 2013
@@ -17,12 +17,18 @@
     logger.debug("on_start(%r)" % event)
     global id
     id = event['stasis_start']['channel']['uniqueid']
-    resp = ari.post('channels', id, 'continue')
-    resp.raise_for_status()
+    ari.post('channels', id, 'continue')
     return True
 
 
 def on_end(ari, event):
+    global id
     logger.debug("on_end(%r)" % event)
+    return id == event['stasis_end']['channel']['uniqueid']
+
+
+def on_second_start(ari, event):
     global id
-    return id == event['stasis_end']['channel']['uniqueid']
+    logger.debug("on_second_start(%r)" % event)
+    ari.delete('channels', id)
+    return id == event['stasis_start']['channel']['uniqueid']

Modified: asterisk/team/dlee/ari-tests/tests/rest_api/continue/test-config.yaml
URL: http://svnview.digium.com/svn/testsuite/asterisk/team/dlee/ari-tests/tests/rest_api/continue/test-config.yaml?view=diff&rev=3846&r1=3845&r2=3846
==============================================================================
--- asterisk/team/dlee/ari-tests/tests/rest_api/continue/test-config.yaml (original)
+++ asterisk/team/dlee/ari-tests/tests/rest_api/continue/test-config.yaml Wed Jun 12 15:21:29 2013
@@ -12,8 +12,6 @@
     modules:
         -   config-section: ari-config
             typename: ari.WebSocketEventModule
-        -   config-section: ami-config
-            typename: ami.AMIEventModule
 
 test-object-config:
     spawn-after-hangup: True
@@ -37,20 +35,19 @@
                 match:
                     application: continue-test
                     stasis_end:
+            count: 2
+            callback:
+                module: rest_continue
+                method: on_end
+        -   conditions:
+                match:
+                    application: continue-test
+                    stasis_start:
+                        args: [fin]
             count: 1
             callback:
                 module: rest_continue
-                method: on_end
-
-ami-config:
-    -   type: headermatch
-        conditions:
-            match:
-                Event: UserEvent
-        requirements:
-            match:
-                Result: pass
-        count: 1
+                method: on_second_start
 
 properties:
     minversion: '12.0.0'
@@ -60,7 +57,6 @@
         - python : twisted
         - python : starpy
         - asterisk : res_stasis_http_channels
-        - asterisk : app_userevent
         - asterisk : app_echo
     tags:
         - ARI




More information about the asterisk-commits mailing list