10236.5.4
by Jeroen Vermeulen
Replace Stub with generic FakeMethod. No Stuart, not you. |
1 |
# Copyright 2009-2010 Canonical Ltd. This software is licensed under the
|
9942.2.1
by Jeroen Vermeulen
Added test, reproduced problem. |
2 |
# GNU Affero General Public License version 3 (see the file LICENSE).
|
9942.2.5
by Jeroen Vermeulen
Polish on the generic stub. |
3 |
# pylint: disable-msg=E0702
|
9942.2.1
by Jeroen Vermeulen
Added test, reproduced problem. |
4 |
|
5 |
"""Test handling of EC2 machine images."""
|
|
6 |
||
7 |
__metaclass__ = type |
|
8 |
||
13677.3.2
by Steve Kowalik
Remove unneeded unittest imports. |
9 |
from unittest import TestCase |
9942.2.1
by Jeroen Vermeulen
Added test, reproduced problem. |
10 |
|
14612.2.5
by William Grant
Format the non-contrib bits of lib. |
11 |
from devscripts.ec2test.instance import EC2Instance |
10236.5.4
by Jeroen Vermeulen
Replace Stub with generic FakeMethod. No Stuart, not you. |
12 |
from lp.testing.fakemethod import FakeMethod |
13 |
||
9942.2.1
by Jeroen Vermeulen
Added test, reproduced problem. |
14 |
|
9942.2.10
by Jeroen Vermeulen
Review changes: use 'fake' classes, not 'mock' classes. |
15 |
class FakeAccount: |
9942.2.4
by Jeroen Vermeulen
Cleaned up the test: generalized the stubbing. |
16 |
"""Helper for setting up an `EC2Instance` without EC2."""
|
10236.5.4
by Jeroen Vermeulen
Replace Stub with generic FakeMethod. No Stuart, not you. |
17 |
acquire_private_key = FakeMethod() |
18 |
acquire_security_group = FakeMethod() |
|
9942.2.1
by Jeroen Vermeulen
Added test, reproduced problem. |
19 |
|
20 |
||
9942.2.10
by Jeroen Vermeulen
Review changes: use 'fake' classes, not 'mock' classes. |
21 |
class FakeOutput: |
22 |
"""Pretend stdout/stderr output from EC2 instance."""
|
|
23 |
output = "Fake output." |
|
24 |
||
25 |
||
26 |
class FakeBotoInstance: |
|
9942.2.4
by Jeroen Vermeulen
Cleaned up the test: generalized the stubbing. |
27 |
"""Helper for setting up an `EC2Instance` without EC2."""
|
9942.2.1
by Jeroen Vermeulen
Added test, reproduced problem. |
28 |
id = 0 |
29 |
state = 'running' |
|
9942.2.10
by Jeroen Vermeulen
Review changes: use 'fake' classes, not 'mock' classes. |
30 |
public_dns_name = 'fake-instance' |
9942.2.1
by Jeroen Vermeulen
Added test, reproduced problem. |
31 |
|
10236.5.4
by Jeroen Vermeulen
Replace Stub with generic FakeMethod. No Stuart, not you. |
32 |
update = FakeMethod() |
33 |
stop = FakeMethod() |
|
9942.2.10
by Jeroen Vermeulen
Review changes: use 'fake' classes, not 'mock' classes. |
34 |
get_console_output = FakeOutput |
35 |
||
36 |
||
37 |
class FakeReservation: |
|
9942.2.4
by Jeroen Vermeulen
Cleaned up the test: generalized the stubbing. |
38 |
"""Helper for setting up an `EC2Instance` without EC2."""
|
9942.2.1
by Jeroen Vermeulen
Added test, reproduced problem. |
39 |
def __init__(self): |
9942.2.10
by Jeroen Vermeulen
Review changes: use 'fake' classes, not 'mock' classes. |
40 |
self.instances = [FakeBotoInstance()] |
41 |
||
42 |
||
43 |
class FakeImage: |
|
9942.2.4
by Jeroen Vermeulen
Cleaned up the test: generalized the stubbing. |
44 |
"""Helper for setting up an `EC2Instance` without EC2."""
|
10236.5.4
by Jeroen Vermeulen
Replace Stub with generic FakeMethod. No Stuart, not you. |
45 |
run = FakeMethod(result=FakeReservation()) |
9942.2.10
by Jeroen Vermeulen
Review changes: use 'fake' classes, not 'mock' classes. |
46 |
|
47 |
||
48 |
class FakeFailure(Exception): |
|
9942.2.4
by Jeroen Vermeulen
Cleaned up the test: generalized the stubbing. |
49 |
"""A pretend failure from the test runner."""
|
9942.2.1
by Jeroen Vermeulen
Added test, reproduced problem. |
50 |
|
51 |
||
52 |
class TestEC2Instance(TestCase): |
|
9942.2.4
by Jeroen Vermeulen
Cleaned up the test: generalized the stubbing. |
53 |
"""Test running of an `EC2Instance` without EC2."""
|
54 |
||
9942.2.9
by Jeroen Vermeulen
Review changes. |
55 |
def _makeInstance(self): |
9942.2.10
by Jeroen Vermeulen
Review changes: use 'fake' classes, not 'mock' classes. |
56 |
"""Set up an `EC2Instance`, with stubbing where needed.
|
57 |
||
10236.5.4
by Jeroen Vermeulen
Replace Stub with generic FakeMethod. No Stuart, not you. |
58 |
`EC2Instance.shutdown` is replaced with a `FakeMethod`, so check
|
59 |
its call_count to see whether it's been invoked.
|
|
9942.2.10
by Jeroen Vermeulen
Review changes: use 'fake' classes, not 'mock' classes. |
60 |
"""
|
9942.2.2
by Jeroen Vermeulen
Bit of simplification. |
61 |
session_name = None |
9942.2.10
by Jeroen Vermeulen
Review changes: use 'fake' classes, not 'mock' classes. |
62 |
image = FakeImage() |
9942.2.1
by Jeroen Vermeulen
Added test, reproduced problem. |
63 |
instance_type = 'c1.xlarge' |
64 |
demo_networks = None |
|
9942.2.10
by Jeroen Vermeulen
Review changes: use 'fake' classes, not 'mock' classes. |
65 |
account = FakeAccount() |
9942.2.1
by Jeroen Vermeulen
Added test, reproduced problem. |
66 |
from_scratch = None |
67 |
user_key = None |
|
68 |
login = None |
|
14302.5.29
by mbp at canonical
Small update to ec2test unit tests |
69 |
region = None |
9942.2.1
by Jeroen Vermeulen
Added test, reproduced problem. |
70 |
|
9942.2.9
by Jeroen Vermeulen
Review changes. |
71 |
instance = EC2Instance( |
9942.2.1
by Jeroen Vermeulen
Added test, reproduced problem. |
72 |
session_name, image, instance_type, demo_networks, account, |
14302.5.29
by mbp at canonical
Small update to ec2test unit tests |
73 |
from_scratch, user_key, login, |
74 |
region) |
|
9942.2.1
by Jeroen Vermeulen
Added test, reproduced problem. |
75 |
|
10236.5.4
by Jeroen Vermeulen
Replace Stub with generic FakeMethod. No Stuart, not you. |
76 |
instance.shutdown = FakeMethod() |
77 |
instance._report_traceback = FakeMethod() |
|
78 |
instance.log = FakeMethod() |
|
9942.2.9
by Jeroen Vermeulen
Review changes. |
79 |
|
80 |
return instance |
|
81 |
||
82 |
def _runInstance(self, instance, runnee=None, headless=False): |
|
83 |
"""Set up and run an `EC2Instance` (but without EC2)."""
|
|
9942.2.4
by Jeroen Vermeulen
Cleaned up the test: generalized the stubbing. |
84 |
if runnee is None: |
10236.5.4
by Jeroen Vermeulen
Replace Stub with generic FakeMethod. No Stuart, not you. |
85 |
runnee = FakeMethod() |
9942.2.4
by Jeroen Vermeulen
Cleaned up the test: generalized the stubbing. |
86 |
|
9942.2.9
by Jeroen Vermeulen
Review changes. |
87 |
instance.set_up_and_run(False, not headless, runnee) |
9942.2.4
by Jeroen Vermeulen
Cleaned up the test: generalized the stubbing. |
88 |
|
89 |
def test_EC2Instance_test_baseline(self): |
|
9942.2.9
by Jeroen Vermeulen
Review changes. |
90 |
# The EC2 instances we set up have neither started nor been shut
|
91 |
# down. After running, they have started.
|
|
9942.2.4
by Jeroen Vermeulen
Cleaned up the test: generalized the stubbing. |
92 |
# Not a very useful test, except it establishes the basic
|
9942.2.6
by Jeroen Vermeulen
Docstring typo. |
93 |
# assumptions for the other tests.
|
9942.2.9
by Jeroen Vermeulen
Review changes. |
94 |
instance = self._makeInstance() |
10236.5.4
by Jeroen Vermeulen
Replace Stub with generic FakeMethod. No Stuart, not you. |
95 |
runnee = FakeMethod() |
9942.2.4
by Jeroen Vermeulen
Cleaned up the test: generalized the stubbing. |
96 |
|
97 |
self.assertEqual(0, runnee.call_count) |
|
9942.2.9
by Jeroen Vermeulen
Review changes. |
98 |
self.assertEqual(0, instance.shutdown.call_count) |
9942.2.4
by Jeroen Vermeulen
Cleaned up the test: generalized the stubbing. |
99 |
|
9942.2.9
by Jeroen Vermeulen
Review changes. |
100 |
self._runInstance(instance, runnee=runnee) |
9942.2.4
by Jeroen Vermeulen
Cleaned up the test: generalized the stubbing. |
101 |
|
102 |
self.assertEqual(1, runnee.call_count) |
|
9942.2.1
by Jeroen Vermeulen
Added test, reproduced problem. |
103 |
|
104 |
def test_set_up_and_run_headful(self): |
|
105 |
# A non-headless run executes all tests in the instance, then
|
|
106 |
# shuts down.
|
|
9942.2.9
by Jeroen Vermeulen
Review changes. |
107 |
instance = self._makeInstance() |
108 |
||
109 |
self._runInstance(instance, headless=False) |
|
110 |
||
111 |
self.assertEqual(1, instance.shutdown.call_count) |
|
9942.2.1
by Jeroen Vermeulen
Added test, reproduced problem. |
112 |
|
113 |
def test_set_up_and_run_headless(self): |
|
114 |
# An asynchronous, headless run kicks off the tests on the
|
|
115 |
# instance but does not shut it down.
|
|
9942.2.9
by Jeroen Vermeulen
Review changes. |
116 |
instance = self._makeInstance() |
117 |
||
118 |
self._runInstance(instance, headless=True) |
|
119 |
||
120 |
self.assertEqual(0, instance.shutdown.call_count) |
|
9942.2.4
by Jeroen Vermeulen
Cleaned up the test: generalized the stubbing. |
121 |
|
122 |
def test_set_up_and_run_headful_failure(self): |
|
123 |
# If the test runner barfs, the instance swallows the exception
|
|
124 |
# and shuts down.
|
|
9942.2.9
by Jeroen Vermeulen
Review changes. |
125 |
instance = self._makeInstance() |
10236.5.4
by Jeroen Vermeulen
Replace Stub with generic FakeMethod. No Stuart, not you. |
126 |
runnee = FakeMethod(failure=FakeFailure("Headful barfage.")) |
9942.2.4
by Jeroen Vermeulen
Cleaned up the test: generalized the stubbing. |
127 |
|
9942.2.9
by Jeroen Vermeulen
Review changes. |
128 |
self._runInstance(instance, runnee=runnee, headless=False) |
9942.2.4
by Jeroen Vermeulen
Cleaned up the test: generalized the stubbing. |
129 |
|
9942.2.9
by Jeroen Vermeulen
Review changes. |
130 |
self.assertEqual(1, instance.shutdown.call_count) |
9942.2.4
by Jeroen Vermeulen
Cleaned up the test: generalized the stubbing. |
131 |
|
132 |
def test_set_up_and_run_headless_failure(self): |
|
133 |
# If the instance's test runner fails to set up for a headless
|
|
134 |
# run, the instance swallows the exception and shuts down.
|
|
9942.2.9
by Jeroen Vermeulen
Review changes. |
135 |
instance = self._makeInstance() |
10236.5.4
by Jeroen Vermeulen
Replace Stub with generic FakeMethod. No Stuart, not you. |
136 |
runnee = FakeMethod(failure=FakeFailure("Headless boom.")) |
9942.2.4
by Jeroen Vermeulen
Cleaned up the test: generalized the stubbing. |
137 |
|
9942.2.9
by Jeroen Vermeulen
Review changes. |
138 |
self._runInstance(instance, runnee=runnee, headless=True) |
9942.2.4
by Jeroen Vermeulen
Cleaned up the test: generalized the stubbing. |
139 |
|
9942.2.9
by Jeroen Vermeulen
Review changes. |
140 |
self.assertEqual(1, instance.shutdown.call_count) |