1
"""Tests for assess_unregister module."""
3
from contextlib import nested
12
from textwrap import dedent
14
import assess_unregister as a_unreg
19
from tests.test_jujupy import fake_juju_client
20
from utility import JujuAssertionError
23
class TestParseArgs(TestCase):
25
def test_common_args(self):
26
args = a_unreg.parse_args(
27
["an-env", "/bin/juju", "/tmp/logs", "an-env-mod"])
28
self.assertEqual("an-env", args.env)
29
self.assertEqual("/bin/juju", args.juju_bin)
30
self.assertEqual("/tmp/logs", args.logs)
31
self.assertEqual("an-env-mod", args.temp_env_name)
32
self.assertEqual(False, args.debug)
35
fake_stdout = StringIO.StringIO()
36
with parse_error(self) as fake_stderr:
37
with patch("sys.stdout", fake_stdout):
38
a_unreg.parse_args(["--help"])
39
self.assertEqual("", fake_stderr.getvalue())
40
self.assertIn("Test unregister feature", fake_stdout.getvalue())
43
class TestMain(TestCase):
46
argv = ["an-env", "/bin/juju", "/tmp/logs", "an-env-mod", "--verbose"]
48
client = Mock(spec=["is_jes_enabled"])
50
patch("assess_unregister.configure_logging", autospec=True),
51
patch("assess_unregister.BootstrapManager.booted_context",
53
patch("jujupy.SimpleEnvironment.from_config", return_value=env),
54
patch("jujupy.EnvJujuClient.by_version", return_value=client),
55
patch("assess_unregister.assess_unregister", autospec=True),
56
) as (mock_cl, mock_bc, mock_e, mock_c, mock_assess):
58
mock_cl.assert_called_once_with(logging.DEBUG)
59
mock_e.assert_called_once_with("an-env")
60
mock_c.assert_called_once_with(env, "/bin/juju", debug=False)
61
self.assertEqual(mock_bc.call_count, 1)
62
mock_assess.assert_called_once_with(client)
65
class TestAssess(TestCase):
67
def test_unregister(self):
70
patch.object(a_unreg, 'register_user', return_value=fake_user),
71
patch.object(a_unreg, 'assert_controller_list', autospec=True),
72
patch.object(a_unreg, 'assert_switch_raises_error', autospec=True)
73
) as (mock_reg_user, mock_assert_list, mock_assert_switch):
74
fake_client = Mock(wraps=fake_juju_client())
75
fake_client.env.controller.name = 'testing-controller'
76
fake_client.bootstrap()
77
a_unreg.assess_unregister(fake_client)
79
mock_assert_list.mock_calls,
81
call(fake_user, ['testuser_controller']),
83
call(fake_client, ['testing-controller'])
87
class TestAssertControllerList(TestCase):
89
def test_passes_on_empty_controller_list_expecting_empty(self):
90
json_output = '{"controllers":null,"current-controller":""}\n'
91
fake_client = Mock(spec=['get_juju_output'])
92
fake_client.get_juju_output.return_value = json_output
93
a_unreg.assert_controller_list(fake_client, [])
95
def test_raises_when_no_controllers_with_expected_list(self):
96
json_output = '{"controllers":null,"current-controller":""}\n'
97
fake_client = Mock(spec=['get_juju_output'])
98
fake_client.get_juju_output.return_value = json_output
101
a_unreg.assert_controller_list,
105
def test_raises_when_list_controllers_expecting_none(self):
108
{"local-temp": {"current-model":"testing-model","user":"admin@local"}},
109
"current-controller":"local-temp"}\n"""
111
fake_client = Mock(spec=['get_juju_output'])
112
fake_client.get_juju_output.return_value = json_output
115
a_unreg.assert_controller_list,
119
def test_passes_when_expected_and_listed_match(self):
122
{"local-temp": {"current-model":"testing-model","user":"admin@local"}},
123
"current-controller":"local-temp"}\n"""
125
fake_client = Mock(spec=['get_juju_output'])
126
fake_client.get_juju_output.return_value = json_output
129
a_unreg.assert_controller_list,
133
def test_exception_message_is_correct(self):
134
json_output = '{"controllers":null,"current-controller":""}\n'
135
fake_client = Mock(spec=['get_juju_output'])
136
fake_client.get_juju_output.return_value = json_output
137
expected_message = dedent("""\
138
Unexpected controller names.
139
Expected: ['testing-model']
143
a_unreg.assert_controller_list(fake_client, ['testing-model'])
144
except JujuAssertionError as e:
145
if str(e) != expected_message:
149
class TestAssessSwitchRaisesError(TestCase):
151
def test_raises_exception_if_switch_doesnt_fail_at_all(self):
152
fake_client = Mock(spec=['get_juju_output'])
155
a_unreg.assert_switch_raises_error,
158
def test_raises_exception_when_switch_doesnt_fail_as_expected(self):
159
fake_client = Mock(spec=['get_juju_output'])
160
exception = subprocess.CalledProcessError(-1, '')
161
exception.stderr = ''
162
fake_client.get_juju_output.side_effect = exception
165
a_unreg.assert_switch_raises_error,
168
def test_passes_when_switch_errors_as_expected(self):
169
fake_client = Mock(spec=['get_juju_output'])
170
exception = subprocess.CalledProcessError(-1, '')
171
exception.stderr = 'no currently specified model'
172
fake_client.get_juju_output.side_effect = exception
174
a_unreg.assert_switch_raises_error(fake_client)