[asterisk-commits] mmichelson: branch mmichelson/bridge-tests r3291 - /asterisk/team/mmichelson/...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Jul 3 12:56:53 CDT 2012


Author: mmichelson
Date: Tue Jul  3 12:56:51 2012
New Revision: 3291

URL: http://svnview.digium.com/svn/testsuite?view=rev&rev=3291
Log:
Initial code for an AMI event module.


Modified:
    asterisk/team/mmichelson/bridge-tests/lib/python/asterisk/TestCase.py
    asterisk/team/mmichelson/bridge-tests/lib/python/asterisk/ami.py

Modified: asterisk/team/mmichelson/bridge-tests/lib/python/asterisk/TestCase.py
URL: http://svnview.digium.com/svn/testsuite/asterisk/team/mmichelson/bridge-tests/lib/python/asterisk/TestCase.py?view=diff&rev=3291&r1=3290&r2=3291
==============================================================================
--- asterisk/team/mmichelson/bridge-tests/lib/python/asterisk/TestCase.py (original)
+++ asterisk/team/mmichelson/bridge-tests/lib/python/asterisk/TestCase.py Tue Jul  3 12:56:51 2012
@@ -72,6 +72,7 @@
         self.testlogdir = os.path.join(Asterisk.test_suite_root, self.base, str(os.getpid()))
         self.ast_version = AsteriskVersion()
         self._stop_callbacks = []
+        self._ami_callbacks = []
 
         os.makedirs(self.testlogdir)
 
@@ -392,6 +393,8 @@
         self.ami[ami.id] = ami
         try:
             self.ami_connect(ami)
+            for callback in self._ami_callbacks:
+                callback(ami)
         except:
             logger.error("Exception raised in ami_connect:")
             logger.error(traceback.format_exc())
@@ -456,3 +459,12 @@
         all instances of Asterisk are stopped.
         '''
         self._stop_callbacks.append(callback)
+
+    def register_ami_observer(self, callback):
+        ''' Register an observer that will be called when TestCase connects with
+        Asterisk over the Manager interface
+
+        Parameters:
+        callback The deferred callback function to be called when AMI connects
+        '''
+        self._ami_callbacks.append(callback)

Modified: asterisk/team/mmichelson/bridge-tests/lib/python/asterisk/ami.py
URL: http://svnview.digium.com/svn/testsuite/asterisk/team/mmichelson/bridge-tests/lib/python/asterisk/ami.py?view=diff&rev=3291&r1=3290&r2=3291
==============================================================================
--- asterisk/team/mmichelson/bridge-tests/lib/python/asterisk/ami.py (original)
+++ asterisk/team/mmichelson/bridge-tests/lib/python/asterisk/ami.py Tue Jul  3 12:56:51 2012
@@ -3,8 +3,101 @@
 import datetime
 import sys
 import logging
+import re
+import TestRunner
 
 logger = logging.getLogger(__name__)
+
+class AMIEventInstance:
+    def __init__(self, instance_config, test_object):
+        self.match_conditions = dict(instance_config['conditions']['match'])
+        self.nonmatch_conditions = dict(instance_config['conditions']['nomatch'])
+        self.passed = True if instance_config['start'] == 'pass' else False
+        self.ids = instance_config['id'].split(',')
+
+        if 'Event' not in self.matches:
+            logger.error("No event specified to match on. Aborting test")
+            raise
+
+        test_object.register_ami_observer(self.ami_connect)
+
+    def ami_connect(self, ami):
+        if str(ami.id) in self.ids:
+            ami.registerEvent(self.match_conditions['Event'], self.__event_callback)
+
+    def event_callback(self, ami, event):
+        """Virtual method overridden by specific AMI Event
+        instance types"""
+
+    def __event_callback(self, ami, event):
+        """Check event conditions to see if subclasses should
+        be called into"""
+
+        for k,v in self.match_conditions:
+            if not re.match(v, event.get(k)):
+                return
+
+        for k,v in self.nonmatch_conditions:
+            if re.match(v, event.get(k)):
+                return
+
+        #Conditions have matched up as expected
+        #so leave it to the individual types to determine
+        #how to proceed
+
+        self.event_callback(ami, event)
+
+class AMIHeaderMatchInstance(AMIEventInstance):
+    def __init__(self, instance_config, test_object):
+        super(instance_config)
+        self.match_requirements = dict(instance_config['requirements']['match'])
+        self.nonmatch_requirements =
+            dict(instance_config['requirements']['nomatch'])
+
+    def event_callback(self, ami, event):
+        for k,v in self.match_requirements:
+            if not re.match(v, event.get(k)):
+                raise
+
+        for k,v, in self.nonmatch_requirements:
+            if re.match(v, event.get(k)):
+                raise
+
+class AMICallbackInstance(AMIEventInstance):
+    def __init__(self, instance_config, test_object):
+        super(instance_config, test_object)
+        self.callback_module = instance_config['callback_module']
+        self.callback_method = instance_config['callback_method']
+        self.module = None
+
+    def event_callback(self, ami, event):
+        if not self.module:
+            finder = TestModuleFinder()
+            self.module = finder.find_module(self.callback_module)
+            if not self.module:
+                raise
+            self.module.load_module(self.callback_module)
+            #XXX Now what? Just call the method? getattr?
+            self.callback_method(ami, event)
+
+class AMIEventInstanceFactory:
+    def create_instance(instance_config):
+        instance_type = instance_config['type']
+        if instance_type == "headermatch":
+            return AMIHeaderMatchInstance(instance_config)
+        elif instance_type = "callback:":
+            return AMICallbackInstance(instance_config)
+        else:
+            logger.error("Invalid type %s specified for AMI event instance" %
+                    instance_type)
+            raise
+
+class AMIEvent(object):
+    def __init__(self, module_config, test_object):
+        self.test_object = test_object
+        self.ami_instances = []
+        for instance in module_config:
+            self.ami_instances.append(AMIEventInstanceFactory.create_instance(instance)
 
 class AMI:
     def __init__(self, on_login, on_error, timeout=60, user="mark", secret="mysecret", host="127.0.0.1", port=5038):




More information about the asterisk-commits mailing list