]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
DRY for network() and subnet() in test_db_plugin.py
authorIryoung Jeong <iryoung@gmail.com>
Fri, 30 Nov 2012 08:21:44 +0000 (17:21 +0900)
committerIryoung Jeong <iryoung@gmail.com>
Mon, 10 Dec 2012 18:15:57 +0000 (03:15 +0900)
This update avoids the repetition of code for using contextmanager
by introducing dummy contextmanager function.

Change-Id: I84d785b4b50ea447cb7f6a8807d9caad75088f32

quantum/tests/unit/test_db_plugin.py

index a00116362afad49e82a4d13a6cd1991e81835085..4c2dccce48b5203259a8eaf276f9dcd9b7a139a6 100644 (file)
@@ -50,6 +50,11 @@ ROOTDIR = os.path.dirname(os.path.dirname(__file__))
 ETCDIR = os.path.join(ROOTDIR, 'etc')
 
 
+@contextlib.contextmanager
+def dummy_context_func():
+    yield None
+
+
 def etcdir(*p):
     return os.path.join(ETCDIR, *p)
 
@@ -433,28 +438,12 @@ class QuantumDbPluginV2TestCase(unittest2.TestCase):
                dns_nameservers=None,
                host_routes=None,
                shared=None):
-        # TODO(anyone) DRY this
-        # NOTE(salvatore-orlando): we can pass the network object
-        # to gen function anyway, and then avoid the repetition
-        if not network:
-            with self.network() as network:
-                subnet = self._make_subnet(fmt,
-                                           network,
-                                           gateway_ip,
-                                           cidr,
-                                           allocation_pools,
-                                           ip_version,
-                                           enable_dhcp,
-                                           dns_nameservers,
-                                           host_routes,
-                                           shared=shared)
-                try:
-                    yield subnet
-                finally:
-                    self._delete('subnets', subnet['subnet']['id'])
-        else:
+        with (self.network() if not network
+              else dummy_context_func()) as network_to_use:
+            if network:
+                network_to_use = network
             subnet = self._make_subnet(fmt,
-                                       network,
+                                       network_to_use,
                                        gateway_ip,
                                        cidr,
                                        allocation_pools,
@@ -471,17 +460,11 @@ class QuantumDbPluginV2TestCase(unittest2.TestCase):
     @contextlib.contextmanager
     def port(self, subnet=None, fmt='json', no_delete=False,
              **kwargs):
-        if not subnet:
-            with self.subnet() as subnet:
-                net_id = subnet['subnet']['network_id']
-                port = self._make_port(fmt, net_id, **kwargs)
-                try:
-                    yield port
-                finally:
-                    if not no_delete:
-                        self._delete('ports', port['port']['id'])
-        else:
-            net_id = subnet['subnet']['network_id']
+        with (self.subnet() if not subnet
+              else dummy_context_func()) as subnet_to_use:
+            if subnet:
+                subnet_to_use = subnet
+            net_id = subnet_to_use['subnet']['network_id']
             port = self._make_port(fmt, net_id, **kwargs)
             try:
                 yield port