<NME> test_volumes_actions.py
<BEF> # Copyright 2013 Huawei Technologies Co.,LTD
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from tempest.api.volume import base
from tempest.common.utils import data_utils as utils
from tempest.common import waiters
from tempest import config
from tempest import test

from tempest.lib import decorators

CONF = config.CONF


class VolumesActionsTest(base.BaseVolumeAdminTest):
    """Test volume actions"""

    create_default_network = True

    def _create_reset_and_force_delete_temp_volume(self, status=None):
        # Create volume, reset volume status, and force delete temp volume
        super(VolumesActionsV2Test, cls).resource_setup()

        # Create a test shared volume for tests
        vol_name = utils.rand_name(cls.__name__ + '-Volume')
        cls.name_field = cls.special_fields['name_field']
        params = {cls.name_field: vol_name, 'size': CONF.volume.volume_size}

        cls.volume = cls.client.create_volume(**params)['volume']
        waiters.wait_for_volume_status(cls.client,
                                       cls.volume['id'], 'available')

    @classmethod
    def resource_cleanup(cls):
        # Delete the test volume
        cls.delete_volume(cls.client, cls.volume['id'])

        super(VolumesActionsV2Test, cls).resource_cleanup()

    def _reset_volume_status(self, volume_id, status):
        # Reset the volume status
        body = self.admin_volume_client.reset_volume_status(volume_id,
                                                            status=status)
        return body

    def tearDown(self):
        # Set volume's status to available after test
        self._reset_volume_status(self.volume['id'], status='available')
        super(VolumesActionsV2Test, self).tearDown()

    def _create_temp_volume(self):
        # Create a temp volume for force delete tests
        vol_name = utils.rand_name(self.__class__.__name__ + '-Volume')
        params = {self.name_field: vol_name, 'size': CONF.volume.volume_size}
        temp_volume = self.client.create_volume(**params)['volume']
        waiters.wait_for_volume_status(self.client,
                                       temp_volume['id'], 'available')

        return temp_volume

    def _create_reset_and_force_delete_temp_volume(self, status=None):
        # Create volume, reset volume status, and force delete temp volume
        temp_volume = self._create_temp_volume()
        if status:
            self._reset_volume_status(temp_volume['id'], status)
        self.admin_volume_client.force_delete_volume(temp_volume['id'])
        for status in ['error', 'available', 'maintenance']:
            self.admin_volume_client.reset_volume_status(
                volume['id'], status=status)
            waiters.wait_for_volume_resource_status(
                self.volumes_client, volume['id'], status)

    @decorators.idempotent_id('21737d5a-92f2-46d7-b009-a0cc0ee7a570')
    def test_volume_force_delete_when_volume_is_creating(self):
        """Test force deleting volume when its status is creating"""
        self._create_reset_and_force_delete_temp_volume('creating')

    @decorators.idempotent_id('db8d607a-aa2e-4beb-b51d-d4005c232011')
    def test_volume_force_delete_when_volume_is_attaching(self):
        """Test force deleting volume when its status is attaching"""
        self._create_reset_and_force_delete_temp_volume('attaching')

    @decorators.idempotent_id('3e33a8a8-afd4-4d64-a86b-c27a185c5a4a')
    def test_volume_force_delete_when_volume_is_error(self):
        """Test force deleting volume when its status is error"""
        self._create_reset_and_force_delete_temp_volume('error')

    @decorators.idempotent_id('b957cabd-1486-4e21-90cf-a9ed3c39dfb2')
    def test_volume_force_delete_when_volume_is_maintenance(self):
        """Test force deleting volume when its status is maintenance"""
        self._create_reset_and_force_delete_temp_volume('maintenance')

    @decorators.idempotent_id('d38285d9-929d-478f-96a5-00e66a115b81')
    @utils.services('compute')
    def test_force_detach_volume(self):
        """Test force detaching volume when its status is error"""
        # Create a server and a volume
        server_id = self.create_server()['id']
        volume_id = self.create_volume()['id']

        # Attach volume
        self.volumes_client.attach_volume(
            volume_id,
            instance_uuid=server_id,
            mountpoint='/dev/%s' % CONF.compute.volume_device_name)
        waiters.wait_for_volume_resource_status(self.volumes_client,
                                                volume_id, 'in-use')
        self.addCleanup(waiters.wait_for_volume_resource_status,
                        self.volumes_client, volume_id, 'available')
        self.addCleanup(self.volumes_client.detach_volume, volume_id)
        attachment = self.volumes_client.show_volume(
            volume_id)['volume']['attachments'][0]

        # Reset volume's status to error
        self.admin_volume_client.reset_volume_status(volume_id, status='error')
        waiters.wait_for_volume_resource_status(self.volumes_client,
                                                volume_id, 'error')

        # Force detach volume
        self.admin_volume_client.force_detach_volume(
            volume_id, connector=None,
            attachment_id=attachment['attachment_id'])
        waiters.wait_for_volume_resource_status(self.volumes_client,
                                                volume_id, 'available')
        vol_info = self.volumes_client.show_volume(volume_id)['volume']
        self.assertEmpty(vol_info['attachments'])

<MSG> Use common func create_volume in test_volumes_actions

The multi lines to create test volume can be replaced by one
call to base.create_volume to keep code clean.
Besides, _reset_volume_status's return value is never used and
so can be removed.

Change-Id: I526f8940c4f29a93f25f896bc4779a771f4c3eb5

<DFF> @@ -14,8 +14,6 @@
 #    under the License.
 
 from tempest.api.volume import base
-from tempest.common.utils import data_utils as utils
-from tempest.common import waiters
 from tempest import config
 from tempest import test
 
@@ -34,45 +32,20 @@ class VolumesActionsV2Test(base.BaseVolumeAdminTest):
         super(VolumesActionsV2Test, cls).resource_setup()
 
         # Create a test shared volume for tests
-        vol_name = utils.rand_name(cls.__name__ + '-Volume')
-        cls.name_field = cls.special_fields['name_field']
-        params = {cls.name_field: vol_name, 'size': CONF.volume.volume_size}
-
-        cls.volume = cls.client.create_volume(**params)['volume']
-        waiters.wait_for_volume_status(cls.client,
-                                       cls.volume['id'], 'available')
-
-    @classmethod
-    def resource_cleanup(cls):
-        # Delete the test volume
-        cls.delete_volume(cls.client, cls.volume['id'])
-
-        super(VolumesActionsV2Test, cls).resource_cleanup()
+        cls.volume = cls.create_volume()
 
     def _reset_volume_status(self, volume_id, status):
         # Reset the volume status
-        body = self.admin_volume_client.reset_volume_status(volume_id,
-                                                            status=status)
-        return body
+        self.admin_volume_client.reset_volume_status(volume_id, status=status)
 
     def tearDown(self):
         # Set volume's status to available after test
         self._reset_volume_status(self.volume['id'], status='available')
         super(VolumesActionsV2Test, self).tearDown()
 
-    def _create_temp_volume(self):
-        # Create a temp volume for force delete tests
-        vol_name = utils.rand_name(self.__class__.__name__ + '-Volume')
-        params = {self.name_field: vol_name, 'size': CONF.volume.volume_size}
-        temp_volume = self.client.create_volume(**params)['volume']
-        waiters.wait_for_volume_status(self.client,
-                                       temp_volume['id'], 'available')
-
-        return temp_volume
-
     def _create_reset_and_force_delete_temp_volume(self, status=None):
         # Create volume, reset volume status, and force delete temp volume
-        temp_volume = self._create_temp_volume()
+        temp_volume = self.create_volume()
         if status:
             self._reset_volume_status(temp_volume['id'], status)
         self.admin_volume_client.force_delete_volume(temp_volume['id'])

{'addition_count': 3, 'commit_subject': 'Use common func create_volume in test_volumes_actions', 'deletion_count': 30, 'file_extension': '.py', 'lang': 'py', 'license': 'apache-2.0', 'repo_name': 'openstack/tempest'}