#    Copyright 2012 OpenStack Foundation
#
#    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 testtools
from mockito import mock, when, unstub, any, verify, never, times
from mock import Mock
from trove.quota.quota import DbQuotaDriver
from trove.quota.models import Resource
from trove.quota.models import Quota
from trove.quota.models import QuotaUsage
from trove.quota.models import Reservation
from trove.db.models import DatabaseModelBase
from trove.extensions.mgmt.quota.service import QuotaController
from trove.common import exception
from trove.common import cfg
from trove.quota.quota import run_with_quotas
from trove.quota.quota import QUOTAS
"""
Unit tests for the classes and functions in DbQuotaDriver.py.
"""

CONF = cfg.CONF
resources = {
    Resource.INSTANCES: Resource(Resource.INSTANCES, 'max_instances_per_user'),
    Resource.VOLUMES: Resource(Resource.VOLUMES, 'max_volumes_per_user'),
}

FAKE_TENANT1 = "123456"
FAKE_TENANT2 = "654321"


class Run_with_quotasTest(testtools.TestCase):

    def setUp(self):
        super(Run_with_quotasTest, self).setUp()
        self.quota_reserve_orig = QUOTAS.reserve
        self.quota_rollback_orig = QUOTAS.rollback
        self.quota_commit_orig = QUOTAS.commit
        QUOTAS.reserve = Mock()
        QUOTAS.rollback = Mock()
        QUOTAS.commit = Mock()

    def tearDown(self):
        super(Run_with_quotasTest, self).tearDown()
        QUOTAS.reserve = self.quota_reserve_orig
        QUOTAS.rollback = self.quota_rollback_orig
        QUOTAS.commit = self.quota_commit_orig

    def test_run_with_quotas(self):

        f = Mock()
        run_with_quotas(FAKE_TENANT1, {'instances': 1, 'volumes': 5}, f)

        self.assertTrue(QUOTAS.reserve.called)
        self.assertTrue(QUOTAS.commit.called)
        self.assertFalse(QUOTAS.rollback.called)
        self.assertTrue(f.called)

    def test_run_with_quotas_error(self):

        f = Mock(side_effect=Exception())

        self.assertRaises(Exception, run_with_quotas, FAKE_TENANT1,
                          {'instances': 1, 'volumes': 5}, f)
        self.assertTrue(QUOTAS.reserve.called)
        self.assertTrue(QUOTAS.rollback.called)
        self.assertFalse(QUOTAS.commit.called)
        self.assertTrue(f.called)


class QuotaControllerTest(testtools.TestCase):

    def setUp(self):
        super(QuotaControllerTest, self).setUp()
        context = mock()
        context.is_admin = True
        req = mock()
        req.environ = mock()
        when(req.environ).get(any()).thenReturn(context)
        self.req = req
        self.controller = QuotaController()

    def tearDown(self):
        super(QuotaControllerTest, self).tearDown()
        unstub()

    def test_update_unknown_resource(self):
        body = {'quotas': {'unknown_resource': 5}}
        self.assertRaises(exception.QuotaResourceUnknown,
                          self.controller.update, self.req, body,
                          FAKE_TENANT1, FAKE_TENANT2)

    def test_update_resource_no_value(self):
        quota = mock(Quota)
        when(DatabaseModelBase).find_by(tenant_id=FAKE_TENANT2,
                                        resource='instances').thenReturn(quota)
        body = {'quotas': {'instances': None}}
        result = self.controller.update(self.req, body, FAKE_TENANT1,
                                        FAKE_TENANT2)
        verify(quota, never).save()
        self.assertEquals(200, result.status)

    def test_update_resource_instance(self):
        instance_quota = mock(Quota)
        when(DatabaseModelBase).find_by(
            tenant_id=FAKE_TENANT2,
            resource='instances').thenReturn(instance_quota)
        body = {'quotas': {'instances': 2}}
        result = self.controller.update(self.req, body, FAKE_TENANT1,
                                        FAKE_TENANT2)
        verify(instance_quota, times=1).save()
        self.assertTrue('instances' in result._data['quotas'])
        self.assertEquals(200, result.status)
        self.assertEquals(2, result._data['quotas']['instances'])

    @testtools.skipIf(not CONF.trove_volume_support,
                      'Volume support is not enabled')
    def test_update_resource_volume(self):
        instance_quota = mock(Quota)
        when(DatabaseModelBase).find_by(
            tenant_id=FAKE_TENANT2,
            resource='instances').thenReturn(instance_quota)
        volume_quota = mock(Quota)
        when(DatabaseModelBase).find_by(
            tenant_id=FAKE_TENANT2,
            resource='volumes').thenReturn(volume_quota)
        body = {'quotas': {'instances': None, 'volumes': 10}}
        result = self.controller.update(self.req, body, FAKE_TENANT1,
                                        FAKE_TENANT2)
        verify(instance_quota, never).save()
        self.assertFalse('instances' in result._data['quotas'])
        verify(volume_quota, times=1).save()
        self.assertEquals(200, result.status)
        self.assertEquals(10, result._data['quotas']['volumes'])


class DbQuotaDriverTest(testtools.TestCase):

    def setUp(self):

        super(DbQuotaDriverTest, self).setUp()
        self.driver = DbQuotaDriver(resources)
        self.orig_Quota_find_all = Quota.find_all
        self.orig_QuotaUsage_find_all = QuotaUsage.find_all
        self.orig_QuotaUsage_find_by = QuotaUsage.find_by
        self.orig_Reservation_create = Reservation.create
        self.orig_QuotaUsage_create = QuotaUsage.create
        self.orig_QuotaUsage_save = QuotaUsage.save
        self.orig_Reservation_save = Reservation.save
        self.mock_quota_result = Mock()
        self.mock_usage_result = Mock()
        Quota.find_all = Mock(return_value=self.mock_quota_result)
        QuotaUsage.find_all = Mock(return_value=self.mock_usage_result)

    def tearDown(self):
        super(DbQuotaDriverTest, self).tearDown()
        Quota.find_all = self.orig_Quota_find_all
        QuotaUsage.find_all = self.orig_QuotaUsage_find_all
        QuotaUsage.find_by = self.orig_QuotaUsage_find_by
        Reservation.create = self.orig_Reservation_create
        QuotaUsage.create = self.orig_QuotaUsage_create
        QuotaUsage.save = self.orig_QuotaUsage_save
        Reservation.save = self.orig_Reservation_save

    def test_get_defaults(self):
        defaults = self.driver.get_defaults(resources)
        self.assertEqual(CONF.max_instances_per_user,
                         defaults[Resource.INSTANCES])
        self.assertEqual(CONF.max_volumes_per_user,
                         defaults[Resource.VOLUMES])

    def test_get_quota_by_tenant(self):

        FAKE_QUOTAS = [Quota(tenant_id=FAKE_TENANT1,
                             resource=Resource.INSTANCES,
                             hard_limit=12)]

        self.mock_quota_result.all = Mock(return_value=FAKE_QUOTAS)

        quota = self.driver.get_quota_by_tenant(FAKE_TENANT1,
                                                Resource.VOLUMES)

        self.assertEquals(FAKE_TENANT1, quota.tenant_id)
        self.assertEquals(Resource.INSTANCES, quota.resource)
        self.assertEquals(12, quota.hard_limit)

    def test_get_quota_by_tenant_default(self):

        self.mock_quota_result.all = Mock(return_value=[])

        quota = self.driver.get_quota_by_tenant(FAKE_TENANT1,
                                                Resource.VOLUMES)

        self.assertEquals(FAKE_TENANT1, quota.tenant_id)
        self.assertEquals(Resource.VOLUMES, quota.resource)
        self.assertEquals(CONF.max_volumes_per_user, quota.hard_limit)

    def test_get_all_quotas_by_tenant(self):

        FAKE_QUOTAS = [Quota(tenant_id=FAKE_TENANT1,
                             resource=Resource.INSTANCES,
                             hard_limit=22),
                       Quota(tenant_id=FAKE_TENANT1,
                             resource=Resource.VOLUMES,
                             hard_limit=15)]

        self.mock_quota_result.all = Mock(return_value=FAKE_QUOTAS)

        quotas = self.driver.get_all_quotas_by_tenant(FAKE_TENANT1,
                                                      resources.keys())

        self.assertEquals(FAKE_TENANT1, quotas[Resource.INSTANCES].tenant_id)
        self.assertEquals(Resource.INSTANCES,
                          quotas[Resource.INSTANCES].resource)
        self.assertEquals(22, quotas[Resource.INSTANCES].hard_limit)
        self.assertEquals(FAKE_TENANT1, quotas[Resource.VOLUMES].tenant_id)
        self.assertEquals(Resource.VOLUMES, quotas[Resource.VOLUMES].resource)
        self.assertEquals(15, quotas[Resource.VOLUMES].hard_limit)

    def test_get_all_quotas_by_tenant_with_all_default(self):

        self.mock_quota_result.all = Mock(return_value=[])

        quotas = self.driver.get_all_quotas_by_tenant(FAKE_TENANT1,
                                                      resources.keys())

        self.assertEquals(FAKE_TENANT1, quotas[Resource.INSTANCES].tenant_id)
        self.assertEquals(Resource.INSTANCES,
                          quotas[Resource.INSTANCES].resource)
        self.assertEquals(CONF.max_instances_per_user,
                          quotas[Resource.INSTANCES].hard_limit)
        self.assertEquals(FAKE_TENANT1, quotas[Resource.VOLUMES].tenant_id)
        self.assertEquals(Resource.VOLUMES, quotas[Resource.VOLUMES].resource)
        self.assertEquals(CONF.max_volumes_per_user,
                          quotas[Resource.VOLUMES].hard_limit)

    def test_get_all_quotas_by_tenant_with_one_default(self):

        FAKE_QUOTAS = [Quota(tenant_id=FAKE_TENANT1,
                             resource=Resource.INSTANCES,
                             hard_limit=22)]

        self.mock_quota_result.all = Mock(return_value=FAKE_QUOTAS)

        quotas = self.driver.get_all_quotas_by_tenant(FAKE_TENANT1,
                                                      resources.keys())

        self.assertEquals(FAKE_TENANT1, quotas[Resource.INSTANCES].tenant_id)
        self.assertEquals(Resource.INSTANCES,
                          quotas[Resource.INSTANCES].resource)
        self.assertEquals(22, quotas[Resource.INSTANCES].hard_limit)
        self.assertEquals(FAKE_TENANT1, quotas[Resource.VOLUMES].tenant_id)
        self.assertEquals(Resource.VOLUMES, quotas[Resource.VOLUMES].resource)
        self.assertEquals(CONF.max_volumes_per_user,
                          quotas[Resource.VOLUMES].hard_limit)

    def test_get_quota_usage_by_tenant(self):

        FAKE_QUOTAS = [QuotaUsage(tenant_id=FAKE_TENANT1,
                                  resource=Resource.VOLUMES,
                                  in_use=3,
                                  reserved=1)]

        self.mock_usage_result.all = Mock(return_value=FAKE_QUOTAS)

        usage = self.driver.get_quota_usage_by_tenant(FAKE_TENANT1,
                                                      Resource.VOLUMES)

        self.assertEquals(FAKE_TENANT1, usage.tenant_id)
        self.assertEquals(Resource.VOLUMES, usage.resource)
        self.assertEquals(3, usage.in_use)
        self.assertEquals(1, usage.reserved)

    def test_get_quota_usage_by_tenant_default(self):

        FAKE_QUOTA = QuotaUsage(tenant_id=FAKE_TENANT1,
                                resource=Resource.VOLUMES,
                                in_use=0,
                                reserved=0)

        self.mock_usage_result.all = Mock(return_value=[])
        QuotaUsage.create = Mock(return_value=FAKE_QUOTA)

        usage = self.driver.get_quota_usage_by_tenant(FAKE_TENANT1,
                                                      Resource.VOLUMES)

        self.assertEquals(FAKE_TENANT1, usage.tenant_id)
        self.assertEquals(Resource.VOLUMES, usage.resource)
        self.assertEquals(0, usage.in_use)
        self.assertEquals(0, usage.reserved)

    def test_get_all_quota_usages_by_tenant(self):

        FAKE_QUOTAS = [QuotaUsage(tenant_id=FAKE_TENANT1,
                                  resource=Resource.INSTANCES,
                                  in_use=2,
                                  reserved=1),
                       QuotaUsage(tenant_id=FAKE_TENANT1,
                                  resource=Resource.VOLUMES,
                                  in_use=1,
                                  reserved=1)]

        self.mock_usage_result.all = Mock(return_value=FAKE_QUOTAS)

        usages = self.driver.get_all_quota_usages_by_tenant(FAKE_TENANT1,
                                                            resources.keys())

        self.assertEquals(FAKE_TENANT1, usages[Resource.INSTANCES].tenant_id)
        self.assertEquals(Resource.INSTANCES,
                          usages[Resource.INSTANCES].resource)
        self.assertEquals(2, usages[Resource.INSTANCES].in_use)
        self.assertEquals(1, usages[Resource.INSTANCES].reserved)
        self.assertEquals(FAKE_TENANT1, usages[Resource.VOLUMES].tenant_id)
        self.assertEquals(Resource.VOLUMES, usages[Resource.VOLUMES].resource)
        self.assertEquals(1, usages[Resource.VOLUMES].in_use)
        self.assertEquals(1, usages[Resource.VOLUMES].reserved)

    def test_get_all_quota_usages_by_tenant_with_all_default(self):

        FAKE_QUOTAS = [QuotaUsage(tenant_id=FAKE_TENANT1,
                                  resource=Resource.INSTANCES,
                                  in_use=0,
                                  reserved=0),
                       QuotaUsage(tenant_id=FAKE_TENANT1,
                                  resource=Resource.VOLUMES,
                                  in_use=0,
                                  reserved=0)]

        self.mock_usage_result.all = Mock(return_value=[])
        QuotaUsage.create = Mock(side_effect=FAKE_QUOTAS)

        usages = self.driver.get_all_quota_usages_by_tenant(FAKE_TENANT1,
                                                            resources.keys())

        self.assertEquals(FAKE_TENANT1, usages[Resource.INSTANCES].tenant_id)
        self.assertEquals(Resource.INSTANCES,
                          usages[Resource.INSTANCES].resource)
        self.assertEquals(0, usages[Resource.INSTANCES].in_use)
        self.assertEquals(0, usages[Resource.INSTANCES].reserved)
        self.assertEquals(FAKE_TENANT1, usages[Resource.VOLUMES].tenant_id)
        self.assertEquals(Resource.VOLUMES, usages[Resource.VOLUMES].resource)
        self.assertEquals(0, usages[Resource.VOLUMES].in_use)
        self.assertEquals(0, usages[Resource.VOLUMES].reserved)

    def test_get_all_quota_usages_by_tenant_with_one_default(self):

        FAKE_QUOTAS = [QuotaUsage(tenant_id=FAKE_TENANT1,
                                  resource=Resource.INSTANCES,
                                  in_use=0,
                                  reserved=0)]

        NEW_FAKE_QUOTA = QuotaUsage(tenant_id=FAKE_TENANT1,
                                    resource=Resource.VOLUMES,
                                    in_use=0,
                                    reserved=0)
        self.mock_usage_result.all = Mock(return_value=FAKE_QUOTAS)
        QuotaUsage.create = Mock(return_value=NEW_FAKE_QUOTA)

        usages = self.driver.get_all_quota_usages_by_tenant(FAKE_TENANT1,
                                                            resources.keys())

        self.assertEquals(FAKE_TENANT1, usages[Resource.INSTANCES].tenant_id)
        self.assertEquals(Resource.INSTANCES,
                          usages[Resource.INSTANCES].resource)
        self.assertEquals(0, usages[Resource.INSTANCES].in_use)
        self.assertEquals(0, usages[Resource.INSTANCES].reserved)
        self.assertEquals(FAKE_TENANT1, usages[Resource.VOLUMES].tenant_id)
        self.assertEquals(Resource.VOLUMES, usages[Resource.VOLUMES].resource)
        self.assertEquals(0, usages[Resource.VOLUMES].in_use)
        self.assertEquals(0, usages[Resource.VOLUMES].reserved)

    def test_reserve(self):

        FAKE_QUOTAS = [QuotaUsage(id=1,
                                  tenant_id=FAKE_TENANT1,
                                  resource=Resource.INSTANCES,
                                  in_use=1,
                                  reserved=2),
                       QuotaUsage(id=2,
                                  tenant_id=FAKE_TENANT1,
                                  resource=Resource.VOLUMES,
                                  in_use=1,
                                  reserved=1)]

        self.mock_quota_result.all = Mock(return_value=[])
        self.mock_usage_result.all = Mock(return_value=FAKE_QUOTAS)
        QuotaUsage.save = Mock()
        Reservation.create = Mock()

        delta = {'instances': 2, 'volumes': 3}
        self.driver.reserve(FAKE_TENANT1, resources, delta)
        _, kw = Reservation.create.call_args_list[0]
        self.assertEquals(1, kw['usage_id'])
        self.assertEquals(2, kw['delta'])
        self.assertEquals(Reservation.Statuses.RESERVED, kw['status'])
        _, kw = Reservation.create.call_args_list[1]
        self.assertEquals(2, kw['usage_id'])
        self.assertEquals(3, kw['delta'])
        self.assertEquals(Reservation.Statuses.RESERVED, kw['status'])

    def test_reserve_resource_unknown(self):

        delta = {'instances': 10, 'volumes': 2000, 'Fake_resource': 123}
        self.assertRaises(exception.QuotaResourceUnknown,
                          self.driver.reserve,
                          FAKE_TENANT1,
                          resources,
                          delta)

    def test_reserve_over_quota(self):

        FAKE_QUOTAS = [QuotaUsage(id=1,
                                  tenant_id=FAKE_TENANT1,
                                  resource=Resource.INSTANCES,
                                  in_use=0,
                                  reserved=0),
                       QuotaUsage(id=2,
                                  tenant_id=FAKE_TENANT1,
                                  resource=Resource.VOLUMES,
                                  in_use=0,
                                  reserved=0)]

        self.mock_quota_result.all = Mock(return_value=[])
        self.mock_usage_result.all = Mock(return_value=FAKE_QUOTAS)

        delta = {'instances': 1, 'volumes': CONF.max_volumes_per_user + 1}
        self.assertRaises(exception.QuotaExceeded,
                          self.driver.reserve,
                          FAKE_TENANT1,
                          resources,
                          delta)

    def test_reserve_over_quota_with_usage(self):

        FAKE_QUOTAS = [QuotaUsage(id=1,
                                  tenant_id=FAKE_TENANT1,
                                  resource=Resource.INSTANCES,
                                  in_use=1,
                                  reserved=0),
                       QuotaUsage(id=2,
                                  tenant_id=FAKE_TENANT1,
                                  resource=Resource.VOLUMES,
                                  in_use=0,
                                  reserved=0)]

        self.mock_quota_result.all = Mock(return_value=[])
        self.mock_usage_result.all = Mock(return_value=FAKE_QUOTAS)

        delta = {'instances': 5, 'volumes': 3}
        self.assertRaises(exception.QuotaExceeded,
                          self.driver.reserve,
                          FAKE_TENANT1,
                          resources,
                          delta)

    def test_reserve_over_quota_with_reserved(self):

        FAKE_QUOTAS = [QuotaUsage(id=1,
                                  tenant_id=FAKE_TENANT1,
                                  resource=Resource.INSTANCES,
                                  in_use=1,
                                  reserved=2),
                       QuotaUsage(id=2,
                                  tenant_id=FAKE_TENANT1,
                                  resource=Resource.VOLUMES,
                                  in_use=0,
                                  reserved=0)]

        self.mock_quota_result.all = Mock(return_value=[])
        self.mock_usage_result.all = Mock(return_value=FAKE_QUOTAS)

        delta = {'instances': 4, 'volumes': 2}
        self.assertRaises(exception.QuotaExceeded,
                          self.driver.reserve,
                          FAKE_TENANT1,
                          resources,
                          delta)

    def test_reserve_over_quota_but_can_apply_negative_deltas(self):

        FAKE_QUOTAS = [QuotaUsage(id=1,
                                  tenant_id=FAKE_TENANT1,
                                  resource=Resource.INSTANCES,
                                  in_use=10,
                                  reserved=0),
                       QuotaUsage(id=2,
                                  tenant_id=FAKE_TENANT1,
                                  resource=Resource.VOLUMES,
                                  in_use=50,
                                  reserved=0)]

        self.mock_quota_result.all = Mock(return_value=[])
        self.mock_usage_result.all = Mock(return_value=FAKE_QUOTAS)

        QuotaUsage.save = Mock()
        Reservation.create = Mock()

        delta = {'instances': -1, 'volumes': -3}
        self.driver.reserve(FAKE_TENANT1, resources, delta)
        _, kw = Reservation.create.call_args_list[0]
        self.assertEquals(1, kw['usage_id'])
        self.assertEquals(-1, kw['delta'])
        self.assertEquals(Reservation.Statuses.RESERVED, kw['status'])
        _, kw = Reservation.create.call_args_list[1]
        self.assertEquals(2, kw['usage_id'])
        self.assertEquals(-3, kw['delta'])
        self.assertEquals(Reservation.Statuses.RESERVED, kw['status'])

    def test_commit(self):

        Reservation.save = Mock()
        QuotaUsage.save = Mock()

        FAKE_QUOTAS = [QuotaUsage(id=1,
                                  tenant_id=FAKE_TENANT1,
                                  resource=Resource.INSTANCES,
                                  in_use=5,
                                  reserved=2),
                       QuotaUsage(id=2,
                                  tenant_id=FAKE_TENANT1,
                                  resource=Resource.VOLUMES,
                                  in_use=1,
                                  reserved=2)]

        FAKE_RESERVATIONS = [Reservation(usage_id=1,
                                         delta=1,
                                         status=Reservation.Statuses.RESERVED),
                             Reservation(usage_id=2,
                                         delta=2,
                                         status=Reservation.Statuses.RESERVED)]

        QuotaUsage.find_by = Mock(side_effect=FAKE_QUOTAS)
        self.driver.commit(FAKE_RESERVATIONS)

        self.assertEqual(6, FAKE_QUOTAS[0].in_use)
        self.assertEqual(1, FAKE_QUOTAS[0].reserved)
        self.assertEqual(Reservation.Statuses.COMMITTED,
                         FAKE_RESERVATIONS[0].status)

        self.assertEqual(3, FAKE_QUOTAS[1].in_use)
        self.assertEqual(0, FAKE_QUOTAS[1].reserved)
        self.assertEqual(Reservation.Statuses.COMMITTED,
                         FAKE_RESERVATIONS[1].status)

    def test_rollback(self):

        Reservation.save = Mock()
        QuotaUsage.save = Mock()

        FAKE_QUOTAS = [QuotaUsage(id=1,
                                  tenant_id=FAKE_TENANT1,
                                  resource=Resource.INSTANCES,
                                  in_use=5,
                                  reserved=2),
                       QuotaUsage(id=2,
                                  tenant_id=FAKE_TENANT1,
                                  resource=Resource.VOLUMES,
                                  in_use=1,
                                  reserved=2)]

        FAKE_RESERVATIONS = [Reservation(usage_id=1,
                                         delta=1,
                                         status=Reservation.Statuses.RESERVED),
                             Reservation(usage_id=2,
                                         delta=2,
                                         status=Reservation.Statuses.RESERVED)]

        QuotaUsage.find_by = Mock(side_effect=FAKE_QUOTAS)
        self.driver.rollback(FAKE_RESERVATIONS)

        self.assertEqual(5, FAKE_QUOTAS[0].in_use)
        self.assertEqual(1, FAKE_QUOTAS[0].reserved)
        self.assertEqual(Reservation.Statuses.ROLLEDBACK,
                         FAKE_RESERVATIONS[0].status)

        self.assertEqual(1, FAKE_QUOTAS[1].in_use)
        self.assertEqual(0, FAKE_QUOTAS[1].reserved)
        self.assertEqual(Reservation.Statuses.ROLLEDBACK,
                         FAKE_RESERVATIONS[1].status)

[((34, 24, 34, 78), 'trove.quota.models.Resource', 'Resource', ({(34, 33, 34, 51): 'Resource.INSTANCES', (34, 53, 34, 77): '"""max_instances_per_user"""'}, {}), "(Resource.INSTANCES, 'max_instances_per_user')", False, 'from trove.quota.models import Resource\n'), ((35, 22, 35, 72), 'trove.quota.models.Resource', 'Resource', ({(35, 31, 35, 47): 'Resource.VOLUMES', (35, 49, 35, 71): '"""max_volumes_per_user"""'}, {}), "(Resource.VOLUMES, 'max_volumes_per_user')", False, 'from trove.quota.models import Resource\n'), ((126, 5, 127, 54), 'testtools.skipIf', 'testtools.skipIf', ({(126, 22, 126, 51): '(not CONF.trove_volume_support)', (127, 22, 127, 53): '"""Volume support is not enabled"""'}, {}), "(not CONF.trove_volume_support, 'Volume support is not enabled'\n    )", False, 'import testtools\n'), ((49, 25, 49, 31), 'mock.Mock', 'Mock', ({}, {}), '()', False, 'from mock import Mock\n'), ((50, 26, 50, 32), 'mock.Mock', 'Mock', ({}, {}), '()', False, 'from mock import Mock\n'), ((51, 24, 51, 30), 'mock.Mock', 'Mock', ({}, {}), '()', False, 'from mock import Mock\n'), ((61, 12, 61, 18), 'mock.Mock', 'Mock', ({}, {}), '()', False, 'from mock import Mock\n'), ((62, 8, 62, 72), 'trove.quota.quota.run_with_quotas', 'run_with_quotas', ({(62, 24, 62, 36): 'FAKE_TENANT1', (62, 38, 62, 68): "{'instances': 1, 'volumes': 5}", (62, 70, 62, 71): 'f'}, {}), "(FAKE_TENANT1, {'instances': 1, 'volumes': 5}, f)", False, 'from trove.quota.quota import run_with_quotas\n'), ((85, 18, 85, 24), 'mockito.mock', 'mock', ({}, {}), '()', False, 'from mockito import mock, when, unstub, any, verify, never, times\n'), ((87, 14, 87, 20), 'mockito.mock', 'mock', ({}, {}), '()', False, 'from mockito import mock, when, unstub, any, verify, never, times\n'), ((88, 22, 88, 28), 'mockito.mock', 'mock', ({}, {}), '()', False, 'from mockito import mock, when, unstub, any, verify, never, times\n'), ((91, 26, 91, 43), 'trove.extensions.mgmt.quota.service.QuotaController', 'QuotaController', ({}, {}), '()', False, 'from trove.extensions.mgmt.quota.service import QuotaController\n'), ((95, 8, 95, 16), 'mockito.unstub', 'unstub', ({}, {}), '()', False, 'from mockito import mock, when, unstub, any, verify, never, times\n'), ((104, 16, 104, 27), 'mockito.mock', 'mock', ({(104, 21, 104, 26): 'Quota'}, {}), '(Quota)', False, 'from mockito import mock, when, unstub, any, verify, never, times\n'), ((114, 25, 114, 36), 'mockito.mock', 'mock', ({(114, 30, 114, 35): 'Quota'}, {}), '(Quota)', False, 'from mockito import mock, when, unstub, any, verify, never, times\n'), ((129, 25, 129, 36), 'mockito.mock', 'mock', ({(129, 30, 129, 35): 'Quota'}, {}), '(Quota)', False, 'from mockito import mock, when, unstub, any, verify, never, times\n'), ((133, 23, 133, 34), 'mockito.mock', 'mock', ({(133, 28, 133, 33): 'Quota'}, {}), '(Quota)', False, 'from mockito import mock, when, unstub, any, verify, never, times\n'), ((152, 22, 152, 46), 'trove.quota.quota.DbQuotaDriver', 'DbQuotaDriver', ({(152, 36, 152, 45): 'resources'}, {}), '(resources)', False, 'from trove.quota.quota import DbQuotaDriver\n'), ((160, 33, 160, 39), 'mock.Mock', 'Mock', ({}, {}), '()', False, 'from mock import Mock\n'), ((161, 33, 161, 39), 'mock.Mock', 'Mock', ({}, {}), '()', False, 'from mock import Mock\n'), ((162, 25, 162, 66), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((163, 30, 163, 71), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((188, 37, 188, 67), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((199, 37, 199, 58), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((217, 37, 217, 67), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((232, 37, 232, 58), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((253, 37, 253, 67), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((274, 37, 274, 67), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((286, 21, 289, 43), 'trove.quota.models.QuotaUsage', 'QuotaUsage', (), '', False, 'from trove.quota.models import QuotaUsage\n'), ((291, 37, 291, 58), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((292, 28, 292, 57), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((313, 37, 313, 67), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((339, 37, 339, 58), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((340, 28, 340, 57), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((362, 25, 365, 47), 'trove.quota.models.QuotaUsage', 'QuotaUsage', (), '', False, 'from trove.quota.models import QuotaUsage\n'), ((366, 37, 366, 67), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((367, 28, 367, 61), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((395, 37, 395, 58), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((396, 37, 396, 67), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((397, 26, 397, 32), 'mock.Mock', 'Mock', ({}, {}), '()', False, 'from mock import Mock\n'), ((398, 29, 398, 35), 'mock.Mock', 'Mock', ({}, {}), '()', False, 'from mock import Mock\n'), ((433, 37, 433, 58), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((434, 37, 434, 67), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((456, 37, 456, 58), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((457, 37, 457, 67), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((479, 37, 479, 58), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((480, 37, 480, 67), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((502, 37, 502, 58), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((503, 37, 503, 67), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((505, 26, 505, 32), 'mock.Mock', 'Mock', ({}, {}), '()', False, 'from mock import Mock\n'), ((506, 29, 506, 35), 'mock.Mock', 'Mock', ({}, {}), '()', False, 'from mock import Mock\n'), ((521, 27, 521, 33), 'mock.Mock', 'Mock', ({}, {}), '()', False, 'from mock import Mock\n'), ((522, 26, 522, 32), 'mock.Mock', 'Mock', ({}, {}), '()', False, 'from mock import Mock\n'), ((542, 29, 542, 58), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((557, 27, 557, 33), 'mock.Mock', 'Mock', ({}, {}), '()', False, 'from mock import Mock\n'), ((558, 26, 558, 32), 'mock.Mock', 'Mock', ({}, {}), '()', False, 'from mock import Mock\n'), ((578, 29, 578, 58), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((184, 23, 186, 43), 'trove.quota.models.Quota', 'Quota', (), '', False, 'from trove.quota.models import Quota\n'), ((210, 23, 212, 43), 'trove.quota.models.Quota', 'Quota', (), '', False, 'from trove.quota.models import Quota\n'), ((213, 23, 215, 43), 'trove.quota.models.Quota', 'Quota', (), '', False, 'from trove.quota.models import Quota\n'), ((249, 23, 251, 43), 'trove.quota.models.Quota', 'Quota', (), '', False, 'from trove.quota.models import Quota\n'), ((269, 23, 272, 45), 'trove.quota.models.QuotaUsage', 'QuotaUsage', (), '', False, 'from trove.quota.models import QuotaUsage\n'), ((304, 23, 307, 45), 'trove.quota.models.QuotaUsage', 'QuotaUsage', (), '', False, 'from trove.quota.models import QuotaUsage\n'), ((308, 23, 311, 45), 'trove.quota.models.QuotaUsage', 'QuotaUsage', (), '', False, 'from trove.quota.models import QuotaUsage\n'), ((330, 23, 333, 45), 'trove.quota.models.QuotaUsage', 'QuotaUsage', (), '', False, 'from trove.quota.models import QuotaUsage\n'), ((334, 23, 337, 45), 'trove.quota.models.QuotaUsage', 'QuotaUsage', (), '', False, 'from trove.quota.models import QuotaUsage\n'), ((357, 23, 360, 45), 'trove.quota.models.QuotaUsage', 'QuotaUsage', (), '', False, 'from trove.quota.models import QuotaUsage\n'), ((384, 23, 388, 45), 'trove.quota.models.QuotaUsage', 'QuotaUsage', (), '', False, 'from trove.quota.models import QuotaUsage\n'), ((389, 23, 393, 45), 'trove.quota.models.QuotaUsage', 'QuotaUsage', (), '', False, 'from trove.quota.models import QuotaUsage\n'), ((422, 23, 426, 45), 'trove.quota.models.QuotaUsage', 'QuotaUsage', (), '', False, 'from trove.quota.models import QuotaUsage\n'), ((427, 23, 431, 45), 'trove.quota.models.QuotaUsage', 'QuotaUsage', (), '', False, 'from trove.quota.models import QuotaUsage\n'), ((445, 23, 449, 45), 'trove.quota.models.QuotaUsage', 'QuotaUsage', (), '', False, 'from trove.quota.models import QuotaUsage\n'), ((450, 23, 454, 45), 'trove.quota.models.QuotaUsage', 'QuotaUsage', (), '', False, 'from trove.quota.models import QuotaUsage\n'), ((468, 23, 472, 45), 'trove.quota.models.QuotaUsage', 'QuotaUsage', (), '', False, 'from trove.quota.models import QuotaUsage\n'), ((473, 23, 477, 45), 'trove.quota.models.QuotaUsage', 'QuotaUsage', (), '', False, 'from trove.quota.models import QuotaUsage\n'), ((491, 23, 495, 45), 'trove.quota.models.QuotaUsage', 'QuotaUsage', (), '', False, 'from trove.quota.models import QuotaUsage\n'), ((496, 23, 500, 45), 'trove.quota.models.QuotaUsage', 'QuotaUsage', (), '', False, 'from trove.quota.models import QuotaUsage\n'), ((524, 23, 528, 45), 'trove.quota.models.QuotaUsage', 'QuotaUsage', (), '', False, 'from trove.quota.models import QuotaUsage\n'), ((529, 23, 533, 45), 'trove.quota.models.QuotaUsage', 'QuotaUsage', (), '', False, 'from trove.quota.models import QuotaUsage\n'), ((535, 29, 537, 78), 'trove.quota.models.Reservation', 'Reservation', (), '', False, 'from trove.quota.models import Reservation\n'), ((538, 29, 540, 78), 'trove.quota.models.Reservation', 'Reservation', (), '', False, 'from trove.quota.models import Reservation\n'), ((560, 23, 564, 45), 'trove.quota.models.QuotaUsage', 'QuotaUsage', (), '', False, 'from trove.quota.models import QuotaUsage\n'), ((565, 23, 569, 45), 'trove.quota.models.QuotaUsage', 'QuotaUsage', (), '', False, 'from trove.quota.models import QuotaUsage\n'), ((571, 29, 573, 78), 'trove.quota.models.Reservation', 'Reservation', (), '', False, 'from trove.quota.models import Reservation\n'), ((574, 29, 576, 78), 'trove.quota.models.Reservation', 'Reservation', (), '', False, 'from trove.quota.models import Reservation\n'), ((110, 8, 110, 28), 'mockito.verify', 'verify', ({(110, 15, 110, 20): 'quota', (110, 22, 110, 27): 'never'}, {}), '(quota, never)', False, 'from mockito import mock, when, unstub, any, verify, never, times\n'), ((121, 8, 121, 39), 'mockito.verify', 'verify', (), '', False, 'from mockito import mock, when, unstub, any, verify, never, times\n'), ((140, 8, 140, 37), 'mockito.verify', 'verify', ({(140, 15, 140, 29): 'instance_quota', (140, 31, 140, 36): 'never'}, {}), '(instance_quota, never)', False, 'from mockito import mock, when, unstub, any, verify, never, times\n'), ((142, 8, 142, 37), 'mockito.verify', 'verify', (), '', False, 'from mockito import mock, when, unstub, any, verify, never, times\n'), ((89, 30, 89, 35), 'mockito.any', 'any', ({}, {}), '()', False, 'from mockito import mock, when, unstub, any, verify, never, times\n'), ((89, 8, 89, 25), 'mockito.when', 'when', ({(89, 13, 89, 24): 'req.environ'}, {}), '(req.environ)', False, 'from mockito import mock, when, unstub, any, verify, never, times\n'), ((105, 8, 105, 31), 'mockito.when', 'when', ({(105, 13, 105, 30): 'DatabaseModelBase'}, {}), '(DatabaseModelBase)', False, 'from mockito import mock, when, unstub, any, verify, never, times\n'), ((115, 8, 115, 31), 'mockito.when', 'when', ({(115, 13, 115, 30): 'DatabaseModelBase'}, {}), '(DatabaseModelBase)', False, 'from mockito import mock, when, unstub, any, verify, never, times\n'), ((130, 8, 130, 31), 'mockito.when', 'when', ({(130, 13, 130, 30): 'DatabaseModelBase'}, {}), '(DatabaseModelBase)', False, 'from mockito import mock, when, unstub, any, verify, never, times\n'), ((134, 8, 134, 31), 'mockito.when', 'when', ({(134, 13, 134, 30): 'DatabaseModelBase'}, {}), '(DatabaseModelBase)', False, 'from mockito import mock, when, unstub, any, verify, never, times\n')]