Merge "Adding latency test for vfw"
[yardstick.git] / yardstick / benchmark / scenarios / lib / create_keypair.py
1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 from __future__ import print_function
11 from __future__ import absolute_import
12
13 import logging
14 import paramiko
15
16 from yardstick.benchmark.scenarios import base
17 import yardstick.common.openstack_utils as op_utils
18
19 LOG = logging.getLogger(__name__)
20
21
22 class CreateKeypair(base.Scenario):
23     """Create an OpenStack keypair"""
24
25     __scenario_type__ = "CreateKeypair"
26
27     def __init__(self, scenario_cfg, context_cfg):
28         self.scenario_cfg = scenario_cfg
29         self.context_cfg = context_cfg
30         self.options = self.scenario_cfg['options']
31
32         self.key_name = self.options.get("key_name", "yardstick_key")
33         self.key_filename = self.options.get("key_path", "/tmp/yardstick_key")
34
35         self.setup_done = False
36
37     def setup(self):
38         """scenario setup"""
39
40         self.setup_done = True
41
42     def run(self, result):
43         """execute the test"""
44
45         if not self.setup_done:
46             self.setup()
47
48         rsa_key = paramiko.RSAKey.generate(bits=2048, progress_func=None)
49         rsa_key.write_private_key_file(self.key_filename)
50         print("Writing %s ..." % self.key_filename)
51         with open(self.key_filename + ".pub", "w") as pubkey_file:
52             pubkey_file.write(
53                 "%s %s\n" % (rsa_key.get_name(), rsa_key.get_base64()))
54         del rsa_key
55
56         keypair = op_utils.create_keypair(self.key_name,
57                                           self.key_filename + ".pub")
58
59         if keypair:
60             result.update({"keypair_create": 1})
61             LOG.info("Create keypair successful!")
62         else:
63             result.update({"keypair_create": 0})
64             LOG.info("Create keypair failed!")
65         try:
66             keys = self.scenario_cfg.get('output', '').split()
67         except KeyError:
68             pass
69         else:
70             values = [keypair.id]
71             return self._push_to_outputs(keys, values)