~bcsaller/pyjuju/subordinate-endpoints

« back to all changes in this revision

Viewing changes to juju/state/tests/test_sshclient.py

  • Committer: Benjamin Saller
  • Date: 2011-12-16 02:20:56 UTC
  • Revision ID: bcsaller@gmail.com-20111216022056-1j7jiaic8l31tkgz
move to non-namedtuple and default kw arguments

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import collections
2
 
import socket
3
2
import time
4
3
 
5
4
from twisted.internet.process import Process
6
5
from twisted.internet.protocol import ProcessProtocol
7
6
from twisted.internet.defer import succeed, fail, Deferred, inlineCallbacks
 
7
from twisted.python.failure import Failure
8
8
 
9
9
from txzookeeper import ZookeeperClient
10
10
from txzookeeper.client import ConnectionTimeoutException
12
12
from juju.errors import NoConnection, InvalidHost, InvalidUser
13
13
from juju.lib.mocker import MATCH, match_params
14
14
from juju.lib.testing import TestCase
15
 
from juju.state.sshclient import SSHClient
 
15
from juju.state.sshclient import SSHClient, called_aware_deferred_chain
16
16
 
17
17
 
18
18
MATCH_PROTOCOL = MATCH(lambda x: isinstance(x, ProcessProtocol))
33
33
MATCH_LOCALHOST_PORT = MATCH(_match_localhost_port)
34
34
 
35
35
 
 
36
class DeferredChainTest(TestCase):
 
37
 
 
38
    def test_non_true_value(self):
 
39
        chained = Deferred()
 
40
        chained.addBoth(
 
41
            lambda x: self.fail("Should not chain"))
 
42
 
 
43
        self.assertEquals(
 
44
            called_aware_deferred_chain(None, chained),
 
45
            None)
 
46
 
 
47
    def test_chained_deferred_already_called(self):
 
48
        chained = Deferred()
 
49
        chained.callback(None)
 
50
        # attempting to callback again would raise an AlreadyCalled
 
51
        # exception.
 
52
        self.assertEquals(
 
53
            called_aware_deferred_chain(21, chained),
 
54
            21)
 
55
 
 
56
    def test_error_propogated(self):
 
57
        error = SyntaxError()
 
58
        failure = Failure(error)
 
59
        chained = Deferred()
 
60
        self.failUnlessFailure(chained, SyntaxError)
 
61
 
 
62
        self.assertEquals(
 
63
            called_aware_deferred_chain(failure, chained),
 
64
            None)
 
65
        return chained
 
66
 
 
67
    def test_value_propogated(self):
 
68
        marker = object()
 
69
        chained = Deferred()
 
70
 
 
71
        def validate_result(result):
 
72
            self.assertIdentical(result, marker)
 
73
 
 
74
        chained.addCallback(validate_result)
 
75
        self.assertEquals(
 
76
            called_aware_deferred_chain(marker, chained),
 
77
            None)
 
78
        return chained
 
79
 
 
80
 
36
81
class TestDeferredSequence(object):
37
82
    """Helper class for testing sequence of Deferred values w/ mocker."""
38
83
 
155
200
        self.mocker.result(protocol)
156
201
 
157
202
        thread(MATCH_FUNC)
158
 
        self.mocker.result(fail(socket.error("a",)))
 
203
        self.mocker.result(fail(SyntaxError()))
159
204
 
160
205
        protocol.signalProcess("TERM")
161
206
        protocol.loseConnection()