From f01dea6918048d92984be69c6d96377dbd759d46 Mon Sep 17 00:00:00 2001 From: JingLu5 Date: Fri, 11 Aug 2017 06:16:19 +0000 Subject: Add common openstack opertation scenarios: network JIRA: YARDSTICK-781 This patch adds some common openstack opertation scenarios Change-Id: I854fc435a5c951245a5997cd4e3e63c5162030af Signed-off-by: JingLu5 --- .../benchmark/scenarios/lib/test_attach_volume.py | 33 +++++++++++ .../scenarios/lib/test_create_floating_ip.py | 34 +++++++++++ .../benchmark/scenarios/lib/test_create_keypair.py | 35 +++++++++++ yardstick/benchmark/scenarios/lib/attach_volume.py | 53 +++++++++++++++++ .../benchmark/scenarios/lib/create_floating_ip.py | 60 +++++++++++++++++++ .../benchmark/scenarios/lib/create_keypair.py | 69 ++++++++++++++++++++++ yardstick/common/openstack_utils.py | 32 ++++++++++ 7 files changed, 316 insertions(+) create mode 100644 tests/unit/benchmark/scenarios/lib/test_attach_volume.py create mode 100644 tests/unit/benchmark/scenarios/lib/test_create_floating_ip.py create mode 100644 tests/unit/benchmark/scenarios/lib/test_create_keypair.py create mode 100644 yardstick/benchmark/scenarios/lib/attach_volume.py create mode 100644 yardstick/benchmark/scenarios/lib/create_floating_ip.py create mode 100644 yardstick/benchmark/scenarios/lib/create_keypair.py diff --git a/tests/unit/benchmark/scenarios/lib/test_attach_volume.py b/tests/unit/benchmark/scenarios/lib/test_attach_volume.py new file mode 100644 index 000000000..e69924072 --- /dev/null +++ b/tests/unit/benchmark/scenarios/lib/test_attach_volume.py @@ -0,0 +1,33 @@ +############################################################################## +# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +import unittest +import mock + +from yardstick.benchmark.scenarios.lib.attach_volume import AttachVolume + + +class AttachVolumeTestCase(unittest.TestCase): + + @mock.patch('yardstick.common.openstack_utils.attach_server_volume') + def test_attach_volume(self, mock_attach_server_volume): + options = { + 'volume_id': '123-456-000', + 'server_id': '000-123-456' + } + args = {"options": options} + obj = AttachVolume(args, {}) + obj.run({}) + self.assertTrue(mock_attach_server_volume.called) + +def main(): + unittest.main() + + +if __name__ == '__main__': + main() diff --git a/tests/unit/benchmark/scenarios/lib/test_create_floating_ip.py b/tests/unit/benchmark/scenarios/lib/test_create_floating_ip.py new file mode 100644 index 000000000..72dbcd7cd --- /dev/null +++ b/tests/unit/benchmark/scenarios/lib/test_create_floating_ip.py @@ -0,0 +1,34 @@ +############################################################################## +# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +import unittest +import mock + +from yardstick.benchmark.scenarios.lib.create_floating_ip import CreateFloatingIp + + +class CreateFloatingIpTestCase(unittest.TestCase): + + @mock.patch('yardstick.common.openstack_utils.create_floating_ip') + @mock.patch('yardstick.common.openstack_utils.get_network_id') + @mock.patch('yardstick.common.openstack_utils.get_neutron_client') + def test_create_floating_ip(self, mock_create_floating_ip, mock_get_network_id, mock_get_neutron_client): + options = {} + args = {"options": options} + obj = CreateFloatingIp(args, {}) + obj.run({}) + self.assertTrue(mock_create_floating_ip.called) + self.assertTrue(mock_get_network_id.called) + self.assertTrue(mock_get_neutron_client.called) + +def main(): + unittest.main() + + +if __name__ == '__main__': + main() diff --git a/tests/unit/benchmark/scenarios/lib/test_create_keypair.py b/tests/unit/benchmark/scenarios/lib/test_create_keypair.py new file mode 100644 index 000000000..99e6b9afa --- /dev/null +++ b/tests/unit/benchmark/scenarios/lib/test_create_keypair.py @@ -0,0 +1,35 @@ +############################################################################## +# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +import unittest +import mock +import paramiko + +from yardstick.benchmark.scenarios.lib.create_keypair import CreateKeypair + + +class CreateKeypairTestCase(unittest.TestCase): + + @mock.patch('yardstick.common.openstack_utils.create_keypair') + def test_create_keypair(self, mock_create_keypair): + options = { + 'key_name': 'yardstick_key', + 'key_path': '/tmp/yardstick_key' + } + args = {"options": options} + obj = CreateKeypair(args, {}) + obj.run({}) + self.assertTrue(mock_create_keypair.called) + + +def main(): + unittest.main() + + +if __name__ == '__main__': + main() diff --git a/yardstick/benchmark/scenarios/lib/attach_volume.py b/yardstick/benchmark/scenarios/lib/attach_volume.py new file mode 100644 index 000000000..88124964b --- /dev/null +++ b/yardstick/benchmark/scenarios/lib/attach_volume.py @@ -0,0 +1,53 @@ +############################################################################## +# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +from __future__ import print_function +from __future__ import absolute_import + +import logging + +from yardstick.benchmark.scenarios import base +import yardstick.common.openstack_utils as op_utils + +LOG = logging.getLogger(__name__) + + +class AttachVolume(base.Scenario): + """Attach a volmeu to an instance""" + + __scenario_type__ = "AttachVolume" + + def __init__(self, scenario_cfg, context_cfg): + self.scenario_cfg = scenario_cfg + self.context_cfg = context_cfg + self.options = self.scenario_cfg['options'] + + self.server_id = self.options.get("server_id", "TestServer") + self.volume_id = self.options.get("volume_id", None) + + self.setup_done = False + + def setup(self): + """scenario setup""" + + self.setup_done = True + + def run(self, result): + """execute the test""" + + if not self.setup_done: + self.setup() + + status = op_utils.attach_server_volume(self.server_id, + self.volume_id) + + if status: + LOG.info("Attach volume to server successful!") + else: + LOG.info("Attach volume to server failed!") diff --git a/yardstick/benchmark/scenarios/lib/create_floating_ip.py b/yardstick/benchmark/scenarios/lib/create_floating_ip.py new file mode 100644 index 000000000..328566d48 --- /dev/null +++ b/yardstick/benchmark/scenarios/lib/create_floating_ip.py @@ -0,0 +1,60 @@ +############################################################################## +# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +from __future__ import print_function +from __future__ import absolute_import + +import logging +import os + +from yardstick.benchmark.scenarios import base +import yardstick.common.openstack_utils as op_utils + +LOG = logging.getLogger(__name__) + + +class CreateFloatingIp(base.Scenario): + """Create an OpenStack floating ip""" + + __scenario_type__ = "CreateFloatingIp" + + def __init__(self, scenario_cfg, context_cfg): + self.scenario_cfg = scenario_cfg + self.context_cfg = context_cfg + self.ext_net_id = os.getenv("EXTERNAL_NETWORK", "external") + + self.neutron_client = op_utils.get_neutron_client() + self.setup_done = False + + def setup(self): + """scenario setup""" + + self.setup_done = True + + def run(self, result): + """execute the test""" + + if not self.setup_done: + self.setup() + + net_id = op_utils.get_network_id(self.neutron_client, self.ext_net_id) + floating_info = op_utils.create_floating_ip(self.neutron_client, + extnet_id=net_id) + if floating_info: + LOG.info("Creating floating ip successful!") + else: + LOG.error("Creating floating ip failed!") + + try: + keys = self.scenario_cfg.get('output', '').split() + except KeyError: + pass + else: + values = [floating_info["fip_id"], floating_info["fip_addr"]] + return self._push_to_outputs(keys, values) diff --git a/yardstick/benchmark/scenarios/lib/create_keypair.py b/yardstick/benchmark/scenarios/lib/create_keypair.py new file mode 100644 index 000000000..5610de651 --- /dev/null +++ b/yardstick/benchmark/scenarios/lib/create_keypair.py @@ -0,0 +1,69 @@ +############################################################################## +# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +from __future__ import print_function +from __future__ import absolute_import + +import logging +import paramiko + +from yardstick.benchmark.scenarios import base +import yardstick.common.openstack_utils as op_utils + +LOG = logging.getLogger(__name__) + + +class CreateKeypair(base.Scenario): + """Create an OpenStack keypair""" + + __scenario_type__ = "CreateKeypair" + + def __init__(self, scenario_cfg, context_cfg): + self.scenario_cfg = scenario_cfg + self.context_cfg = context_cfg + self.options = self.scenario_cfg['options'] + + self.key_name = self.options.get("key_name", "yardstick_key") + self.key_filename = self.options.get("key_path", "/tmp/yardstick_key") + + self.setup_done = False + + def setup(self): + """scenario setup""" + + self.setup_done = True + + def run(self, result): + """execute the test""" + + if not self.setup_done: + self.setup() + + rsa_key = paramiko.RSAKey.generate(bits=2048, progress_func=None) + rsa_key.write_private_key_file(self.key_filename) + print("Writing %s ..." % self.key_filename) + with open(self.key_filename + ".pub", "w") as pubkey_file: + pubkey_file.write( + "%s %s\n" % (rsa_key.get_name(), rsa_key.get_base64())) + del rsa_key + + keypair = op_utils.create_keypair(self.key_name, + self.key_filename + ".pub") + + if keypair: + LOG.info("Create keypair successful!") + else: + LOG.info("Create keypair failed!") + try: + keys = self.scenario_cfg.get('output', '').split() + except KeyError: + pass + else: + values = [keypair.id] + return self._push_to_outputs(keys, values) diff --git a/yardstick/common/openstack_utils.py b/yardstick/common/openstack_utils.py index 51be6882a..c95c91bfe 100644 --- a/yardstick/common/openstack_utils.py +++ b/yardstick/common/openstack_utils.py @@ -264,6 +264,15 @@ def create_aggregate_with_host(nova_client, aggregate_name, av_zone, return True +def create_keypair(nova_client, name, key_path=None): # pragma: no cover + try: + with open(key_path) as fpubkey: + keypair = get_nova_client().keypairs.create(name=name, public_key=fpubkey.read()) + return keypair + except Exception: + log.exception("Error [create_keypair(nova_client)]") + + def create_instance(json_body): # pragma: no cover try: return get_nova_client().servers.create(**json_body) @@ -290,6 +299,17 @@ def create_instance_and_wait_for_active(json_body): # pragma: no cover return None +def attach_server_volume(server_id, volume_id, device=None): # pragma: no cover + try: + get_nova_client().volumes.create_server_volume(server_id, volume_id, device) + except Exception: + log.exception("Error [attach_server_volume(nova_client, '%s', '%s')]", + server_id, volume_id) + return False + else: + return True + + def delete_instance(nova_client, instance_id): # pragma: no cover try: nova_client.servers.force_delete(instance_id) @@ -417,6 +437,18 @@ def get_port_id_by_ip(neutron_client, ip_address): # pragma: no cover 'fixed_ips') if j['ip_address'] == ip_address), None) +def create_floating_ip(neutron_client, extnet_id): # pragma: no cover + props = {'floating_network_id': extnet_id} + try: + ip_json = neutron_client.create_floatingip({'floatingip': props}) + fip_addr = ip_json['floatingip']['floating_ip_address'] + fip_id = ip_json['floatingip']['id'] + except Exception: + log.error("Error [create_floating_ip(neutron_client)]") + return None + return {'fip_addr': fip_addr, 'fip_id': fip_id} + + # ********************************************* # GLANCE # ********************************************* -- cgit 1.2.3-korg