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