Create TLD for DNSIntegrationTests

This is required to make sure concurrent tests that adds tlds
will not conflict.

Related-Prod: PRODX-29414
Change-Id: I3208363bb6997db28f31325634c5414c6a9bcd5e
diff --git a/neutron_tempest_plugin/scenario/test_dns_integration.py b/neutron_tempest_plugin/scenario/test_dns_integration.py
index 6f2756c..623d676 100644
--- a/neutron_tempest_plugin/scenario/test_dns_integration.py
+++ b/neutron_tempest_plugin/scenario/test_dns_integration.py
@@ -41,6 +41,22 @@
     DNSMixin = object
 
 
+def rand_zone_name(name='', prefix='rand', suffix=None):
+    """Generate a random zone name
+
+    :param str name: The name that you want to include
+    :param prefix: the exact text to start the string. Defaults to "rand"
+    :param suffix: the exact text to end the string
+    :return: a random zone name e.g. example.org.
+    :rtype: string
+    """
+
+    if suffix is None:
+        suffix = '.{}.'.format(CONF.dns.tld_suffix)
+    name = data_utils.rand_name(name=name, prefix=prefix)
+    return name + suffix
+
+
 class BaseDNSIntegrationTests(base.BaseTempestTestCase, DNSMixin):
     credentials = ['primary', 'admin']
 
@@ -49,6 +65,12 @@
         super(BaseDNSIntegrationTests, cls).setup_clients()
         cls.dns_client = cls.os_tempest.dns_v2.ZonesClient()
         cls.query_client.build_timeout = 30
+        if CONF.enforce_scope.designate:
+            cls.admin_tld_client = cls.os_system_admin.dns_v2.TldClient()
+            cls.rec_client = cls.os_system_admin.dns_v2.RecordsetClient()
+        else:
+            cls.admin_tld_client = cls.os_admin.dns_v2.TldClient()
+            cls.rec_client = cls.os_admin.dns_v2.RecordsetClient()
 
     @classmethod
     def skip_checks(cls):
@@ -63,7 +85,14 @@
     @utils.requires_ext(extension="dns-integration", service="network")
     def resource_setup(cls):
         super(BaseDNSIntegrationTests, cls).resource_setup()
-        _, cls.zone = cls.dns_client.create_zone()
+        # Make sure we have an allowed TLD available
+        tld_name = rand_zone_name("zonestest", prefix='')
+        cls.tld_name = f".{tld_name}"
+        cls.class_tld = cls.admin_tld_client.create_tld(tld_name=tld_name[:-1])
+
+        zone_name = rand_zone_name(
+            name="dnsinttest", prefix='', suffix=cls.tld_name)
+        _, cls.zone = cls.dns_client.create_zone(name=zone_name)
         cls.addClassResourceCleanup(cls.dns_client.delete_zone,
             cls.zone['id'], ignore_errors=lib_exc.NotFound)
         dns_waiters.wait_for_zone_status(
@@ -76,6 +105,11 @@
         cls.create_router_interface(cls.router['id'], cls.subnet['id'])
         cls.keypair = cls.create_keypair()
 
+    @classmethod
+    def resource_cleanup(cls):
+        cls.admin_tld_client.delete_tld(cls.class_tld[1]['id'])
+        super(BaseDNSIntegrationTests, cls).resource_cleanup()
+
     def _create_floatingip_with_dns(self, dns_name):
         return self.create_floatingip(client=self.os_primary.network_client,
                                       dns_name=dns_name,