# vim: tabstop=4 shiftwidth=4 softtabstop=4

import os
import mock
import httplib2

from drstack import shell as drstack_shell
from novaclient import exceptions
from tests import utils


class ShellTest(utils.TestCase):

    # Patch os.environ to avoid required auth info.
    def setUp(self):
        global _old_env
        fake_env = {
            'OS_AUTH_URL': 'http://127.0.0.1:5000/v2.0',
            'OS_TENANT_NAME': 'tenant_name',
            'OS_USERNAME': 'username',
            'OS_PASSWORD': 'password',
        }
        _old_env, os.environ = os.environ, fake_env.copy()

        # Make a fake shell object, a helping wrapper to call it, and a quick
        # way of asserting that certain API calls were made.
        global shell, _shell, assert_called, assert_called_anytime
        _shell = drstack_shell
        shell = lambda cmd: _shell.main([__file__] + cmd.split())

    def tearDown(self):
        global _old_env
        os.environ = _old_env

    def test_help_unknown_command(self):
        self.assertRaises(exceptions.CommandError, shell, 'foo foo')

    def test_debug(self):
        httplib2.debuglevel = 0
        shell('--debug help')
        assert httplib2.debuglevel == 1

@@ -180,16 +180,342 @@
 utils%0A%0A%0A
+DEFAULT_AUTH_URL = 'http://127.0.0.1:5000/v2.0/'%0ADEFAULT_TENANT_NAME = 'tenant_name'%0ADEFAULT_USERNAME = 'username'%0ADEFAULT_PASSWORD = 'password'%0A%0AAUTH_ARGS = %7B%0A    'auth_url': DEFAULT_AUTH_URL,%0A    'auth_token': '',%0A    'tenant_name': DEFAULT_TENANT_NAME,%0A    'username': DEFAULT_USERNAME,%0A    'password': DEFAULT_PASSWORD%7D%0A%0A%0A
 class Sh
@@ -689,36 +689,24 @@
 L': 
-'http://127.0.0.1:5000/v2.0'
+DEFAULT_AUTH_URL
 ,%0A  
@@ -733,29 +733,35 @@
 _NAME': 
-'tenant_name'
+DEFAULT_TENANT_NAME
 ,%0A      
@@ -781,26 +781,32 @@
 RNAME': 
-'username'
+DEFAULT_USERNAME
 ,%0A      
@@ -826,26 +826,32 @@
 SWORD': 
-'password'
+DEFAULT_PASSWORD
 ,%0A      
@@ -1218,16 +1218,291 @@
 lit())%0A%0A
+        # Patch out some common methods%0A        self.kc_patch = mock.patch('drstack.shell.DrStack._get_keystone')%0A        self.kc_save = self.kc_patch.start()%0A        self.nc_patch = mock.patch('drstack.shell.DrStack._get_nova')%0A        self.nc_save = self.nc_patch.start()%0A%0A
     def 
@@ -1570,16 +1570,74 @@
 _old_env
+%0A        self.kc_patch.stop()%0A        self.nc_patch.stop()
 %0A%0A    de
@@ -1869,8 +1869,1278 @@
 el == 1%0A
+%0A    def test_shell_args(self):%0A%0A        def test_args(desc, default_args):%0A            onecmd_mock = mock.Mock(desc)%0A            with mock.patch('drstack.shell.DrStack.onecmd', onecmd_mock):%0A                shell('list user')%0A                onecmd_mock.assert_called_with(('list user'))%0A                auth_mock = mock.Mock(desc)%0A                with mock.patch('drstack.shell.DrStack.set_auth', auth_mock):%0A                    shell('list user')%0A                    assert auth_mock.call_args == ((), default_args)%0A                    shell('--auth_url http://0.0.0.0:5000/ --tenant_name fred '%0A                          '--username barney --password xyzpdq '%0A                          'list user')%0A                    assert auth_mock.call_args == ((), %7B%0A                        'auth_url': 'http://0.0.0.0:5000/',%0A                        'auth_token': '',%0A                        'tenant_name': 'fred',%0A                        'username': 'barney',%0A                        'password': 'xyzpdq'%7D)%0A%0A        test_args('default environment', AUTH_ARGS)%0A        save_env, os.environ = os.environ, %7B%7D%0A        test_args('no environment', %7B'auth_url': '', 'auth_token': '',%0A                'tenant_name': '', 'username': '', 'password': ''%7D)%0A        os.environ = save_env%0A

Add some actual shell tests