From 40e6be281c0139690c1786c3ad0acc4c51c68968 Mon Sep 17 00:00:00 2001 From: Thomas Maddox Date: Mon, 6 Mar 2017 18:09:06 +0000 Subject: [PATCH] Ensure consistent vars shell behavior across resources This patch DRYs out the variables shell test cases even further to ensure the same pattern of behavior across all resources' variables shell implementations. Closes-Bug: 1670446 Change-Id: I69cc3ac025dfdcd0b0bbe2084125014cb7f659e9 --- cratonclient/tests/integration/shell/base.py | 82 ++++++++++++++++++- .../integration/shell/v1/test_cells_shell.py | 75 +---------------- .../integration/shell/v1/test_clouds_shell.py | 75 +---------------- .../integration/shell/v1/test_hosts_shell.py | 75 +---------------- .../shell/v1/test_projects_shell.py | 75 +---------------- .../shell/v1/test_regions_shell.py | 75 +---------------- 6 files changed, 95 insertions(+), 362 deletions(-) diff --git a/cratonclient/tests/integration/shell/base.py b/cratonclient/tests/integration/shell/base.py index a8c7b4e..682af39 100644 --- a/cratonclient/tests/integration/shell/base.py +++ b/cratonclient/tests/integration/shell/base.py @@ -17,6 +17,7 @@ import six from cratonclient.shell import main from cratonclient.tests import base +from cratonclient.v1 import variables class ShellTestCase(base.TestCase): @@ -41,9 +42,11 @@ class VariablesTestCase(base.TestCase): def setUp(self): """Basic set up for all tests in this suite.""" super(VariablesTestCase, self).setUp() - self.resource_url = 'http://127.0.0.1/v1/hosts/1' + self.resources = '{}s'.format(self.resource) + self.resource_url = 'http://127.0.0.1/v1/{}/{}' \ + .format(self.resources, self.resource_id) self.variables_url = '{}/variables'.format(self.resource_url) - self.test_args = Namespace(id=1, formatter=mock.Mock()) + self.test_args = Namespace(id=self.resource_id, formatter=mock.Mock()) # NOTE(thomasem): Make all calls seem like they come from CLI args self.stdin_patcher = \ @@ -59,7 +62,82 @@ class VariablesTestCase(base.TestCase): self.mock_delete_response = self.mock_session.delete.return_value self.mock_delete_response.status_code = 204 + # NOTE(thomasem): Mock out a client to assert craton Python API calls + self.client = mock.Mock() + mock_resource = \ + getattr(self.client, self.resources).get.return_value + mock_resource.variables = variables.VariableManager( + self.mock_session, self.resource_url + ) + def tearDown(self): """Clean up between tests.""" super(VariablesTestCase, self).tearDown() self.stdin_patcher.stop() + + def _get_shell_func_for(self, suffix): + return getattr( + self.shell, + 'do_{}_vars_{}'.format(self.resource, suffix) + ) + + def test_do_vars_get_gets_correct_resource(self): + """Assert the proper resource is retrieved when calling get.""" + self.mock_get_response.json.return_value = \ + {"variables": {"foo": "bar"}} + self._get_shell_func_for('get')(self.client, self.test_args) + getattr(self.client, self.resources).get.assert_called_once_with( + vars(self.test_args)['id']) + + def test_do_vars_delete_gets_correct_resource(self): + """Assert the proper resource is retrieved when calling delete.""" + self.test_args.variables = ['foo', 'bar'] + self._get_shell_func_for('delete')(self.client, self.test_args) + getattr(self.client, self.resources).get.assert_called_once_with( + vars(self.test_args)['id']) + + def test_do_vars_update_gets_correct_resource(self): + """Assert the proper resource is retrieved when calling update.""" + self.test_args.variables = ['foo=', 'bar='] + mock_resp_json = {"variables": {"foo": "bar"}} + self.mock_get_response.json.return_value = mock_resp_json + self.mock_put_response.json.return_value = mock_resp_json + + self._get_shell_func_for('set')(self.client, self.test_args) + getattr(self.client, self.resources).get.assert_called_once_with( + vars(self.test_args)['id']) + + def test_do_vars_get_calls_session_get(self): + """Assert the proper resource is retrieved when calling get.""" + self.mock_get_response.json.return_value = \ + {"variables": {"foo": "bar"}} + self._get_shell_func_for('get')(self.client, self.test_args) + self.mock_session.get.assert_called_once_with(self.variables_url) + + def test_do_vars_delete_calls_session_delete(self): + """Verify that -vars-delete calls expected session.delete.""" + self.test_args.variables = ['foo', 'bar'] + self._get_shell_func_for('delete')(self.client, self.test_args) + self.mock_session.delete.assert_called_once_with( + self.variables_url, + json=('foo', 'bar'), + params={}, + ) + + def test_do_vars_update_calls_session_put(self): + """Verify that -vars-delete calls expected session.delete.""" + self.test_args.variables = ['foo=baz', 'bar=boo', 'test='] + mock_resp_json = {"variables": {"foo": "bar"}} + self.mock_get_response.json.return_value = mock_resp_json + self.mock_put_response.json.return_value = mock_resp_json + + self._get_shell_func_for('set')(self.client, self.test_args) + self.mock_session.delete.assert_called_once_with( + self.variables_url, + json=('test',), + params={}, + ) + self.mock_session.put.assert_called_once_with( + self.variables_url, + json={'foo': 'baz', 'bar': 'boo'} + ) diff --git a/cratonclient/tests/integration/shell/v1/test_cells_shell.py b/cratonclient/tests/integration/shell/v1/test_cells_shell.py index 7f40a4e..e28bc0f 100644 --- a/cratonclient/tests/integration/shell/v1/test_cells_shell.py +++ b/cratonclient/tests/integration/shell/v1/test_cells_shell.py @@ -22,7 +22,6 @@ from cratonclient import exceptions as exc from cratonclient.shell.v1 import cells_shell from cratonclient.tests.integration.shell import base from cratonclient.v1 import cells -from cratonclient.v1 import variables class TestCellsShell(base.ShellTestCase): @@ -334,74 +333,6 @@ class TestCellsShell(base.ShellTestCase): class TestCellsVarsShell(base.VariablesTestCase): """Test Cell Variable shell calls.""" - def setUp(self): - """Basic set up for all tests in this suite.""" - super(TestCellsVarsShell, self).setUp() - - # NOTE(thomasem): Mock out a client to assert craton Python API calls - self.client = mock.Mock() - self.mock_cell_resource = self.client.cells.get.return_value - self.mock_cell_resource.variables = variables.VariableManager( - self.mock_session, self.resource_url - ) - - def test_do_cell_vars_get_gets_correct_cell(self): - """Assert the proper cell is retrieved when calling get.""" - self.mock_get_response.json.return_value = \ - {"variables": {"foo": "bar"}} - cells_shell.do_cell_vars_get(self.client, self.test_args) - self.client.cells.get.assert_called_once_with( - vars(self.test_args)['id']) - - def test_do_cell_vars_delete_gets_correct_cell(self): - """Assert the proper cell is retrieved when calling delete.""" - self.test_args.variables = ['foo', 'bar'] - cells_shell.do_cell_vars_delete(self.client, self.test_args) - self.client.cells.get.assert_called_once_with( - vars(self.test_args)['id']) - - def test_do_cell_vars_update_gets_correct_cell(self): - """Assert the proper cell is retrieved when calling update.""" - self.test_args.variables = ['foo=', 'bar='] - mock_resp_json = {"variables": {"foo": "bar"}} - self.mock_get_response.json.return_value = mock_resp_json - self.mock_put_response.json.return_value = mock_resp_json - - cells_shell.do_cell_vars_set(self.client, self.test_args) - self.client.cells.get.assert_called_once_with( - vars(self.test_args)['id']) - - def test_do_cell_vars_get_calls_session_get(self): - """Assert the proper cell is retrieved when calling get.""" - self.mock_get_response.json.return_value = \ - {"variables": {"foo": "bar"}} - cells_shell.do_cell_vars_get(self.client, self.test_args) - self.mock_session.get.assert_called_once_with(self.variables_url) - - def test_do_cell_vars_delete_calls_session_delete(self): - """Verify that do cell-vars-delete calls expected session.delete.""" - self.test_args.variables = ['foo', 'bar'] - cells_shell.do_cell_vars_delete(self.client, self.test_args) - self.mock_session.delete.assert_called_once_with( - self.variables_url, - json=('foo', 'bar'), - params={}, - ) - - def test_do_cell_vars_update_calls_session_put(self): - """Verify that do cell-vars-delete calls expected session.delete.""" - self.test_args.variables = ['foo=baz', 'bar=boo', 'test='] - mock_resp_json = {"variables": {"foo": "bar"}} - self.mock_get_response.json.return_value = mock_resp_json - self.mock_put_response.json.return_value = mock_resp_json - - cells_shell.do_cell_vars_set(self.client, self.test_args) - self.mock_session.delete.assert_called_once_with( - self.variables_url, - json=('test',), - params={}, - ) - self.mock_session.put.assert_called_once_with( - self.variables_url, - json={'foo': 'baz', 'bar': 'boo'} - ) + resource = 'cell' + resource_id = '1' + shell = cells_shell diff --git a/cratonclient/tests/integration/shell/v1/test_clouds_shell.py b/cratonclient/tests/integration/shell/v1/test_clouds_shell.py index a84ced3..6e6db3d 100644 --- a/cratonclient/tests/integration/shell/v1/test_clouds_shell.py +++ b/cratonclient/tests/integration/shell/v1/test_clouds_shell.py @@ -19,7 +19,6 @@ from testtools import matchers from cratonclient.shell.v1 import clouds_shell from cratonclient.tests.integration.shell import base from cratonclient.v1 import clouds -from cratonclient.v1 import variables class TestCloudsShell(base.ShellTestCase): @@ -167,74 +166,6 @@ class TestCloudsShell(base.ShellTestCase): class TestCloudsVarsShell(base.VariablesTestCase): """Test Cloud Variable shell calls.""" - def setUp(self): - """Basic set up for all tests in this suite.""" - super(TestCloudsVarsShell, self).setUp() - - # NOTE(thomasem): Mock out a client to assert craton Python API calls - self.client = mock.Mock() - self.mock_cloud_resource = self.client.clouds.get.return_value - self.mock_cloud_resource.variables = variables.VariableManager( - self.mock_session, self.resource_url - ) - - def test_do_cloud_vars_get_gets_correct_cloud(self): - """Assert the proper cloud is retrieved when calling get.""" - self.mock_get_response.json.return_value = \ - {"variables": {"foo": "bar"}} - clouds_shell.do_cloud_vars_get(self.client, self.test_args) - self.client.clouds.get.assert_called_once_with( - vars(self.test_args)['id']) - - def test_do_cloud_vars_delete_gets_correct_cloud(self): - """Assert the proper cloud is retrieved when calling delete.""" - self.test_args.variables = ['foo', 'bar'] - clouds_shell.do_cloud_vars_delete(self.client, self.test_args) - self.client.clouds.get.assert_called_once_with( - vars(self.test_args)['id']) - - def test_do_cloud_vars_update_gets_correct_cloud(self): - """Assert the proper cloud is retrieved when calling update.""" - self.test_args.variables = ['foo=', 'bar='] - mock_resp_json = {"variables": {"foo": "bar"}} - self.mock_get_response.json.return_value = mock_resp_json - self.mock_put_response.json.return_value = mock_resp_json - - clouds_shell.do_cloud_vars_set(self.client, self.test_args) - self.client.clouds.get.assert_called_once_with( - vars(self.test_args)['id']) - - def test_do_cloud_vars_get_calls_session_get(self): - """Assert the proper cloud is retrieved when calling get.""" - self.mock_get_response.json.return_value = \ - {"variables": {"foo": "bar"}} - clouds_shell.do_cloud_vars_get(self.client, self.test_args) - self.mock_session.get.assert_called_once_with(self.variables_url) - - def test_do_cloud_vars_delete_calls_session_delete(self): - """Verify that do cloud-vars-delete calls expected session.delete.""" - self.test_args.variables = ['foo', 'bar'] - clouds_shell.do_cloud_vars_delete(self.client, self.test_args) - self.mock_session.delete.assert_called_once_with( - self.variables_url, - json=('foo', 'bar'), - params={}, - ) - - def test_do_cloud_vars_update_calls_session_put(self): - """Verify that do cloud-vars-delete calls expected session.delete.""" - self.test_args.variables = ['foo=baz', 'bar=boo', 'test='] - mock_resp_json = {"variables": {"foo": "bar"}} - self.mock_get_response.json.return_value = mock_resp_json - self.mock_put_response.json.return_value = mock_resp_json - - clouds_shell.do_cloud_vars_set(self.client, self.test_args) - self.mock_session.delete.assert_called_once_with( - self.variables_url, - json=('test',), - params={}, - ) - self.mock_session.put.assert_called_once_with( - self.variables_url, - json={'foo': 'baz', 'bar': 'boo'} - ) + resource = 'cloud' + resource_id = '1' + shell = clouds_shell diff --git a/cratonclient/tests/integration/shell/v1/test_hosts_shell.py b/cratonclient/tests/integration/shell/v1/test_hosts_shell.py index cfabc78..1a89b82 100644 --- a/cratonclient/tests/integration/shell/v1/test_hosts_shell.py +++ b/cratonclient/tests/integration/shell/v1/test_hosts_shell.py @@ -22,7 +22,6 @@ from cratonclient import exceptions as exc from cratonclient.shell.v1 import hosts_shell from cratonclient.tests.integration.shell import base from cratonclient.v1 import hosts -from cratonclient.v1 import variables class TestHostsShell(base.ShellTestCase): @@ -395,74 +394,6 @@ class TestHostsShell(base.ShellTestCase): class TestHostsVarsShell(base.VariablesTestCase): """Test Host Variable shell calls.""" - def setUp(self): - """Basic set up for all tests in this suite.""" - super(TestHostsVarsShell, self).setUp() - - # NOTE(thomasem): Mock out a client to assert craton Python API calls - self.client = mock.Mock() - self.mock_host_resource = self.client.hosts.get.return_value - self.mock_host_resource.variables = variables.VariableManager( - self.mock_session, self.resource_url - ) - - def test_do_host_vars_get_gets_correct_host(self): - """Assert the proper host is retrieved when calling get.""" - self.mock_get_response.json.return_value = \ - {"variables": {"foo": "bar"}} - hosts_shell.do_host_vars_get(self.client, self.test_args) - self.client.hosts.get.assert_called_once_with( - vars(self.test_args)['id']) - - def test_do_host_vars_delete_gets_correct_host(self): - """Assert the proper host is retrieved when calling delete.""" - self.test_args.variables = ['foo', 'bar'] - hosts_shell.do_host_vars_delete(self.client, self.test_args) - self.client.hosts.get.assert_called_once_with( - vars(self.test_args)['id']) - - def test_do_host_vars_update_gets_correct_host(self): - """Assert the proper host is retrieved when calling update.""" - self.test_args.variables = ['foo=', 'bar='] - mock_resp_json = {"variables": {"foo": "bar"}} - self.mock_get_response.json.return_value = mock_resp_json - self.mock_put_response.json.return_value = mock_resp_json - - hosts_shell.do_host_vars_set(self.client, self.test_args) - self.client.hosts.get.assert_called_once_with( - vars(self.test_args)['id']) - - def test_do_host_vars_get_calls_session_get(self): - """Assert the proper host is retrieved when calling get.""" - self.mock_get_response.json.return_value = \ - {"variables": {"foo": "bar"}} - hosts_shell.do_host_vars_get(self.client, self.test_args) - self.mock_session.get.assert_called_once_with(self.variables_url) - - def test_do_host_vars_delete_calls_session_delete(self): - """Verify that do host-vars-delete calls expected session.delete.""" - self.test_args.variables = ['foo', 'bar'] - hosts_shell.do_host_vars_delete(self.client, self.test_args) - self.mock_session.delete.assert_called_once_with( - self.variables_url, - json=('foo', 'bar'), - params={}, - ) - - def test_do_host_vars_update_calls_session_put(self): - """Verify that do host-vars-delete calls expected session.delete.""" - self.test_args.variables = ['foo=baz', 'bar=boo', 'test='] - mock_resp_json = {"variables": {"foo": "bar"}} - self.mock_get_response.json.return_value = mock_resp_json - self.mock_put_response.json.return_value = mock_resp_json - - hosts_shell.do_host_vars_set(self.client, self.test_args) - self.mock_session.delete.assert_called_once_with( - self.variables_url, - json=('test',), - params={}, - ) - self.mock_session.put.assert_called_once_with( - self.variables_url, - json={'foo': 'baz', 'bar': 'boo'} - ) + resource = 'host' + resource_id = '1' + shell = hosts_shell diff --git a/cratonclient/tests/integration/shell/v1/test_projects_shell.py b/cratonclient/tests/integration/shell/v1/test_projects_shell.py index 357145b..802ef37 100644 --- a/cratonclient/tests/integration/shell/v1/test_projects_shell.py +++ b/cratonclient/tests/integration/shell/v1/test_projects_shell.py @@ -22,7 +22,6 @@ from cratonclient import exceptions as exc from cratonclient.shell.v1 import projects_shell from cratonclient.tests.integration.shell import base from cratonclient.v1 import projects -from cratonclient.v1 import variables class TestProjectsShell(base.ShellTestCase): @@ -196,74 +195,6 @@ class TestProjectsShell(base.ShellTestCase): class TestProjectsVarsShell(base.VariablesTestCase): """Test Project Variable shell calls.""" - def setUp(self): - """Basic set up for all tests in this suite.""" - super(TestProjectsVarsShell, self).setUp() - - # NOTE(thomasem): Mock out a client to assert craton Python API calls - self.client = mock.Mock() - self.mock_project_resource = self.client.projects.get.return_value - self.mock_project_resource.variables = variables.VariableManager( - self.mock_session, self.resource_url - ) - - def test_do_project_vars_get_gets_correct_project(self): - """Assert the proper project is retrieved when calling get.""" - self.mock_get_response.json.return_value = \ - {"variables": {"foo": "bar"}} - projects_shell.do_project_vars_get(self.client, self.test_args) - self.client.projects.get.assert_called_once_with( - vars(self.test_args)['id']) - - def test_do_project_vars_delete_gets_correct_project(self): - """Assert the proper project is retrieved when calling delete.""" - self.test_args.variables = ['foo', 'bar'] - projects_shell.do_project_vars_delete(self.client, self.test_args) - self.client.projects.get.assert_called_once_with( - vars(self.test_args)['id']) - - def test_do_project_vars_update_gets_correct_project(self): - """Assert the proper project is retrieved when calling update.""" - self.test_args.variables = ['foo=', 'bar='] - mock_resp_json = {"variables": {"foo": "bar"}} - self.mock_get_response.json.return_value = mock_resp_json - self.mock_put_response.json.return_value = mock_resp_json - - projects_shell.do_project_vars_set(self.client, self.test_args) - self.client.projects.get.assert_called_once_with( - vars(self.test_args)['id']) - - def test_do_project_vars_get_calls_session_get(self): - """Assert the proper project is retrieved when calling get.""" - self.mock_get_response.json.return_value = \ - {"variables": {"foo": "bar"}} - projects_shell.do_project_vars_get(self.client, self.test_args) - self.mock_session.get.assert_called_once_with(self.variables_url) - - def test_do_project_vars_delete_calls_session_delete(self): - """Verify that do project-vars-delete calls expected session.delete.""" - self.test_args.variables = ['foo', 'bar'] - projects_shell.do_project_vars_delete(self.client, self.test_args) - self.mock_session.delete.assert_called_once_with( - self.variables_url, - json=('foo', 'bar'), - params={}, - ) - - def test_do_project_vars_update_calls_session_put(self): - """Verify that do project-vars-delete calls expected session.delete.""" - self.test_args.variables = ['foo=baz', 'bar=boo', 'test='] - mock_resp_json = {"variables": {"foo": "bar"}} - self.mock_get_response.json.return_value = mock_resp_json - self.mock_put_response.json.return_value = mock_resp_json - - projects_shell.do_project_vars_set(self.client, self.test_args) - self.mock_session.delete.assert_called_once_with( - self.variables_url, - json=('test',), - params={}, - ) - self.mock_session.put.assert_called_once_with( - self.variables_url, - json={'foo': 'baz', 'bar': 'boo'} - ) + resource = 'project' + resource_id = 'c9f10eca-66ac-4c27-9c13-9d01e65f96b4' + shell = projects_shell diff --git a/cratonclient/tests/integration/shell/v1/test_regions_shell.py b/cratonclient/tests/integration/shell/v1/test_regions_shell.py index 19cb10d..be8c7ee 100644 --- a/cratonclient/tests/integration/shell/v1/test_regions_shell.py +++ b/cratonclient/tests/integration/shell/v1/test_regions_shell.py @@ -19,7 +19,6 @@ from testtools import matchers from cratonclient.shell.v1 import regions_shell from cratonclient.tests.integration.shell import base from cratonclient.v1 import regions -from cratonclient.v1 import variables class TestRegionsShell(base.ShellTestCase): @@ -178,74 +177,6 @@ class TestRegionsShell(base.ShellTestCase): class TestRegionsVarsShell(base.VariablesTestCase): """Test Region Variable shell calls.""" - def setUp(self): - """Basic set up for all tests in this suite.""" - super(TestRegionsVarsShell, self).setUp() - - # NOTE(thomasem): Mock out a client to assert craton Python API calls - self.client = mock.Mock() - self.mock_region_resource = self.client.regions.get.return_value - self.mock_region_resource.variables = variables.VariableManager( - self.mock_session, self.resource_url - ) - - def test_do_region_vars_get_gets_correct_region(self): - """Assert the proper region is retrieved when calling get.""" - self.mock_get_response.json.return_value = \ - {"variables": {"foo": "bar"}} - regions_shell.do_region_vars_get(self.client, self.test_args) - self.client.regions.get.assert_called_once_with( - vars(self.test_args)['id']) - - def test_do_region_vars_delete_gets_correct_region(self): - """Assert the proper region is retrieved when calling delete.""" - self.test_args.variables = ['foo', 'bar'] - regions_shell.do_region_vars_delete(self.client, self.test_args) - self.client.regions.get.assert_called_once_with( - vars(self.test_args)['id']) - - def test_do_region_vars_update_gets_correct_region(self): - """Assert the proper region is retrieved when calling update.""" - self.test_args.variables = ['foo=', 'bar='] - mock_resp_json = {"variables": {"foo": "bar"}} - self.mock_get_response.json.return_value = mock_resp_json - self.mock_put_response.json.return_value = mock_resp_json - - regions_shell.do_region_vars_set(self.client, self.test_args) - self.client.regions.get.assert_called_once_with( - vars(self.test_args)['id']) - - def test_do_region_vars_get_calls_session_get(self): - """Assert the proper region is retrieved when calling get.""" - self.mock_get_response.json.return_value = \ - {"variables": {"foo": "bar"}} - regions_shell.do_region_vars_get(self.client, self.test_args) - self.mock_session.get.assert_called_once_with(self.variables_url) - - def test_do_region_vars_delete_calls_session_delete(self): - """Verify that do region-vars-delete calls expected session.delete.""" - self.test_args.variables = ['foo', 'bar'] - regions_shell.do_region_vars_delete(self.client, self.test_args) - self.mock_session.delete.assert_called_once_with( - self.variables_url, - json=('foo', 'bar'), - params={}, - ) - - def test_do_region_vars_update_calls_session_put(self): - """Verify that do region-vars-delete calls expected session.delete.""" - self.test_args.variables = ['foo=baz', 'bar=boo', 'test='] - mock_resp_json = {"variables": {"foo": "bar"}} - self.mock_get_response.json.return_value = mock_resp_json - self.mock_put_response.json.return_value = mock_resp_json - - regions_shell.do_region_vars_set(self.client, self.test_args) - self.mock_session.delete.assert_called_once_with( - self.variables_url, - json=('test',), - params={}, - ) - self.mock_session.put.assert_called_once_with( - self.variables_url, - json={'foo': 'baz', 'bar': 'boo'} - ) + resource = 'region' + resource_id = '1' + shell = regions_shell