# Copyright 2016 Andrew Kerr
# 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.

import ddt
from tempest import config
from tempest.lib.common.utils import data_utils
from tempest.lib import decorators
from testtools import testcase as tc

from manila_tempest_tests.common import constants
from manila_tempest_tests.common import waiters
from manila_tempest_tests import share_exceptions
from manila_tempest_tests.tests.api import base
from manila_tempest_tests import utils


CONF = config.CONF


@ddt.ddt
class RevertToSnapshotTest(base.BaseSharesMixedTest):

    @classmethod
    def skip_checks(cls):
        super(RevertToSnapshotTest, cls).skip_checks()
        if not CONF.share.run_revert_to_snapshot_tests:
            msg = "Revert to snapshot tests are disabled."
            raise cls.skipException(msg)
        if not CONF.share.capability_snapshot_support:
            msg = "Snapshot support is disabled."
            raise cls.skipException(msg)
        if not CONF.share.run_snapshot_tests:
            msg = "Snapshot tests are disabled."
            raise cls.skipException(msg)

        utils.check_skip_if_microversion_not_supported(
            constants.REVERT_TO_SNAPSHOT_MICROVERSION)

    @classmethod
    def resource_setup(cls):
        super(RevertToSnapshotTest, cls).resource_setup()
        cls.admin_client = cls.admin_shares_v2_client
        cls.replication_multitenancy = (
            utils.replication_with_multitenancy_support())
        pools = cls.admin_client.list_pools(detail=True)['pools']
        revert_support = [
            pool['capabilities'][constants.REVERT_TO_SNAPSHOT_SUPPORT]
            for pool in pools]
        if not any(revert_support):
            msg = "Revert to snapshot not supported."
            raise cls.skipException(msg)

        cls.share_type_name = data_utils.rand_name("share-type")
        extra_specs = {
            "snapshot_support": True,
            constants.REVERT_TO_SNAPSHOT_SUPPORT: True,
        }
        cls.revert_enabled_extra_specs = cls.add_extra_specs_to_dict(
            extra_specs=extra_specs)

        cls.share_type = cls.create_share_type(
            cls.share_type_name,
            extra_specs=cls.revert_enabled_extra_specs,
            client=cls.admin_client)

        cls.st_id = cls.share_type['id']

        cls.share = cls.create_share(share_type_id=cls.st_id)

        if CONF.share.run_replication_tests:
            # Create replicated share type
            cls.replicated_share_type_name = data_utils.rand_name("share-type")
            cls.replication_type = CONF.share.backend_replication_type
            if cls.replication_type not in constants.REPLICATION_TYPE_CHOICES:
                raise share_exceptions.ShareReplicationTypeException(
                    replication_type=cls.replication_type
                )
            extra_specs = cls.add_extra_specs_to_dict({
                "replication_type": cls.replication_type,
                "snapshot_support": True,
                constants.REVERT_TO_SNAPSHOT_SUPPORT: True,
            })
            cls.replicated_share_type = cls.create_share_type(
                cls.replicated_share_type_name,
                extra_specs=extra_specs,
                client=cls.admin_client)
            cls.zones = cls.get_availability_zones_matching_share_type(
                cls.replicated_share_type, client=cls.admin_client)
            cls.share_zone = cls.zones[0]
            cls.replica_zone = cls.zones[-1]
            cls.sn_id = None
            if cls.replication_multitenancy:
                cls.share_network = cls.shares_v2_client.get_share_network(
                    cls.shares_v2_client.share_network_id)['share_network']
                cls.sn_id = cls.share_network['id']

    @decorators.idempotent_id('196f2bc5-e13a-4730-ac51-61e339068a06')
    @tc.attr(base.TAG_POSITIVE, base.TAG_BACKEND)
    @ddt.data(
        *utils.deduplicate([constants.REVERT_TO_SNAPSHOT_MICROVERSION,
                            CONF.share.max_api_microversion])
    )
    def test_revert_to_latest_snapshot(self, version):
        snapshot = self.create_snapshot_wait_for_active(self.share['id'],
                                                        cleanup_in_class=False)
        self.shares_v2_client.revert_to_snapshot(
            self.share['id'],
            snapshot['id'],
            version=version)
        waiters.wait_for_resource_status(
            self.shares_v2_client, self.share['id'],
            constants.STATUS_AVAILABLE)
        waiters.wait_for_resource_status(
            self.shares_v2_client, snapshot['id'], constants.STATUS_AVAILABLE,
            resource_name='snapshot')

    @decorators.idempotent_id('09bd9942-7ef9-4d24-b2dd-f83bdda27b50')
    @tc.attr(base.TAG_POSITIVE, base.TAG_BACKEND)
    @ddt.data(
        *utils.deduplicate([constants.REVERT_TO_SNAPSHOT_MICROVERSION,
                            CONF.share.max_api_microversion])
    )
    def test_revert_to_previous_snapshot(self, version):
        snapshot1 = self.create_snapshot_wait_for_active(
            self.share['id'], cleanup_in_class=False)
        snapshot2 = self.create_snapshot_wait_for_active(
            self.share['id'], cleanup_in_class=False)

        self.shares_v2_client.delete_snapshot(snapshot2['id'])
        self.shares_v2_client.wait_for_resource_deletion(
            snapshot_id=snapshot2['id'])

        self.shares_v2_client.revert_to_snapshot(self.share['id'],
                                                 snapshot1['id'],
                                                 version=version)
        waiters.wait_for_resource_status(
            self.shares_v2_client, self.share['id'],
            constants.STATUS_AVAILABLE)
        waiters.wait_for_resource_status(
            self.shares_v2_client, snapshot1['id'], constants.STATUS_AVAILABLE,
            resource_name='snapshot')

    @decorators.idempotent_id('146de138-d351-49dc-a13a-5cdbed40b9ac')
    @tc.attr(base.TAG_POSITIVE, base.TAG_BACKEND)
    @tc.skipUnless(CONF.share.run_replication_tests,
                   'Replication tests are disabled.')
    @ddt.data(
        *utils.deduplicate([constants.REVERT_TO_SNAPSHOT_MICROVERSION,
                            CONF.share.max_api_microversion])
    )
    def test_revert_to_replicated_snapshot(self, version):
        """Test reverting to a replicated snapshot."""
        share = self.create_share(
            share_type_id=self.replicated_share_type['id'],
            availability_zone=self.share_zone,
            share_network_id=self.sn_id
        )

        share_replica = self.create_share_replica(share["id"],
                                                  self.replica_zone)
        waiters.wait_for_resource_status(
            self.shares_v2_client, share_replica['id'],
            constants.REPLICATION_STATE_IN_SYNC, resource_name='share_replica',
            status_attr='replica_state')

        snapshot = self.create_snapshot_wait_for_active(share["id"])

        self.shares_v2_client.revert_to_snapshot(
            share['id'],
            snapshot['id'],
            version=version)
        waiters.wait_for_resource_status(
            self.shares_v2_client, share['id'], constants.STATUS_AVAILABLE)
        waiters.wait_for_resource_status(
            self.shares_v2_client, share_replica['id'],
            constants.REPLICATION_STATE_IN_SYNC, resource_name='share_replica',
            status_attr='replica_state')
        waiters.wait_for_resource_status(
            self.shares_v2_client, snapshot['id'], constants.STATUS_AVAILABLE,
            resource_name='snapshot')
