# Copyright (c) 2016 Hitachi Data Systems, Inc.
# 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 copy
import ddt
import mock

from manila.common import constants
from manila import context
from manila import db
from manila import exception
from manila.share import snapshot_access
from manila import test
from manila.tests import db_utils
from manila import utils


@ddt.ddt
class SnapshotAccessTestCase(test.TestCase):
    def setUp(self):
        super(SnapshotAccessTestCase, self).setUp()
        self.driver = self.mock_class("manila.share.driver.ShareDriver",
                                      mock.Mock())
        self.snapshot_access = snapshot_access.ShareSnapshotInstanceAccess(
            db, self.driver)
        self.context = context.get_admin_context()
        share = db_utils.create_share()
        self.snapshot = db_utils.create_snapshot(share_id=share['id'])
        self.snapshot_instance = db_utils.create_snapshot_instance(
            snapshot_id=self.snapshot['id'],
            share_instance_id=self.snapshot['share']['instance']['id'])

    @ddt.data(constants.ACCESS_STATE_QUEUED_TO_APPLY,
              constants.ACCESS_STATE_QUEUED_TO_DENY)
    def test_update_access_rules(self, state):

        rules = []
        for i in range(2):
            rules.append({
                'id': 'id-%s' % i,
                'state': state,
                'access_id': 'rule_id%s' % i
            })
        all_rules = copy.deepcopy(rules)
        all_rules.append({
            'id': 'id-3',
            'state': constants.ACCESS_STATE_ERROR,
            'access_id': 'rule_id3'
        })

        snapshot_instance_get = self.mock_object(
            db, 'share_snapshot_instance_get',
            mock.Mock(return_value=self.snapshot_instance))

        snap_get_all_for_snap_instance = self.mock_object(
            db, 'share_snapshot_access_get_all_for_snapshot_instance',
            mock.Mock(return_value=all_rules))

        self.mock_object(db, 'share_snapshot_instance_access_update')
        self.mock_object(self.driver, 'snapshot_update_access')
        self.mock_object(self.snapshot_access, '_check_needs_refresh',
                         mock.Mock(return_value=False))
        self.mock_object(db, 'share_snapshot_instance_access_delete')

        self.snapshot_access.update_access_rules(self.context,
                                                 self.snapshot_instance['id'])

        snapshot_instance_get.assert_called_once_with(
            utils.IsAMatcher(context.RequestContext),
            self.snapshot_instance['id'], with_share_data=True)
        snap_get_all_for_snap_instance.assert_called_once_with(
            utils.IsAMatcher(context.RequestContext),
            self.snapshot_instance['id'])
        if state == constants.ACCESS_STATE_QUEUED_TO_APPLY:
            self.driver.snapshot_update_access.assert_called_once_with(
                utils.IsAMatcher(context.RequestContext),
                self.snapshot_instance, rules, add_rules=rules,
                delete_rules=[], share_server=None)
        else:
            self.driver.snapshot_update_access.assert_called_once_with(
                utils.IsAMatcher(context.RequestContext),
                self.snapshot_instance, [], add_rules=[],
                delete_rules=rules, share_server=None)

    def test_update_access_rules_delete_all_rules(self):

        rules = []
        for i in range(2):
            rules.append({
                'id': 'id-%s' % i,
                'state': constants.ACCESS_STATE_QUEUED_TO_DENY,
                'access_id': 'rule_id%s' % i
            })

        snapshot_instance_get = self.mock_object(
            db, 'share_snapshot_instance_get',
            mock.Mock(return_value=self.snapshot_instance))

        snap_get_all_for_snap_instance = self.mock_object(
            db, 'share_snapshot_access_get_all_for_snapshot_instance',
            mock.Mock(side_effect=[rules, []]))

        self.mock_object(db, 'share_snapshot_instance_access_update')
        self.mock_object(self.driver, 'snapshot_update_access')
        self.mock_object(db, 'share_snapshot_instance_access_delete')

        self.snapshot_access.update_access_rules(self.context,
                                                 self.snapshot_instance['id'],
                                                 delete_all_rules=True)

        snapshot_instance_get.assert_called_once_with(
            utils.IsAMatcher(context.RequestContext),
            self.snapshot_instance['id'], with_share_data=True)
        snap_get_all_for_snap_instance.assert_called_with(
            utils.IsAMatcher(context.RequestContext),
            self.snapshot_instance['id'])
        self.driver.snapshot_update_access.assert_called_with(
            utils.IsAMatcher(context.RequestContext), self.snapshot_instance,
            [], add_rules=[], delete_rules=rules, share_server=None)

    def test_update_access_rules_exception(self):

        rules = []
        for i in range(2):
            rules.append({
                'id': 'id-%s' % i,
                'state': constants.ACCESS_STATE_APPLYING,
                'access_id': 'rule_id%s' % i
            })

        snapshot_instance_get = self.mock_object(
            db, 'share_snapshot_instance_get',
            mock.Mock(return_value=self.snapshot_instance))

        snap_get_all_for_snap_instance = self.mock_object(
            db, 'share_snapshot_access_get_all_for_snapshot_instance',
            mock.Mock(return_value=rules))

        self.mock_object(db, 'share_snapshot_instance_access_update')
        self.mock_object(self.driver, 'snapshot_update_access',
                         mock.Mock(side_effect=exception.NotFound))

        self.assertRaises(exception.NotFound,
                          self.snapshot_access.update_access_rules,
                          self.context, self.snapshot_instance['id'])

        snapshot_instance_get.assert_called_once_with(
            utils.IsAMatcher(context.RequestContext),
            self.snapshot_instance['id'], with_share_data=True)
        snap_get_all_for_snap_instance.assert_called_once_with(
            utils.IsAMatcher(context.RequestContext),
            self.snapshot_instance['id'])

        self.driver.snapshot_update_access.assert_called_once_with(
            utils.IsAMatcher(context.RequestContext), self.snapshot_instance,
            rules, add_rules=rules, delete_rules=[], share_server=None)

[((45, 5, 46, 52), 'ddt.data', 'ddt.data', ({(45, 14, 45, 52): 'constants.ACCESS_STATE_QUEUED_TO_APPLY', (46, 14, 46, 51): 'constants.ACCESS_STATE_QUEUED_TO_DENY'}, {}), '(constants.ACCESS_STATE_QUEUED_TO_APPLY, constants.\n    ACCESS_STATE_QUEUED_TO_DENY)', False, 'import ddt\n'), ((36, 31, 37, 28), 'manila.share.snapshot_access.ShareSnapshotInstanceAccess', 'snapshot_access.ShareSnapshotInstanceAccess', ({(37, 12, 37, 14): 'db', (37, 16, 37, 27): 'self.driver'}, {}), '(db, self.driver)', False, 'from manila.share import snapshot_access\n'), ((38, 23, 38, 50), 'manila.context.get_admin_context', 'context.get_admin_context', ({}, {}), '()', False, 'from manila import context\n'), ((39, 16, 39, 39), 'manila.tests.db_utils.create_share', 'db_utils.create_share', ({}, {}), '()', False, 'from manila.tests import db_utils\n'), ((40, 24, 40, 70), 'manila.tests.db_utils.create_snapshot', 'db_utils.create_snapshot', (), '', False, 'from manila.tests import db_utils\n'), ((41, 33, 43, 71), 'manila.tests.db_utils.create_snapshot_instance', 'db_utils.create_snapshot_instance', (), '', False, 'from manila.tests import db_utils\n'), ((56, 20, 56, 40), 'copy.deepcopy', 'copy.deepcopy', ({(56, 34, 56, 39): 'rules'}, {}), '(rules)', False, 'import copy\n'), ((35, 38, 35, 49), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((65, 12, 65, 58), 'mock.Mock', 'mock.Mock', (), '', False, 'import mock\n'), ((69, 12, 69, 45), 'mock.Mock', 'mock.Mock', (), '', False, 'import mock\n'), ((74, 25, 74, 54), 'mock.Mock', 'mock.Mock', (), '', False, 'import mock\n'), ((81, 12, 81, 52), 'manila.utils.IsAMatcher', 'utils.IsAMatcher', ({(81, 29, 81, 51): 'context.RequestContext'}, {}), '(context.RequestContext)', False, 'from manila import utils\n'), ((84, 12, 84, 52), 'manila.utils.IsAMatcher', 'utils.IsAMatcher', ({(84, 29, 84, 51): 'context.RequestContext'}, {}), '(context.RequestContext)', False, 'from manila import utils\n'), ((109, 12, 109, 58), 'mock.Mock', 'mock.Mock', (), '', False, 'import mock\n'), ((113, 12, 113, 46), 'mock.Mock', 'mock.Mock', (), '', False, 'import mock\n'), ((124, 12, 124, 52), 'manila.utils.IsAMatcher', 'utils.IsAMatcher', ({(124, 29, 124, 51): 'context.RequestContext'}, {}), '(context.RequestContext)', False, 'from manila import utils\n'), ((127, 12, 127, 52), 'manila.utils.IsAMatcher', 'utils.IsAMatcher', ({(127, 29, 127, 51): 'context.RequestContext'}, {}), '(context.RequestContext)', False, 'from manila import utils\n'), ((130, 12, 130, 52), 'manila.utils.IsAMatcher', 'utils.IsAMatcher', ({(130, 29, 130, 51): 'context.RequestContext'}, {}), '(context.RequestContext)', False, 'from manila import utils\n'), ((145, 12, 145, 58), 'mock.Mock', 'mock.Mock', (), '', False, 'import mock\n'), ((149, 12, 149, 41), 'mock.Mock', 'mock.Mock', (), '', False, 'import mock\n'), ((153, 25, 153, 66), 'mock.Mock', 'mock.Mock', (), '', False, 'import mock\n'), ((160, 12, 160, 52), 'manila.utils.IsAMatcher', 'utils.IsAMatcher', ({(160, 29, 160, 51): 'context.RequestContext'}, {}), '(context.RequestContext)', False, 'from manila import utils\n'), ((163, 12, 163, 52), 'manila.utils.IsAMatcher', 'utils.IsAMatcher', ({(163, 29, 163, 51): 'context.RequestContext'}, {}), '(context.RequestContext)', False, 'from manila import utils\n'), ((167, 12, 167, 52), 'manila.utils.IsAMatcher', 'utils.IsAMatcher', ({(167, 29, 167, 51): 'context.RequestContext'}, {}), '(context.RequestContext)', False, 'from manila import utils\n'), ((88, 16, 88, 56), 'manila.utils.IsAMatcher', 'utils.IsAMatcher', ({(88, 33, 88, 55): 'context.RequestContext'}, {}), '(context.RequestContext)', False, 'from manila import utils\n'), ((93, 16, 93, 56), 'manila.utils.IsAMatcher', 'utils.IsAMatcher', ({(93, 33, 93, 55): 'context.RequestContext'}, {}), '(context.RequestContext)', False, 'from manila import utils\n')]