~kfogel/launchpad/255868-patches-view-from-bugs-page

« back to all changes in this revision

Viewing changes to lib/devscripts/ec2test/tests/test_ec2instance.py

  • Committer: Karl Fogel
  • Date: 2010-02-17 16:33:34 UTC
  • mfrom: (9003.2.6 launchpad)
  • Revision ID: karl.fogel@canonical.com-20100217163334-w1r92lthu2w39naj
Merge from db-devel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
4
4
 
8
8
 
9
9
from unittest import TestCase, TestLoader
10
10
 
 
11
from lp.testing.fakemethod import FakeMethod
 
12
 
11
13
from devscripts.ec2test.instance import EC2Instance
12
14
 
13
15
 
14
 
class Stub:
15
 
    """Generic recipient of invocations.
16
 
 
17
 
    Use this to:
18
 
     - Stub methods that should do nothing.
19
 
     - Inject failures into methods.
20
 
     - Record whether a method is being called.
21
 
    """
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. 
25
 
 
26
 
    call_count = 0
27
 
 
28
 
    def __init__(self, return_value=None, simulated_failure=None):
29
 
        """Define what to do when this stub gets invoked.
30
 
 
31
 
        :param return_value: Something to return from the invocation.
32
 
        :parma simulated_failure: Something to raise in the invocation.
33
 
        """
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
38
 
 
39
 
    def __call__(self, *args, **kwargs):
40
 
        """Catch a call.
41
 
 
42
 
        Records the fact that an invocation was made in
43
 
        `call_count`.
44
 
 
45
 
        If you passed a `simulated_failure` to the constructor, that
46
 
        object is raised.
47
 
 
48
 
        :return: The `return_value` you passed to the constructor.
49
 
        """
50
 
        self.call_count += 1
51
 
 
52
 
        if self.simulated_failure is not None:
53
 
            raise self.simulated_failure
54
 
 
55
 
        return self.return_value
56
 
 
57
 
 
58
16
class FakeAccount:
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()
62
20
 
63
21
 
64
22
class FakeOutput:
72
30
    state = 'running'
73
31
    public_dns_name = 'fake-instance'
74
32
 
75
 
    update = Stub()
76
 
    stop = Stub()
 
33
    update = FakeMethod()
 
34
    stop = FakeMethod()
77
35
    get_console_output = FakeOutput
78
36
 
79
37
 
85
43
 
86
44
class FakeImage:
87
45
    """Helper for setting up an `EC2Instance` without EC2."""
88
 
    run = Stub(return_value=FakeReservation())
 
46
    run = FakeMethod(result=FakeReservation())
89
47
 
90
48
 
91
49
class FakeFailure(Exception):
98
56
    def _makeInstance(self):
99
57
        """Set up an `EC2Instance`, with stubbing where needed.
100
58
 
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.
103
61
        """
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)
116
74
 
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()
120
78
 
121
79
        return instance
122
80
 
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:
126
 
            runnee = Stub()
 
84
            runnee = FakeMethod()
127
85
 
128
86
        instance.set_up_and_run(False, not headless, runnee)
129
87
 
133
91
        # Not a very useful test, except it establishes the basic
134
92
        # assumptions for the other tests.
135
93
        instance = self._makeInstance()
136
 
        runnee = Stub()
 
94
        runnee = FakeMethod()
137
95
 
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."))
168
126
 
169
127
        self._runInstance(instance, runnee=runnee, headless=False)
170
128
 
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."))
178
136
 
179
137
        self._runInstance(instance, runnee=runnee, headless=True)
180
138