summaryrefslogtreecommitdiff
path: root/tests/unit/test_shell.py
diff options
context:
space:
mode:
authorDaniel Wakefield <daniel.wakefield@hp.com>2014-10-20 18:00:01 +0100
committerAlistair Coles <alistair.coles@hp.com>2014-10-21 08:23:26 +0000
commit025da14326d29051119fff50424748e41bf4be42 (patch)
tree8de2977739d6009ba1597396ae0c22fd8ab661e2 /tests/unit/test_shell.py
parent43c1141baa6c5a5987fd4ab71d1c2299898ea251 (diff)
downloadpython-swiftclient-025da14326d29051119fff50424748e41bf4be42.tar.gz
Replaces Stacktraces with useful error messages.
Changed the message shown when a user doesn't enter project/tenant id/name to be more informative. When attempting to stat a container without supplying project/tenant name or id, an empty response was being returned instead of an error being raised. Changed the error raised in swiftclient.client when no tenant or project is specified to be more specific. Add tests for basic regression checking. Closes-Bug: #1372589 Change-Id: I4eb6964d9f1702db119cc0294edc02841b1ecd5f
Diffstat (limited to 'tests/unit/test_shell.py')
-rw-r--r--tests/unit/test_shell.py73
1 files changed, 69 insertions, 4 deletions
diff --git a/tests/unit/test_shell.py b/tests/unit/test_shell.py
index 8e61cb4..29d7472 100644
--- a/tests/unit/test_shell.py
+++ b/tests/unit/test_shell.py
@@ -24,6 +24,7 @@ import swiftclient
from swiftclient.service import SwiftError
import swiftclient.shell
import swiftclient.utils
+from swiftclient.multithreading import OutputManager
from os.path import basename, dirname
from tests.unit.test_swiftclient import MockHttpTest
@@ -551,13 +552,13 @@ class TestParsing(unittest.TestCase):
# check the expected opts are set
for key, v in opts.items():
actual = getattr(actual_opts, key)
- self.assertEqual(v, actual, 'Expected %s for key %s, found %s'
- % (v, key, actual))
+ self.assertEqual(v, actual, 'Expected %s for key %s, found %s' %
+ (v, key, actual))
for key, v in os_opts.items():
actual = getattr(actual_opts, "os_" + key)
- self.assertEqual(v, actual, 'Expected %s for key %s, found %s'
- % (v, key, actual))
+ self.assertEqual(v, actual, 'Expected %s for key %s, found %s' %
+ (v, key, actual))
# check the os_options dict values are set
self.assertTrue(hasattr(actual_opts, 'os_options'))
@@ -725,6 +726,55 @@ class TestParsing(unittest.TestCase):
args = _make_args("stat", opts, os_opts)
self.assertRaises(SystemExit, swiftclient.shell.main, args)
+ def test_no_tenant_name_or_id_v2(self):
+ os_opts = {"password": "secret",
+ "username": "user",
+ "auth_url": "http://example.com:5000/v3",
+ "tenant_name": "",
+ "tenant_id": ""}
+
+ out = six.StringIO()
+ err = six.StringIO()
+ mock_output = _make_output_manager(out, err)
+ with mock.patch('swiftclient.shell.OutputManager', mock_output):
+ args = _make_args("stat", {}, os_opts)
+ self.assertRaises(SystemExit, swiftclient.shell.main, args)
+ self.assertEqual(err.getvalue().strip(), 'No tenant specified')
+
+ out = six.StringIO()
+ err = six.StringIO()
+ mock_output = _make_output_manager(out, err)
+ with mock.patch('swiftclient.shell.OutputManager', mock_output):
+ args = _make_args("stat", {}, os_opts, cmd_args=["testcontainer"])
+ self.assertRaises(SystemExit, swiftclient.shell.main, args)
+ self.assertEqual(err.getvalue().strip(), 'No tenant specified')
+
+ def test_no_tenant_name_or_id_v3(self):
+ os_opts = {"password": "secret",
+ "username": "user",
+ "auth_url": "http://example.com:5000/v3",
+ "tenant_name": "",
+ "tenant_id": ""}
+
+ out = six.StringIO()
+ err = six.StringIO()
+ mock_output = _make_output_manager(out, err)
+ with mock.patch('swiftclient.shell.OutputManager', mock_output):
+ args = _make_args("stat", {"auth_version": "3"}, os_opts)
+ self.assertRaises(SystemExit, swiftclient.shell.main, args)
+ self.assertEqual(err.getvalue().strip(),
+ 'No project name or project id specified.')
+
+ out = six.StringIO()
+ err = six.StringIO()
+ mock_output = _make_output_manager(out, err)
+ with mock.patch('swiftclient.shell.OutputManager', mock_output):
+ args = _make_args("stat", {"auth_version": "3"},
+ os_opts, cmd_args=["testcontainer"])
+ self.assertRaises(SystemExit, swiftclient.shell.main, args)
+ self.assertEqual(err.getvalue().strip(),
+ 'No project name or project id specified.')
+
def test_insufficient_env_vars_v3(self):
args = _make_args("stat", {}, {})
opts = {"auth_version": "3"}
@@ -1043,3 +1093,18 @@ class TestKeystoneOptions(MockHttpTest):
opts = {'auth-version': '2.0'}
self._test_options(opts, os_opts)
+
+
+def _make_output_manager(stdout, stderr):
+ class MockOutputManager(OutputManager):
+ # This class is used to mock OutputManager so that we can
+ # override stdout and stderr. Mocking sys.stdout & sys.stdout
+ # doesn't work because they are argument defaults in the
+ # OutputManager constructor and those defaults are pinned to
+ # the value of sys.stdout/stderr before we get chance to mock them.
+ def __init__(self, print_stream=None, error_stream=None):
+ super(MockOutputManager, self).__init__()
+ self.print_stream = stdout
+ self.error_stream = stderr
+
+ return MockOutputManager