From d7b3e7b890fa21a018900e459c8b85e7a8f68332 Mon Sep 17 00:00:00 2001 From: thuva4 Date: Thu, 26 Apr 2018 13:15:03 +0530 Subject: Add get functionality for the import module Tested Module will deserialize the json objects from the server to Pod instance. Change-Id: I3e2cb9386f8949d544624be687ee227ae4529d72 Signed-off-by: thuva4 --- .../developer/devguide/testapi-client-import.rst | 65 ++++++++++++++++ testapi/testapi-client/testapiclient/cli/pods.py | 7 +- .../testapi-client/testapiclient/client/pods.py | 20 +++++ .../testapi-client/testapiclient/models/pods.py | 2 +- .../testapiclient/tests/unit/test_pod_client.py | 89 ++++++++++++++++++++++ 5 files changed, 180 insertions(+), 3 deletions(-) create mode 100644 testapi/docs/developer/devguide/testapi-client-import.rst create mode 100644 testapi/testapi-client/testapiclient/tests/unit/test_pod_client.py diff --git a/testapi/docs/developer/devguide/testapi-client-import.rst b/testapi/docs/developer/devguide/testapi-client-import.rst new file mode 100644 index 0000000..69cb6ad --- /dev/null +++ b/testapi/docs/developer/devguide/testapi-client-import.rst @@ -0,0 +1,65 @@ +.. This work is licensed under a Creative Commons Attribution 4.0 International License. +.. http://creativecommons.org/licenses/by/4.0 +.. (c) 2017 ZTE Corp. + +===================== +TestAPI client import +===================== + +**Python module to communicate with the TestAPI Server** + +This project aims to provide a python module which can +communicate with the TestAPI Server. The user can use this client +to fetch/post/modify the resources on the TestAPI Server. + +Usage +----- + +Pod +^^^ + +GET +""" + +User will get the json Pod objects with the get request. + +.. code-block:: shell + + from testapiclient.client import pods + + pod_client = pods.PodsClient() + pod_client.get() + +User can use search parameters to get pods + +.. code-block:: shell + + from testapiclient.client import pods + + pod_client = pods.PodsClient() + pod_client.get(name='pod1') + +GET ONE +""""""" + +User will get the json Pod objects with the get request. + +.. code-block:: shell + + from testapiclient.client import pods + + pod_client = pods.PodsClient() + pod_client.get_one('name') + +CREATE +"""""" +User has to authenticate before running the function. + +.. code-block:: shell + + from testapiclient.client import pods + + pod_client = pods.PodsClient(user='test', password='pass') + pod_client.create({'name': 'test-api', 'mode':'metal', + 'role':'community_ci', 'details':''} + diff --git a/testapi/testapi-client/testapiclient/cli/pods.py b/testapi/testapi-client/testapiclient/cli/pods.py index df63737..a7706f6 100644 --- a/testapi/testapi-client/testapiclient/cli/pods.py +++ b/testapi/testapi-client/testapiclient/cli/pods.py @@ -3,6 +3,7 @@ import json from testapiclient.client import pods from testapiclient.utils import command from testapiclient.utils import urlparse +from testapiclient.models import pods as pm def pods_url(): @@ -61,8 +62,10 @@ class PodCreate(command.ShowOne): parser.add_argument('pod', type=json.loads, help='Pod create request format :\n' - '\'{"role": "", "name": "", "details": "", ' - '"mode": ""}\',\n role should be either ' + '\'{}\''.format(json.dumps( + pm.PodCreateRequest().__dict__ + )) + + '\n role should be either ' '"community-ci" or "production-ci", and ' 'mode should be either "metal" or "virtual.') return parser diff --git a/testapi/testapi-client/testapiclient/client/pods.py b/testapi/testapi-client/testapiclient/client/pods.py index 4254da7..d08114f 100644 --- a/testapi/testapi-client/testapiclient/client/pods.py +++ b/testapi/testapi-client/testapiclient/client/pods.py @@ -1,4 +1,7 @@ +import json + from testapiclient.client import base +from testapiclient.utils import urlparse class PodsClient(base.Client): @@ -9,3 +12,20 @@ class PodsClient(base.Client): def create(self, pod_req): return self.clientmanager.post(self.url, pod_req) + + def get(self, **queries): + if queries: + return json.dumps( + self.clientmanager.get( + urlparse.query_join(self.url, **queries))['pods']) + else: + return json.dumps( + self.clientmanager.get(self.url)['pods']) + + def get_one(self, name): + return json.dumps(self.clientmanager.get( + urlparse.path_join(self.url, name))) + + def delete(self, name): + return self.clientmanager.delete( + urlparse.path_join(self.url, name)) diff --git a/testapi/testapi-client/testapiclient/models/pods.py b/testapi/testapi-client/testapiclient/models/pods.py index 27ea311..4fa42e7 100644 --- a/testapi/testapi-client/testapiclient/models/pods.py +++ b/testapi/testapi-client/testapiclient/models/pods.py @@ -1,5 +1,5 @@ class PodCreateRequest(object): - def __init__(self, name='', mode='', details='', role=""): + def __init__(self, name='', mode='', details='', role=''): self.name = name self.mode = mode self.details = details diff --git a/testapi/testapi-client/testapiclient/tests/unit/test_pod_client.py b/testapi/testapi-client/testapiclient/tests/unit/test_pod_client.py new file mode 100644 index 0000000..1df5660 --- /dev/null +++ b/testapi/testapi-client/testapiclient/tests/unit/test_pod_client.py @@ -0,0 +1,89 @@ +import json + +from six.moves.urllib import parse +import testtools + +from testapiclient.client import pods +from testapiclient.tests.unit import fakes +from testapiclient.tests.unit import utils +from testapiclient.utils import clientmanager + + +class PodClientTest(utils.TestCommand): + def setUp(self): + super(PodClientTest, self).setUp() + self.base_url = parse.urljoin(self.api_url, 'pods') + self.pod_json = { + 'role': 'community-ci', + 'name': 'test_pod', + 'details': '', + 'mode': 'metal' + } + self.pod_client = pods.PodsClient() + self.pod_string = json.dumps(self.pod_json) + + +class PodClientGetTest(PodClientTest): + + def setUp(self): + super(PodClientGetTest, self).setUp() + self.pods_rsp = {'pods': [self.pod_json]} + + def test_get(self): + self.get_mock.return_value = fakes.FakeResponse(data=self.pods_rsp) + self.pod_client.get() + self.get_mock.assert_called_once_with( + self.base_url, + headers=clientmanager.ClientManager.headers) + + def test_get_search(self): + self.get_mock.return_value = fakes.FakeResponse(data=self.pods_rsp) + self.pod_client.get(name='pod1') + self.get_mock.assert_called_once_with( + self.base_url + '?name=pod1', + headers=clientmanager.ClientManager.headers) + + def test_get_one(self): + self.get_mock.return_value = fakes.FakeResponse(data=self.pod_json) + self.pod_client.get_one('def') + self.get_mock.assert_called_once_with( + self.base_url + '/def', + headers=clientmanager.ClientManager.headers) + + +class PodClientCreateTest(PodClientTest): + + def setUp(self): + super(PodClientCreateTest, self).setUp() + self.succ_rsp = { + 'href': '{}/{}'.format(self.base_url, self.pod_json.get('name')) + } + + def test_create_success(self): + self.post_mock.return_value = fakes.FakeResponse(data=self.succ_rsp) + self.pod_client.create(self.pod_json) + self.post_mock.assert_called_once() + + def test_create_failure(self): + with testtools.ExpectedException(Exception, 'Create failed: Error'): + self.post_mock.return_value = utils.FAKE_FAILURE + self.pod_client.create(self.pod_json) + + +class PodClientDeleteTest(PodClientTest): + + def setUp(self): + super(PodClientDeleteTest, self).setUp() + + def test_delete_success(self): + self.delete_mock.return_value = fakes.FakeResponse() + self.pod_client.delete('def') + self.delete_mock.assert_called_once_with( + self.base_url + '/def', + data=None, + headers=clientmanager.ClientManager.headers) + + def test_delete_failure(self): + with testtools.ExpectedException(Exception, 'Delete failed: Error'): + self.delete_mock.return_value = utils.FAKE_FAILURE + self.pod_client.delete('def') -- cgit 1.2.3-korg