DRY out Variables shell tests

This patch pulls out the common setup bits for
Variables shell tests.

Change-Id: I8012f51b06a40d0c8d8b0aaeff407881789da9b2
Partial-Bug: 1659110
This commit is contained in:
Thomas Maddox 2017-03-06 16:31:52 +00:00
parent c0cdd3950f
commit 0ad237ed60
2 changed files with 33 additions and 24 deletions

View File

@ -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()

View File

@ -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 = \