From 0ad237ed60997676d3b6b765dbef8936c9d80bd3 Mon Sep 17 00:00:00 2001 From: Thomas Maddox Date: Mon, 6 Mar 2017 16:31:52 +0000 Subject: [PATCH] DRY out Variables shell tests This patch pulls out the common setup bits for Variables shell tests. Change-Id: I8012f51b06a40d0c8d8b0aaeff407881789da9b2 Partial-Bug: 1659110 --- cratonclient/tests/integration/shell/base.py | 31 +++++++++++++++++++ .../integration/shell/v1/test_hosts_shell.py | 26 ++-------------- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/cratonclient/tests/integration/shell/base.py b/cratonclient/tests/integration/shell/base.py index 9077efe..a8c7b4e 100644 --- a/cratonclient/tests/integration/shell/base.py +++ b/cratonclient/tests/integration/shell/base.py @@ -11,6 +11,7 @@ # under the License. """Resources for the shell integration tests.""" +from argparse import Namespace import mock import six @@ -32,3 +33,33 @@ class ShellTestCase(base.TestCase): except SystemExit: pass return (mock_stdout.getvalue(), mock_stderr.getvalue()) + + +class VariablesTestCase(base.TestCase): + """Test Host Variable shell calls.""" + + 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.variables_url = '{}/variables'.format(self.resource_url) + self.test_args = Namespace(id=1, formatter=mock.Mock()) + + # NOTE(thomasem): Make all calls seem like they come from CLI args + self.stdin_patcher = \ + mock.patch('cratonclient.common.cliutils.sys.stdin') + self.patched_stdin = self.stdin_patcher.start() + self.patched_stdin.isatty.return_value = True + + # NOTE(thomasem): Mock out a session object to assert resulting API + # calls + self.mock_session = mock.Mock() + self.mock_get_response = self.mock_session.get.return_value + self.mock_put_response = self.mock_session.put.return_value + self.mock_delete_response = self.mock_session.delete.return_value + self.mock_delete_response.status_code = 204 + + def tearDown(self): + """Clean up between tests.""" + super(VariablesTestCase, self).tearDown() + self.stdin_patcher.stop() diff --git a/cratonclient/tests/integration/shell/v1/test_hosts_shell.py b/cratonclient/tests/integration/shell/v1/test_hosts_shell.py index c97e5ef..cfabc78 100644 --- a/cratonclient/tests/integration/shell/v1/test_hosts_shell.py +++ b/cratonclient/tests/integration/shell/v1/test_hosts_shell.py @@ -392,42 +392,20 @@ class TestHostsShell(base.ShellTestCase): mock_delete.assert_called_once_with(vars(test_args)['id']) -class TestHostsVarsShell(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() - self.host_url = 'http://127.0.0.1/v1/hosts/1' - self.variables_url = '{}/variables'.format(self.host_url) - self.test_args = Namespace(id=1, formatter=mock.Mock()) - - # NOTE(thomasem): Make all calls seem like they come from CLI args - self.stdin_patcher = \ - mock.patch('cratonclient.common.cliutils.sys.stdin') - self.patched_stdin = self.stdin_patcher.start() - self.patched_stdin.isatty.return_value = True - - # NOTE(thomasem): Mock out a session object to assert resulting API - # calls - self.mock_session = mock.Mock() - self.mock_get_response = self.mock_session.get.return_value - self.mock_put_response = self.mock_session.put.return_value - 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() self.mock_host_resource = self.client.hosts.get.return_value self.mock_host_resource.variables = variables.VariableManager( - self.mock_session, self.host_url + self.mock_session, self.resource_url ) - def tearDown(self): - """Clean up between tests.""" - super(TestHostsVarsShell, self).tearDown() - self.stdin_patcher.stop() - 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 = \