[Asterisk-code-review] testsuite: Add tests for mailbox polling. (...testsuite[13])

Friendly Automation asteriskteam at digium.com
Thu Apr 18 09:22:57 CDT 2019


Friendly Automation has submitted this change and it was merged. ( https://gerrit.asterisk.org/c/testsuite/+/11284 )

Change subject: testsuite: Add tests for mailbox polling.
......................................................................

testsuite: Add tests for mailbox polling.

Added five new tests for mailbox polling:
- Polling of a single mailbox
- Polling of multiple mailboxes
- Polling a single mailbox with multiple subscriptions
- Polling when no mailboxes have subscriptions
- Polling before and after an endpoint unregisters

Change-Id: Id3a40527624c094e253d56ee2df5381b03b22169
---
A tests/apps/voicemail/polling/poll_multiple_mailboxes/configs/ast1/pjsip.conf
A tests/apps/voicemail/polling/poll_multiple_mailboxes/configs/ast1/voicemail.conf
A tests/apps/voicemail/polling/poll_multiple_mailboxes/run-test
A tests/apps/voicemail/polling/poll_multiple_mailboxes/test-config.yaml
A tests/apps/voicemail/polling/poll_no_subs/configs/ast1/extensions.conf
A tests/apps/voicemail/polling/poll_no_subs/configs/ast1/voicemail.conf
A tests/apps/voicemail/polling/poll_no_subs/run-test
A tests/apps/voicemail/polling/poll_no_subs/test-config.yaml
A tests/apps/voicemail/polling/poll_same_mailbox/configs/ast1/pjsip.conf
A tests/apps/voicemail/polling/poll_same_mailbox/configs/ast1/voicemail.conf
A tests/apps/voicemail/polling/poll_same_mailbox/run-test
A tests/apps/voicemail/polling/poll_same_mailbox/test-config.yaml
A tests/apps/voicemail/polling/poll_single_mailbox/configs/ast1/pjsip.conf
A tests/apps/voicemail/polling/poll_single_mailbox/configs/ast1/voicemail.conf
A tests/apps/voicemail/polling/poll_single_mailbox/run-test
A tests/apps/voicemail/polling/poll_single_mailbox/test-config.yaml
A tests/apps/voicemail/polling/poll_sub_unsub/configs/ast1/extensions.conf
A tests/apps/voicemail/polling/poll_sub_unsub/configs/ast1/pjsip.conf
A tests/apps/voicemail/polling/poll_sub_unsub/configs/ast1/voicemail.conf
A tests/apps/voicemail/polling/poll_sub_unsub/run-test
A tests/apps/voicemail/polling/poll_sub_unsub/sipp/register.xml
A tests/apps/voicemail/polling/poll_sub_unsub/sipp/unregister.xml
A tests/apps/voicemail/polling/poll_sub_unsub/test-config.yaml
A tests/apps/voicemail/polling/tests.yaml
M tests/apps/voicemail/tests.yaml
25 files changed, 812 insertions(+), 0 deletions(-)

Approvals:
  Joshua Colp: Looks good to me, but someone else must approve
  George Joseph: Looks good to me, approved
  Friendly Automation: Approved for Submit



diff --git a/tests/apps/voicemail/polling/poll_multiple_mailboxes/configs/ast1/pjsip.conf b/tests/apps/voicemail/polling/poll_multiple_mailboxes/configs/ast1/pjsip.conf
new file mode 100644
index 0000000..3bffe84
--- /dev/null
+++ b/tests/apps/voicemail/polling/poll_multiple_mailboxes/configs/ast1/pjsip.conf
@@ -0,0 +1,51 @@
+[dummy-transport]
+type=transport
+bind=0.0.0.0
+protocol=udp
+
+[aor](!)
+type=aor
+max_contacts=1
+
+[auth](!)
+type=auth
+auth_type=userpass
+
+[endpoint](!)
+type=endpoint
+allow=!all,ulaw
+transport=dummy-transport
+context=default
+
+[1234](aor)
+
+[1234](auth)
+username=1234
+password=1234
+
+[1234](endpoint)
+auth=1234
+aors=1234
+mailboxes=1234 at default
+
+[5678](aor)
+
+[5678](auth)
+username=5678
+password=5678
+
+[5678](endpoint)
+auth=5678
+aors=5678
+mailboxes=5678 at default
+
+[9999](aor)
+
+[9999](auth)
+username=9999
+password=9999
+
+[9999](endpoint)
+auth=9999
+aors=9999
+mailboxes=9999 at default
diff --git a/tests/apps/voicemail/polling/poll_multiple_mailboxes/configs/ast1/voicemail.conf b/tests/apps/voicemail/polling/poll_multiple_mailboxes/configs/ast1/voicemail.conf
new file mode 100644
index 0000000..489a7a3
--- /dev/null
+++ b/tests/apps/voicemail/polling/poll_multiple_mailboxes/configs/ast1/voicemail.conf
@@ -0,0 +1,6 @@
+; Voicemail Configuration
+
+[general]
+format = ulaw
+pollmailboxes=yes
+pollfreq=2
diff --git a/tests/apps/voicemail/polling/poll_multiple_mailboxes/run-test b/tests/apps/voicemail/polling/poll_multiple_mailboxes/run-test
new file mode 100755
index 0000000..4db0314
--- /dev/null
+++ b/tests/apps/voicemail/polling/poll_multiple_mailboxes/run-test
@@ -0,0 +1,98 @@
+#!/usr/bin/env python
+
+'''
+Copyright (C) 2019, Sangoma Technologies Corporation
+Ben Ford <bford 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 asterisk.asterisk import Asterisk
+from asterisk.test_case import TestCase
+from asterisk.voicemail import VoiceMailMailboxManagement
+from twisted.internet import reactor
+
+LOGGER = logging.getLogger(__name__)
+
+class PollingTest(TestCase):
+    def __init__(self):
+        TestCase.__init__(self)
+        self.passed = False
+        self.flags = 0
+        self.create_asterisk()
+
+    def stop_test(self, passed=False):
+        self.passed = passed
+        reactor.stop()
+
+    def handle_message_waiting(self, ami, event):
+
+        mailbox = event.get("mailbox")
+        old = int(event.get("old"))
+        new = int(event.get("new"))
+        waiting = int(event.get("waiting"))
+
+        # Conditions:
+        # - If INBOX count is 1, then we need to remove the voicemail and set flag
+        # - If INBOX count is 0, just set flag
+        # - Once flags reaches 2^6-1 (111111), we've met all conditions
+        if new == 1 and waiting == 1:
+            if old != 0:
+                LOGGER.error("Expected 'Old' to be 0")
+                self.stop_test()
+            if mailbox == "1234 at default":
+                self.flags |= 1
+            elif mailbox == "5678 at default":
+                self.flags |= 2
+            elif mailbox == "9999 at default":
+                self.flags |= 4
+
+            data = mailbox.split("@")
+            mailbox = data[0]
+            if not self.voicemailManager.remove_mailbox("default", mailbox):
+                LOGGER.error("Failed to remove voicemail for mailbox {0}".format(mailbox))
+                self.stop_test()
+
+        elif new == 0 and waiting == 0 and old == 0:
+            if mailbox == "1234 at default":
+                self.flags |= 8
+            elif mailbox == "5678 at default":
+                self.flags |= 16
+            elif mailbox == "9999 at default":
+                self.flags |= 32
+        
+        # self.flags should be 63 (2^6-1 == 111111 in binary)
+        if self.flags == 63:
+            self.stop_test(True)
+
+    def ami_connect(self, ami):
+        ami.registerEvent("MessageWaiting", self.handle_message_waiting)
+
+        mailboxes = ["1234", "5678", "9999"]
+        self.voicemailManager = VoiceMailMailboxManagement(self.ast[0])
+        for mailbox in mailboxes:
+            self.voicemailManager.create_mailbox("default", mailbox, True)
+
+        formats = ["ulaw"]
+        for mailbox in mailboxes:
+            if not self.voicemailManager.create_dummy_voicemail("default", mailbox, VoiceMailMailboxManagement.inbox_folder_name, 1, formats):
+                LOGGER.error("Failed to create voicemail for mailbox {0}".format(mailbox))
+                self.stop_test()
+
+    def run(self):
+        TestCase.run(self)
+        self.create_ami_factory()
+
+def main():
+    test = PollingTest()
+    reactor.run()
+    if not test.passed:
+        return 1
+    return 0
+
+if __name__ == "__main__":
+    sys.exit(main() or 0)
diff --git a/tests/apps/voicemail/polling/poll_multiple_mailboxes/test-config.yaml b/tests/apps/voicemail/polling/poll_multiple_mailboxes/test-config.yaml
new file mode 100644
index 0000000..cfedd6d
--- /dev/null
+++ b/tests/apps/voicemail/polling/poll_multiple_mailboxes/test-config.yaml
@@ -0,0 +1,19 @@
+testinfo:
+    summary: 'Test polling of multiple mailboxes'
+    description: |
+        This test verifies that polling multiple mailboxes works as intended. A fake
+        message is added to various mailboxes that triggers events when the polling
+        frequency timeout is reached, which will cause the test to then remove the
+        messages and end the test.
+
+properties:
+    buildoption: 'TEST_FRAMEWORK'
+    dependencies:
+        - python : 'twisted'
+        - python : 'starpy'
+        - asterisk: 'app_voicemail'
+        - asterisk: 'chan_pjsip'
+        - asterisk: 'res_pjsip'
+    tags:
+        - voicemail
+        - apps
diff --git a/tests/apps/voicemail/polling/poll_no_subs/configs/ast1/extensions.conf b/tests/apps/voicemail/polling/poll_no_subs/configs/ast1/extensions.conf
new file mode 100644
index 0000000..eed7ea0
--- /dev/null
+++ b/tests/apps/voicemail/polling/poll_no_subs/configs/ast1/extensions.conf
@@ -0,0 +1,11 @@
+[default]
+
+exten => playback,1,NoOp()
+ same => n,Answer()
+ same => n,Playback(silence/5)
+ same => n,Hangup()
+
+exten => echo,1,NoOp()
+ same => n,Answer()
+ same => n,Echo()
+ same => n,Hangup()
diff --git a/tests/apps/voicemail/polling/poll_no_subs/configs/ast1/voicemail.conf b/tests/apps/voicemail/polling/poll_no_subs/configs/ast1/voicemail.conf
new file mode 100644
index 0000000..489a7a3
--- /dev/null
+++ b/tests/apps/voicemail/polling/poll_no_subs/configs/ast1/voicemail.conf
@@ -0,0 +1,6 @@
+; Voicemail Configuration
+
+[general]
+format = ulaw
+pollmailboxes=yes
+pollfreq=2
diff --git a/tests/apps/voicemail/polling/poll_no_subs/run-test b/tests/apps/voicemail/polling/poll_no_subs/run-test
new file mode 100755
index 0000000..0d6a1c9
--- /dev/null
+++ b/tests/apps/voicemail/polling/poll_no_subs/run-test
@@ -0,0 +1,79 @@
+#!/usr/bin/env python
+
+'''
+Copyright (C) 2019, Sangoma Technologies Corporation
+Ben Ford <bford 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 asterisk.asterisk import Asterisk
+from asterisk.test_case import TestCase
+from asterisk.voicemail import VoiceMailMailboxManagement
+from twisted.internet import reactor
+
+LOGGER = logging.getLogger(__name__)
+
+class PollingTest(TestCase):
+    def __init__(self):
+        TestCase.__init__(self)
+        self.passed = False
+        self.create_asterisk()
+
+    def stop_test(self, passed=False):
+        self.passed = passed
+        # Sometimes the reactor has already stopped, we only care if the test passed
+        try:
+            reactor.stop()
+        except Exception as e:
+            LOGGER.info("Error while stopping reactor: '{0}'".format(e))
+
+    def handle_message_waiting(self, ami, event):
+        LOGGER.error("Got a MessageWaiting event we weren't expecting!")
+        self.stop_test()
+
+    def handle_hangup(self, ami, event):
+        if "Local/playback at default" in event.get("channel"):
+            self.stop_test(True)
+
+    def handle_originate_failure(self, reason):
+        LOGGER.error("Failed to originate call! Reason: {0}".format(reason))
+        self.stop_test()
+
+    def ami_connect(self, ami):
+        ami.registerEvent("MessageWaiting", self.handle_message_waiting)
+        ami.registerEvent("Hangup", self.handle_hangup)
+
+        self.voicemailManager = VoiceMailMailboxManagement(self.ast[0])
+        self.voicemailManager.create_mailbox("default", "1234", True)
+
+        formats = ["ulaw"]
+        if not self.voicemailManager.create_dummy_voicemail("default", "1234", VoiceMailMailboxManagement.inbox_folder_name, 1, formats):
+            LOGGER.error("Failed to create voicemail")
+            self.stop_test()
+
+        variable = {}
+        ami.originate(channel="Local/playback at default",
+                      context="default",
+                      exten="echo",
+                      priority="1",
+                      variable=variable
+                    ).addErrback(self.handle_originate_failure)
+
+    def run(self):
+        TestCase.run(self)
+        self.create_ami_factory()
+
+def main():
+    test = PollingTest()
+    reactor.run()
+    if not test.passed:
+        return 1
+    return 0
+
+if __name__ == "__main__":
+    sys.exit(main() or 0)
diff --git a/tests/apps/voicemail/polling/poll_no_subs/test-config.yaml b/tests/apps/voicemail/polling/poll_no_subs/test-config.yaml
new file mode 100644
index 0000000..3f8f08d
--- /dev/null
+++ b/tests/apps/voicemail/polling/poll_no_subs/test-config.yaml
@@ -0,0 +1,20 @@
+testinfo:
+    summary: 'Test polling of mailboxes with no subscriptions'
+    description: |
+        This test verifies that polling a mailbox with no subscriptions does
+        not trigger a notification that a change was made. The test will end
+        after a short playback, and if an event for mailbox polling is detected
+        during that time, the test will fail.
+
+properties:
+    buildoption: 'TEST_FRAMEWORK'
+    dependencies:
+        - python : 'twisted'
+        - python : 'starpy'
+        - asterisk: 'app_playback'
+        - asterisk: 'app_voicemail'
+        - asterisk: 'chan_pjsip'
+        - asterisk: 'res_pjsip'
+    tags:
+        - voicemail
+        - apps
diff --git a/tests/apps/voicemail/polling/poll_same_mailbox/configs/ast1/pjsip.conf b/tests/apps/voicemail/polling/poll_same_mailbox/configs/ast1/pjsip.conf
new file mode 100644
index 0000000..4c7b3e1
--- /dev/null
+++ b/tests/apps/voicemail/polling/poll_same_mailbox/configs/ast1/pjsip.conf
@@ -0,0 +1,49 @@
+[dummy-transport]
+type=transport
+bind=0.0.0.0
+protocol=udp
+
+[aor](!)
+type=aor
+max_contacts=1
+
+[auth](!)
+type=auth
+auth_type=userpass
+
+[endpoint](!)
+type=endpoint
+allow=!all,ulaw
+transport=dummy-transport
+context=default
+mailboxes=1234 at default
+
+[1234](aor)
+
+[1234](auth)
+username=1234
+password=1234
+
+[1234](endpoint)
+auth=1234
+aors=1234
+
+[5678](aor)
+
+[5678](auth)
+username=5678
+password=5678
+
+[5678](endpoint)
+auth=5678
+aors=5678
+
+[9999](aor)
+
+[9999](auth)
+username=9999
+password=9999
+
+[9999](endpoint)
+auth=9999
+aors=9999
diff --git a/tests/apps/voicemail/polling/poll_same_mailbox/configs/ast1/voicemail.conf b/tests/apps/voicemail/polling/poll_same_mailbox/configs/ast1/voicemail.conf
new file mode 100644
index 0000000..489a7a3
--- /dev/null
+++ b/tests/apps/voicemail/polling/poll_same_mailbox/configs/ast1/voicemail.conf
@@ -0,0 +1,6 @@
+; Voicemail Configuration
+
+[general]
+format = ulaw
+pollmailboxes=yes
+pollfreq=2
diff --git a/tests/apps/voicemail/polling/poll_same_mailbox/run-test b/tests/apps/voicemail/polling/poll_same_mailbox/run-test
new file mode 100755
index 0000000..78d0773
--- /dev/null
+++ b/tests/apps/voicemail/polling/poll_same_mailbox/run-test
@@ -0,0 +1,87 @@
+#!/usr/bin/env python
+
+'''
+Copyright (C) 2019, Sangoma Technologies Corporation
+Ben Ford <bford 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 asterisk.asterisk import Asterisk
+from asterisk.test_case import TestCase
+from asterisk.voicemail import VoiceMailMailboxManagement
+from twisted.internet import reactor
+
+LOGGER = logging.getLogger(__name__)
+
+class PollingTest(TestCase):
+    def __init__(self):
+        TestCase.__init__(self)
+        self.passed = False
+        self.counter = 0
+        self.create_asterisk()
+
+    def stop_test(self, passed=False):
+        self.passed = passed
+        reactor.stop()
+
+    def handle_message_waiting(self, ami, event):
+
+        if event.get("mailbox") != "1234 at default":
+            return
+
+        old = int(event.get("old"))
+        new = int(event.get("new"))
+        waiting = int(event.get("waiting"))
+
+        # Conditions:
+        # - If INBOX count is 1, increment self.counter
+        # - When self.counter reaches 3, we got all 3 events we expected,
+        #   so delete the voicemail
+        # - INBOX count should only be 0 after we delete the voicemail, so
+        #   increment the counter here to end the test
+        # - Once self.counter reaches 3 after removing the voicemail, we've
+        #   received all the events, so stop the test
+        if new == 1 and waiting == 1:
+            if old != 0:
+                LOGGER.error("Expected 'Old' to be 0")
+                self.stop_test()
+            self.counter += 1
+            if self.counter == 3:
+                self.counter = 0
+                if not self.voicemailManager.remove_mailbox("default", "1234"):
+                    LOGGER.error("Failed to remove voicemail")
+                    self.stop_test()
+        elif new == 0 and waiting == 0 and old == 0:
+            self.counter += 1
+            if self.counter == 3:
+                self.stop_test(True)
+
+    def ami_connect(self, ami):
+        ami.registerEvent("MessageWaiting", self.handle_message_waiting)
+
+        self.voicemailManager = VoiceMailMailboxManagement(self.ast[0])
+        self.voicemailManager.create_mailbox("default", "1234", True)
+
+        formats = ["ulaw"]
+        if not self.voicemailManager.create_dummy_voicemail("default", "1234", VoiceMailMailboxManagement.inbox_folder_name, 1, formats):
+            LOGGER.error("Failed to create voicemail")
+            self.stop_test()
+
+    def run(self):
+        TestCase.run(self)
+        self.create_ami_factory()
+
+def main():
+    test = PollingTest()
+    reactor.run()
+    if not test.passed:
+        return 1
+    return 0
+
+if __name__ == "__main__":
+    sys.exit(main() or 0)
diff --git a/tests/apps/voicemail/polling/poll_same_mailbox/test-config.yaml b/tests/apps/voicemail/polling/poll_same_mailbox/test-config.yaml
new file mode 100644
index 0000000..fedb1db
--- /dev/null
+++ b/tests/apps/voicemail/polling/poll_same_mailbox/test-config.yaml
@@ -0,0 +1,18 @@
+testinfo:
+    summary: 'Test polling of a single mailbox with multiple subscriptions'
+    description: |
+        This test verifies that polling a single mailbox with multiple subscriptions
+        works as intended. The test will wait for the given number of notifications
+        to occur, then remove the voicemail and end the test.
+
+properties:
+    buildoption: 'TEST_FRAMEWORK'
+    dependencies:
+        - python : 'twisted'
+        - python : 'starpy'
+        - asterisk: 'app_voicemail'
+        - asterisk: 'chan_pjsip'
+        - asterisk: 'res_pjsip'
+    tags:
+        - voicemail
+        - apps
diff --git a/tests/apps/voicemail/polling/poll_single_mailbox/configs/ast1/pjsip.conf b/tests/apps/voicemail/polling/poll_single_mailbox/configs/ast1/pjsip.conf
new file mode 100644
index 0000000..dce5f75
--- /dev/null
+++ b/tests/apps/voicemail/polling/poll_single_mailbox/configs/ast1/pjsip.conf
@@ -0,0 +1,23 @@
+[dummy-transport]
+type=transport
+bind=0.0.0.0
+protocol=udp
+
+[1234]
+type=aor
+max_contacts=1
+
+[1234]
+type=auth
+auth_type=userpass
+username=1234
+password=1234
+
+[1234]
+type=endpoint
+allow=!all,ulaw
+auth=1234
+aors=1234
+transport=dummy-transport
+context=default
+mailboxes=1234 at default
diff --git a/tests/apps/voicemail/polling/poll_single_mailbox/configs/ast1/voicemail.conf b/tests/apps/voicemail/polling/poll_single_mailbox/configs/ast1/voicemail.conf
new file mode 100644
index 0000000..489a7a3
--- /dev/null
+++ b/tests/apps/voicemail/polling/poll_single_mailbox/configs/ast1/voicemail.conf
@@ -0,0 +1,6 @@
+; Voicemail Configuration
+
+[general]
+format = ulaw
+pollmailboxes=yes
+pollfreq=2
diff --git a/tests/apps/voicemail/polling/poll_single_mailbox/run-test b/tests/apps/voicemail/polling/poll_single_mailbox/run-test
new file mode 100755
index 0000000..df85667
--- /dev/null
+++ b/tests/apps/voicemail/polling/poll_single_mailbox/run-test
@@ -0,0 +1,81 @@
+#!/usr/bin/env python
+
+'''
+Copyright (C) 2019, Sangoma Technologies Corporation
+Ben Ford <bford 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 asterisk.asterisk import Asterisk
+from asterisk.test_case import TestCase
+from asterisk.voicemail import VoiceMailMailboxManagement
+from twisted.internet import reactor
+
+LOGGER = logging.getLogger(__name__)
+
+class PollingTest(TestCase):
+    def __init__(self):
+        TestCase.__init__(self)
+        self.passed = False
+        self.test_phase = 1
+        self.create_asterisk()
+
+    def stop_test(self, passed=False):
+        self.passed = passed
+        reactor.stop()
+
+    def handle_message_waiting(self, ami, event):
+
+        if event.get("mailbox") != "1234 at default":
+            return
+
+        old = int(event.get("old"))
+        new = int(event.get("new"))
+        waiting = int(event.get("waiting"))
+
+        # Phase 1: We are looking for an INBOX count of 1
+        if self.test_phase == 1:
+            if new != 1 or waiting != 1:
+                return
+            if old != 0:
+                self.stop_test()
+            self.test_phase = 2
+            if not self.voicemailManager.remove_mailbox("default", "1234"):
+                LOGGER.error("Failed to remove voicemail")
+                self.stop_test()
+        # Phase 2: We don't expect there to be any messages
+        elif self.test_phase == 2:
+            if new != 0 or old != 0 or waiting != 0:
+                LOGGER.error("Expected empty mailbox")
+                self.stop_test()
+            self.stop_test(True)
+
+    def ami_connect(self, ami):
+        ami.registerEvent("MessageWaiting", self.handle_message_waiting)
+
+        self.voicemailManager = VoiceMailMailboxManagement(self.ast[0])
+        self.voicemailManager.create_mailbox("default", "1234", True)
+
+        formats = ["ulaw"]
+        if not self.voicemailManager.create_dummy_voicemail("default", "1234", VoiceMailMailboxManagement.inbox_folder_name, 1, formats):
+            LOGGER.error("Failed to create voicemail")
+            self.stop_test()
+
+    def run(self):
+        TestCase.run(self)
+        self.create_ami_factory()
+
+def main():
+    test = PollingTest()
+    reactor.run()
+    if not test.passed:
+        return 1
+    return 0
+
+if __name__ == "__main__":
+    sys.exit(main() or 0)
diff --git a/tests/apps/voicemail/polling/poll_single_mailbox/test-config.yaml b/tests/apps/voicemail/polling/poll_single_mailbox/test-config.yaml
new file mode 100644
index 0000000..cd60cb0
--- /dev/null
+++ b/tests/apps/voicemail/polling/poll_single_mailbox/test-config.yaml
@@ -0,0 +1,19 @@
+testinfo:
+    summary: 'Test polling of a single mailbox'
+    description: |
+        This test verifies that polling a single mailbox works as intended. A fake
+        message is added to the mailbox that triggers an event when the polling
+        frequency timeout is reached, which will cause the test to then remove the
+        message to end the test.
+
+properties:
+    buildoption: 'TEST_FRAMEWORK'
+    dependencies:
+        - python : 'twisted'
+        - python : 'starpy'
+        - asterisk: 'app_voicemail'
+        - asterisk: 'chan_pjsip'
+        - asterisk: 'res_pjsip'
+    tags:
+        - voicemail
+        - apps
diff --git a/tests/apps/voicemail/polling/poll_sub_unsub/configs/ast1/extensions.conf b/tests/apps/voicemail/polling/poll_sub_unsub/configs/ast1/extensions.conf
new file mode 100644
index 0000000..eed7ea0
--- /dev/null
+++ b/tests/apps/voicemail/polling/poll_sub_unsub/configs/ast1/extensions.conf
@@ -0,0 +1,11 @@
+[default]
+
+exten => playback,1,NoOp()
+ same => n,Answer()
+ same => n,Playback(silence/5)
+ same => n,Hangup()
+
+exten => echo,1,NoOp()
+ same => n,Answer()
+ same => n,Echo()
+ same => n,Hangup()
diff --git a/tests/apps/voicemail/polling/poll_sub_unsub/configs/ast1/pjsip.conf b/tests/apps/voicemail/polling/poll_sub_unsub/configs/ast1/pjsip.conf
new file mode 100644
index 0000000..091bdab
--- /dev/null
+++ b/tests/apps/voicemail/polling/poll_sub_unsub/configs/ast1/pjsip.conf
@@ -0,0 +1,16 @@
+[dummy-transport]
+type=transport
+bind=0.0.0.0
+protocol=udp
+
+[1234]
+type=aor
+max_contacts=1
+
+[1234]
+type=endpoint
+allow=!all,ulaw
+aors=1234
+transport=dummy-transport
+context=default
+mailboxes=1234 at default
diff --git a/tests/apps/voicemail/polling/poll_sub_unsub/configs/ast1/voicemail.conf b/tests/apps/voicemail/polling/poll_sub_unsub/configs/ast1/voicemail.conf
new file mode 100644
index 0000000..489a7a3
--- /dev/null
+++ b/tests/apps/voicemail/polling/poll_sub_unsub/configs/ast1/voicemail.conf
@@ -0,0 +1,6 @@
+; Voicemail Configuration
+
+[general]
+format = ulaw
+pollmailboxes=yes
+pollfreq=2
diff --git a/tests/apps/voicemail/polling/poll_sub_unsub/run-test b/tests/apps/voicemail/polling/poll_sub_unsub/run-test
new file mode 100755
index 0000000..c77827e
--- /dev/null
+++ b/tests/apps/voicemail/polling/poll_sub_unsub/run-test
@@ -0,0 +1,123 @@
+#!/usr/bin/env python
+
+'''
+Copyright (C) 2019, Sangoma Technologies Corporation
+Ben Ford <bford 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 asterisk.asterisk import Asterisk
+from asterisk.test_case import TestCase
+from asterisk.voicemail import VoiceMailMailboxManagement
+from asterisk.sipp import SIPpScenario
+from twisted.internet import reactor
+
+LOGGER = logging.getLogger(__name__)
+
+class PollingTest(TestCase):
+    def __init__(self):
+        TestCase.__init__(self)
+        self.passed = False
+        self.test_phase = 1
+        self._registered_done = False
+        self._unregistered_done = False
+        self.sipp_register = {'scenario': 'register.xml',
+                              '-i': '127.0.0.2', '-p': '5061',
+                              '-s': '1234'}
+        self.sipp_unregister = {'scenario': 'unregister.xml',
+                                '-i': '127.0.0.2', '-p': '5061',
+                                '-s': '1234'}
+        self.create_asterisk()
+
+    def stop_test(self, passed=False):
+        self.passed = passed
+        # Sometimes the reactor has already stopped, we only care if the test passed
+        try:
+            reactor.stop()
+        except Exception as e:
+            LOGGER.info("Error while stopping reactor: '{0}'".format(e))
+
+    def handle_message_waiting(self, ami, event):
+
+        if event.get("mailbox") != "1234 at default":
+            return
+
+        old = int(event.get("old"))
+        new = int(event.get("new"))
+        waiting = int(event.get("waiting"))
+
+        # Phase 1: We should get a MessageWaiting notification after registering
+        if self.test_phase == 1:
+            if new != 1 or waiting != 1 or old != 0:
+                LOGGER.error("Mailbox count was different than expected")
+                self.stop_test()
+            sipp = SIPpScenario(self.test_name, self.sipp_unregister)
+            sipp.run(self)
+            self.test_phase = 2
+        # Phase 2: There should not be a notification after unregistering
+        elif self.test_phase == 2:
+            LOGGER.error("Got a MessageWaiting event in a test phase we shouldn't have")
+            self.stop_test()
+
+    def handle_originate_failure(self, reason):
+        LOGGER.error("Failed to originate call! Reason: {0}".format(reason))
+        self.stop_test()
+
+    def handle_hangup(self, ami, event):
+        if "Local/playback at default" in event.get("channel"):
+            self.stop_test(True)
+
+    def handle_contact_status(self, ami, event):
+
+        if event.get("aor") != "1234":
+            return
+
+        status = event.get("contactstatus")
+
+        if status == "Created":
+            self.voicemailManager = VoiceMailMailboxManagement(self.ast[0])
+            self.voicemailManager.create_mailbox("default", "1234", True)
+            formats = ["ulaw"]
+            if not self.voicemailManager.create_dummy_voicemail("default", "1234", VoiceMailMailboxManagement.inbox_folder_name, 1, formats):
+                LOGGER.error("Failed to create voicemail")
+                self.stop_test()
+        elif status == "Removed":
+            # Remove the voicemail from the mailbox
+            self.voicemailManager.remove_mailbox("default", "1234")
+            
+            # Originate a channel that will end the test on hangup
+            variable = {}
+            ami.originate(channel="Local/playback at default",
+                          context="default",
+                          exten="echo",
+                          priority="1",
+                          variable=variable
+                         ).addErrback(self.handle_originate_failure)
+
+    def ami_connect(self, ami):
+
+        ami.registerEvent("ContactStatus", self.handle_contact_status)
+        ami.registerEvent("MessageWaiting", self.handle_message_waiting)
+        ami.registerEvent("Hangup", self.handle_hangup)
+
+        sipp = SIPpScenario(self.test_name, self.sipp_register)
+        sipp.run(self)
+
+    def run(self):
+        TestCase.run(self)
+        self.create_ami_factory()
+
+def main():
+    test = PollingTest()
+    reactor.run()
+    if not test.passed:
+        return 1
+    return 0
+
+if __name__ == "__main__":
+    sys.exit(main() or 0)
diff --git a/tests/apps/voicemail/polling/poll_sub_unsub/sipp/register.xml b/tests/apps/voicemail/polling/poll_sub_unsub/sipp/register.xml
new file mode 100644
index 0000000..5918b96
--- /dev/null
+++ b/tests/apps/voicemail/polling/poll_sub_unsub/sipp/register.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<!DOCTYPE scenario SYSTEM "sipp.dtd">
+
+<scenario name="Basic Sipstone UAC">
+    <send retrans="500">
+        <![CDATA[
+
+        REGISTER sip:[remote_ip]:[remote_port] SIP/2.0
+        Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
+        From: "[service]" <sip:[service]@[local_ip]:[local_port]>;tag=[pid]SIPpTag00[call_number]
+        To: "[service]" <sip:[service]@[remote_ip]:[remote_port]>
+        Call-ID: [call_id]
+        CSeq: 1 REGISTER
+        Max-Forwards: 70
+        Contact: <sip:[service]@[local_ip]:[local_port]>;transport=[transport]
+        Subject: Performance Test
+        Content-Length: 0
+
+        ]]>
+    </send>
+
+    <recv response="200" rtd="true" timeout="5000"/>
+
+</scenario>
diff --git a/tests/apps/voicemail/polling/poll_sub_unsub/sipp/unregister.xml b/tests/apps/voicemail/polling/poll_sub_unsub/sipp/unregister.xml
new file mode 100644
index 0000000..cc340ad
--- /dev/null
+++ b/tests/apps/voicemail/polling/poll_sub_unsub/sipp/unregister.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<!DOCTYPE scenario SYSTEM "sipp.dtd">
+
+<scenario name="Basic Sipstone UAC">
+    <send retrans="500">
+        <![CDATA[
+
+        REGISTER sip:[remote_ip]:[remote_port] SIP/2.0
+        Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
+        From: "[service]" <sip:[service]@[local_ip]:[local_port]>;tag=[pid]SIPpTag00[call_number]
+        To: "[service]" <sip:[service]@[remote_ip]:[remote_port]>
+        Call-ID: [call_id]
+        CSeq: 1 REGISTER
+	Max-Forwards: 70
+	Expires: 0
+        Contact: <sip:[service]@[local_ip]:[local_port]>;transport=[transport]
+        Subject: Performance Test
+        Content-Length: 0
+
+        ]]>
+    </send>
+
+    <recv response="200" rtd="true"/>
+
+</scenario>
diff --git a/tests/apps/voicemail/polling/poll_sub_unsub/test-config.yaml b/tests/apps/voicemail/polling/poll_sub_unsub/test-config.yaml
new file mode 100644
index 0000000..c2c8982
--- /dev/null
+++ b/tests/apps/voicemail/polling/poll_sub_unsub/test-config.yaml
@@ -0,0 +1,20 @@
+testinfo:
+    summary: 'Test polling after subscribing then unsubscribing'
+    description: |
+        This test verifies that a notification is received after subscribing to
+        a mailbox, and that no notification is received after unsubscribing from
+        the mailbox.
+
+properties:
+    buildoption: 'TEST_FRAMEWORK'
+    dependencies:
+        - python : 'twisted'
+        - python : 'starpy'
+        - asterisk: 'app_echo'
+        - asterisk: 'app_playback'
+        - asterisk: 'app_voicemail'
+        - asterisk: 'chan_pjsip'
+        - asterisk: 'res_pjsip'
+    tags:
+        - voicemail
+        - apps
diff --git a/tests/apps/voicemail/polling/tests.yaml b/tests/apps/voicemail/polling/tests.yaml
new file mode 100644
index 0000000..171a45f
--- /dev/null
+++ b/tests/apps/voicemail/polling/tests.yaml
@@ -0,0 +1,7 @@
+# Enter tests here in the order they should be considered for execution:
+tests:
+    - test: 'poll_multiple_mailboxes'
+    - test: 'poll_no_subs'
+    - test: 'poll_same_mailbox'
+    - test: 'poll_single_mailbox'
+    - test: 'poll_sub_unsub'
diff --git a/tests/apps/voicemail/tests.yaml b/tests/apps/voicemail/tests.yaml
index 0ec6c61..735dbb7 100644
--- a/tests/apps/voicemail/tests.yaml
+++ b/tests/apps/voicemail/tests.yaml
@@ -30,3 +30,4 @@
     - test: 'leave_voicemail_nominal'
     - test: 'leave_voicemail_priority'
     - test: 'play_message'
+    - dir: 'polling'

-- 
To view, visit https://gerrit.asterisk.org/c/testsuite/+/11284
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: testsuite
Gerrit-Branch: 13
Gerrit-Change-Id: Id3a40527624c094e253d56ee2df5381b03b22169
Gerrit-Change-Number: 11284
Gerrit-PatchSet: 2
Gerrit-Owner: Benjamin Keith Ford <bford at digium.com>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20190418/4b3be0c8/attachment-0001.html>


More information about the asterisk-code-review mailing list