Add yardstick.benchmark.contexts.heat.HeatContext._delete_key_file() 49/52349/11
authorEmma Foley <emma.l.foley@intel.com>
Mon, 19 Feb 2018 16:06:50 +0000 (16:06 +0000)
committerEmma Foley <emma.l.foley@intel.com>
Fri, 2 Mar 2018 19:08:37 +0000 (19:08 +0000)
* Move logic for removing key file into its own method
* Update the log message to be more useful

JIRA: YARDSTICK-1026
Change-Id: I8c131720ed91c939698c41ad63d586396fcce1fe
Signed-off-by: Emma Foley <emma.l.foley@intel.com>
yardstick/benchmark/contexts/heat.py
yardstick/tests/unit/benchmark/contexts/test_heat.py

index 75e26e0..77ac248 100644 (file)
@@ -29,6 +29,7 @@ from yardstick.common.openstack_utils import get_neutron_client
 from yardstick.orchestrator.heat import HeatStack
 from yardstick.orchestrator.heat import HeatTemplate
 from yardstick.common import constants as consts
+from yardstick.common import utils
 from yardstick.common.utils import source_env
 from yardstick.ssh import SSH
 
@@ -402,6 +403,14 @@ class HeatContext(Context):
             "local_ip": private_ip,
         }
 
+    def _delete_key_file(self):
+        try:
+            utils.remove_file(self.key_filename)
+            utils.remove_file(self.key_filename + ".pub")
+        except OSError:
+            LOG.exception("There was an error removing the key file %s",
+                          self.key_filename)
+
     def undeploy(self):
         """undeploys stack from cloud"""
         if self._flags.no_teardown:
@@ -414,12 +423,7 @@ class HeatContext(Context):
             self.stack = None
             LOG.info("Undeploying context '%s' DONE", self.name)
 
-        if os.path.exists(self.key_filename):
-            try:
-                os.remove(self.key_filename)
-                os.remove(self.key_filename + ".pub")
-            except OSError:
-                LOG.exception("Key filename %s", self.key_filename)
+            self._delete_key_file()
 
         super(HeatContext, self).undeploy()
 
index c449b2a..f66133b 100644 (file)
@@ -355,8 +355,9 @@ class HeatContextTestCase(unittest.TestCase):
         self.assertDictEqual(server.interfaces['port_a'], expected)
 
     @mock.patch('yardstick.benchmark.contexts.heat.os')
+    @mock.patch.object(heat.HeatContext, '_delete_key_file')
     @mock.patch('yardstick.benchmark.contexts.heat.HeatTemplate')
-    def test_undeploy(self, mock_template, *args):
+    def test_undeploy(self, mock_template, mock_delete_key, *args):
         self.test_context.stack = mock_template
         self.test_context._name = 'foo'
         self.test_context._task_id = '1234567890'
@@ -365,6 +366,7 @@ class HeatContextTestCase(unittest.TestCase):
         # mock_os.path.exists.return_value = True
         self.test_context.key_filename = 'foo/bar/foobar'
         self.test_context.undeploy()
+        mock_delete_key.assert_called()
         self.assertTrue(mock_template.delete.called)
 
     @mock.patch('yardstick.benchmark.contexts.heat.HeatTemplate')