[asterisk-commits] dlee: branch dlee/ari-bridge-tests r4025 - /asterisk/team/dlee/ari-bridge-tes...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Aug 15 16:11:15 CDT 2013


Author: dlee
Date: Thu Aug 15 16:11:14 2013
New Revision: 4025

URL: http://svnview.digium.com/svn/testsuite?view=rev&rev=4025
Log:
pep8. minor cleanup.

And by 'minor cleanup' it's like I just pushed trash around without
actually cleaning anything up.

Modified:
    asterisk/team/dlee/ari-bridge-tests/tests/rest_api/bridges/unhappy/bridge_unhappy.py

Modified: asterisk/team/dlee/ari-bridge-tests/tests/rest_api/bridges/unhappy/bridge_unhappy.py
URL: http://svnview.digium.com/svn/testsuite/asterisk/team/dlee/ari-bridge-tests/tests/rest_api/bridges/unhappy/bridge_unhappy.py?view=diff&rev=4025&r1=4024&r2=4025
==============================================================================
--- asterisk/team/dlee/ari-bridge-tests/tests/rest_api/bridges/unhappy/bridge_unhappy.py (original)
+++ asterisk/team/dlee/ari-bridge-tests/tests/rest_api/bridges/unhappy/bridge_unhappy.py Thu Aug 15 16:11:14 2013
@@ -11,7 +11,10 @@
 import requests
 import traceback
 
+from twisted.internet import reactor, defer
+
 LOGGER = logging.getLogger(__name__)
+
 
 class BridgeUnhappy(object):
     def __init__(self):
@@ -19,6 +22,7 @@
         self.stasis_channel_id = None
         self.ami_ready = False
         self.stasis_ready = False
+        self.has_run = False
 
     def on_start(self, ari, event):
         if not self.ami_channel_id:
@@ -40,7 +44,8 @@
         elif event['channel']['id'] == self.stasis_channel_id:
             pass
         else:
-            assert False, "Unexpected channel %s leaving Stasis" % event['channel']['id']
+            assert False, "Unexpected channel %s leaving Stasis" % \
+                event['channel']['id']
         return True
 
     def run_test(self, ari):
@@ -48,66 +53,100 @@
             # Not ready to run the test
             return
 
+        if self.has_run:
+            # only run once
+            return
+
         try:
             self.passing = True
             self.__run_test(ari)
             assert self.passing
         finally:
+            self.has_run = True
             ari.delete('channels', self.ami_channel_id)
-            self.ami_ready = False
             ari.delete('channels', self.stasis_channel_id)
-            self.stasis_ready = False
             ari.set_allow_errors(False)
 
     def __run_test(self, ari):
+        # Build some bridges to run the test with
         bridge_id = ari.post('bridges').json()['id']
         other_bridge_id = ari.post('bridges').json()['id']
-        ari.set_allow_errors(True)
 
         def validate(expected, resp):
+            '''Validate a response against its expected code'''
             expected_code = requests.codes[expected]
             if expected_code != resp.status_code:
                 self.passing = False
-                LOGGER.error("Expected %d (%s), got %s (%r)", expected_code, expected, resp.status_code, resp.json())
+                LOGGER.error("Expected %d (%s), got %s (%r)", expected_code,
+                             expected, resp.status_code, resp.json())
                 # Log current stack frame
-                for line in traceback.format_stack(inspect.currentframe())[-2:-4:-1]:
-                    if line:
+                frame = inspect.currentframe()
+                lines = traceback.format_stack(frame)[-2:-4:-1]
+                for line in lines:
                         LOGGER.error('"%s"' % line.strip())
 
+        # Disable auto-exceptions on HTTP errors.
+        ari.set_allow_errors(True)
+
+        #
         # Add to a nonexistent bridge
-        resp = ari.post('bridges', 'i-am-not-a-bridge', 'addChannel', channel=self.stasis_channel_id)
+        #
+        resp = ari.post('bridges', 'i-am-not-a-bridge', 'addChannel',
+                        channel=self.stasis_channel_id)
         validate('not_found', resp)
 
+        #
         # Add to a nonexistent bridge
-        resp = ari.post('bridges', 'i-am-not-a-bridge', 'removeChannel', channel=self.stasis_channel_id)
+        #
+        resp = ari.post('bridges', 'i-am-not-a-bridge', 'removeChannel',
+                        channel=self.stasis_channel_id)
         validate('not_found', resp)
 
+        #
         # Add a non-Stasis channel
-        resp = ari.post('bridges', bridge_id, 'addChannel', channel=self.ami_channel_id)
+        #
+        resp = ari.post('bridges', bridge_id, 'addChannel',
+                        channel=self.ami_channel_id)
         validate('unprocessable_entity', resp)
 
+        #
         # Add a nonexistent channel
-        resp = ari.post('bridges', bridge_id, 'addChannel', channel='i-am-not-a-channel')
+        #
+        resp = ari.post('bridges', bridge_id, 'addChannel',
+                        channel='i-am-not-a-channel')
         validate('bad_request', resp)
 
+        #
         # Remove a nonexistent channel
-        resp = ari.post('bridges', bridge_id, 'removeChannel', channel='i-am-not-a-channel')
+        #
+        resp = ari.post('bridges', bridge_id, 'removeChannel',
+                        channel='i-am-not-a-channel')
         validate('bad_request', resp)
 
+        #
         # Remove a channel that isn't in Stasis
-        resp = ari.post('bridges', bridge_id, 'removeChannel', channel=self.ami_channel_id)
+        #
+        resp = ari.post('bridges', bridge_id, 'removeChannel',
+                        channel=self.ami_channel_id)
         validate('unprocessable_entity', resp)
 
+        #
         # Remove a Stasis channel that isn't in a bridge at all
-        resp = ari.post('bridges', bridge_id, 'removeChannel', channel=self.stasis_channel_id)
+        #
+        resp = ari.post('bridges', bridge_id, 'removeChannel',
+                        channel=self.stasis_channel_id)
         validate('unprocessable_entity', resp)
 
         # We need a channel in a bridge for the next few tests
-        resp = ari.post('bridges', bridge_id, 'addChannel', channel=self.stasis_channel_id)
+        resp = ari.post('bridges', bridge_id, 'addChannel',
+                        channel=self.stasis_channel_id)
         resp.raise_for_status()
 
+        #
         # Remove a channel from the wrong bridge
-        resp = ari.post('bridges', other_bridge_id, 'removeChannel', channel=self.stasis_channel_id)
+        #
+        resp = ari.post('bridges', other_bridge_id, 'removeChannel',
+                        channel=self.stasis_channel_id)
         validate('unprocessable_entity', resp)
 
         # And, just to be safe, make sure the channel is still in its bridge
@@ -117,14 +156,14 @@
         assert [self.stasis_channel_id] == channels_in_bridge
 
         # Okay, now remove it
-        resp = ari.post('bridges', bridge_id, 'removeChannel', channel=self.stasis_channel_id)
+        resp = ari.post('bridges', bridge_id, 'removeChannel',
+                        channel=self.stasis_channel_id)
         resp.raise_for_status()
 
 
 TEST = BridgeUnhappy()
 
 
-# Forward missing functions to the TEST object
 def on_start(ari, event):
     r = TEST.on_start(ari, event)
     if r:




More information about the asterisk-commits mailing list