py3: port cli recon tests

Also a drive by HASH_PATH_PREFIX/SUFFIX bytesting fix which was
breaking ring and relinker tests.

Change-Id: I1db8f102361770cfed2b12f689789d1e8c21c3e4
This commit is contained in:
Matthew Oliver 2018-03-09 00:24:43 +00:00
parent 800e585176
commit 817ca078e1
4 changed files with 28 additions and 19 deletions

View File

@ -87,6 +87,8 @@ class Scout(object):
url = base_url + recon_type url = base_url + recon_type
try: try:
body = urllib2.urlopen(url, timeout=self.timeout).read() body = urllib2.urlopen(url, timeout=self.timeout).read()
if six.PY3 and isinstance(body, six.binary_type):
body = body.decode('utf8')
content = json.loads(body) content = json.loads(body)
if self.verbose: if self.verbose:
print("-> %s: %s" % (url, content)) print("-> %s: %s" % (url, content))
@ -129,7 +131,7 @@ class Scout(object):
req = urllib2.Request(url) req = urllib2.Request(url)
req.get_method = lambda: 'OPTIONS' req.get_method = lambda: 'OPTIONS'
conn = urllib2.urlopen(req) conn = urllib2.urlopen(req)
header = conn.info().getheader('Server') header = conn.info().get('Server')
server_header = header.split('/') server_header = header.split('/')
content = server_header[0] content = server_header[0]
status = 200 status = 200

View File

@ -205,8 +205,8 @@ def set_swift_dir(swift_dir):
swift_dir != os.path.dirname(SWIFT_CONF_FILE)): swift_dir != os.path.dirname(SWIFT_CONF_FILE)):
SWIFT_CONF_FILE = os.path.join( SWIFT_CONF_FILE = os.path.join(
swift_dir, os.path.basename(SWIFT_CONF_FILE)) swift_dir, os.path.basename(SWIFT_CONF_FILE))
HASH_PATH_PREFIX = '' HASH_PATH_PREFIX = b''
HASH_PATH_SUFFIX = '' HASH_PATH_SUFFIX = b''
validate_configuration() validate_configuration()
return True return True
return False return False

View File

@ -39,8 +39,10 @@ from test.unit import patch_policies
if six.PY3: if six.PY3:
from eventlet.green.urllib import request as urllib2 from eventlet.green.urllib import request as urllib2
GREEN_URLLIB_URLOPEN = 'eventlet.green.urllib.request.urlopen'
else: else:
from eventlet.green import urllib2 from eventlet.green import urllib2
GREEN_URLLIB_URLOPEN = 'eventlet.green.urllib2.urlopen'
class TestHelpers(unittest.TestCase): class TestHelpers(unittest.TestCase):
@ -68,7 +70,7 @@ class TestScout(unittest.TestCase):
self.url = 'http://127.0.0.1:8080/recon/type' self.url = 'http://127.0.0.1:8080/recon/type'
self.server_type_url = 'http://127.0.0.1:8080/' self.server_type_url = 'http://127.0.0.1:8080/'
@mock.patch('eventlet.green.urllib2.urlopen') @mock.patch(GREEN_URLLIB_URLOPEN)
def test_scout_ok(self, mock_urlopen): def test_scout_ok(self, mock_urlopen):
mock_urlopen.return_value.read = lambda: json.dumps([]) mock_urlopen.return_value.read = lambda: json.dumps([])
url, content, status, ts_start, ts_end = self.scout_instance.scout( url, content, status, ts_start, ts_end = self.scout_instance.scout(
@ -77,7 +79,7 @@ class TestScout(unittest.TestCase):
self.assertEqual(content, []) self.assertEqual(content, [])
self.assertEqual(status, 200) self.assertEqual(status, 200)
@mock.patch('eventlet.green.urllib2.urlopen') @mock.patch(GREEN_URLLIB_URLOPEN)
def test_scout_url_error(self, mock_urlopen): def test_scout_url_error(self, mock_urlopen):
mock_urlopen.side_effect = urllib2.URLError("") mock_urlopen.side_effect = urllib2.URLError("")
url, content, status, ts_start, ts_end = self.scout_instance.scout( url, content, status, ts_start, ts_end = self.scout_instance.scout(
@ -86,7 +88,7 @@ class TestScout(unittest.TestCase):
self.assertEqual(url, self.url) self.assertEqual(url, self.url)
self.assertEqual(status, -1) self.assertEqual(status, -1)
@mock.patch('eventlet.green.urllib2.urlopen') @mock.patch(GREEN_URLLIB_URLOPEN)
def test_scout_http_error(self, mock_urlopen): def test_scout_http_error(self, mock_urlopen):
mock_urlopen.side_effect = urllib2.HTTPError( mock_urlopen.side_effect = urllib2.HTTPError(
self.url, 404, "Internal error", None, None) self.url, 404, "Internal error", None, None)
@ -96,7 +98,7 @@ class TestScout(unittest.TestCase):
self.assertIsInstance(content, urllib2.HTTPError) self.assertIsInstance(content, urllib2.HTTPError)
self.assertEqual(status, 404) self.assertEqual(status, 404)
@mock.patch('eventlet.green.urllib2.urlopen') @mock.patch(GREEN_URLLIB_URLOPEN)
def test_scout_socket_timeout(self, mock_urlopen): def test_scout_socket_timeout(self, mock_urlopen):
mock_urlopen.side_effect = socket.timeout("timeout") mock_urlopen.side_effect = socket.timeout("timeout")
url, content, status, ts_start, ts_end = self.scout_instance.scout( url, content, status, ts_start, ts_end = self.scout_instance.scout(
@ -105,19 +107,19 @@ class TestScout(unittest.TestCase):
self.assertEqual(url, self.url) self.assertEqual(url, self.url)
self.assertEqual(status, -1) self.assertEqual(status, -1)
@mock.patch('eventlet.green.urllib2.urlopen') @mock.patch(GREEN_URLLIB_URLOPEN)
def test_scout_server_type_ok(self, mock_urlopen): def test_scout_server_type_ok(self, mock_urlopen):
def getheader(name): def getheader(name):
d = {'Server': 'server-type'} d = {'Server': 'server-type'}
return d.get(name) return d.get(name)
mock_urlopen.return_value.info.return_value.getheader = getheader mock_urlopen.return_value.info.return_value.get = getheader
url, content, status = self.scout_instance.scout_server_type( url, content, status = self.scout_instance.scout_server_type(
("127.0.0.1", "8080")) ("127.0.0.1", "8080"))
self.assertEqual(url, self.server_type_url) self.assertEqual(url, self.server_type_url)
self.assertEqual(content, 'server-type') self.assertEqual(content, 'server-type')
self.assertEqual(status, 200) self.assertEqual(status, 200)
@mock.patch('eventlet.green.urllib2.urlopen') @mock.patch(GREEN_URLLIB_URLOPEN)
def test_scout_server_type_url_error(self, mock_urlopen): def test_scout_server_type_url_error(self, mock_urlopen):
mock_urlopen.side_effect = urllib2.URLError("") mock_urlopen.side_effect = urllib2.URLError("")
url, content, status = self.scout_instance.scout_server_type( url, content, status = self.scout_instance.scout_server_type(
@ -126,7 +128,7 @@ class TestScout(unittest.TestCase):
self.assertEqual(url, self.server_type_url) self.assertEqual(url, self.server_type_url)
self.assertEqual(status, -1) self.assertEqual(status, -1)
@mock.patch('eventlet.green.urllib2.urlopen') @mock.patch(GREEN_URLLIB_URLOPEN)
def test_scout_server_type_http_error(self, mock_urlopen): def test_scout_server_type_http_error(self, mock_urlopen):
mock_urlopen.side_effect = urllib2.HTTPError( mock_urlopen.side_effect = urllib2.HTTPError(
self.server_type_url, 404, "Internal error", None, None) self.server_type_url, 404, "Internal error", None, None)
@ -136,7 +138,7 @@ class TestScout(unittest.TestCase):
self.assertIsInstance(content, urllib2.HTTPError) self.assertIsInstance(content, urllib2.HTTPError)
self.assertEqual(status, 404) self.assertEqual(status, 404)
@mock.patch('eventlet.green.urllib2.urlopen') @mock.patch(GREEN_URLLIB_URLOPEN)
def test_scout_server_type_socket_timeout(self, mock_urlopen): def test_scout_server_type_socket_timeout(self, mock_urlopen):
mock_urlopen.side_effect = socket.timeout("timeout") mock_urlopen.side_effect = socket.timeout("timeout")
url, content, status = self.scout_instance.scout_server_type( url, content, status = self.scout_instance.scout_server_type(
@ -160,9 +162,8 @@ class TestRecon(unittest.TestCase):
self.swift_dir, self.ring_name2 + '.ring.gz') self.swift_dir, self.ring_name2 + '.ring.gz')
swift_conf = os.path.join(self.swift_dir, 'swift.conf') swift_conf = os.path.join(self.swift_dir, 'swift.conf')
self.policy_name = ''.join(random.sample(string.letters, 20)) self.policy_name = ''.join(random.sample(string.ascii_letters, 20))
with open(swift_conf, "wb") as sc: swift_conf_data = '''
sc.write('''
[swift-hash] [swift-hash]
swift_hash_path_suffix = changeme swift_hash_path_suffix = changeme
@ -173,7 +174,9 @@ default = yes
[storage-policy:1] [storage-policy:1]
name = unu name = unu
aliases = %s aliases = %s
''' % self.policy_name) ''' % self.policy_name
with open(swift_conf, "wb") as sc:
sc.write(swift_conf_data.encode('utf8'))
def tearDown(self, *_args, **_kwargs): def tearDown(self, *_args, **_kwargs):
utils.SWIFT_CONF_FILE = self.swift_conf_file utils.SWIFT_CONF_FILE = self.swift_conf_file
@ -668,10 +671,11 @@ class TestReconCommands(unittest.TestCase):
response_body = resps[(host, port, path[7:])] response_body = resps[(host, port, path[7:])]
resp = mock.MagicMock() resp = mock.MagicMock()
resp.read = mock.MagicMock(side_effect=[response_body]) resp.read = mock.MagicMock(side_effect=[
response_body if six.PY2 else response_body.encode('utf8')])
return resp return resp
return mock.patch('eventlet.green.urllib2.urlopen', fake_urlopen) return mock.patch(GREEN_URLLIB_URLOPEN, fake_urlopen)
def test_server_type_check(self): def test_server_type_check(self):
hosts = [('127.0.0.1', 6010), ('127.0.0.1', 6011), hosts = [('127.0.0.1', 6010), ('127.0.0.1', 6011),
@ -870,7 +874,9 @@ class TestReconCommands(unittest.TestCase):
mock.call('Disk usage: space used: 260 of 300'), mock.call('Disk usage: space used: 260 of 300'),
mock.call('Disk usage: space free: 40 of 300'), mock.call('Disk usage: space free: 40 of 300'),
mock.call('Disk usage: lowest: 85.0%, ' + mock.call('Disk usage: lowest: 85.0%, ' +
'highest: 90.0%, avg: 86.6666666667%'), 'highest: 90.0%%, avg: %s' %
('86.6666666667%' if six.PY2 else
'86.66666666666667%')),
mock.call('=' * 79), mock.call('=' * 79),
] ]

View File

@ -31,6 +31,7 @@ commands =
nosetests \ nosetests \
test/unit/cli/test_dispersion_report.py \ test/unit/cli/test_dispersion_report.py \
test/unit/cli/test_info.py \ test/unit/cli/test_info.py \
test/unit/cli/test_recon.py \
test/unit/cli/test_relinker.py \ test/unit/cli/test_relinker.py \
test/unit/cli/test_ring_builder_analyzer.py \ test/unit/cli/test_ring_builder_analyzer.py \
test/unit/cli/test_ringbuilder.py \ test/unit/cli/test_ringbuilder.py \