self.assertEqual(''foo'', utils.xhtml_escape("'foo'"))
def test_hash_file(self):
- data = 'Mary had a little lamb, its fleece as white as snow'
- flo = six.StringIO(data)
+ data = b'Mary had a little lamb, its fleece as white as snow'
+ flo = six.BytesIO(data)
h1 = utils.hash_file(flo)
h2 = hashlib.sha1(data).hexdigest()
self.assertEqual(h1, h2)
class SSHPoolTestCase(test.TestCase):
"""Unit test for SSH Connection Pool."""
@mock.patch('cinder.ssh_utils.CONF')
- @mock.patch('__builtin__.open')
+ @mock.patch('six.moves.builtins.open')
@mock.patch('paramiko.SSHClient')
@mock.patch('os.path.isfile', return_value=True)
def test_ssh_default_hosts_key_file(self, mock_isfile, mock_sshclient,
'/var/lib/cinder/ssh_known_hosts')
@mock.patch('cinder.ssh_utils.CONF')
- @mock.patch('__builtin__.open')
+ @mock.patch('six.moves.builtins.open')
@mock.patch('paramiko.SSHClient')
@mock.patch('os.path.isfile', return_value=True)
def test_ssh_host_key_file_kwargs(self, mock_isfile, mock_sshclient,
mock_ssh.assert_has_calls(expected, any_order=True)
@mock.patch('cinder.ssh_utils.CONF')
- @mock.patch('__builtin__.open')
+ @mock.patch('six.moves.builtins.open')
@mock.patch('os.path.isfile', return_value=True)
@mock.patch('paramiko.RSAKey.from_private_key_file')
@mock.patch('paramiko.SSHClient')
min_size=1,
max_size=1)
- @mock.patch('__builtin__.open')
+ @mock.patch('six.moves.builtins.open')
@mock.patch('paramiko.SSHClient')
def test_closed_reopened_ssh_connections(self, mock_sshclient, mock_open):
mock_sshclient.return_value = eval('FakeSSHClient')()
self.assertNotEqual(first_id, third_id)
@mock.patch('cinder.ssh_utils.CONF')
- @mock.patch('__builtin__.open')
+ @mock.patch('six.moves.builtins.open')
@mock.patch('paramiko.SSHClient')
def test_missing_ssh_hosts_key_config(self, mock_sshclient, mock_open,
mock_conf):
min_size=1,
max_size=1)
- @mock.patch('__builtin__.open')
+ @mock.patch('six.moves.builtins.open')
@mock.patch('paramiko.SSHClient')
def test_create_default_known_hosts_file(self, mock_sshclient,
mock_open):
ssh_pool.remove(ssh)
@mock.patch('os.path.isfile', return_value=False)
- @mock.patch('__builtin__.open')
+ @mock.patch('six.moves.builtins.open')
@mock.patch('paramiko.SSHClient')
def test_ssh_missing_hosts_key_file(self, mock_sshclient, mock_open,
mock_isfile):
@mock.patch.multiple('cinder.ssh_utils.CONF',
strict_ssh_host_key_policy=True,
ssh_hosts_key_file='/var/lib/cinder/ssh_known_hosts')
- @mock.patch('__builtin__.open')
+ @mock.patch('six.moves.builtins.open')
@mock.patch('paramiko.SSHClient')
@mock.patch('os.path.isfile', return_value=True)
def test_ssh_strict_host_key_policy(self, mock_isfile, mock_sshclient,
self.assertTrue(isinstance(ssh.get_policy(),
paramiko.RejectPolicy))
- @mock.patch('__builtin__.open')
+ @mock.patch('six.moves.builtins.open')
@mock.patch('paramiko.SSHClient')
@mock.patch('os.path.isfile', return_value=True)
def test_ssh_not_strict_host_key_policy(self, mock_isfile, mock_sshclient,
def sanitize_hostname(hostname):
"""Return a hostname which conforms to RFC-952 and RFC-1123 specs."""
- if isinstance(hostname, six.text_type):
+ if six.PY3:
hostname = hostname.encode('latin-1', 'ignore')
+ hostname = hostname.decode('latin-1')
+ else:
+ if isinstance(hostname, six.text_type):
+ hostname = hostname.encode('latin-1', 'ignore')
hostname = re.sub('[ _]', '-', hostname)
hostname = re.sub('[^\w.-]+', '', hostname)
def hash_file(file_like_object):
"""Generate a hash for the contents of a file."""
checksum = hashlib.sha1()
- any(map(checksum.update, iter(lambda: file_like_object.read(32768), '')))
+ any(map(checksum.update, iter(lambda: file_like_object.read(32768), b'')))
return checksum.hexdigest()
if isinstance(version, six.string_types):
version = convert_version_to_tuple(version)
if isinstance(version, tuple):
- return reduce(lambda x, y: (x * 1000) + y, version)
+ return six.moves.reduce(lambda x, y: (x * 1000) + y, version)
except Exception:
msg = _("Version %s is invalid.") % version
raise exception.CinderException(msg)
while version_int != 0:
version_number = version_int - (version_int // factor * factor)
version_numbers.insert(0, six.text_type(version_number))
- version_int = version_int / factor
+ version_int = version_int // factor
- return reduce(lambda x, y: "%s.%s" % (x, y), version_numbers)
+ return '.'.join(map(str, version_numbers))
def convert_version_to_tuple(version_str):