[openstack-dev] [Cinder] Need some help with mock
Jay Pipes
jaypipes at gmail.com
Mon Apr 28 18:08:52 UTC 2014
On 04/28/2014 02:00 PM, Deepak Shetty wrote:
> I was writing this in test_glusterfs.py
>
> def test_ensure_shares_unmounted_1share(self):
> with contextlib.nested(
> mock.patch.object(self._driver, '_load_shares_config'),
> mock.patch.object(self._driver, '_ensure_share_unmounted')
> ) as (self._fake_load_shares_config, mock_ensure_share_unmounted):
>
> #mock_shares = {'127.7.7.7:/gluster1': None}
> #mock_load_shares_config.return_value = mock_shares
> #self._driver.shares = mock_load_shares_config.return_value
>
> self._driver._ensure_shares_unmounted()
>
> mock_ensure_share_unmounted.assert_called_once()
> mock_ensure_share_unmounted.assert_called_once_with(
> '127.7.7.7:/gluster1')
>
> for my patch @ https://review.openstack.org/#/c/86888/6
>
> and i get the output as ..
>
> ======================================================================
> FAIL:
> cinder.tests.test_glusterfs.GlusterFsDriverTestCase.test_ensure_shares_unmounted_1share
> ----------------------------------------------------------------------
> ...
> ...
>
> stderr: {{{
> cinder/tests/test_glusterfs.py:736: DeprecationWarning: With-statements
> now directly support multiple context managers
> mock.patch.object(self._driver, '_ensure_share_unmounted')
> }}}
>
> Traceback (most recent call last):
> File "cinder/tests/test_glusterfs.py", line 747, in
> test_ensure_shares_unmounted_1share
> '127.7.7.7:/gluster1')
> File "/usr/lib/python2.7/site-packages/mock.py", line 845, in
> assert_called_once_with
> raise AssertionError(msg)
> AssertionError: Expected to be called once. Called 0 times.
>
>
> Can you help with why
> 'mock_ensure_share_unmounted.assert_called_once()' check passes
> but
> 'mock_ensure_share_unmounted.assert_called_once_with('127.7.7.7:/gluster1')
> check fails ?
Sure. This is because assert_called_once() is not a method of
mock.Mock() and therefore is "magic-mocked" to return a mock.MagicMock()
itself.
assert_called_once_with(), however, *is* an actual method of the
mock.Mock() object and therefore is failing because the
mock_unsure_share_unmounted mock was not called once with the expected
arguments.
The way to avoid the above problem is to use something called autospec'ing.
You can read more about this intricacy of mock here:
http://www.voidspace.org.uk/python/mock/helpers.html#autospeccing
Best,
-jay
> In glusterfs.py ...
>
> def _ensure_shares_unmounted(self):
> self._load_shares_config(self.configuration.glusterfs_shares_config)
> for share in self.shares.keys():
> try:
> self._ensure_share_unmounted(share)
> except Exception as exc:
> LOG.warning(_('Exception during unmounting %s') % (exc,))
>
>
> thanx,
> deepak
>
>
>
> _______________________________________________
> OpenStack-dev mailing list
> OpenStack-dev at lists.openstack.org
> http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
>
More information about the OpenStack-dev
mailing list