From 11fe6cc670696e5e9ae73b9501e05fa19eeb6e13 Mon Sep 17 00:00:00 2001 From: thuva4 Date: Tue, 10 Apr 2018 10:48:17 +0530 Subject: Add CRUD operations for deployresults with tests Change-Id: I679b6c1b0723ed3df2a9b0e6af5d9a149ede9987 Signed-off-by: thuva4 --- .../testapiclient/cli/deployresults.py | 100 +++++++++++++++++++ .../testapiclient/tests/unit/test_deployresults.py | 109 +++++++++++++++++++++ 2 files changed, 209 insertions(+) create mode 100644 testapi/testapi-client/testapiclient/cli/deployresults.py create mode 100644 testapi/testapi-client/testapiclient/tests/unit/test_deployresults.py (limited to 'testapi/testapi-client/testapiclient') diff --git a/testapi/testapi-client/testapiclient/cli/deployresults.py b/testapi/testapi-client/testapiclient/cli/deployresults.py new file mode 100644 index 0000000..a6fe13e --- /dev/null +++ b/testapi/testapi-client/testapiclient/cli/deployresults.py @@ -0,0 +1,100 @@ +import json + +from testapiclient.utils import command +from testapiclient.utils import urlparse + + +def deployresults_url(): + return urlparse.resource_join('deployresults') + + +def deployresult_url(parsed_args): + return urlparse.path_join(deployresults_url(), parsed_args.deployresult_id) + + +class DeployresultGet(command.Lister): + + def get_parser(self, prog_name): + parser = super(DeployresultGet, self).get_parser(prog_name) + parser.add_argument('-build-id', + help='Search deployresults using build tag') + parser.add_argument('-from', + help='Search deployresults using from date') + parser.add_argument('-scenario', + help='Search deployresults using scenario') + parser.add_argument('-period', + help='Search deployresults using period') + parser.add_argument('-page', + help='Search deployresults using page') + parser.add_argument('-to', + help='Search deployresults using to') + parser.add_argument('---version', + help='Search deployresults using version') + parser.add_argument('-last', + help='Search deployresults using last date') + parser.add_argument('-pod-name', + help='Search deployresults using pod') + parser.add_argument('-criteria', + help='Search deployresults using version') + parser.add_argument('-installer', + help='Search deployresults using installer') + parser.add_argument('-job-name', + help='Search deployresults using project') + + return parser + + def take_action(self, parsed_args): + columns = ( + '_id', + 'pod_name', + 'version', + 'criteria', + 'start_date', + 'stop_date', + 'scenario', + 'installer', + + ) + data = self.app.client_manager.get( + urlparse.query_by(deployresults_url(), + ['build_id', 'from', 'last', + 'scenario', 'period', 'job_name', + 'to', 'version', + 'criteria', 'installer', 'pod_name', 'page'], + parsed_args)) + return self.format_output(columns, data.get('deployresults', [])) + + +class DeployresultGetOne(command.ShowOne): + + def get_parser(self, prog_name): + parser = super(DeployresultGetOne, self).get_parser(prog_name) + parser.add_argument('deployresult_id', + help='Search deployresult by deployresult id') + return parser + + def take_action(self, parsed_args): + return self.format_output( + self.app.client_manager.get(deployresult_url(parsed_args))) + + +class DeployresultCreate(command.ShowOne): + + def get_parser(self, prog_name): + parser = super(DeployresultCreate, self).get_parser(prog_name) + parser.add_argument('deployresult', + type=json.loads, + help='Deployresult create request format:\n' + '\'{"job_name" : "","scenario" : "",' + '"stop_date" : "", "build_id" : "",' + '"upstream_job_name": "",' + '"version" : "", "pod_name" : "",' + '"criteria" : "", "installer" : "",' + '"upstream_build_id" : "",' + '"start_date" : "", "details" : ""}\'') + return parser + + def take_action(self, parsed_args): + return self.format_output( + self.app.client_manager.post( + deployresults_url(), parsed_args.deployresult)) diff --git a/testapi/testapi-client/testapiclient/tests/unit/test_deployresults.py b/testapi/testapi-client/testapiclient/tests/unit/test_deployresults.py new file mode 100644 index 0000000..0e0385b --- /dev/null +++ b/testapi/testapi-client/testapiclient/tests/unit/test_deployresults.py @@ -0,0 +1,109 @@ +import json + +from mock import mock +from six.moves.urllib import parse +import testtools + +from testapiclient.cli import deployresults +from testapiclient.tests.unit import fakes +from testapiclient.tests.unit import utils +from testapiclient.utils import clientmanager + + +class DeployresultTest(utils.TestCommand): + def setUp(self): + super(DeployresultTest, self).setUp() + self.base_url = parse.urljoin(self.api_url, 'deployresults') + self.deployresult_json = { + 'job_name': 'daisy-deploy', + 'scenario': 'test-scenario', + 'stop_date': '2018-04-09 13:44:53', + 'upstream_job_name': 'test-job', + 'build_id': 'test-build', + 'version': 'test-version', + 'pod_name': 'test-pod', + 'criteria': 'test-criteria', + 'installer': 'test-installer', + 'start_date': '2018-04-09 13:44:53', + 'details': 'test-details', + 'upstream_build_id': 'test-stream' + } + self.deployresult_string = json.dumps(self.deployresult_json) + + +class DeployresultGetTest(DeployresultTest): + + def setUp(self): + super(DeployresultGetTest, self).setUp() + self.deployresults_rsp = {'deployresults': [self.deployresult_json]} + + def test_get(self): + self.get_mock.return_value = fakes.FakeResponse( + data=self.deployresults_rsp) + deployresult_get = deployresults.DeployresultGet( + self.app, mock.Mock()) + args = ['-job-name', 'dfs'] + verifies = [('job_name', 'dfs')] + parsed_args = self.check_parser(deployresult_get, args, verifies) + deployresult_get.take_action(parsed_args) + self.get_mock.assert_called_once_with( + self.base_url + '?job_name=dfs', + headers=clientmanager.ClientManager.headers) + + def test_get_all(self): + self.get_mock.return_value = fakes.FakeResponse( + data=self.deployresults_rsp) + deployresult_get = deployresults.DeployresultGet( + self.app, mock.Mock()) + args = [] + verifies = [] + parsed_args = self.check_parser(deployresult_get, args, verifies) + deployresult_get.take_action(parsed_args) + self.get_mock.assert_called_once_with( + self.base_url, + headers=clientmanager.ClientManager.headers) + + def test_get_one(self): + self.get_mock.return_value = fakes.FakeResponse( + data=self.deployresult_json) + deployresult_get_one = deployresults.DeployresultGetOne( + self.app, mock.Mock()) + args = ['def'] + verifies = [('deployresult_id', 'def')] + parsed_args = self.check_parser( + deployresult_get_one, args, verifies) + deployresult_get_one.take_action(parsed_args) + self.get_mock.assert_called_once_with( + self.base_url + '/def', + headers=clientmanager.ClientManager.headers) + + +class DeployresultCreateTest(DeployresultTest): + + def setUp(self): + super(DeployresultCreateTest, self).setUp() + + def test_create_success(self): + succ_rsp = { + 'href': '{}/{}'.format(self.base_url, + self.deployresult_json.get('project_name')) + } + self.post_mock.return_value = fakes.FakeResponse(data=succ_rsp) + deployresult_create = deployresults.DeployresultCreate( + self.app, mock.Mock()) + args = [self.deployresult_string] + verifies = [('deployresult', self.deployresult_json)] + parsed_args = self.check_parser(deployresult_create, args, verifies) + deployresult_create.take_action(parsed_args) + 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 + deployresult_create = deployresults.DeployresultCreate( + self.app, mock.Mock()) + args = [self.deployresult_string] + verifies = [('deployresult', self.deployresult_json)] + parsed_args = self.check_parser( + deployresult_create, args, verifies) + deployresult_create.take_action(parsed_args) -- cgit 1.2.3-korg