Correctly mocking builtin open in test_v1_shell

There's now a seperate module 'builtins' from python-future(this package
is now installed by stestr, and stestr in required by oslotest-3.6.0),
so under Python 2.x 'builtins' is also imported as a module and
'bultins.open' is mocked instead of '__builtin__.open'. And according to
the Mock document, we should always patch the object where it's looked
up, so only necessary mocking is made in this patch.

Change-Id: I57ea61cefd3b01b19475b0fa7e16f716eaaa14e6
Signed-off-by: Zhao Chao <zhaochao1984@gmail.com>
This commit is contained in:
Zhao Chao 2018-07-10 13:47:04 +08:00
parent 9e2d282b4d
commit 0fc7d9e33d
1 changed files with 2 additions and 9 deletions

View File

@ -13,13 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.
try:
# handle py34
import builtins
except ImportError:
# and py27
import __builtin__ as builtins
import base64
import fixtures
import mock
@ -699,7 +692,7 @@ class ShellTest(utils.TestCase):
self.assert_called('GET', '/modules/4321')
def test_module_create(self):
with mock.patch.object(builtins, 'open'):
with mock.patch('argparse.open'):
return_value = b'mycontents'
expected_contents = str(return_value.decode('utf-8'))
mock_encode = mock.Mock(return_value=return_value)
@ -787,7 +780,7 @@ class ShellTest(utils.TestCase):
def test_module_retrieve(self):
with mock.patch.object(troveclient.v1.modules.Module, '__getattr__',
mock.Mock(return_value='4321')):
with mock.patch.object(builtins, 'open'):
with mock.patch('troveclient.v1.instances.open'):
self.run_command('module-retrieve 1234')
self.assert_called(
'GET',