[asterisk-commits] mjordan: testsuite/asterisk/trunk r4631 - in /asterisk/trunk/tests/cdr: ./ sq...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Jan 31 09:50:20 CST 2014


Author: mjordan
Date: Fri Jan 31 09:49:53 2014
New Revision: 4631

URL: http://svnview.digium.com/svn/testsuite?view=rev&rev=4631
Log:
cdr/sqlite3: Add test for cdr_sqlite3_custom CDR backend

This patch adds a test for SQLite3 CDRs. It verifies that the records
are written out to a SQLite3 database, and that custom values can be written
out as well. It also verifies proper appending of userfield data, as well
as some other odds and ends of CDR behavior.

(issue ASTERISK-23162)

Review: https://reviewboard.asterisk.org/r/3152/

Added:
    asterisk/trunk/tests/cdr/sqlite3/
    asterisk/trunk/tests/cdr/sqlite3/cdr_sqlite3.py   (with props)
    asterisk/trunk/tests/cdr/sqlite3/configs/
    asterisk/trunk/tests/cdr/sqlite3/configs/ast1/
    asterisk/trunk/tests/cdr/sqlite3/configs/ast1/cdr.conf   (with props)
    asterisk/trunk/tests/cdr/sqlite3/configs/ast1/cdr_sqlite3_custom.conf   (with props)
    asterisk/trunk/tests/cdr/sqlite3/configs/ast1/extensions.conf   (with props)
    asterisk/trunk/tests/cdr/sqlite3/test-config.yaml   (with props)
Modified:
    asterisk/trunk/tests/cdr/tests.yaml

Added: asterisk/trunk/tests/cdr/sqlite3/cdr_sqlite3.py
URL: http://svnview.digium.com/svn/testsuite/asterisk/trunk/tests/cdr/sqlite3/cdr_sqlite3.py?view=auto&rev=4631
==============================================================================
--- asterisk/trunk/tests/cdr/sqlite3/cdr_sqlite3.py (added)
+++ asterisk/trunk/tests/cdr/sqlite3/cdr_sqlite3.py Fri Jan 31 09:49:53 2014
@@ -1,0 +1,146 @@
+#!/usr/bin/env python
+"""Pluggable module for the sqlite3 test
+
+Copyright (C) 2013, Digium, Inc.
+Matt Jordan <mjordan at digium.com>
+
+This program is free software, distributed under the terms of
+the GNU General Public License Version 2.
+"""
+
+import sys
+import logging
+import sqlite3
+import re
+
+sys.path.append("lib/python")
+from config import ConfigFile
+
+LOGGER = logging.getLogger(__name__)
+
+class CDRSQLite3Verifier(object):
+    """A class that verifies CDRs in SQLite3 records"""
+
+
+    def __init__(self, module_config, test_object):
+        """Constructor"""
+
+        self.module_config = module_config
+        self.test_object = test_object
+
+        # Hook ourselves onto the test object
+        test_object.register_stop_observer(self.check_cdr_records)
+
+
+    def verify_record(self, actual, expected):
+        """Verify two records are the same
+
+        Note that we allow fields in actual to exist that aren't
+        in expected. Every field in expected should exist in the
+        actual record.
+
+        Keyword Arguments:
+        actual The actual record retrieved
+        expected The expected record
+
+        Returns:
+        True if the two records are a match
+        False otherwise
+        """
+
+        for expected_key, expected_value in expected.items():
+            if expected_key not in actual:
+                LOGGER.debug("Field %s is not in actual record" % expected_key)
+                return False
+
+            actual_value = actual[expected_key]
+            if not re.match(expected_value.lower(),
+                            actual_value.strip().lower()):
+                LOGGER.debug("Field %s: actual %s != expected %s" %
+                    (expected_key, actual_value, expected_value))
+                return False
+        return True
+
+    def get_sqlite_config(self, ast_instance):
+        """Retrieve necessary SQLite3 config parameters from the config file
+
+        Keyword Arguments:
+        ast_instance The instance of Asterisk that used the config file
+
+        Returns:
+        Tuple of (table, columns)
+        """
+        sqlite_config_file = ("%s/%s/cdr_sqlite3_custom.conf" %
+                              (ast_instance.base,
+                               ast_instance.directories['astetcdir']))
+
+        sqlite_config = ConfigFile(sqlite_config_file)
+        for option in sqlite_config.categories[0].options:
+            if option[0] == 'table':
+                table = option[1]
+            elif option[0] == 'columns':
+                columns = [col.strip() for col in option[1].split(',')]
+        return (table, columns)
+
+    def check_cdr_records(self, callback_param):
+        """A deferred callback method that is called by the TestCase
+        derived object when all Asterisk instances have stopped
+
+        Parameters:
+        callback_param
+        """
+
+        overall_success = []
+
+        for instance in self.module_config:
+            instance = instance or {}
+            ast_index = instance.get('asterisk-instance') or 0
+            database = instance.get('database') or 'master.db'
+            lines = instance.get('lines')
+
+            if not lines:
+                LOGGER.warning('No expected CDR entries in config?')
+                continue
+
+            ast_instance = self.test_object.ast[ast_index]
+
+            LOGGER.debug("Checking CDR records from %s" % ast_instance.host)
+
+            table, columns = self.get_sqlite_config(ast_instance)
+
+            sqlite_database = "%s/%s/%s" % (ast_instance.base,
+                 ast_instance.directories['astlogdir'],
+                 database)
+
+            conn = sqlite3.connect(sqlite_database)
+            cursor = conn.cursor()
+            cursor.execute("SELECT %s FROM %s" % (','.join(columns), table))
+            entries = cursor.fetchall()
+
+            # Convert each SQL result to a dictionary of columns, values
+            cdr_entries = [dict(zip(columns, list(entry))) for entry in entries]
+            if len(cdr_entries) != len(lines):
+                LOGGER.error("Expected entries %d != actual number %d" %
+                    (len(lines), len(cdr_entries)))
+                overall_success.append(False)
+                continue
+
+            # Test each against the expected
+            for cdr_entry in cdr_entries:
+                new_lines = [line for line in lines if
+                             not self.verify_record(cdr_entry, line)]
+                success = (len(new_lines) != len(lines))
+                if not success:
+                    LOGGER.error("CDR record %s failed to match any expected" %
+                        str(cdr_entry))
+                overall_success.append(success)
+
+                lines = new_lines
+
+            conn.close()
+
+        self.test_object.set_passed(all(overall_success))
+        return callback_param
+
+
+

Propchange: asterisk/trunk/tests/cdr/sqlite3/cdr_sqlite3.py
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: asterisk/trunk/tests/cdr/sqlite3/cdr_sqlite3.py
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: asterisk/trunk/tests/cdr/sqlite3/cdr_sqlite3.py
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: asterisk/trunk/tests/cdr/sqlite3/configs/ast1/cdr.conf
URL: http://svnview.digium.com/svn/testsuite/asterisk/trunk/tests/cdr/sqlite3/configs/ast1/cdr.conf?view=auto&rev=4631
==============================================================================
--- asterisk/trunk/tests/cdr/sqlite3/configs/ast1/cdr.conf (added)
+++ asterisk/trunk/tests/cdr/sqlite3/configs/ast1/cdr.conf Fri Jan 31 09:49:53 2014
@@ -1,0 +1,3 @@
+[general]
+unanswered=no
+debug=yes

Propchange: asterisk/trunk/tests/cdr/sqlite3/configs/ast1/cdr.conf
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: asterisk/trunk/tests/cdr/sqlite3/configs/ast1/cdr.conf
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: asterisk/trunk/tests/cdr/sqlite3/configs/ast1/cdr.conf
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: asterisk/trunk/tests/cdr/sqlite3/configs/ast1/cdr_sqlite3_custom.conf
URL: http://svnview.digium.com/svn/testsuite/asterisk/trunk/tests/cdr/sqlite3/configs/ast1/cdr_sqlite3_custom.conf?view=auto&rev=4631
==============================================================================
--- asterisk/trunk/tests/cdr/sqlite3/configs/ast1/cdr_sqlite3_custom.conf (added)
+++ asterisk/trunk/tests/cdr/sqlite3/configs/ast1/cdr_sqlite3_custom.conf Fri Jan 31 09:49:53 2014
@@ -1,0 +1,5 @@
+[master]
+
+table => cdr
+columns => calldate, clid, dcontext, channel, dstchannel, lastapp, lastdata, duration, billsec, disposition, amaflags, accountcode, uniqueid, userfield, test_caller, test_callee
+values => '${CDR(start)}','${CDR(clid)}','${CDR(dcontext)}','${CDR(channel)}','${CDR(dstchannel)}','${CDR(lastapp)}','${CDR(lastdata)}','${CDR(duration)}','${CDR(billsec)}','${CDR(disposition)}','${CDR(amaflags)}','${CDR(accountcode)}','${CDR(uniqueid)}','${CDR(userfield)}','${CDR(test_caller)}','${CDR(test_callee)}'

Propchange: asterisk/trunk/tests/cdr/sqlite3/configs/ast1/cdr_sqlite3_custom.conf
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: asterisk/trunk/tests/cdr/sqlite3/configs/ast1/cdr_sqlite3_custom.conf
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: asterisk/trunk/tests/cdr/sqlite3/configs/ast1/cdr_sqlite3_custom.conf
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: asterisk/trunk/tests/cdr/sqlite3/configs/ast1/extensions.conf
URL: http://svnview.digium.com/svn/testsuite/asterisk/trunk/tests/cdr/sqlite3/configs/ast1/extensions.conf?view=auto&rev=4631
==============================================================================
--- asterisk/trunk/tests/cdr/sqlite3/configs/ast1/extensions.conf (added)
+++ asterisk/trunk/tests/cdr/sqlite3/configs/ast1/extensions.conf Fri Jan 31 09:49:53 2014
@@ -1,0 +1,33 @@
+
+[pre_dial]
+
+exten => callee,1,NoOp()
+ same => n,Set(CDR(test_callee)=callee_value)
+ same => n,Set(CDR(userfield)=callee_userfield)
+ same => n,Set(TIMEOUT(absolute)=5)
+ same => n,Return()
+
+exten => caller,1,NoOp()
+ same => n,Set(CDR(test_caller)=caller_value)
+ same => n,Set(CDR(userfield)=caller_userfield)[
+ same => n,Return()
+
+[default]
+
+exten => 1000,1,NoOp()
+ same => n,Set(CALLERID(name)=Alice)
+ same => n,Set(CALLERID(num)=555-5555)
+ same => n,Set(CHANNEL(accountcode)=foobar)
+ same => n,Set(CHANNEL(amaflags)=BILLING)
+ same => n,Dial(Local/target,,b(pre_dial^callee^1)B(pre_dial^caller^1))
+ same => n,Hangup()
+
+exten => target,1,NoOp()
+ same => n,Set(CHANNEL(amaflags)=OMIT)
+ same => n,Answer()
+ same => n,Echo()
+ same => n,Hangup()
+
+exten => h,1,NoOp()
+
+exten => t,1,NoOp()

Propchange: asterisk/trunk/tests/cdr/sqlite3/configs/ast1/extensions.conf
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: asterisk/trunk/tests/cdr/sqlite3/configs/ast1/extensions.conf
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: asterisk/trunk/tests/cdr/sqlite3/configs/ast1/extensions.conf
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: asterisk/trunk/tests/cdr/sqlite3/test-config.yaml
URL: http://svnview.digium.com/svn/testsuite/asterisk/trunk/tests/cdr/sqlite3/test-config.yaml?view=auto&rev=4631
==============================================================================
--- asterisk/trunk/tests/cdr/sqlite3/test-config.yaml (added)
+++ asterisk/trunk/tests/cdr/sqlite3/test-config.yaml Fri Jan 31 09:49:53 2014
@@ -1,0 +1,95 @@
+testinfo:
+    summary: Verify CDRs using a SQLite3 backend
+    description: |
+        "This test verifies the following:
+         1. That the SQLite3 backend records CDRs appropriately
+         2. That adding custom fields using the CDR function results
+            in those records being recorded appropriately
+         3. That the userfield from two channels is combined
+            appropriately when those channels are bridged
+         4. That setting AMA flags through the CHANNEL function is
+            set on the channels correctly
+         5. That the presence of the 'h' extension doesn't result in
+            additional CDRs
+        This seems like a lot, but it's all relatively common
+        dialplan."
+
+test-modules:
+    add-test-to-search-path: True
+    test-object:
+        config-section: test-object-config
+        typename: 'test_case.SimpleTestCase'
+    modules:
+        -
+            minversion: '12.0.0'
+            config-section: 'cdr-config-12'
+            typename: 'cdr_sqlite3.CDRSQLite3Verifier'
+        -
+            config-section: 'hangup-monitor'
+            typename: 'pluggable_modules.HangupMonitor'
+
+test-object-config:
+    ignore-originate-failures: True
+    test-iterations:
+        -
+            channel: 'Local/1000 at default'
+            application: 'Echo'
+            async: True
+
+hangup-monitor:
+    ids: '0'
+
+cdr-config-12:
+    -
+        asterisk-instance: 0
+        lines:
+            -
+                clid: '"Alice" <555-5555>'
+                dcontext: 'default'
+                channel: 'Local/1000 at default-.{8};2'
+                dstchannel: 'Local/target at default-.{7}1;'
+                lastapp: 'Dial'
+                lastdata: 'Local/target.*'
+                disposition: 'ANSWERED'
+                amaflags: 'BILLING'
+                accountcode: 'foobar'
+                userfield: 'caller_userfield;callee_userfield'
+                test_caller: 'caller_value'
+                test_callee: 'callee_value'
+            -
+                clid: '"" <>'
+                dcontext: 'default'
+                channel: 'Local/1000 at default-.{8};1'
+                dstchannel: ''
+                lastapp: 'Echo'
+                lastdata: ''
+                disposition: 'ANSWERED'
+                amaflags: 'BILLING'
+                accountcode: 'foobar'
+                userfield: ''
+                test_caller: ''
+                test_callee: ''
+            -
+                clid: '"Alice" <555-5555>'
+                dcontext: 'default'
+                channel: 'Local/target at default-.{8};2'
+                dstchannel: ''
+                lastapp: 'Echo'
+                lastdata: ''
+                disposition: 'ANSWERED'
+                amaflags: 'OMIT'
+                accountcode: 'foobar'
+                userfield: ''
+                test_caller: ''
+                test_callee: ''
+
+properties:
+    minversion: '12.0.0'
+    dependencies:
+        - python : 'twisted'
+        - python : 'starpy'
+        - python : 'sqlite3'
+        - asterisk : 'cdr_sqlite3_custom'
+
+    tags:
+        - CDR

Propchange: asterisk/trunk/tests/cdr/sqlite3/test-config.yaml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: asterisk/trunk/tests/cdr/sqlite3/test-config.yaml
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: asterisk/trunk/tests/cdr/sqlite3/test-config.yaml
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: asterisk/trunk/tests/cdr/tests.yaml
URL: http://svnview.digium.com/svn/testsuite/asterisk/trunk/tests/cdr/tests.yaml?view=diff&rev=4631&r1=4630&r2=4631
==============================================================================
--- asterisk/trunk/tests/cdr/tests.yaml (original)
+++ asterisk/trunk/tests/cdr/tests.yaml Fri Jan 31 09:49:53 2014
@@ -13,4 +13,5 @@
     - test: 'batch_cdrs'
     - dir: 'cdr_manipulation'
     - dir: 'cdr_properties'
+    - test: 'sqlite3'
 




More information about the asterisk-commits mailing list