[openstack-dev] [Cinder] Need some help with mock
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 ? 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@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
Re: [openstack-dev] [Cinder] Need some help with mock
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@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
Re: [openstack-dev] [Cinder] Need some help with mock
Hi Jay, (I havent checked your link yet) But just to get some more clarification.. I haven't understood yet, why you think its not called w/ the expected args ? I expect it to get called with the expected args bcos ... _load_shares_config is mocked to _fake_load_shares_config which sets self._driver.shares dict as my 'expected' share key:value pair Hence i expect my ensure_share_unmounted to get called with the above share key:value pair that i set using _fake_load_shares_config.. and thats not happening. Your reponse doesn't address this, does it ? thanx, deepak On Mon, Apr 28, 2014 at 11:38 PM, Jay Pipes jaypi...@gmail.com wrote: 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@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev