associate an uuid to yardstick_key and yardstick_key.pub 05/26705/3
authorJingLu5 <lvjing5@huawei.com>
Mon, 9 Jan 2017 11:35:47 +0000 (11:35 +0000)
committerJingLu5 <lvjing5@huawei.com>
Tue, 10 Jan 2017 10:06:32 +0000 (10:06 +0000)
JIRA: YARDSTICK-527

This work is to support yardstick parallel tasks in the future.
Currently, the RSA key we generated and used to access the VM is named
'yardstick_key'.
If more than two tasks are running paralleled, the later 'yardstick_key' will
cover the former.
We want associate an uuid to identify differnets for each tasks. So the key
files won't conflict.
The first 8 digits will be used, as there is no need to used a full-length uuid.

Change-Id: If8eaf47ae527cf9b3bd50f37ab3051fbdccf5f03
Signed-off-by: JingLu5 <lvjing5@huawei.com>
tests/unit/benchmark/contexts/test_heat.py
yardstick/benchmark/contexts/heat.py
yardstick/orchestrator/heat.py

index f891b0a..dd830a4 100644 (file)
@@ -11,6 +11,7 @@
 
 # Unittest for yardstick.benchmark.contexts.heat
 
+import os
 import mock
 import unittest
 
@@ -39,6 +40,8 @@ class HeatContextTestCase(unittest.TestCase):
         self.assertIsNone(self.test_context._user)
         self.assertIsNone(self.test_context.template_file)
         self.assertIsNone(self.test_context.heat_parameters)
+        self.assertIsNotNone(self.test_context.key_uuid)
+        self.assertIsNotNone(self.test_context.key_filename)
 
     @mock.patch('yardstick.benchmark.contexts.heat.PlacementGroup')
     @mock.patch('yardstick.benchmark.contexts.heat.Network')
@@ -55,6 +58,7 @@ class HeatContextTestCase(unittest.TestCase):
 
         self.test_context.init(attrs)
 
+        self.assertEqual(self.test_context.name, "foo")
         self.assertEqual(self.test_context.keypair_name, "foo-key")
         self.assertEqual(self.test_context.secgroup_name, "foo-secgroup")
 
@@ -69,14 +73,23 @@ class HeatContextTestCase(unittest.TestCase):
         mock_server.assert_called_with('baz', self.test_context, servers['baz'])
         self.assertTrue(len(self.test_context.servers) == 1)
 
+        if os.path.exists(self.test_context.key_filename):
+            try:
+                os.remove(self.test_context.key_filename)
+                os.remove(self.test_context.key_filename + ".pub")
+            except OSError:
+                LOG.exception("key_filename: %s", e.key_filename)
+
     @mock.patch('yardstick.benchmark.contexts.heat.HeatTemplate')
     def test__add_resources_to_template_no_servers(self, mock_template):
 
         self.test_context.keypair_name = "foo-key"
         self.test_context.secgroup_name = "foo-secgroup"
+        self.test_context.key_uuid = "2f2e4997-0a8e-4eb7-9fa4-f3f8fbbc393b"
 
         self.test_context._add_resources_to_template(mock_template)
-        mock_template.add_keypair.assert_called_with("foo-key")
+        mock_template.add_keypair.assert_called_with("foo-key",
+                                                     "2f2e4997-0a8e-4eb7-9fa4-f3f8fbbc393b")
         mock_template.add_security_group.assert_called_with("foo-secgroup")
 
     @mock.patch('yardstick.benchmark.contexts.heat.HeatTemplate')
@@ -100,6 +113,7 @@ class HeatContextTestCase(unittest.TestCase):
 
         self.assertTrue(mock_template.delete.called)
 
+
     def test__get_server(self):
 
         self.mock_context.name = 'bar'
index fcbe825..166ca40 100644 (file)
@@ -9,6 +9,7 @@
 
 import os
 import sys
+import uuid
 import pkg_resources
 import paramiko
 
@@ -40,8 +41,11 @@ class HeatContext(Context):
         self._user = None
         self.template_file = None
         self.heat_parameters = None
+        # generate an uuid to identify yardstick_key
+        # the first 8 digits of the uuid will be used
+        self.key_uuid = uuid.uuid4()
         self.key_filename = YARDSTICK_ROOT_PATH + \
-            'yardstick/resources/files/yardstick_key'
+            'yardstick/resources/files/yardstick_key-' + str(self.key_uuid)[:8]
         super(self.__class__, self).__init__()
 
     def init(self, attrs):
@@ -79,16 +83,12 @@ class HeatContext(Context):
             self.servers.append(server)
             self._server_map[server.dn] = server
 
-        print "Generating RSA host key ..."
         rsa_key = paramiko.RSAKey.generate(bits=2048, progress_func=None)
-        print "Writing yardstick_key ..."
         rsa_key.write_private_key_file(self.key_filename)
-        print "Writing yardstick_key.pub ..."
         open(self.key_filename + ".pub", "w").write("%s %s\n" %
                                                     (rsa_key.get_name(),
                                                      rsa_key.get_base64()))
         del rsa_key
-        print "... done!"
 
     @property
     def image(self):
@@ -107,7 +107,7 @@ class HeatContext(Context):
 
     def _add_resources_to_template(self, template):
         '''add to the template the resources represented by this context'''
-        template.add_keypair(self.keypair_name)
+        template.add_keypair(self.keypair_name, self.key_uuid)
         template.add_security_group(self.secgroup_name)
 
         for network in self.networks:
@@ -243,7 +243,8 @@ class HeatContext(Context):
         with attribute name mapping when using external heat templates
         '''
         key_filename = pkg_resources.resource_filename(
-            'yardstick.resources', 'files/yardstick_key')
+            'yardstick.resources', 'files/yardstick_key-{:.{width}}'.format(
+                                      self.key_uuid, width=8))
 
         if type(attr_name) is dict:
             cname = attr_name["name"].split(".")[1]
index 4839455..f1104d6 100644 (file)
@@ -17,6 +17,7 @@ import logging
 import pkg_resources
 import json
 
+from oslo_utils import encodeutils
 import heatclient
 
 from yardstick.common import template_format
@@ -297,15 +298,19 @@ class HeatTemplate(HeatObject):
             }
         }
 
-    def add_keypair(self, name):
+    def add_keypair(self, name, key_uuid):
         '''add to the template a Nova KeyPair'''
         log.debug("adding Nova::KeyPair '%s'", name)
         self.resources[name] = {
             'type': 'OS::Nova::KeyPair',
             'properties': {
                 'name': name,
-                'public_key': pkg_resources.resource_string(
-                    'yardstick.resources', 'files/yardstick_key.pub')
+                'public_key': encodeutils.safe_decode(
+                    pkg_resources.resource_string(
+                        'yardstick.resources',
+                        'files/yardstick_key-{:.{width}}.pub'.format(
+                            key_uuid, width=8)),
+                    'utf-8')
             }
         }