diff options
Diffstat (limited to 'testapi')
6 files changed, 154 insertions, 6 deletions
diff --git a/testapi/testapi-client/setup.cfg b/testapi/testapi-client/setup.cfg index f085f59..d4f8704 100644 --- a/testapi/testapi-client/setup.cfg +++ b/testapi/testapi-client/setup.cfg @@ -37,6 +37,11 @@ testapi = scenario getone = testapiclient.cli.scenarios:ScenarioGetOne scenario delete = testapiclient.cli.scenarios:ScenarioDelete scenario put = testapiclient.cli.scenarios:ScenarioPut + + scenario installer create = testapiclient.cli.scenarios:InstallerCreate + scenario installer delete = testapiclient.cli.scenarios:InstallerDelete + scenario installer put = testapiclient.cli.scenarios:InstallerPut + [egg_info] tag_build = tag_date = 0 diff --git a/testapi/testapi-client/testapiclient/cli/scenarios.py b/testapi/testapi-client/testapiclient/cli/scenarios.py index fc6a2db..3f70772 100644 --- a/testapi/testapi-client/testapiclient/cli/scenarios.py +++ b/testapi/testapi-client/testapiclient/cli/scenarios.py @@ -12,6 +12,10 @@ def scenario_url(parsed_args): return urlparse.path_join(scenarios_url(), parsed_args.name) +def resources_url(name, resuorce): + return urlparse.resource_join('scenarios', name, resuorce) + + class ScenarioGet(command.Lister): def get_parser(self, prog_name): @@ -129,3 +133,64 @@ class ScenarioPut(command.ShowOne): return self.format_output( self.app.client_manager.put( scenario_url(parsed_args), parsed_args.scenario)) + + +class InstallerCreate(command.Command): + + def get_parser(self, prog_name): + parser = super(InstallerCreate, self).get_parser(prog_name) + parser.add_argument('--scenario-name', + required=True, + help='Create installer under scenario name') + parser.add_argument('installer', + type=json.loads, + help='Intaller create request format :\n' + '\'[{"installer": "","versions": []}]\',\n') + return parser + + def take_action(self, parsed_args): + return self.app.client_manager.post( + resources_url( + parsed_args.scenario_name, + 'installers'), parsed_args.installer) + + +class InstallerDelete(command.Command): + + def get_parser(self, prog_name): + parser = super(InstallerDelete, self).get_parser(prog_name) + parser.add_argument('--scenario-name', + required=True, + type=str, + help='Delete installer by scenario name') + parser.add_argument('name', + nargs='+', + help='Delete installer by name') + return parser + + def take_action(self, parsed_args): + return self.app.client_manager.delete( + resources_url( + parsed_args.scenario_name, + 'installers'), parsed_args.name) + + +class InstallerPut(command.Command): + + def get_parser(self, prog_name): + parser = super(InstallerPut, self).get_parser(prog_name) + parser.add_argument('--scenario-name', + type=str, + required=True, + help='Update installer by scenario name') + parser.add_argument('installer', + type=json.loads, + help='Intaller create request format :\n' + '\'[{"installer": "","versions": []}]\',\n') + return parser + + def take_action(self, parsed_args): + return self.app.client_manager.put( + resources_url( + parsed_args.scenario_name, + 'installers'), parsed_args.installer) diff --git a/testapi/testapi-client/testapiclient/tests/unit/test_scenario.py b/testapi/testapi-client/testapiclient/tests/unit/test_scenario.py index 2458d2b..b14cf04 100644 --- a/testapi/testapi-client/testapiclient/tests/unit/test_scenario.py +++ b/testapi/testapi-client/testapiclient/tests/unit/test_scenario.py @@ -45,11 +45,6 @@ class ScenarioGetTest(ScenarioTest): args[0], self.base_url + '?version=v1&name=s1&installer=i1&project=p1') - def assert_url(self, actual_url, expected_url): - actual_parsed = parse.parse_qs(parse.urlparse(actual_url).query) - expected_parsed = parse.parse_qs(parse.urlparse(expected_url).query) - assert actual_parsed == expected_parsed - def test_get_all(self): self.get_mock.return_value = fk.FakeResponse(data=self.scenarios_rsp) scenario_get = scenarios.ScenarioGet(self.app, mock.Mock()) diff --git a/testapi/testapi-client/testapiclient/tests/unit/test_scenario_installer.py b/testapi/testapi-client/testapiclient/tests/unit/test_scenario_installer.py new file mode 100644 index 0000000..2246a59 --- /dev/null +++ b/testapi/testapi-client/testapiclient/tests/unit/test_scenario_installer.py @@ -0,0 +1,74 @@ +import json + +from mock import mock +from six.moves.urllib import parse + +from testapiclient.cli import scenarios +from testapiclient.tests.unit import fakes +from testapiclient.tests.unit import utils +from testapiclient.utils import clientmanager + + +class InstallerTest(utils.TestCommand): + def setUp(self): + super(InstallerTest, self).setUp() + self.base_url = parse.urljoin(self.api_url, 'scenarios/{}/installers') + self.scenario_name = 's1' + self.installer_json = { + 'versions': [], + 'installer': 'test-installer', + } + self.installer_string = json.dumps(self.installer_json) + + +class InstallerCreateTest(InstallerTest): + + def setUp(self): + super(InstallerCreateTest, self).setUp() + + def test_create_success(self): + self.post_mock.return_value = fakes.FakeResponse(data=None) + installer_create = scenarios.InstallerCreate(self.app, mock.Mock()) + args = ['--scenario-name', 's1', self.installer_string] + verifies = [ + ('scenario_name', 's1'), + ('installer', self.installer_json)] + parsed_args = self.check_parser(installer_create, args, verifies) + installer_create.take_action(parsed_args) + self.post_mock.assert_called_once() + + +class InstallerDeleteTest(InstallerTest): + + def setUp(self): + super(InstallerDeleteTest, self).setUp() + + def test_delete_success(self): + self.delete_mock.return_value = fakes.FakeResponse(data=None) + installer_delete = scenarios.InstallerDelete(self.app, mock.Mock()) + args = ['--scenario-name', 's1', 'def'] + verifies = [('scenario_name', 's1'), ('name', ['def'])] + parsed_args = self.check_parser(installer_delete, args, verifies) + installer_delete.take_action(parsed_args) + self.delete_mock.assert_called_once_with( + self.base_url.format(parsed_args.scenario_name), + data=json.dumps(['def']), + headers=clientmanager.ClientManager.headers) + + +class InstallerPutTest(InstallerTest): + + def setUp(self): + super(InstallerPutTest, self).setUp() + + def test_put_success(self): + self.put_mock.return_value = fakes.FakeResponse( + data=None) + installer_put = scenarios.InstallerPut(self.app, mock.Mock()) + args = ['--scenario-name', 's1', self.installer_string] + verifies = [ + ('scenario_name', 's1'), + ('installer', self.installer_json)] + parsed_args = self.check_parser(installer_put, args, verifies) + installer_put.take_action(parsed_args) + self.put_mock.assert_called_once() diff --git a/testapi/testapi-client/testapiclient/tests/unit/utils.py b/testapi/testapi-client/testapiclient/tests/unit/utils.py index 20f9a47..21f98c4 100644 --- a/testapi/testapi-client/testapiclient/tests/unit/utils.py +++ b/testapi/testapi-client/testapiclient/tests/unit/utils.py @@ -1,6 +1,7 @@ import httplib from mock import mock +from six.moves.urllib import parse import testtools from testapiclient.tests.unit import fakes @@ -50,3 +51,8 @@ class TestCommand(testtools.TestCase): self.assertIn(attr, parsed_args) self.assertEqual(value, getattr(parsed_args, attr)) return parsed_args + + def assert_url(self, actual_url, expected_url): + actual_parsed = parse.parse_qs(parse.urlparse(actual_url).query) + expected_parsed = parse.parse_qs(parse.urlparse(expected_url).query) + assert actual_parsed == expected_parsed diff --git a/testapi/testapi-client/testapiclient/utils/clientmanager.py b/testapi/testapi-client/testapiclient/utils/clientmanager.py index 4401231..8858819 100644 --- a/testapi/testapi-client/testapiclient/utils/clientmanager.py +++ b/testapi/testapi-client/testapiclient/utils/clientmanager.py @@ -75,6 +75,9 @@ class ClientManager(object): def _parse_response(self, op, response): if response.status_code == httplib.OK: - return response.json() if op != 'Delete' else None + if op != 'Delete' and response.text != '': + return response.json() + else: + return None else: self._raise_failure(op, response) |