Add common openstack opertation scenarios: volume & floating ip 27/39927/6
authorJingLu5 <lvjing5@huawei.com>
Wed, 23 Aug 2017 02:15:13 +0000 (02:15 +0000)
committerJingLu5 <lvjing5@huawei.com>
Wed, 23 Aug 2017 03:02:08 +0000 (03:02 +0000)
JIRA: YARDSTICK-781

This patch adds some common openstack opertation scenarios

Change-Id: I1300a61b389202242f112b6d280ab47746379546
Signed-off-by: JingLu5 <lvjing5@huawei.com>
tests/unit/benchmark/scenarios/lib/test_delete_floating_ip.py [new file with mode: 0644]
tests/unit/benchmark/scenarios/lib/test_delete_keypair.py [new file with mode: 0644]
tests/unit/benchmark/scenarios/lib/test_delete_volume.py [new file with mode: 0644]
tests/unit/benchmark/scenarios/lib/test_detach_volume.py [new file with mode: 0644]
yardstick/benchmark/scenarios/lib/delete_floating_ip.py [new file with mode: 0644]
yardstick/benchmark/scenarios/lib/delete_keypair.py [new file with mode: 0644]
yardstick/benchmark/scenarios/lib/delete_volume.py [new file with mode: 0644]
yardstick/benchmark/scenarios/lib/detach_volume.py [new file with mode: 0644]
yardstick/common/openstack_utils.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 (file)
index 0000000..7592c80
--- /dev/null
@@ -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 (file)
index 0000000..9663fe9
--- /dev/null
@@ -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 (file)
index 0000000..a11d012
--- /dev/null
@@ -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 (file)
index 0000000..0cffcba
--- /dev/null
@@ -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 (file)
index 0000000..4314952
--- /dev/null
@@ -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 (file)
index 0000000..1351399
--- /dev/null
@@ -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 (file)
index 0000000..ea2b858
--- /dev/null
@@ -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 (file)
index 0000000..0b02a3a
--- /dev/null
@@ -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!")
index 76acc95..c862a6b 100644 (file)
@@ -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