~andrewjbeach/juju-ci-tools/make-local-patcher

« back to all changes in this revision

Viewing changes to tests/test_assess_unregister.py

Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""Tests for assess_unregister module."""
2
 
 
3
 
import logging
4
 
from mock import (
5
 
    Mock,
6
 
    call,
7
 
    patch,
8
 
)
9
 
import StringIO
10
 
import subprocess
11
 
from textwrap import dedent
12
 
 
13
 
import assess_unregister as a_unreg
14
 
from tests import (
15
 
    parse_error,
16
 
    TestCase,
17
 
)
18
 
from tests.test_jujupy import fake_juju_client
19
 
from utility import JujuAssertionError
20
 
 
21
 
 
22
 
class TestParseArgs(TestCase):
23
 
 
24
 
    def test_common_args(self):
25
 
        args = a_unreg.parse_args(
26
 
            ["an-env", "/bin/juju", "/tmp/logs", "an-env-mod"])
27
 
        self.assertEqual("an-env", args.env)
28
 
        self.assertEqual("/bin/juju", args.juju_bin)
29
 
        self.assertEqual("/tmp/logs", args.logs)
30
 
        self.assertEqual("an-env-mod", args.temp_env_name)
31
 
        self.assertEqual(False, args.debug)
32
 
 
33
 
    def test_help(self):
34
 
        fake_stdout = StringIO.StringIO()
35
 
        with parse_error(self) as fake_stderr:
36
 
            with patch("sys.stdout", fake_stdout):
37
 
                a_unreg.parse_args(["--help"])
38
 
        self.assertEqual("", fake_stderr.getvalue())
39
 
        self.assertIn("Test unregister feature", fake_stdout.getvalue())
40
 
 
41
 
 
42
 
class TestMain(TestCase):
43
 
 
44
 
    def test_main(self):
45
 
        argv = ["an-env", "/bin/juju", "/tmp/logs", "an-env-mod", "--verbose"]
46
 
        client = Mock(spec=["is_jes_enabled"])
47
 
        with patch("assess_unregister.configure_logging",
48
 
                   autospec=True) as mock_cl:
49
 
            with patch("assess_unregister.BootstrapManager.booted_context",
50
 
                       autospec=True) as mock_bc:
51
 
                with patch("deploy_stack.client_from_config",
52
 
                           return_value=client) as mock_c:
53
 
                    with patch("assess_unregister.assess_unregister",
54
 
                               autospec=True) as mock_assess:
55
 
                        a_unreg.main(argv)
56
 
        mock_cl.assert_called_once_with(logging.DEBUG)
57
 
        mock_c.assert_called_once_with('an-env', "/bin/juju", debug=False,
58
 
                                       soft_deadline=None)
59
 
        self.assertEqual(mock_bc.call_count, 1)
60
 
        mock_assess.assert_called_once_with(client)
61
 
 
62
 
 
63
 
class TestAssess(TestCase):
64
 
 
65
 
    def test_unregister(self):
66
 
        fake_user = Mock()
67
 
        with patch('jujupy.EnvJujuClient.register_user',
68
 
                   return_value=fake_user):
69
 
            with patch.object(
70
 
                    a_unreg, 'assert_controller_list',
71
 
                    autospec=True) as mock_assert_list:
72
 
                with patch.object(
73
 
                        a_unreg, 'assert_switch_raises_error', autospec=True):
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)
78
 
        self.assertEqual(
79
 
            mock_assert_list.mock_calls,
80
 
            [
81
 
                call(fake_user, ['testuser_controller']),
82
 
                call(fake_user, []),
83
 
                call(fake_client, ['testing-controller'])
84
 
            ])
85
 
 
86
 
 
87
 
class TestAssertControllerList(TestCase):
88
 
 
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, [])
94
 
 
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
99
 
        self.assertRaises(
100
 
            JujuAssertionError,
101
 
            a_unreg.assert_controller_list,
102
 
            fake_client,
103
 
            ['some_controller'])
104
 
 
105
 
    def test_raises_when_list_controllers_expecting_none(self):
106
 
        json_output = """\
107
 
        {"controllers":
108
 
        {"local-temp": {"current-model":"testing-model","user":"admin@local"}},
109
 
        "current-controller":"local-temp"}\n"""
110
 
 
111
 
        fake_client = Mock(spec=['get_juju_output'])
112
 
        fake_client.get_juju_output.return_value = json_output
113
 
        self.assertRaises(
114
 
            JujuAssertionError,
115
 
            a_unreg.assert_controller_list,
116
 
            fake_client,
117
 
            [])
118
 
 
119
 
    def test_passes_when_expected_and_listed_match(self):
120
 
        json_output = """\
121
 
        {"controllers":
122
 
        {"local-temp": {"current-model":"testing-model","user":"admin@local"}},
123
 
        "current-controller":"local-temp"}\n"""
124
 
 
125
 
        fake_client = Mock(spec=['get_juju_output'])
126
 
        fake_client.get_juju_output.return_value = json_output
127
 
        self.assertRaises(
128
 
            JujuAssertionError,
129
 
            a_unreg.assert_controller_list,
130
 
            fake_client,
131
 
            ['testing-model'])
132
 
 
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']
140
 
        Got: []""")
141
 
 
142
 
        try:
143
 
            a_unreg.assert_controller_list(fake_client, ['testing-model'])
144
 
        except JujuAssertionError as e:
145
 
            if str(e) != expected_message:
146
 
                raise
147
 
 
148
 
 
149
 
class TestAssessSwitchRaisesError(TestCase):
150
 
 
151
 
    def test_raises_exception_if_switch_doesnt_fail_at_all(self):
152
 
        fake_client = Mock(spec=['get_juju_output'])
153
 
        self.assertRaises(
154
 
            JujuAssertionError,
155
 
            a_unreg.assert_switch_raises_error,
156
 
            fake_client)
157
 
 
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
163
 
        self.assertRaises(
164
 
            JujuAssertionError,
165
 
            a_unreg.assert_switch_raises_error,
166
 
            fake_client)
167
 
 
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
173
 
 
174
 
        a_unreg.assert_switch_raises_error(fake_client)