Use six.moves.builtins to mock open in unit tests
This commit switches all the mocks for the open call to use the six
builtins module instead of __builtin__. This is needed because on
python 3 it was renamed to builtins, so to enable running the unit
tests on either environment we need to leverage the six module.
Change-Id: Idf8c0e9858451507c9ac0cfae747f90c6feb7f1e
diff --git a/tempest/tests/common/test_accounts.py b/tempest/tests/common/test_accounts.py
index 87c4bb3..596e811 100644
--- a/tempest/tests/common/test_accounts.py
+++ b/tempest/tests/common/test_accounts.py
@@ -107,7 +107,8 @@
def test_create_hash_file_previous_file(self):
# Emulate the lock existing on the filesystem
self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
- with mock.patch('__builtin__.open', mock.mock_open(), create=True):
+ with mock.patch('six.moves.builtins.open', mock.mock_open(),
+ create=True):
test_account_class = accounts.Accounts('v2', 'test_name')
res = test_account_class._create_hash_file('12345')
self.assertFalse(res, "_create_hash_file should return False if the "
@@ -116,7 +117,8 @@
def test_create_hash_file_no_previous_file(self):
# Emulate the lock not existing on the filesystem
self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))
- with mock.patch('__builtin__.open', mock.mock_open(), create=True):
+ with mock.patch('six.moves.builtins.open', mock.mock_open(),
+ create=True):
test_account_class = accounts.Accounts('v2', 'test_name')
res = test_account_class._create_hash_file('12345')
self.assertTrue(res, "_create_hash_file should return True if the "
@@ -130,7 +132,7 @@
mkdir_mock = self.useFixture(mockpatch.Patch('os.mkdir'))
self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))
test_account_class = accounts.Accounts('v2', 'test_name')
- with mock.patch('__builtin__.open', mock.mock_open(),
+ with mock.patch('six.moves.builtins.open', mock.mock_open(),
create=True) as open_mock:
test_account_class._get_free_hash(hash_list)
lock_path = os.path.join(lockutils.get_lock_path(accounts.CONF),
@@ -149,7 +151,8 @@
# Emulate all lcoks in list are in use
self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
test_account_class = accounts.Accounts('v2', 'test_name')
- with mock.patch('__builtin__.open', mock.mock_open(), create=True):
+ with mock.patch('six.moves.builtins.open', mock.mock_open(),
+ create=True):
self.assertRaises(exceptions.InvalidConfiguration,
test_account_class._get_free_hash, hash_list)
@@ -168,7 +171,7 @@
return True
self.stubs.Set(os.path, 'isfile', _fake_is_file)
- with mock.patch('__builtin__.open', mock.mock_open(),
+ with mock.patch('six.moves.builtins.open', mock.mock_open(),
create=True) as open_mock:
test_account_class._get_free_hash(hash_list)
lock_path = os.path.join(lockutils.get_lock_path(accounts.CONF),
diff --git a/tempest/tests/common/utils/test_file_utils.py b/tempest/tests/common/utils/test_file_utils.py
index 605e82a..937aefa 100644
--- a/tempest/tests/common/utils/test_file_utils.py
+++ b/tempest/tests/common/utils/test_file_utils.py
@@ -22,7 +22,8 @@
class TestFileUtils(base.TestCase):
def test_have_effective_read_path(self):
- with mock.patch('__builtin__.open', mock.mock_open(), create=True):
+ with mock.patch('six.moves.builtins.open', mock.mock_open(),
+ create=True):
result = file_utils.have_effective_read_access('fake_path')
self.assertTrue(result)