Fix broken jumphost detach method when orig file exists 39/42139/1
authorTim Rozet <trozet@redhat.com>
Sat, 16 Sep 2017 13:42:47 +0000 (09:42 -0400)
committerTim Rozet <trozet@redhat.com>
Sat, 16 Sep 2017 13:42:47 +0000 (09:42 -0400)
In the case where the backup ifcfg file exists (.orig) the functionality
to detach the interface from ovs was broken.  This fixes it and adds
unit test case to cover that scenario.

Change-Id: If6d8ca0ba5cf80df71bb82f52e9d204435374479
Signed-off-by: Tim Rozet <trozet@redhat.com>
apex/network/jumphost.py
apex/tests/test_apex_network_jumphost.py

index 2ecb7f4..96b06cb 100644 (file)
@@ -227,15 +227,14 @@ def detach_interface_from_ovs(network):
     orig_ifcfg_file = os.path.join(NET_CFG_PATH,
                                    "ifcfg-{}.orig".format(real_interface))
     ifcfg_file = orig_ifcfg_file[:-len('.orig')]
+    bridge_ifcfg_file = os.path.join(NET_CFG_PATH,
+                                     "ifcfg-{}".format(bridge))
     if os.path.isfile(orig_ifcfg_file):
         logging.debug("Original interface file found: "
                       "{}".format(orig_ifcfg_file))
-        shutil.move(orig_ifcfg_file, ifcfg_file)
     else:
         logging.info("No original ifcfg file found...will attempt to use "
-                     "bridge icfg file and re-create")
-        bridge_ifcfg_file = os.path.join(NET_CFG_PATH,
-                                         "ifcfg-{}".format(bridge))
+                     "bridge ifcfg file and re-create")
         if os.path.isfile(bridge_ifcfg_file):
             ifcfg_params = generate_ifcfg_params(bridge_ifcfg_file, network)
             if_content = """DEVICE={}
index a23f1c5..da9703e 100644 (file)
@@ -202,6 +202,29 @@ class TestNetworkJumpHost:
             if os.path.isfile(ifcfg_path):
                 os.remove(ifcfg_path)
 
+    @patch('subprocess.check_call')
+    @patch('apex.network.jumphost.is_ovs_bridge', return_value=True)
+    @patch('apex.network.jumphost.dump_ovs_ports', return_value=['enpfakes0'])
+    def test_detach_interface_orig_exists(self, dump_ports_func,
+                                          is_bridge_func, subprocess_func):
+        ifcfg_dir = con.TEST_DUMMY_CONFIG
+        shutil.copyfile(os.path.join(ifcfg_dir, 'ifcfg-br-dummy'),
+                        os.path.join(ifcfg_dir, 'ifcfg-br-admin'))
+        shutil.copyfile(os.path.join(ifcfg_dir, 'ifcfg-dummy'),
+                        os.path.join(ifcfg_dir, 'ifcfg-enpfakes0.orig'))
+        jumphost.NET_CFG_PATH = ifcfg_dir
+        output = jumphost.detach_interface_from_ovs('admin')
+        assert output is None
+        assert os.path.isfile(os.path.join(ifcfg_dir, 'ifcfg-enpfakes0'))
+        assert os.path.isfile(os.path.join(ifcfg_dir, 'ifcfg-br-admin'))
+        assert not os.path.isfile(os.path.join(ifcfg_dir,
+                                               'ifcfg-enpfakes0.orig'))
+        for ifcfg in ('ifcfg-enpfakes0', 'ifcfg-enpfakes0.orig',
+                      'ifcfg-br-admin'):
+            ifcfg_path = os.path.join(ifcfg_dir, ifcfg)
+            if os.path.isfile(ifcfg_path):
+                os.remove(ifcfg_path)
+
     @patch('subprocess.check_call')
     @patch('apex.network.jumphost.is_ovs_bridge', return_value=False)
     @patch('apex.network.jumphost.dump_ovs_ports', return_value=[])