From 57011d5699d49a47ae89073ff27b39140ab4d652 Mon Sep 17 00:00:00 2001 From: Ricardo Ferreira Date: Thu, 12 Mar 2015 23:18:33 +0000 Subject: [PATCH] More user-friendly output for object metadata Split out system, user and other metadata in swift-object-info. Print every position line by line instead of raw dict representation, so it would be easier to parse with tools such as grep. Co-Authored-By: Ricardo Ferreira Co-Authored-By: Kamil Rykowski Change-Id: Ia78da518c18f7e26016700aee87efb534fbd2040 Closes-Bug: #1428866 --- swift/cli/info.py | 24 +++++++- test/unit/cli/test_info.py | 113 +++++++++++++++++++++++++++++++++---- 2 files changed, 125 insertions(+), 12 deletions(-) diff --git a/swift/cli/info.py b/swift/cli/info.py index cffb93eed5..074350c8b2 100644 --- a/swift/cli/info.py +++ b/swift/cli/info.py @@ -251,6 +251,10 @@ def print_obj_metadata(metadata): :raises: ValueError """ + user_metadata = {} + sys_metadata = {} + other_metadata = {} + if not metadata: raise ValueError('Metadata is None') path = metadata.pop('name', '') @@ -280,7 +284,25 @@ def print_obj_metadata(metadata): else: print 'Timestamp: Not found in metadata' - print 'User Metadata: %s' % metadata + for key, value in metadata.iteritems(): + if is_user_meta('Object', key): + user_metadata[key] = value + elif is_sys_meta('Object', key): + sys_metadata[key] = value + else: + other_metadata[key] = value + + def print_metadata(title, items): + print title + if items: + for meta_key in sorted(items): + print ' %s: %s' % (meta_key, items[meta_key]) + else: + print ' No metadata found' + + print_metadata('System Metadata:', sys_metadata) + print_metadata('User Metadata:', user_metadata) + print_metadata('Other Metadata:', other_metadata) def print_info(db_type, db_file, swift_dir='/etc/swift'): diff --git a/test/unit/cli/test_info.py b/test/unit/cli/test_info.py index 2766520fd0..4a03db2ad8 100644 --- a/test/unit/cli/test_info.py +++ b/test/unit/cli/test_info.py @@ -435,14 +435,14 @@ class TestPrintObjFullMeta(TestCliInfoBase): self.assertRaisesMessage(ValueError, 'Metadata is None', print_obj_metadata, []) - def reset_metadata(): + def get_metadata(items): md = dict(name='/AUTH_admin/c/dummy') md['Content-Type'] = 'application/octet-stream' md['X-Timestamp'] = 106.3 - md['X-Object-Meta-Mtime'] = '107.3' + md.update(items) return md - metadata = reset_metadata() + metadata = get_metadata({'X-Object-Meta-Mtime': '107.3'}) out = StringIO() with mock.patch('sys.stdout', out): print_obj_metadata(metadata) @@ -453,17 +453,93 @@ class TestPrintObjFullMeta(TestCliInfoBase): Object hash: 128fdf98bddd1b1e8695f4340e67a67a Content-Type: application/octet-stream Timestamp: 1970-01-01T00:01:46.300000 (%s) -User Metadata: {'X-Object-Meta-Mtime': '107.3'}''' % ( +System Metadata: + No metadata found +User Metadata: + X-Object-Meta-Mtime: 107.3 +Other Metadata: + No metadata found''' % ( utils.Timestamp(106.3).internal) self.assertEquals(out.getvalue().strip(), exp_out) - metadata = reset_metadata() + metadata = get_metadata({ + 'X-Object-Sysmeta-Mtime': '107.3', + 'X-Object-Sysmeta-Name': 'Obj name', + }) + out = StringIO() + with mock.patch('sys.stdout', out): + print_obj_metadata(metadata) + exp_out = '''Path: /AUTH_admin/c/dummy + Account: AUTH_admin + Container: c + Object: dummy + Object hash: 128fdf98bddd1b1e8695f4340e67a67a +Content-Type: application/octet-stream +Timestamp: 1970-01-01T00:01:46.300000 (%s) +System Metadata: + X-Object-Sysmeta-Mtime: 107.3 + X-Object-Sysmeta-Name: Obj name +User Metadata: + No metadata found +Other Metadata: + No metadata found''' % ( + utils.Timestamp(106.3).internal) + + self.assertEquals(out.getvalue().strip(), exp_out) + + metadata = get_metadata({ + 'X-Object-Meta-Mtime': '107.3', + 'X-Object-Sysmeta-Mtime': '107.3', + 'X-Object-Mtime': '107.3', + }) + out = StringIO() + with mock.patch('sys.stdout', out): + print_obj_metadata(metadata) + exp_out = '''Path: /AUTH_admin/c/dummy + Account: AUTH_admin + Container: c + Object: dummy + Object hash: 128fdf98bddd1b1e8695f4340e67a67a +Content-Type: application/octet-stream +Timestamp: 1970-01-01T00:01:46.300000 (%s) +System Metadata: + X-Object-Sysmeta-Mtime: 107.3 +User Metadata: + X-Object-Meta-Mtime: 107.3 +Other Metadata: + X-Object-Mtime: 107.3''' % ( + utils.Timestamp(106.3).internal) + + self.assertEquals(out.getvalue().strip(), exp_out) + + metadata = get_metadata({}) + out = StringIO() + with mock.patch('sys.stdout', out): + print_obj_metadata(metadata) + exp_out = '''Path: /AUTH_admin/c/dummy + Account: AUTH_admin + Container: c + Object: dummy + Object hash: 128fdf98bddd1b1e8695f4340e67a67a +Content-Type: application/octet-stream +Timestamp: 1970-01-01T00:01:46.300000 (%s) +System Metadata: + No metadata found +User Metadata: + No metadata found +Other Metadata: + No metadata found''' % ( + utils.Timestamp(106.3).internal) + + self.assertEquals(out.getvalue().strip(), exp_out) + + metadata = get_metadata({'X-Object-Meta-Mtime': '107.3'}) metadata['name'] = '/a-s' self.assertRaisesMessage(ValueError, 'Path is invalid', print_obj_metadata, metadata) - metadata = reset_metadata() + metadata = get_metadata({'X-Object-Meta-Mtime': '107.3'}) del metadata['name'] out = StringIO() with mock.patch('sys.stdout', out): @@ -471,12 +547,17 @@ User Metadata: {'X-Object-Meta-Mtime': '107.3'}''' % ( exp_out = '''Path: Not found in metadata Content-Type: application/octet-stream Timestamp: 1970-01-01T00:01:46.300000 (%s) -User Metadata: {'X-Object-Meta-Mtime': '107.3'}''' % ( +System Metadata: + No metadata found +User Metadata: + X-Object-Meta-Mtime: 107.3 +Other Metadata: + No metadata found''' % ( utils.Timestamp(106.3).internal) self.assertEquals(out.getvalue().strip(), exp_out) - metadata = reset_metadata() + metadata = get_metadata({'X-Object-Meta-Mtime': '107.3'}) del metadata['Content-Type'] out = StringIO() with mock.patch('sys.stdout', out): @@ -488,12 +569,17 @@ User Metadata: {'X-Object-Meta-Mtime': '107.3'}''' % ( Object hash: 128fdf98bddd1b1e8695f4340e67a67a Content-Type: Not found in metadata Timestamp: 1970-01-01T00:01:46.300000 (%s) -User Metadata: {'X-Object-Meta-Mtime': '107.3'}''' % ( +System Metadata: + No metadata found +User Metadata: + X-Object-Meta-Mtime: 107.3 +Other Metadata: + No metadata found''' % ( utils.Timestamp(106.3).internal) self.assertEquals(out.getvalue().strip(), exp_out) - metadata = reset_metadata() + metadata = get_metadata({'X-Object-Meta-Mtime': '107.3'}) del metadata['X-Timestamp'] out = StringIO() with mock.patch('sys.stdout', out): @@ -505,6 +591,11 @@ User Metadata: {'X-Object-Meta-Mtime': '107.3'}''' % ( Object hash: 128fdf98bddd1b1e8695f4340e67a67a Content-Type: application/octet-stream Timestamp: Not found in metadata -User Metadata: {'X-Object-Meta-Mtime': '107.3'}''' +System Metadata: + No metadata found +User Metadata: + X-Object-Meta-Mtime: 107.3 +Other Metadata: + No metadata found''' self.assertEquals(out.getvalue().strip(), exp_out)