10
with patch('lib.swift_utils.is_paused') as is_paused:
11
with patch('lib.swift_utils.register_configs') as configs:
12
import actions.actions
15
class CharmTestCase(unittest.TestCase):
17
def setUp(self, obj, patches):
18
super(CharmTestCase, self).setUp()
19
self.patches = patches
23
def patch(self, method):
24
_m = mock.patch.object(self.obj, method)
26
self.addCleanup(_m.stop)
30
for method in self.patches:
31
setattr(self, method, self.patch(method))
34
class PauseTestCase(CharmTestCase):
37
super(PauseTestCase, self).setUp(
38
actions.actions, ["service_pause", "HookData", "kv",
41
class FakeArgs(object):
42
services = ['swift-proxy', 'haproxy', 'memcached', 'apache2']
43
self.args = FakeArgs()
45
def test_pauses_services(self):
46
"""Pause action pauses all of the Swift services."""
49
def fake_service_pause(svc):
50
pause_calls.append(svc)
53
self.service_pause.side_effect = fake_service_pause
55
actions.actions.pause(self.args)
57
pause_calls, ['swift-proxy', 'haproxy', 'memcached', 'apache2'])
59
def test_bails_out_early_on_error(self):
60
"""Pause action fails early if there are errors stopping a service."""
67
pause_calls.append(svc)
70
self.service_pause.side_effect = maybe_kill
71
self.assertRaisesRegexp(
72
Exception, "haproxy didn't stop cleanly.",
73
actions.actions.pause, self.args)
74
self.assertEqual(pause_calls, ["swift-proxy"])
76
def test_pause_sets_value(self):
77
"""Pause action sets the unit-paused value to True."""
78
self.HookData()().return_value = True
80
actions.actions.pause(self.args)
81
self.kv().set.assert_called_with('unit-paused', True)
84
class ResumeTestCase(CharmTestCase):
87
super(ResumeTestCase, self).setUp(
88
actions.actions, ["service_resume", "HookData", "kv",
91
class FakeArgs(object):
92
services = ['swift-proxy', 'haproxy', 'memcached', 'apache2']
93
self.args = FakeArgs()
95
def test_resumes_services(self):
96
"""Resume action resumes all of the Swift services."""
99
def fake_service_resume(svc):
100
resume_calls.append(svc)
103
self.service_resume.side_effect = fake_service_resume
104
actions.actions.resume(self.args)
106
resume_calls, ['swift-proxy', 'haproxy', 'memcached', 'apache2'])
108
def test_bails_out_early_on_error(self):
109
"""Resume action fails early if there are errors starting a service."""
116
resume_calls.append(svc)
119
self.service_resume.side_effect = maybe_kill
120
self.assertRaisesRegexp(
121
Exception, "haproxy didn't start cleanly.",
122
actions.actions.resume, self.args)
123
self.assertEqual(resume_calls, ['swift-proxy'])
125
def test_resume_sets_value(self):
126
"""Resume action sets the unit-paused value to False."""
127
self.HookData()().return_value = True
129
actions.actions.resume(self.args)
130
self.kv().set.assert_called_with('unit-paused', False)
133
class GetActionParserTestCase(unittest.TestCase):
135
def test_definition_from_yaml(self):
136
"""ArgumentParser is seeded from actions.yaml."""
137
actions_yaml = tempfile.NamedTemporaryFile(
138
prefix="GetActionParserTestCase", suffix="yaml")
139
actions_yaml.write(yaml.dump({"foo": {"description": "Foo is bar"}}))
141
parser = actions.actions.get_action_parser(actions_yaml.name, "foo",
142
get_services=lambda: [])
143
self.assertEqual(parser.description, 'Foo is bar')
146
class MainTestCase(CharmTestCase):
149
super(MainTestCase, self).setUp(
150
actions.actions, ["_get_action_name",
154
def test_invokes_pause(self):
157
def dummy_action(args):
158
dummy_calls.append(True)
160
self._get_action_name.side_effect = lambda: "foo"
161
self.get_action_parser = lambda: argparse.ArgumentParser()
162
with mock.patch.dict(actions.actions.ACTIONS, {"foo": dummy_action}):
163
actions.actions.main([])
164
self.assertEqual(dummy_calls, [True])
166
def test_unknown_action(self):
167
"""Unknown actions aren't a traceback."""
168
self._get_action_name.side_effect = lambda: "foo"
169
self.get_action_parser = lambda: argparse.ArgumentParser()
170
exit_string = actions.actions.main([])
171
self.assertEqual("Action foo undefined", exit_string)
173
def test_failing_action(self):
174
"""Actions which traceback trigger action_fail() calls."""
177
self.action_fail.side_effect = dummy_calls.append
178
self._get_action_name.side_effect = lambda: "foo"
180
def dummy_action(args):
181
raise ValueError("uh oh")
183
self.get_action_parser = lambda: argparse.ArgumentParser()
184
with mock.patch.dict(actions.actions.ACTIONS, {"foo": dummy_action}):
185
actions.actions.main([])
186
self.assertEqual(dummy_calls, ["uh oh"])