Merge "Adding new test cases to "quota" scenario test suite"
diff --git a/designate_tempest_plugin/common/waiters.py b/designate_tempest_plugin/common/waiters.py
index a606fda..affe332 100644
--- a/designate_tempest_plugin/common/waiters.py
+++ b/designate_tempest_plugin/common/waiters.py
@@ -33,7 +33,7 @@
         time.sleep(client.build_interval)
 
         try:
-            _, zone = client.show_zone(zone_id)
+            zone = client.show_zone(zone_id)[1]
         except lib_exc.NotFound:
             LOG.info('Zone %s is 404ing', zone_id)
             return
@@ -62,12 +62,12 @@
     """Waits for a zone to reach given status."""
     LOG.info('Waiting for zone %s to reach %s', zone_id, status)
 
-    _, zone = client.show_zone(zone_id, headers=headers)
+    zone = client.show_zone(zone_id, headers=headers)[1]
     start = int(time.time())
 
     while zone['status'] != status:
         time.sleep(client.build_interval)
-        _, zone = client.show_zone(zone_id, headers=headers)
+        zone = client.show_zone(zone_id, headers=headers)[1]
         status_curr = zone['status']
         if status_curr == status:
             LOG.info('Zone %s reached %s', zone_id, status)
@@ -98,12 +98,12 @@
     """Waits for an imported zone to reach the given status."""
     LOG.info('Waiting for zone import %s to reach %s', zone_import_id, status)
 
-    _, zone_import = client.show_zone_import(zone_import_id)
+    zone_import = client.show_zone_import(zone_import_id)[1]
     start = int(time.time())
 
     while zone_import['status'] != status:
         time.sleep(client.build_interval)
-        _, zone_import = client.show_zone_import(zone_import_id)
+        zone_import = client.show_zone_import(zone_import_id)[1]
         status_curr = zone_import['status']
         if status_curr == status:
             LOG.info('Zone import %s reached %s', zone_import_id, status)
@@ -131,16 +131,17 @@
             raise lib_exc.TimeoutException(message)
 
 
-def wait_for_zone_export_status(client, zone_export_id, status):
+def wait_for_zone_export_status(client, zone_export_id, status, headers=None):
     """Waits for an exported zone to reach the given status."""
     LOG.info('Waiting for zone export %s to reach %s', zone_export_id, status)
 
-    _, zone_export = client.show_zone_export(zone_export_id)
+    zone_export = client.show_zone_export(zone_export_id, headers=headers)[1]
     start = int(time.time())
 
     while zone_export['status'] != status:
         time.sleep(client.build_interval)
-        _, zone_export = client.show_zone_export(zone_export_id)
+        zone_export = client.show_zone_export(
+            zone_export_id, headers=headers)[1]
         status_curr = zone_export['status']
         if status_curr == status:
             LOG.info('Zone export %s reached %s', zone_export_id, status)
@@ -168,17 +169,20 @@
             raise lib_exc.TimeoutException(message)
 
 
-def wait_for_recordset_status(client, zone_id, recordset_id, status):
+def wait_for_recordset_status(
+        client, zone_id, recordset_id, status, headers=None):
     """Waits for a recordset to reach the given status."""
     LOG.info('Waiting for recordset %s to reach %s',
              recordset_id, status)
 
-    _, recordset = client.show_recordset(zone_id, recordset_id)
+    recordset = client.show_recordset(
+        zone_id, recordset_id, headers=headers)[1]
     start = int(time.time())
 
     while recordset['status'] != status:
         time.sleep(client.build_interval)
-        _, recordset = client.show_recordset(zone_id, recordset_id)
+        recordset = client.show_recordset(
+            zone_id, recordset_id, headers=headers)[1]
         status_curr = recordset['status']
         if status_curr == status:
             LOG.info('Recordset %s reached %s', recordset_id, status)
diff --git a/designate_tempest_plugin/data_utils.py b/designate_tempest_plugin/data_utils.py
index 61c6da0..d4df276 100644
--- a/designate_tempest_plugin/data_utils.py
+++ b/designate_tempest_plugin/data_utils.py
@@ -125,7 +125,7 @@
 
 
 def rand_recordset_data(record_type, zone_name, name=None, records=None,
-                        ttl=None):
+                        ttl=None, number_of_records=None):
     """Generate random recordset data, with optional overrides
     :return: A RecordsetModel
     """
@@ -133,6 +133,8 @@
         name = rand_zone_name(prefix=record_type, suffix='.' + zone_name)
     if records is None:
         records = [rand_ip()]
+    if number_of_records:
+        records = [rand_ip() for r in range(number_of_records)]
     if ttl is None:
         ttl = rand_ttl()
     return {
diff --git a/designate_tempest_plugin/services/dns/v2/json/recordset_client.py b/designate_tempest_plugin/services/dns/v2/json/recordset_client.py
index d07ca18..4440705 100644
--- a/designate_tempest_plugin/services/dns/v2/json/recordset_client.py
+++ b/designate_tempest_plugin/services/dns/v2/json/recordset_client.py
@@ -35,27 +35,24 @@
 
     @base.handle_errors
     def create_recordset(self, zone_uuid, recordset_data,
-                         params=None, wait_until=False):
+                         params=None, wait_until=False, headers=None):
         """Create a recordset for the specified zone.
-
         :param zone_uuid: Unique identifier of the zone in UUID format..
         :param recordset_data: A dictionary that represents the recordset
                                data.
         :param params: A Python dict that represents the query paramaters to
                        include in the request URI.
+        :param headers (dict): The headers to use for the request.
         :return: A tuple with the server response and the created zone.
         """
         resp, body = self._create_request(
             "/zones/{0}/recordsets".format(zone_uuid), params=params,
-            data=recordset_data)
-
+            data=recordset_data, headers=headers)
         # Create Recordset should Return a HTTP 202
         self.expected_success(202, resp.status)
-
         if wait_until:
             waiters.wait_for_recordset_status(
-                self, zone_uuid, body['id'], wait_until)
-
+                self, zone_uuid, body['id'], wait_until, headers=headers)
         return resp, body
 
     @base.handle_errors
diff --git a/designate_tempest_plugin/services/dns/v2/json/zone_exports_client.py b/designate_tempest_plugin/services/dns/v2/json/zone_exports_client.py
index 1f469ec..5089d36 100644
--- a/designate_tempest_plugin/services/dns/v2/json/zone_exports_client.py
+++ b/designate_tempest_plugin/services/dns/v2/json/zone_exports_client.py
@@ -19,7 +19,8 @@
 class ZoneExportsClient(base.DnsClientV2Base):
 
     @base.handle_errors
-    def create_zone_export(self, uuid, params=None, wait_until=False):
+    def create_zone_export(self, uuid, params=None,
+                           wait_until=False, headers=None):
         """Create a zone export.
 
         :param uuid: Unique identifier of the zone in UUID format.
@@ -27,18 +28,20 @@
                        include in the request URI.
         :param wait_until: Block until the exported zone reaches the
                            desired status
+        :param headers (dict): The headers to use for the request.
         :return: Serialized imported zone as a dictionary.
         """
 
         export_uri = 'zones/{0}/tasks/export'.format(uuid)
         resp, body = self._create_request(
-            export_uri, params=params)
+            export_uri, params=params, headers=headers)
 
         # Create Zone Export should Return a HTTP 202
         self.expected_success(202, resp.status)
 
         if wait_until:
-            waiters.wait_for_zone_export_status(self, body['id'], wait_until)
+            waiters.wait_for_zone_export_status(
+                self, body['id'], wait_until, headers=headers)
 
         return resp, body
 
diff --git a/designate_tempest_plugin/tests/scenario/v2/test_quotas.py b/designate_tempest_plugin/tests/scenario/v2/test_quotas.py
index c5725ed..17fd16f 100644
--- a/designate_tempest_plugin/tests/scenario/v2/test_quotas.py
+++ b/designate_tempest_plugin/tests/scenario/v2/test_quotas.py
@@ -11,13 +11,16 @@
 # 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 random
 from oslo_log import log as logging
 from tempest import config
 from tempest.lib import decorators
+from tempest.lib import exceptions as lib_exc
 
 from designate_tempest_plugin.tests import base
 from designate_tempest_plugin import data_utils as dns_data_utils
+from designate_tempest_plugin.common import constants as const
+
 
 LOG = logging.getLogger(__name__)
 
@@ -28,6 +31,7 @@
 class QuotasV2Test(base.BaseDnsV2Test):
 
     credentials = ['primary', 'admin', 'system_admin', 'alt']
+    test_quota_limit = 3
 
     @classmethod
     def setup_credentials(cls):
@@ -55,7 +59,9 @@
             cls.admin_tld_client = cls.os_admin.dns_v2.TldClient()
         cls.quotas_client = cls.os_primary.dns_v2.QuotasClient()
         cls.alt_client = cls.os_alt.dns_v2.QuotasClient()
+        cls.zone_client = cls.os_primary.dns_v2.ZonesClient()
         cls.alt_zone_client = cls.os_alt.dns_v2.ZonesClient()
+        cls.recordset_client = cls.os_primary.dns_v2.RecordsetClient()
 
     @classmethod
     def resource_setup(cls):
@@ -71,40 +77,203 @@
         cls.admin_tld_client.delete_tld(cls.class_tld[1]['id'])
         super(QuotasV2Test, cls).resource_cleanup()
 
-    @decorators.idempotent_id('6987953a-dccf-11eb-903e-74e5f9e2a801')
-    def test_alt_reaches_zones_quota(self):
-
-        alt_project_id = self.alt_client.project_id
-        http_header = {'x-auth-sudo-project-id': alt_project_id}
-        limit_zones_quota = 3
-
-        LOG.info('As Admin user set Zones quota for Alt user '
-                 'to:{} '.format(limit_zones_quota))
-        quotas = dns_data_utils.rand_quotas()
-        quotas['zones'] = limit_zones_quota
+    def _set_quota_for_project(self, project_id, quotas):
+        http_header = {'x-auth-sudo-project-id': project_id}
         self.admin_client.set_quotas(
-            project_id=alt_project_id, quotas=quotas, headers=http_header)
+            project_id=project_id, quotas=quotas, headers=http_header)
         self.addCleanup(
-            self.admin_client.delete_quotas, project_id=alt_project_id)
+            self.admin_client.delete_quotas,
+            project_id=project_id, headers=http_header)
 
-        LOG.info('As Alt user try to create zones, up untill'
-                 ' "zones" quota (status code 413) is reached')
+    def _reach_quota_limit(
+            self, limit_threshold, quota_type, zone=None):
         attempt_number = 0
-        while attempt_number <= limit_zones_quota + 1:
-            attempt_number += 1
-            LOG.info('Attempt No:{} '.format(attempt_number))
+        not_raised_msg = "Failed, expected '413 over_quota' response of " \
+                         "type:{} wasn't received.".format(quota_type)
+        while attempt_number <= limit_threshold + 1:
             try:
-                zone_name = dns_data_utils.rand_zone_name(
-                    name="alt_reaches_zones_quota", suffix=self.tld_name)
-                zone = self.alt_zone_client.create_zone(name=zone_name)[1]
-                self.addCleanup(
-                    self.wait_zone_delete, self.alt_zone_client, zone['id'])
-            except Exception as err:
-                raised_error = str(err).replace(' ', '')
-                if not "'code':413" and "'type':'over_quota'" in raised_error \
-                        and attempt_number == limit_zones_quota + 1:
-                    raise (
-                        "Failed, expected status code 413 (type:over_quota) "
-                        "was not raised or maybe it has been raised mistakenly"
-                        "(bug) before the quota was actually reached."
-                        " Test failed with: {} ".format(err))
+                attempt_number += 1
+                LOG.info('Attempt No:{} '.format(attempt_number))
+                if quota_type == 'zones_quota':
+                    zone_name = dns_data_utils.rand_zone_name(
+                        name="_reach_quota_limit", suffix=self.tld_name)
+                    zone = self.zone_client.create_zone(
+                        name=zone_name,
+                        description='Test zone for:{}'.format(quota_type))[1]
+                    self.addCleanup(
+                        self.wait_zone_delete,
+                        self.zone_client, zone['id'])
+                else:
+                    if quota_type == 'zone_recordsets':
+                        max_number_of_records = 10
+                        prj_quota = self.admin_client.show_quotas(
+                            project_id=self.zone_client.project_id,
+                            headers=self.all_projects_header)[1][
+                            'zone_records']
+                        if max_number_of_records > prj_quota:
+                            max_number_of_records = prj_quota
+                        recordset_data = dns_data_utils.rand_recordset_data(
+                            record_type='A', zone_name=zone['name'],
+                            number_of_records=random.randint(
+                                1, max_number_of_records))
+                    else:
+                        recordset_data = dns_data_utils.rand_recordset_data(
+                            record_type='A', zone_name=zone['name'])
+                    recordset = self.recordset_client.create_recordset(
+                        zone['id'], recordset_data=recordset_data,
+                        wait_until=const.ACTIVE)[1]
+                    self.addCleanup(
+                        self.wait_recordset_delete,
+                        self.recordset_client,
+                        zone['id'], recordset['id'])
+                self.assertLess(
+                    attempt_number, limit_threshold + 1, not_raised_msg)
+            except Exception as e:
+                raised_err = str(e).replace(' ', '')
+                if not_raised_msg in str(e):
+                    raise AssertionError(not_raised_msg)
+                elif "'code':413" in raised_err and \
+                        "'type':'over_quota'" in raised_err:
+                    LOG.info("OK, type':'over_quota' was raised")
+                    break
+                else:
+                    raise
+
+    @decorators.attr(type='slow')
+    @decorators.idempotent_id('41d9cf2c-866a-11ec-8ccb-201e8823901f')
+    @decorators.skip_because(bug="1960495")
+    def test_api_export_size_quota(self):
+        LOG.info('Admin sets "api_export_size:{}" quota for Primary'
+                 ' user'.format(self.test_quota_limit))
+        quotas = dns_data_utils.rand_quotas()
+        quotas['api_export_size'] = self.test_quota_limit
+        self._set_quota_for_project(
+            self.zone_client.project_id, quotas)
+        LOG.info('Create a Zone, wait until ACTIVE and add:{}'
+                 ' Recordsets'.format(self.test_quota_limit + 1))
+        zone = self.zone_client.create_zone(
+            description='Zone for test_api_export_size_quota',
+            wait_until=const.ACTIVE)[1]
+        self.addCleanup(
+            self.wait_zone_delete,
+            self.zone_client, zone['id'])
+        for i in range(self.test_quota_limit + 1):
+            recordset_data = dns_data_utils.rand_recordset_data(
+                record_type='A', zone_name=zone['name'])
+            LOG.info('Try to create a recordset No:{}'.format(i))
+            recordset = self.recordset_client.create_recordset(
+                zone['id'], recordset_data=recordset_data,
+                wait_until=const.ACTIVE)[1]
+            self.addCleanup(
+                self.wait_recordset_delete,
+                self.recordset_client,
+                zone['id'], recordset['id'])
+        LOG.info(
+            'Ensure that the Number of Recordsets is bigger than configured'
+            ' api_export_size:{}'.format(self.test_quota_limit))
+        number_of_recordsets = len(self.recordset_client.list_recordset(
+            zone['id'])[1]['recordsets'])
+        self.assertGreater(
+            number_of_recordsets, self.test_quota_limit,
+            'Failed, the number of recordsets within a Zone is not enough to'
+            ' trigger "413 over quota" on Zone Export')
+        LOG.info('Try to export Zone. Expected:"413 over_quota"')
+        with self.assertRaisesDns(
+                lib_exc.OverLimit, 'over_quota', 413):
+            self.export_zone_client.create_zone_export(zone['id'])
+
+    @decorators.attr(type='slow')
+    @decorators.idempotent_id('2513cb6e-85ec-11ec-bf7f-201e8823901f')
+    def test_recordset_records_quota(self):
+        LOG.info('Admin sets "recordset_records:{}" quota for Primary'
+                 ' user'.format(self.test_quota_limit))
+        quotas = dns_data_utils.rand_quotas()
+        quotas['recordset_records'] = self.test_quota_limit
+        self._set_quota_for_project(
+            self.zone_client.project_id, quotas)
+        LOG.info('Create a Zone and wait until ACTIVE')
+        zone_name = dns_data_utils.rand_zone_name(
+            name="test_recordset_records_quota", suffix=self.tld_name)
+        zone = self.zone_client.create_zone(
+            name=zone_name,
+            description='Zone for test_recordset_records_quota',
+            wait_until=const.ACTIVE)[1]
+        self.addCleanup(
+            self.wait_zone_delete,
+            self.zone_client, zone['id'])
+        LOG.info(
+            'Create recordset data with:{} records and try to create'
+            ' a recordset. Expected:"413 over_quota"'.format(
+                self.test_quota_limit + 1))
+        recordset_data = dns_data_utils.rand_recordset_data(
+            record_type='A', zone_name=zone['name'],
+            number_of_records=self.test_quota_limit + 1)
+        LOG.info('Try to create a recordset. Expected:"413 over_quota"')
+        with self.assertRaisesDns(
+                lib_exc.OverLimit, 'over_quota', 413):
+            self.recordset_client.create_recordset(
+                zone['id'], recordset_data=recordset_data)
+
+    @decorators.attr(type='slow')
+    @decorators.idempotent_id('893dc648-868d-11ec-8ccb-201e8823901f')
+    def test_zone_records_quota(self):
+        LOG.info('Create a Zone and wait until ACTIVE')
+        zone_name = dns_data_utils.rand_zone_name(
+            name="test_zone_records_quota", suffix=self.tld_name)
+        zone = self.zone_client.create_zone(
+            name=zone_name,
+            description='Zone for test_zone_records_quota',
+            wait_until=const.ACTIVE)[1]
+        self.addCleanup(
+            self.wait_zone_delete,
+            self.zone_client, zone['id'])
+        LOG.info('Admin sets "zone_records:{}" quota for Primary '
+                 'user'.format(self.test_quota_limit))
+        quotas = dns_data_utils.rand_quotas()
+        quotas['zone_records'] = self.test_quota_limit
+        self._set_quota_for_project(
+            self.zone_client.project_id, quotas)
+        LOG.info(
+            'Try to add:{} recordsets (with a single record) to the Zone in'
+            ' loop. Expected:"413 over_quota"'.format(
+                self.test_quota_limit + 1))
+        self._reach_quota_limit(
+            self.test_quota_limit + 1, 'zone_records', zone)
+
+    @decorators.attr(type='slow')
+    @decorators.idempotent_id('f567bdda-86b3-11ec-8ccb-201e8823901f')
+    def test_zone_recordsets_quota(self):
+        LOG.info('Create a Zone and wait until ACTIVE')
+        zone_name = dns_data_utils.rand_zone_name(
+            name="test_zone_recordsets_quota", suffix=self.tld_name)
+        zone = self.zone_client.create_zone(
+            name=zone_name,
+            description='Zone for test_zone_recordsets_quota',
+            wait_until=const.ACTIVE)[1]
+        self.addCleanup(
+            self.wait_zone_delete,
+            self.zone_client, zone['id'])
+        LOG.info('Admin sets "zone_recordsets:{}" quota for Primary '
+                 'user'.format(self.test_quota_limit))
+        quotas = dns_data_utils.rand_quotas()
+        quotas['zone_recordsets'] = self.test_quota_limit
+        self._set_quota_for_project(
+            self.zone_client.project_id, quotas)
+        LOG.info(
+            'Try to add:{} recordsets (with a random number of records) to a'
+            ' Zone in loop. Expected:"413 over_quota"'.format(
+                self.test_quota_limit + 1))
+        self._reach_quota_limit(
+            self.test_quota_limit + 1,
+            'zone_recordsets', zone)
+
+    @decorators.attr(type='slow')
+    @decorators.idempotent_id('6987953a-dccf-11eb-903e-74e5f9e2a801')
+    def test_zones_quota(self):
+        LOG.info('Admin sets "zones" quota for Primary user')
+        quotas = dns_data_utils.rand_quotas()
+        quotas['zones'] = self.test_quota_limit
+        self._set_quota_for_project(
+            self.zone_client.project_id, quotas)
+        LOG.info('Try to create Zones. Expected:"413 over_quota"')
+        self._reach_quota_limit(self.test_quota_limit, 'zones_quota')