From b8f0aedae524539e9d0d069c07662e288046bc6f Mon Sep 17 00:00:00 2001 From: JingLu5 Date: Wed, 23 Aug 2017 02:15:13 +0000 Subject: [PATCH] Add common openstack opertation scenarios: volume & floating ip JIRA: YARDSTICK-781 This patch adds some common openstack opertation scenarios Change-Id: I1300a61b389202242f112b6d280ab47746379546 Signed-off-by: JingLu5 --- .../scenarios/lib/test_delete_floating_ip.py | 36 ++++++++++++++ .../benchmark/scenarios/lib/test_delete_keypair.py | 36 ++++++++++++++ .../benchmark/scenarios/lib/test_delete_volume.py | 36 ++++++++++++++ .../benchmark/scenarios/lib/test_detach_volume.py | 35 ++++++++++++++ .../benchmark/scenarios/lib/delete_floating_ip.py | 54 +++++++++++++++++++++ .../benchmark/scenarios/lib/delete_keypair.py | 56 ++++++++++++++++++++++ yardstick/benchmark/scenarios/lib/delete_volume.py | 55 +++++++++++++++++++++ yardstick/benchmark/scenarios/lib/detach_volume.py | 54 +++++++++++++++++++++ yardstick/common/openstack_utils.py | 49 +++++++++++++++++++ 9 files changed, 411 insertions(+) create mode 100644 tests/unit/benchmark/scenarios/lib/test_delete_floating_ip.py create mode 100644 tests/unit/benchmark/scenarios/lib/test_delete_keypair.py create mode 100644 tests/unit/benchmark/scenarios/lib/test_delete_volume.py create mode 100644 tests/unit/benchmark/scenarios/lib/test_detach_volume.py create mode 100644 yardstick/benchmark/scenarios/lib/delete_floating_ip.py create mode 100644 yardstick/benchmark/scenarios/lib/delete_keypair.py create mode 100644 yardstick/benchmark/scenarios/lib/delete_volume.py create mode 100644 yardstick/benchmark/scenarios/lib/detach_volume.py diff --git a/tests/unit/benchmark/scenarios/lib/test_delete_floating_ip.py b/tests/unit/benchmark/scenarios/lib/test_delete_floating_ip.py new file mode 100644 index 000000000..7592c8070 --- /dev/null +++ b/tests/unit/benchmark/scenarios/lib/test_delete_floating_ip.py @@ -0,0 +1,36 @@ +############################################################################## +# 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.delete_floating_ip import DeleteFloatingIp + + +class DeleteFloatingIpTestCase(unittest.TestCase): + + @mock.patch('yardstick.common.openstack_utils.get_nova_client') + @mock.patch('yardstick.common.openstack_utils.delete_floating_ip') + def test_delete_floating_ip(self, mock_get_nova_client, mock_delete_floating_ip): + options = { + 'floating_ip_id': '123-123-123' + } + args = {"options": options} + obj = DeleteFloatingIp(args, {}) + obj.run({}) + self.assertTrue(mock_get_nova_client.called) + self.assertTrue(mock_delete_floating_ip.called) + + +def main(): + unittest.main() + + +if __name__ == '__main__': + main() diff --git a/tests/unit/benchmark/scenarios/lib/test_delete_keypair.py b/tests/unit/benchmark/scenarios/lib/test_delete_keypair.py new file mode 100644 index 000000000..9663fe9fb --- /dev/null +++ b/tests/unit/benchmark/scenarios/lib/test_delete_keypair.py @@ -0,0 +1,36 @@ +############################################################################## +# 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.delete_keypair import DeleteKeypair + + +class DeleteKeypairTestCase(unittest.TestCase): + + @mock.patch('yardstick.common.openstack_utils.get_nova_client') + @mock.patch('yardstick.common.openstack_utils.delete_keypair') + def test_detach_volume(self, mock_get_nova_client, mock_delete_keypair): + options = { + 'key_name': 'yardstick_key' + } + args = {"options": options} + obj = DeleteKeypair(args, {}) + obj.run({}) + self.assertTrue(mock_get_nova_client.called) + self.assertTrue(mock_delete_keypair.called) + + +def main(): + unittest.main() + + +if __name__ == '__main__': + main() diff --git a/tests/unit/benchmark/scenarios/lib/test_delete_volume.py b/tests/unit/benchmark/scenarios/lib/test_delete_volume.py new file mode 100644 index 000000000..a11d0121b --- /dev/null +++ b/tests/unit/benchmark/scenarios/lib/test_delete_volume.py @@ -0,0 +1,36 @@ +############################################################################## +# 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.delete_volume import DeleteVolume + + +class DeleteVolumeTestCase(unittest.TestCase): + + @mock.patch('yardstick.common.openstack_utils.get_cinder_client') + @mock.patch('yardstick.common.openstack_utils.delete_volume') + def test_delete_volume(self, mock_get_cinder_client, mock_delete_volume): + options = { + 'volume_id': '123-123-123' + } + args = {"options": options} + obj = DeleteVolume(args, {}) + obj.run({}) + self.assertTrue(mock_get_cinder_client.called) + self.assertTrue(mock_delete_volume.called) + + +def main(): + unittest.main() + + +if __name__ == '__main__': + main() diff --git a/tests/unit/benchmark/scenarios/lib/test_detach_volume.py b/tests/unit/benchmark/scenarios/lib/test_detach_volume.py new file mode 100644 index 000000000..0cffcba15 --- /dev/null +++ b/tests/unit/benchmark/scenarios/lib/test_detach_volume.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.detach_volume import DetachVolume + + +class DetachVolumeTestCase(unittest.TestCase): + + @mock.patch('yardstick.common.openstack_utils.detach_volume') + def test_detach_volume(self, mock_detach_volume): + options = { + 'server_id': '321-321-321', + 'volume_id': '123-123-123' + } + args = {"options": options} + obj = DetachVolume(args, {}) + obj.run({}) + self.assertTrue(mock_detach_volume.called) + + +def main(): + unittest.main() + + +if __name__ == '__main__': + main() diff --git a/yardstick/benchmark/scenarios/lib/delete_floating_ip.py b/yardstick/benchmark/scenarios/lib/delete_floating_ip.py new file mode 100644 index 000000000..4314952fb --- /dev/null +++ b/yardstick/benchmark/scenarios/lib/delete_floating_ip.py @@ -0,0 +1,54 @@ +############################################################################## +# 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 DeleteFloatingIp(base.Scenario): + """Delete an OpenStack floating ip """ + + __scenario_type__ = "DeleteFloatingIp" + + def __init__(self, scenario_cfg, context_cfg): + self.scenario_cfg = scenario_cfg + self.context_cfg = context_cfg + self.options = self.scenario_cfg['options'] + + self.floating_ip_id = self.options.get("floating_ip_id", None) + + self.nova_client = op_utils.get_nova_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() + + status = op_utils.delete_floating_ip(nova_client=self.nova_client, + floatingip_id=self.floating_ip_id) + if status: + result.update({"delete_floating_ip": 1}) + LOG.info("Delete floating ip successful!") + else: + result.update({"delete_floating_ip": 0}) + LOG.error("Delete floating ip failed!") diff --git a/yardstick/benchmark/scenarios/lib/delete_keypair.py b/yardstick/benchmark/scenarios/lib/delete_keypair.py new file mode 100644 index 000000000..135139959 --- /dev/null +++ b/yardstick/benchmark/scenarios/lib/delete_keypair.py @@ -0,0 +1,56 @@ +############################################################################## +# 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 DeleteKeypair(base.Scenario): + """Delete an OpenStack keypair""" + + __scenario_type__ = "DeleteKeypair" + + 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.nova_client = op_utils.get_nova_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() + + status = op_utils.delete_keypair(self.nova_client, + self.key_name) + + if status: + result.update({"delete_keypair": 1}) + LOG.info("Delete keypair successful!") + else: + result.update({"delete_keypair": 0}) + LOG.info("Delete keypair failed!") diff --git a/yardstick/benchmark/scenarios/lib/delete_volume.py b/yardstick/benchmark/scenarios/lib/delete_volume.py new file mode 100644 index 000000000..ea2b85812 --- /dev/null +++ b/yardstick/benchmark/scenarios/lib/delete_volume.py @@ -0,0 +1,55 @@ +############################################################################## +# 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 DeleteVolume(base.Scenario): + """Delete an OpenStack volume""" + + __scenario_type__ = "DeleteVolume" + + def __init__(self, scenario_cfg, context_cfg): + self.scenario_cfg = scenario_cfg + self.context_cfg = context_cfg + self.options = self.scenario_cfg['options'] + + self.volume_id = self.options.get("volume_id", None) + + self.cinder_client = op_utils.get_cinder_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() + + status = op_utils.delete_volume(self.cinder_client, self.volume_id) + + if status: + result.update({"delete_volume": 1}) + LOG.info("Delete volume successful!") + else: + result.update({"delete_volume": 0}) + LOG.info("Delete volume failed!") diff --git a/yardstick/benchmark/scenarios/lib/detach_volume.py b/yardstick/benchmark/scenarios/lib/detach_volume.py new file mode 100644 index 000000000..0b02a3a81 --- /dev/null +++ b/yardstick/benchmark/scenarios/lib/detach_volume.py @@ -0,0 +1,54 @@ +############################################################################## +# 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 DetachVolume(base.Scenario): + """Detach a volume from an instance""" + + __scenario_type__ = "DetachVolume" + + 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.detach_volume(self.server_id, self.volume_id) + + if status: + result.update({"detach_volume": 1}) + LOG.info("Detach volume from server successful!") + else: + result.update({"detach_volume": 0}) + LOG.info("Detach volume from server failed!") diff --git a/yardstick/common/openstack_utils.py b/yardstick/common/openstack_utils.py index 76acc9508..c862a6ba2 100644 --- a/yardstick/common/openstack_utils.py +++ b/yardstick/common/openstack_utils.py @@ -11,6 +11,7 @@ from __future__ import absolute_import import os import time +import sys import logging from keystoneauth1 import loading @@ -423,6 +424,15 @@ def delete_flavor(flavor_id): # pragma: no cover return True +def delete_keypair(nova_client, key): # pragma: no cover + try: + nova_client.keypairs.delete(key=key) + return True + except Exception: + log.exception("Error [delete_keypair(nova_client)]") + return False + + # ********************************************* # NEUTRON # ********************************************* @@ -479,6 +489,15 @@ def create_floating_ip(neutron_client, extnet_id): # pragma: no cover return {'fip_addr': fip_addr, 'fip_id': fip_id} +def delete_floating_ip(nova_client, floatingip_id): # pragma: no cover + try: + nova_client.floating_ips.delete(floatingip_id) + return True + except Exception: + log.error("Error [delete_floating_ip(nova_client, '%s')]" % floatingip_id) + return False + + def get_security_groups(neutron_client): # pragma: no cover try: security_groups = neutron_client.list_security_groups()[ @@ -667,3 +686,33 @@ def create_volume(cinder_client, volume_name, volume_size, log.exception("Error [create_volume(cinder_client, %s)]", (volume_name, volume_size)) return None + + +def delete_volume(cinder_client, volume_id, forced=False): # pragma: no cover + try: + if forced: + try: + cinder_client.volumes.detach(volume_id) + except: + log.error(sys.exc_info()[0]) + cinder_client.volumes.force_delete(volume_id) + else: + while True: + volume = get_cinder_client().volumes.get(volume_id) + if volume.status.lower() == 'available': + break + cinder_client.volumes.delete(volume_id) + return True + except Exception: + log.exception("Error [delete_volume(cinder_client, '%s')]" % volume_id) + return False + + +def detach_volume(server_id, volume_id): # pragma: no cover + try: + get_nova_client().volumes.delete_server_volume(server_id, volume_id) + return True + except Exception: + log.exception("Error [detach_server_volume(nova_client, '%s', '%s')]", + server_id, volume_id) + return False -- 2.16.6