From 58ac9a009b7992f4d102d9577e5d6dd549ba6de7 Mon Sep 17 00:00:00 2001 From: Adit Sarfaty Date: Tue, 7 Nov 2017 14:31:18 +0200 Subject: [PATCH] Re-apply skipped cluster test Change-Id: I9686ccbcc3acf1e5a7ddafa028a3ebc35d7b2c98 --- .../tests/unit/v3/nsxlib_testcase.py | 43 ++++++++++++------- vmware_nsxlib/tests/unit/v3/test_cluster.py | 16 ++++--- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/vmware_nsxlib/tests/unit/v3/nsxlib_testcase.py b/vmware_nsxlib/tests/unit/v3/nsxlib_testcase.py index dba9f6eb..fa157841 100644 --- a/vmware_nsxlib/tests/unit/v3/nsxlib_testcase.py +++ b/vmware_nsxlib/tests/unit/v3/nsxlib_testcase.py @@ -65,35 +65,40 @@ def _mock_nsxlib(): 'id': uuidutils.generate_uuid()} for rule in rules ]} - - mock.patch( + mocking = [] + mocking.append(mock.patch( "vmware_nsxlib.v3.cluster.NSXRequestsHTTPProvider" - ".validate_connection").start() + ".validate_connection")) - mock.patch( + mocking.append(mock.patch( "vmware_nsxlib.v3.security.NsxLibNsGroup.create", side_effect=_return_id_key - ).start() + )) - mock.patch( + mocking.append(mock.patch( "vmware_nsxlib.v3.security.NsxLibFirewallSection.create_empty", - side_effect=_return_id_key).start() + side_effect=_return_id_key)) - mock.patch( + mocking.append(mock.patch( "vmware_nsxlib.v3.security.NsxLibFirewallSection.init_default", - side_effect=_return_id_key).start() + side_effect=_return_id_key)) - mock.patch( - "vmware_nsxlib.v3.security.NsxLibNsGroup.list").start() + mocking.append(mock.patch( + "vmware_nsxlib.v3.security.NsxLibNsGroup.list")) - mock.patch( + mocking.append(mock.patch( "vmware_nsxlib.v3.security.NsxLibFirewallSection.add_rules", - side_effect=_mock_add_rules_in_section).start() + side_effect=_mock_add_rules_in_section)) - mock.patch( + mocking.append(mock.patch( ("vmware_nsxlib.v3.core_resources." "NsxLibTransportZone.get_id_by_name_or_id"), - return_value=uuidutils.generate_uuid()).start() + return_value=uuidutils.generate_uuid())) + + for m in mocking: + m.start() + + return mocking def get_default_nsxlib_config(): @@ -141,7 +146,7 @@ class NsxLibTestCase(unittest.TestCase): def setUp(self, *args, **kwargs): super(NsxLibTestCase, self).setUp() - _mock_nsxlib() + self.mocking = _mock_nsxlib() if self.use_client_cert_auth(): nsxlib_config = get_nsxlib_config_with_client_cert() @@ -153,6 +158,12 @@ class NsxLibTestCase(unittest.TestCase): # print diffs when assert comparisons fail self.maxDiff = None + def tearDown(self, *args, **kwargs): + # stop the mocks + for m in self.mocking: + m.stop() + super(NsxLibTestCase, self).tearDown() + class MemoryMockAPIProvider(nsx_cluster.AbstractHTTPProvider): """Acts as a HTTP provider for mocking which is backed diff --git a/vmware_nsxlib/tests/unit/v3/test_cluster.py b/vmware_nsxlib/tests/unit/v3/test_cluster.py index b60c0be0..5b05c951 100644 --- a/vmware_nsxlib/tests/unit/v3/test_cluster.py +++ b/vmware_nsxlib/tests/unit/v3/test_cluster.py @@ -16,7 +16,6 @@ import unittest import mock -from oslo_serialization import jsonutils from requests import exceptions as requests_exceptions from requests import models import six.moves.urllib.parse as urlparse @@ -95,19 +94,22 @@ class RequestsHTTPProviderTestCase(unittest.TestCase): self.assertEqual(99, session.timeout) def test_validate_connection(self): - self.skipTest("Revist") mock_conn = mocks.MockRequestSessionApi() + mock_conn.default_headers = {} mock_ep = mock.Mock() mock_ep.provider.url = 'https://1.2.3.4' + mock_cluster = mock.Mock() + mock_cluster.nsxlib_config = mock.Mock() + mock_cluster.nsxlib_config.url_base = 'abc' + mock_cluster.nsxlib_config.keepalive_section = 'transport-zones' provider = cluster.NSXRequestsHTTPProvider() self.assertRaises(nsxlib_exc.ResourceNotFound, provider.validate_connection, - mock.Mock(), mock_ep, mock_conn) + mock_cluster, mock_ep, mock_conn) - mock_conn.post('api/v1/transport-zones', - data=jsonutils.dumps({'id': 'dummy-tz'}), - headers=client.JSONRESTClient._DEFAULT_HEADERS) - provider.validate_connection(mock.Mock(), mock_ep, mock_conn) + with mock.patch.object(client.JSONRESTClient, "get", + return_value={'result_count': 1}): + provider.validate_connection(mock_cluster, mock_ep, mock_conn) class NsxV3ClusteredAPITestCase(nsxlib_testcase.NsxClientTestCase):