#!/usr/bin/env python
'''
Copyright (C) 2010, Digium, Inc.
Russell Bryant <russell@digium.com>

This program is free software, distributed under the terms of
the GNU General Public License Version 2.
'''

import sys
from twisted.internet import reactor
import logging
import logging.config

sys.path.append("lib/python")
from asterisk.asterisk import Asterisk
from asterisk.test_case import TestCase
LOGGER = logging.getLogger(__name__)

TEST_CONFIG={
  'ast-config-options': {
    'live_dangerously': 'no'
  }
}

class AMILoginTest(TestCase):
    def __init__(self):
        TestCase.__init__(self, test_config=TEST_CONFIG)
        self.create_asterisk()

    def on_get_var(self, actual):
        expected = ""
        if expected == actual:
            self.passed = True
        else:
            LOGGER.error(
                "Dangerous execution failed. Expected: '%s' Actual: '%s'" %
                (expected, actual))
            self.passed = False
        self.stop_reactor()

    def on_failure(self, reason):
        LOGGER.error("Failed to execute GetVar")
        self.passed = False
        self.stop_reactor()

    def ami_connect(self, ami):
        d = self.ami[0].getVar(None, "SHELL(echo -n hi)")
        d.addCallbacks(self.on_get_var, self.on_failure)

    def run(self):
        TestCase.run(self)
        self.create_ami_factory()

def main():
    test = AMILoginTest()
    test.start_asterisk()
    reactor.run()
    test.stop_asterisk()
    if test.passed:
        return 0
    return 1


if __name__ == "__main__":
    sys.exit(main() or 0)


# vim:sw=4:ts=4:expandtab:textwidth=79
