#    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.compute import base
from tempest.common import waiters
from tempest import config
from tempest.lib import decorators
from tempest import test

CONF = config.CONF


class TestVolumeSwap(base.BaseV2ComputeAdminTest):
    """The test suite for swapping of volume with admin user.

    The following is the scenario outline:
    1. Create a volume "volume1" with non-admin.
    2. Create a volume "volume2" with non-admin.
    3. Boot an instance "instance1" with non-admin.
    4. Attach "volume1" to "instance1" with non-admin.
    5. Swap volume from "volume1" to "volume2" as admin.
    6. Check the swap volume is successful and "volume2"
       is attached to "instance1" and "volume1" is in available state.
    """

    @classmethod
    def skip_checks(cls):
        super(TestVolumeSwap, cls).skip_checks()
        if not CONF.compute_feature_enabled.swap_volume:
            raise cls.skipException("Swapping volumes is not supported.")

    @decorators.idempotent_id('1769f00d-a693-4d67-a631-6a3496773813')
    @test.services('volume')
    def test_volume_swap(self):
        # Create two volumes.
        # NOTE(gmann): Volumes are created before server creation so that
        # volumes cleanup can happen successfully irrespective of which volume
        # is attached to server.
        volume1 = self.create_volume()
        volume2 = self.create_volume()
        # Boot server
        server = self.create_test_server(wait_until='ACTIVE')
        # Attach "volume1" to server
        self.attach_volume(server, volume1)
        # Swap volume from "volume1" to "volume2"
        self.admin_servers_client.update_attached_volume(
            server['id'], volume1['id'], volumeId=volume2['id'])
        waiters.wait_for_volume_resource_status(self.volumes_client,
                                                volume1['id'], 'available')
        waiters.wait_for_volume_resource_status(self.volumes_client,
                                                volume2['id'], 'in-use')
        self.addCleanup(self.servers_client.detach_volume,
                        server['id'], volume2['id'])
        # Verify "volume2" is attached to the server
        vol_attachments = self.servers_client.list_volume_attachments(
            server['id'])['volumeAttachments']
        self.assertEqual(1, len(vol_attachments))
        self.assertIn(volume2['id'], vol_attachments[0]['volumeId'])

        # TODO(mriedem): Test swapping back from volume2 to volume1 after
        # nova bug 1490236 is fixed.

@@ -1294,16 +1294,201 @@
  state.%0A
+    7. Swap volume from %22volume2%22 to %22volume1%22 as admin.%0A    8. Check the swap volume is successful and %22volume1%22%0A       is attached to %22instance1%22 and %22volume2%22 is in available state.%0A
     %22%22%22%0A
@@ -2766,120 +2766,8 @@
 e')%0A
-        self.addCleanup(self.servers_client.detach_volume,%0A                        server%5B'id'%5D, volume2%5B'id'%5D)%0A
     
@@ -2807,32 +2807,32 @@
 d to the server%0A
+
         vol_atta
@@ -3067,105 +3067,744 @@
   # 
-TODO(mriedem): Test swapping back from volume2 to volume1 after%0A        # nova bug 1490236 is fixed.
+Swap volume from %22volume2%22 to %22volume1%22%0A        self.admin_servers_client.update_attached_volume(%0A            server%5B'id'%5D, volume2%5B'id'%5D, volumeId=volume1%5B'id'%5D)%0A        waiters.wait_for_volume_resource_status(self.volumes_client,%0A                                                volume2%5B'id'%5D, 'available')%0A        waiters.wait_for_volume_resource_status(self.volumes_client,%0A                                                volume1%5B'id'%5D, 'in-use')%0A        # Verify %22volume1%22 is attached to the server%0A        vol_attachments = self.servers_client.list_volume_attachments(%0A            server%5B'id'%5D)%5B'volumeAttachments'%5D%0A        self.assertEqual(1, len(vol_attachments))%0A        self.assertIn(volume1%5B'id'%5D, vol_attachments%5B0%5D%5B'volumeId'%5D)
 %0A

Extend compute API admin test for swap volume