~juju/ubuntu/quantal/juju/0.6

« back to all changes in this revision

Viewing changes to juju/providers/ec2/tests/test_connect.py

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2011-09-28 09:42:32 UTC
  • Revision ID: package-import@ubuntu.com-20110928094232-o1yozyv3nw3ejymz
Tags: upstream-0.5+bzr361
ImportĀ upstreamĀ versionĀ 0.5+bzr361

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from yaml import dump
 
2
 
 
3
from twisted.internet.defer import inlineCallbacks, succeed, fail
 
4
 
 
5
import zookeeper
 
6
 
 
7
from txzookeeper import ZookeeperClient
 
8
from txzookeeper.client import ConnectionTimeoutException
 
9
from txzookeeper.tests.utils import deleteTree
 
10
 
 
11
from juju.lib.testing import TestCase
 
12
 
 
13
from juju.errors import EnvironmentPending, NoConnection
 
14
from juju.state.sshclient import SSHClient
 
15
from juju.providers.ec2.tests.common import EC2TestMixin
 
16
from juju.tests.common import get_test_zookeeper_address
 
17
 
 
18
 
 
19
class EC2ConnectTest(EC2TestMixin, TestCase):
 
20
 
 
21
    def mock_find_zookeepers(self, instance):
 
22
        self.s3.get_object(self.env_name, "provider-state")
 
23
        self.mocker.result(succeed(dump(
 
24
            {"zookeeper-instances": ["i-foobar"]})))
 
25
        self.ec2.describe_instances("i-foobar")
 
26
        self.mocker.result(succeed([instance]))
 
27
 
 
28
    def mock_connect(self, share, instance, result):
 
29
        self.mock_find_zookeepers(instance)
 
30
        client = self.mocker.patch(SSHClient)
 
31
        client.connect("foo.example.com:2181", timeout=30, share=share)
 
32
        self.mocker.result(result)
 
33
 
 
34
    def assert_connect_error(self, error, expect_type, expect_message):
 
35
        instance = self.get_instance("i-foobar", dns_name="foo.example.com")
 
36
        self.mock_connect(False, instance, fail(error))
 
37
        self.mocker.replay()
 
38
 
 
39
        provider = self.get_provider()
 
40
        d = provider.connect()
 
41
 
 
42
        def check_error(error):
 
43
            self.assertEqual(str(error), expect_message)
 
44
        self.assertFailure(d, expect_type)
 
45
        d.addCallback(check_error)
 
46
        return d
 
47
 
 
48
    def test_no_dns_name(self):
 
49
        """
 
50
        `EnvironmentPending` should be raised if no zookeeper nodes have dns
 
51
        names
 
52
        """
 
53
        instance = self.get_instance("i-foobar")
 
54
        self.mock_find_zookeepers(instance)
 
55
        self.mocker.replay()
 
56
 
 
57
        provider = self.get_provider()
 
58
        d = provider.connect()
 
59
 
 
60
        def check_error(error):
 
61
            self.assertEqual(
 
62
                str(error), "No machines have addresses assigned yet")
 
63
 
 
64
        self.assertFailure(d, NoConnection)
 
65
        d.addCallback(check_error)
 
66
        return d
 
67
 
 
68
    def test_provider_connect_forwards_share_option(self):
 
69
        """The `share` kwarg should be passed through to `SSHClient.connect`"""
 
70
        instance = self.get_instance("i-foobar", dns_name="foo.example.com")
 
71
        connected_client = self.mocker.mock(type=SSHClient)
 
72
        self.mock_connect(True, instance, succeed(connected_client))
 
73
        # We'll test the wait on initialization separately.
 
74
        connected_client.exists_and_watch("/initialized")
 
75
        self.mocker.result((succeed(True), None))
 
76
        self.mocker.replay()
 
77
 
 
78
        provider = self.get_provider()
 
79
        d = provider.connect(share=True)
 
80
 
 
81
        def verify_result(result):
 
82
            self.assertIdentical(connected_client, result)
 
83
        d.addCallback(verify_result)
 
84
        return d
 
85
 
 
86
    def test_no_connection(self):
 
87
        """`NoConnection` errors should become `EnvironmentPending`s"""
 
88
        return self.assert_connect_error(
 
89
            NoConnection("KABOOM!"), EnvironmentPending,
 
90
            "Cannot connect to machine i-foobar (perhaps still initializing): "
 
91
            "KABOOM!")
 
92
 
 
93
    def test_txzookeeper_error(self):
 
94
        """
 
95
        `ConnectionTimeoutException` errors should become `EnvironmentPending`s
 
96
        """
 
97
        return self.assert_connect_error(
 
98
            ConnectionTimeoutException("SPLAT!"), EnvironmentPending,
 
99
            "Cannot connect to machine i-foobar (perhaps still initializing): "
 
100
            "SPLAT!")
 
101
 
 
102
    def test_other_error(self):
 
103
        """Other errors should propagate"""
 
104
        return self.assert_connect_error(
 
105
            TypeError("THUD!"), TypeError, "THUD!")
 
106
 
 
107
    @inlineCallbacks
 
108
    def test_provider_connect_waits_on_initialization(self):
 
109
        """
 
110
        A connection to a zookeeper that is running, but whose juju state
 
111
        is not ready, should wait until that state is ready.
 
112
        """
 
113
        # Hand back a real connected client to test the wait on initialization.
 
114
        instance = self.get_instance("i-foobar", dns_name="foo.example.com")
 
115
        connected_client = ZookeeperClient()
 
116
        self.client = connected_client  # for poke_zk
 
117
        self.mock_connect(False, instance, succeed(connected_client))
 
118
 
 
119
        self.mocker.replay()
 
120
 
 
121
        zookeeper.set_debug_level(0)
 
122
        yield connected_client.connect(get_test_zookeeper_address())
 
123
 
 
124
        client_result = []
 
125
 
 
126
        provider = self.get_provider()
 
127
        client_deferred = provider.connect()
 
128
        client_deferred.addCallback(client_result.append)
 
129
 
 
130
        # Give it a chance to do it incorrectly.
 
131
        yield self.poke_zk()
 
132
 
 
133
        try:
 
134
            self.assertEquals(client_result, [])
 
135
 
 
136
            yield connected_client.create("/initialized")
 
137
 
 
138
            yield client_deferred
 
139
            self.assertTrue(client_result, client_result)
 
140
            self.assertIdentical(client_result[0], connected_client)
 
141
        finally:
 
142
            deleteTree("/", connected_client.handle)
 
143
            connected_client.close()