Add tld_client's methods and tests to Designate tempest plugin

Partially-Implements: blueprint designate-tempest-plugin

Change-Id: Ic23a9249d0cf5c34e5c2c083b465200fa6c5da66
diff --git a/designate_tempest_plugin/clients.py b/designate_tempest_plugin/clients.py
index 5edc75a..9e64b15 100644
--- a/designate_tempest_plugin/clients.py
+++ b/designate_tempest_plugin/clients.py
@@ -28,6 +28,8 @@
     RecordsetClient
 from designate_tempest_plugin.services.dns.v2.json.pool_client import \
     PoolClient
+from designate_tempest_plugin.services.dns.v2.json.tld_client import \
+    TldClient
 
 CONF = config.CONF
 
@@ -56,3 +58,5 @@
                                                 **params)
         self.pool_client = PoolClient(self.auth_provider,
                                       **params)
+        self.tld_client = TldClient(self.auth_provider,
+                                    **params)
diff --git a/designate_tempest_plugin/data_utils.py b/designate_tempest_plugin/data_utils.py
index d3c72cc..f18e87c 100644
--- a/designate_tempest_plugin/data_utils.py
+++ b/designate_tempest_plugin/data_utils.py
@@ -206,3 +206,10 @@
     ns_records = [{"hostname": x, "priority": random.randint(1, 999)}
                   for x in records]
     return ns_records
+
+
+def rand_tld():
+    data = {
+        "name": rand_zone_name(prefix='tld', suffix='')
+    }
+    return data
diff --git a/designate_tempest_plugin/services/dns/v2/json/tld_client.py b/designate_tempest_plugin/services/dns/v2/json/tld_client.py
new file mode 100644
index 0000000..d6b3c66
--- /dev/null
+++ b/designate_tempest_plugin/services/dns/v2/json/tld_client.py
@@ -0,0 +1,103 @@
+# Copyright 2016 NEC Corporation.  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.
+
+from tempest.lib.common.utils import data_utils
+
+from designate_tempest_plugin.services.dns.v2.json import base
+
+
+class TldClient(base.DnsClientV2Base):
+    """API V2 Tempest REST client for Designate Tld API"""
+
+    @base.handle_errors
+    def create_tld(self, tld_name=None, description=None, params=None):
+        """Create a tld with the specified parameters.
+        :param tld_name: Name of the tld. e.g .com .
+        :param description: represents details of tld.
+        :param params: A Python dict that represents the query paramaters to
+                       include in the request URI.
+        :return: A tuple with the server response and the created tld.
+        """
+        tld = {
+                "name": tld_name or data_utils.rand_name(name="tld"),
+                "description": description or data_utils.rand_name(
+                               name="description")
+        }
+
+        resp, body = self._create_request('tlds', object_dict=tld,
+                                          params=params)
+
+        # Create Tld should Return a HTTP 201
+        self.expected_success(201, resp.status)
+
+        return resp, body
+
+    @base.handle_errors
+    def show_tld(self, uuid, params=None):
+        """Gets a specific tld.
+        :param uuid: Unique identifier of the tld in UUID format.
+        :param params: A Python dict that represents the query paramaters to
+                       include in the request URI.
+        :return: Serialized tld as a dictionary.
+        """
+        return self._show_request('tlds', uuid, params=params)
+
+    @base.handle_errors
+    def list_tlds(self, params=None):
+        """Gets a list of tlds.
+        :param params: A Python dict that represents the query paramaters to
+                       include in the request URI.
+        :return: Serialized tlds as a list.
+        """
+        return self._list_request('tlds', params=params)
+
+    @base.handle_errors
+    def delete_tld(self, uuid, params=None):
+        """Deletes a tld having the specified UUID.
+        :param uuid: The unique identifier of the tld.
+        :param params: A Python dict that represents the query paramaters to
+                       include in the request URI.
+        :return: A tuple with the server response and the response body.
+        """
+        resp, body = self._delete_request('tlds', uuid, params=params)
+
+        # Delete Tld should Return a HTTP 204
+        self.expected_success(204, resp.status)
+
+        return resp, body
+
+    @base.handle_errors
+    def update_tld(self, uuid, tld_name=None, description=None, params=None):
+        """Update a tld with the specified parameters.
+        :param uuid: The unique identifier of the tld.
+        :param tld_name: Name of the tld. e.g .com .
+        :param description: represents info about tld.
+        :param params: A Python dict that represents the query paramaters to
+                       include in the request URI.
+        :return: A tuple with the server response and the updated tld.
+        """
+
+        tld = {
+                 "name": tld_name or data_utils.rand_name(name="tld"),
+                 "description": description or data_utils.rand_name(
+                                name="description")
+        }
+
+        resp, body = self._update_request('tlds', uuid, tld,
+                                          params=params)
+
+        # Update Tld should Return a HTTP 200
+        self.expected_success(200, resp.status)
+
+        return resp, body
diff --git a/designate_tempest_plugin/tests/api/v2/test_tld.py b/designate_tempest_plugin/tests/api/v2/test_tld.py
new file mode 100644
index 0000000..46e5b48
--- /dev/null
+++ b/designate_tempest_plugin/tests/api/v2/test_tld.py
@@ -0,0 +1,120 @@
+# Copyright 2016 NEC Corporation.  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.
+
+from oslo_log import log as logging
+from tempest import test
+from tempest.lib import exceptions as lib_exc
+
+from designate_tempest_plugin.tests import base
+
+LOG = logging.getLogger(__name__)
+
+
+class BaseTldTest(base.BaseDnsTest):
+    excluded_keys = ['created_at', 'updated_at', 'links']
+
+
+class TldAdminTest(BaseTldTest):
+    credentials = ['admin']
+
+    @classmethod
+    def setup_clients(cls):
+        super(TldAdminTest, cls).setup_clients()
+        cls.admin_client = cls.os_adm.tld_client
+
+    @classmethod
+    def resource_setup(cls):
+        super(TldAdminTest, cls).resource_setup()
+        cls.tld = cls.admin_client.create_tld(tld_name='com',
+                      ignore_errors=lib_exc.Conflict)
+
+    @test.attr(type='smoke')
+    @test.idempotent_id('52a4bb4b-4eff-4591-9dd3-ad98316806c3')
+    def test_create_tld(self):
+        tld_data = {
+                     "name": "org",
+                     "description": "sample tld"}
+
+        LOG.info('Create a tld')
+        _, tld = self.admin_client.create_tld(tld_data['name'],
+                                        tld_data['description'])
+        self.addCleanup(self.admin_client.delete_tld, tld['id'])
+
+        self.assertEqual(tld_data["name"], tld['name'])
+
+    @test.attr(type='smoke')
+    @test.idempotent_id('271af08c-2603-4f61-8eb1-05887b74e25a')
+    def test_show_tld(self):
+        tld_data = {
+                     "name": "org",
+                     "description": "sample tld"}
+
+        LOG.info('Create a tld')
+        _, tld = self.admin_client.create_tld(tld_data['name'],
+                                        tld_data['description'])
+        self.addCleanup(self.admin_client.delete_tld, tld['id'])
+
+        LOG.info('Fetch the tld')
+        _, body = self.admin_client.show_tld(tld['id'])
+
+        LOG.info('Ensure the fetched response matches the created tld')
+        # self.assertExpected(tld, body, self.excluded_keys)
+
+    @test.attr(type='smoke')
+    @test.idempotent_id('26708cb8-7126-48a7-9424-1c225e56e609')
+    def test_delete_tld(self):
+        LOG.info('Create a tld')
+        _, tld = self.admin_client.create_tld()
+        self.addCleanup(self.admin_client.delete_tld, tld['id'],
+                        ignore_errors=lib_exc.NotFound)
+
+        LOG.info('Delete the tld')
+        _, body = self.admin_client.delete_tld(tld['id'])
+
+        self.assertRaises(lib_exc.NotFound,
+           lambda: self.admin_client.show_tld(tld['id']))
+
+    @test.attr(type='smoke')
+    @test.idempotent_id('95b13759-c85c-4791-829b-9591ca15779d')
+    def test_list_tlds(self):
+        LOG.info('List tlds')
+        _, body = self.admin_client.list_tlds()
+
+        self.assertGreater(len(body['tlds']), 0)
+
+    @test.attr(type='smoke')
+    @test.idempotent_id('1a233812-48d9-4d15-af5e-9961744286ff')
+    def test_update_tld(self):
+        _, tld = self.admin_client.create_tld()
+        self.addCleanup(self.admin_client.delete_tld, tld['id'])
+
+        tld_data = {
+                     "name": "org",
+                     "description": "Updated description"
+        }
+
+        LOG.info('Update the tld')
+        _, patch_tld = self.admin_client.update_tld(tld['id'],
+                       tld_data['name'], tld_data['description'])
+
+        self.assertEqual(tld_data["name"], patch_tld["name"])
+        self.assertEqual(tld_data["description"], patch_tld["description"])
+
+    @test.attr(type='smoke')
+    @test.idempotent_id('8116dcf5-a329-47d1-90be-5ff32f299c53')
+    def test_list_tlds_dot_json_fails(self):
+        uri = self.admin_client.get_uri('tlds.json')
+
+        self.assertRaises(lib_exc.NotFound,
+            lambda: self.admin_client.get(uri))