From c690bcb68331818a04c94741b25f40cc40f7b3c4 Mon Sep 17 00:00:00 2001 From: Kazuhiro MIYAHARA Date: Mon, 17 Aug 2015 16:50:56 +0900 Subject: [PATCH] Fix dispersion-reports error message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch fixes Swift to show message "No objects to query. Has swift-dispersion-populate been run?" for "swift-dispersion-report —object-only” with no container for object dispersion. Change-Id: I82da56709cfc296a27f5180681709bc56adbc13d Closes-Bug: #1468120 --- bin/swift-dispersion-report | 8 ++-- test/unit/common/test_internal_client.py | 55 ++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/bin/swift-dispersion-report b/bin/swift-dispersion-report index e81cb2b86b..31a1741dda 100755 --- a/bin/swift-dispersion-report +++ b/bin/swift-dispersion-report @@ -26,6 +26,7 @@ except ImportError: from eventlet import GreenPool, hubs, patcher, Timeout from eventlet.pools import Pool +from eventlet.green import urllib2 from swift.common import direct_client try: @@ -176,9 +177,10 @@ def object_dispersion_report(coropool, connpool, account, object_ring, try: objects = [o['name'] for o in conn.get_container( container, prefix='dispersion_', full_listing=True)[1]] - except ClientException as err: - if err.http_status != 404: - raise + except urllib2.HTTPError as err: + if err.getcode() != 404: + raise err + print >>stderr, 'No objects to query. Has ' \ 'swift-dispersion-populate been run?' stderr.flush() diff --git a/test/unit/common/test_internal_client.py b/test/unit/common/test_internal_client.py index 9fed678faf..3c817fbc9b 100644 --- a/test/unit/common/test_internal_client.py +++ b/test/unit/common/test_internal_client.py @@ -1264,6 +1264,61 @@ class TestSimpleClient(unittest.TestCase): self.assertEqual(mock_urlopen.call_count, 2) self.assertEqual([None, None], retval) + @mock.patch('eventlet.green.urllib2.urlopen') + def test_request_with_retries_with_HTTPError(self, mock_urlopen): + mock_response = mock.MagicMock() + mock_response.read.return_value = '' + c = internal_client.SimpleClient(url='http://127.0.0.1', token='token') + self.assertEqual(c.retries, 5) + + for request_method in 'GET PUT POST DELETE HEAD COPY'.split(): + mock_urlopen.reset_mock() + mock_urlopen.side_effect = urllib2.HTTPError(*[None] * 5) + with mock.patch('swift.common.internal_client.sleep') \ + as mock_sleep: + self.assertRaises(urllib2.HTTPError, + c.retry_request, request_method, retries=1) + self.assertEqual(mock_sleep.call_count, 1) + self.assertEqual(mock_urlopen.call_count, 2) + + @mock.patch('eventlet.green.urllib2.urlopen') + def test_request_container_with_retries_with_HTTPError(self, + mock_urlopen): + mock_response = mock.MagicMock() + mock_response.read.return_value = '' + c = internal_client.SimpleClient(url='http://127.0.0.1', token='token') + self.assertEqual(c.retries, 5) + + for request_method in 'GET PUT POST DELETE HEAD COPY'.split(): + mock_urlopen.reset_mock() + mock_urlopen.side_effect = urllib2.HTTPError(*[None] * 5) + with mock.patch('swift.common.internal_client.sleep') \ + as mock_sleep: + self.assertRaises(urllib2.HTTPError, + c.retry_request, request_method, + container='con', retries=1) + self.assertEqual(mock_sleep.call_count, 1) + self.assertEqual(mock_urlopen.call_count, 2) + + @mock.patch('eventlet.green.urllib2.urlopen') + def test_request_object_with_retries_with_HTTPError(self, + mock_urlopen): + mock_response = mock.MagicMock() + mock_response.read.return_value = '' + c = internal_client.SimpleClient(url='http://127.0.0.1', token='token') + self.assertEqual(c.retries, 5) + + for request_method in 'GET PUT POST DELETE HEAD COPY'.split(): + mock_urlopen.reset_mock() + mock_urlopen.side_effect = urllib2.HTTPError(*[None] * 5) + with mock.patch('swift.common.internal_client.sleep') \ + as mock_sleep: + self.assertRaises(urllib2.HTTPError, + c.retry_request, request_method, + container='con', name='obj', retries=1) + self.assertEqual(mock_sleep.call_count, 1) + self.assertEqual(mock_urlopen.call_count, 2) + def test_proxy(self): # check that proxy arg is passed through to the urllib Request scheme = 'http'