summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xswiftclient/shell.py32
-rw-r--r--test/functional/test_swiftclient.py14
-rw-r--r--test/unit/test_shell.py65
3 files changed, 63 insertions, 48 deletions
diff --git a/swiftclient/shell.py b/swiftclient/shell.py
index 1878272..6da9d66 100755
--- a/swiftclient/shell.py
+++ b/swiftclient/shell.py
@@ -531,8 +531,7 @@ Optional arguments:
def st_list(parser, args, output_manager, return_parser=False):
- def _print_stats(options, stats, human):
- total_count = total_bytes = 0
+ def _print_stats(options, stats, human, totals):
container = stats.get("container", None)
for item in stats["listing"]:
item_name = item.get('name')
@@ -543,7 +542,7 @@ def st_list(parser, args, output_manager, return_parser=False):
item_bytes = item.get('bytes')
byte_str = prt_bytes(item_bytes, human)
count = item.get('count')
- total_count += count
+ totals['count'] += count
try:
meta = item.get('meta')
utc = gmtime(float(meta.get('x-timestamp')))
@@ -579,17 +578,7 @@ def st_list(parser, args, output_manager, return_parser=False):
output_manager.print_msg(
"%s %10s %8s %24s %s",
byte_str, date, xtime, content_type, item_name)
- total_bytes += item_bytes
-
- # report totals
- if options['long'] or human:
- if not container:
- output_manager.print_msg(
- "%12s %s", prt_bytes(total_count, True),
- prt_bytes(total_bytes, human))
- else:
- output_manager.print_msg(
- prt_bytes(total_bytes, human))
+ totals['bytes'] += item_bytes
parser.add_argument(
'-l', '--long', dest='long', action='store_true', default=False,
@@ -643,6 +632,7 @@ def st_list(parser, args, output_manager, return_parser=False):
try:
if not args:
stats_parts_gen = swift.list()
+ container = None
else:
container = args[0]
args = args[1:]
@@ -668,12 +658,24 @@ def st_list(parser, args, output_manager, return_parser=False):
sort_keys=True, indent=2)
output_manager.print_msg('')
return
+
+ totals = {'count': 0, 'bytes': 0}
for stats in stats_parts_gen:
if stats["success"]:
- _print_stats(options, stats, human)
+ _print_stats(options, stats, human, totals)
else:
raise stats["error"]
+ # report totals
+ if options['long'] or human:
+ if container is None:
+ output_manager.print_msg(
+ "%12s %s", prt_bytes(totals['count'], True),
+ prt_bytes(totals['bytes'], human))
+ else:
+ output_manager.print_msg(
+ prt_bytes(totals['bytes'], human))
+
except SwiftError as e:
output_manager.error(e.value)
diff --git a/test/functional/test_swiftclient.py b/test/functional/test_swiftclient.py
index aaade87..8d001d0 100644
--- a/test/functional/test_swiftclient.py
+++ b/test/functional/test_swiftclient.py
@@ -207,7 +207,7 @@ class TestFunctional(unittest.TestCase):
hdrs = self.conn.head_object(self.containername, self.objectname)
self.assertEqual(str(len(self.test_data)),
hdrs.get('content-length'))
- self.assertEqual(self.etag, hdrs.get('etag'))
+ self.assertEqual(self.etag, hdrs.get('etag').strip('"'))
self.assertEqual('application/octet-stream',
hdrs.get('content-type'))
@@ -218,7 +218,7 @@ class TestFunctional(unittest.TestCase):
hdrs = self.conn.head_object(self.containername, self.objectname)
self.assertEqual(str(len(self.test_data)),
hdrs.get('content-length'))
- self.assertEqual(self.etag, hdrs.get('etag'))
+ self.assertEqual(self.etag, hdrs.get('etag').strip('"'))
self.assertEqual('text/plain',
hdrs.get('content-type'))
@@ -229,7 +229,7 @@ class TestFunctional(unittest.TestCase):
hdrs = self.conn.head_object(self.containername, self.objectname)
self.assertEqual(str(len(self.test_data)),
hdrs.get('content-length'))
- self.assertEqual(self.etag, hdrs.get('etag'))
+ self.assertEqual(self.etag, hdrs.get('etag').strip('"'))
self.assertEqual('text/plain',
hdrs.get('content-type'))
@@ -241,7 +241,7 @@ class TestFunctional(unittest.TestCase):
hdrs = self.conn.head_object(self.containername, self.objectname)
self.assertEqual(str(len(self.test_data)),
hdrs.get('content-length'))
- self.assertEqual(self.etag, hdrs.get('etag'))
+ self.assertEqual(self.etag, hdrs.get('etag').strip('"'))
self.assertEqual('image/jpeg',
hdrs.get('content-type'))
@@ -252,7 +252,7 @@ class TestFunctional(unittest.TestCase):
hdrs = self.conn.head_object(self.containername, self.objectname)
self.assertEqual(str(len(self.test_data)),
hdrs.get('content-length'))
- self.assertEqual(self.etag, hdrs.get('etag'))
+ self.assertEqual(self.etag, hdrs.get('etag').strip('"'))
self.assertEqual('application/octet-stream', hdrs.get('content-type'))
# Content from File-like object
@@ -262,7 +262,7 @@ class TestFunctional(unittest.TestCase):
hdrs = self.conn.head_object(self.containername, self.objectname)
self.assertEqual(str(len(self.test_data)),
hdrs.get('content-length'))
- self.assertEqual(self.etag, hdrs.get('etag'))
+ self.assertEqual(self.etag, hdrs.get('etag').strip('"'))
self.assertEqual('application/octet-stream', hdrs.get('content-type'))
# Content from File-like object, but read in chunks
@@ -274,7 +274,7 @@ class TestFunctional(unittest.TestCase):
hdrs = self.conn.head_object(self.containername, self.objectname)
self.assertEqual(str(len(self.test_data)),
hdrs.get('content-length'))
- self.assertEqual(self.etag, hdrs.get('etag'))
+ self.assertEqual(self.etag, hdrs.get('etag').strip('"'))
self.assertEqual('application/octet-stream', hdrs.get('content-type'))
# Wrong etag arg, should raise an exception
diff --git a/test/unit/test_shell.py b/test/unit/test_shell.py
index 0cf2258..295c918 100644
--- a/test/unit/test_shell.py
+++ b/test/unit/test_shell.py
@@ -28,7 +28,6 @@ import textwrap
from time import localtime, mktime, strftime, strptime
import six
-import sys
import swiftclient
from swiftclient.service import SwiftError
@@ -551,9 +550,12 @@ class TestShell(unittest.TestCase):
self.assertEqual(output.out, 'object_a\n')
- # Test container listing with --long
+ # Test container listing with --long and multiple pages
connection.return_value.get_container.side_effect = [
- [None, [{'name': 'object_a', 'bytes': 0,
+ [None, [{'name': 'object_a', 'bytes': 3,
+ 'content_type': 'type/content',
+ 'last_modified': '123T456'}]],
+ [None, [{'name': 'object_b', 'bytes': 5,
'content_type': 'type/content',
'last_modified': '123T456'}]],
[None, []],
@@ -571,9 +573,11 @@ class TestShell(unittest.TestCase):
connection.return_value.get_container.assert_has_calls(calls)
self.assertEqual(output.out,
- ' 0 123 456'
+ ' 3 123 456'
' type/content object_a\n'
- ' 0\n')
+ ' 5 123 456'
+ ' type/content object_b\n'
+ ' 8\n')
@mock.patch('swiftclient.service.Connection')
def test_list_container_with_headers(self, connection):
@@ -1161,9 +1165,12 @@ class TestShell(unittest.TestCase):
'x-object-meta-mtime': mock.ANY},
response_dict={})
+ @mock.patch('swiftclient.shell.stdin')
@mock.patch('swiftclient.shell.io.open')
@mock.patch('swiftclient.service.SwiftService.upload')
- def test_upload_from_stdin(self, upload_mock, io_open_mock):
+ def test_upload_from_stdin(self, upload_mock, io_open_mock, stdin_mock):
+ stdin_mock.fileno.return_value = 123
+
def fake_open(fd, mode):
mock_io = mock.Mock()
mock_io.fileno.return_value = fd
@@ -1179,8 +1186,8 @@ class TestShell(unittest.TestCase):
# element. This is because the upload method takes a container and a
# list of SwiftUploadObjects.
swift_upload_obj = upload_mock.mock_calls[0][1][1][0]
- self.assertEqual(sys.stdin.fileno(), swift_upload_obj.source.fileno())
- io_open_mock.assert_called_once_with(sys.stdin.fileno(), mode='rb')
+ self.assertEqual(123, swift_upload_obj.source.fileno())
+ io_open_mock.assert_called_once_with(123, mode='rb')
@mock.patch('swiftclient.service.SwiftService.upload')
def test_upload_from_stdin_no_name(self, upload_mock):
@@ -3021,24 +3028,30 @@ class TestKeystoneOptions(MockHttpTest):
no_auth=no_auth)
def test_all_args_passed_to_keystone(self):
- # check that all possible command line args are passed to keystone
- opts = {'auth-version': '3'}
- os_opts = dict(self.all_os_opts)
- os_opts.update(self.catalog_opts)
- self._test_options(opts, os_opts, flags=self.flags)
-
- opts = {'auth-version': '2.0'}
- self._test_options(opts, os_opts, flags=self.flags)
-
- opts = {}
- self.defaults['auth-version'] = '3'
- self._test_options(opts, os_opts, flags=self.flags)
-
- for o in ('user-domain-name', 'user-domain-id',
- 'project-domain-name', 'project-domain-id'):
- os_opts.pop(o)
- self.defaults['auth-version'] = '2.0'
- self._test_options(opts, os_opts, flags=self.flags)
+ rootLogger = logging.getLogger()
+ orig_lvl = rootLogger.getEffectiveLevel()
+ try:
+ rootLogger.setLevel(logging.DEBUG)
+ # check that all possible command line args are passed to keystone
+ opts = {'auth-version': '3'}
+ os_opts = dict(self.all_os_opts)
+ os_opts.update(self.catalog_opts)
+ self._test_options(opts, os_opts, flags=self.flags)
+
+ opts = {'auth-version': '2.0'}
+ self._test_options(opts, os_opts, flags=self.flags)
+
+ opts = {}
+ self.defaults['auth-version'] = '3'
+ self._test_options(opts, os_opts, flags=self.flags)
+
+ for o in ('user-domain-name', 'user-domain-id',
+ 'project-domain-name', 'project-domain-id'):
+ os_opts.pop(o)
+ self.defaults['auth-version'] = '2.0'
+ self._test_options(opts, os_opts, flags=self.flags)
+ finally:
+ rootLogger.setLevel(orig_lvl)
def test_catalog_options_and_flags_not_required_v3(self):
# check that all possible command line args are passed to keystone