1
# Copyright 2009 Canonical Ltd. This software is licensed under the
1
# Copyright 2009-2010 Canonical Ltd. This software is licensed under the
2
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
3
# pylint: disable-msg=E0702
9
9
from unittest import TestCase, TestLoader
11
from lp.testing.fakemethod import FakeMethod
11
13
from devscripts.ec2test.instance import EC2Instance
15
"""Generic recipient of invocations.
18
- Stub methods that should do nothing.
19
- Inject failures into methods.
20
- Record whether a method is being called.
22
# XXX JeroenVermeulen 2009-11-26: This probably exists somewhere
23
# already. Or if not, maybe it should. But with a name that won't
24
# turn Stuart Bishop's IRC client into a disco simulator.
28
def __init__(self, return_value=None, simulated_failure=None):
29
"""Define what to do when this stub gets invoked.
31
:param return_value: Something to return from the invocation.
32
:parma simulated_failure: Something to raise in the invocation.
34
assert return_value is None or simulated_failure is None, (
35
"Stub can raise an exception or return a value, but not both.")
36
self.return_value = return_value
37
self.simulated_failure = simulated_failure
39
def __call__(self, *args, **kwargs):
42
Records the fact that an invocation was made in
45
If you passed a `simulated_failure` to the constructor, that
48
:return: The `return_value` you passed to the constructor.
52
if self.simulated_failure is not None:
53
raise self.simulated_failure
55
return self.return_value
59
17
"""Helper for setting up an `EC2Instance` without EC2."""
60
acquire_private_key = Stub()
61
acquire_security_group = Stub()
18
acquire_private_key = FakeMethod()
19
acquire_security_group = FakeMethod()
98
56
def _makeInstance(self):
99
57
"""Set up an `EC2Instance`, with stubbing where needed.
101
`EC2Instance.shutdown` is replaced with a `Stub`, so check its
102
call_count to see whether it's been invoked.
59
`EC2Instance.shutdown` is replaced with a `FakeMethod`, so check
60
its call_count to see whether it's been invoked.
104
62
session_name = None
105
63
image = FakeImage()
114
72
session_name, image, instance_type, demo_networks, account,
115
73
from_scratch, user_key, login)
117
instance.shutdown = Stub()
118
instance._report_traceback = Stub()
119
instance.log = Stub()
75
instance.shutdown = FakeMethod()
76
instance._report_traceback = FakeMethod()
77
instance.log = FakeMethod()
123
81
def _runInstance(self, instance, runnee=None, headless=False):
124
82
"""Set up and run an `EC2Instance` (but without EC2)."""
125
83
if runnee is None:
128
86
instance.set_up_and_run(False, not headless, runnee)
133
91
# Not a very useful test, except it establishes the basic
134
92
# assumptions for the other tests.
135
93
instance = self._makeInstance()
138
96
self.assertEqual(0, runnee.call_count)
139
97
self.assertEqual(0, instance.shutdown.call_count)
164
122
# If the test runner barfs, the instance swallows the exception
165
123
# and shuts down.
166
124
instance = self._makeInstance()
167
runnee = Stub(simulated_failure=FakeFailure("Headful barfage."))
125
runnee = FakeMethod(failure=FakeFailure("Headful barfage."))
169
127
self._runInstance(instance, runnee=runnee, headless=False)
174
132
# If the instance's test runner fails to set up for a headless
175
133
# run, the instance swallows the exception and shuts down.
176
134
instance = self._makeInstance()
177
runnee = Stub(simulated_failure=FakeFailure("Headless boom."))
135
runnee = FakeMethod(failure=FakeFailure("Headless boom."))
179
137
self._runInstance(instance, runnee=runnee, headless=True)