~hazmat/pyjuju/rapi-rollup

« back to all changes in this revision

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

  • Committer: Kapil Thangavelu
  • Date: 2013-01-20 12:17:15 UTC
  • mfrom: (616.2.13 rapi-annotation)
  • Revision ID: kapil@canonical.com-20130120121715-exju6n7miobz9gmc
Merged rapi-annotation into rapi-rollup.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import logging
 
2
 
1
3
from twisted.internet.defer import inlineCallbacks, returnValue
2
4
from juju.lib.pick import pick_attr
3
5
from juju.lib import serializer
98
100
        self.relation = self.mysql_states["relation"]
99
101
 
100
102
    @inlineCallbacks
101
 
    def add_another_blog(self, service_name):
102
 
        blog_ep = RelationEndpoint(
103
 
            service_name, "client-server", "database", "client")
104
 
        mysql_ep = RelationEndpoint(
105
 
            "mysql", "client-server", "db", "server")
106
 
        yield self.add_relation_service_unit_from_endpoints(
107
 
            blog_ep, mysql_ep)
 
103
    def add_another_blog(self, blog_name):
 
104
        blog_ep = RelationEndpoint(blog_name, "client-server", "app", "client")
 
105
        # Fully construct states for the relation connecting to this additional blog
 
106
        other_mysql_states = yield self.add_relation_service_unit_to_another_endpoint(
 
107
                    self.mysql_states, blog_ep)
 
108
        # Then complete in the opposite direction
 
109
        blog_states = yield self.add_opposite_service_unit(other_mysql_states)
 
110
        yield blog_states['service_relations'][-1].add_unit_state(
 
111
            self.mysql_states['unit'])
 
112
        returnValue(blog_states)
108
113
 
109
114
    @inlineCallbacks
110
115
    def add_db_admin_tool(self, admin_name):
111
116
        """Add another relation, using a different relation name"""
112
117
        admin_ep = RelationEndpoint(
113
118
            admin_name, "client-server", "admin-app", "client")
114
 
        mysql_ep = RelationEndpoint(
 
119
        mysql_admin_ep = RelationEndpoint(
115
120
            "mysql", "client-server", "db-admin", "server")
116
 
        yield self.add_relation_service_unit_from_endpoints(
117
 
            admin_ep, mysql_ep)
 
121
        mysql_admin_states = yield self.reuse_service_unit_in_new_relation(
 
122
            self.mysql_states, mysql_admin_ep, admin_ep)
 
123
        admin_states = yield self.add_opposite_service_unit(mysql_admin_states)
 
124
        returnValue(admin_states)
 
125
 
 
126
    @inlineCallbacks
 
127
    def reuse_service_unit_in_new_relation(self, reused_states, *endpoints):
 
128
        """Reuse an existing service unit as part of a new relation"""
 
129
        service_state = reused_states["service"]
 
130
        unit_state = reused_states["unit"]
 
131
 
 
132
        # 1. Setup all service states
 
133
        service_states = [service_state]
 
134
        for endpoint in endpoints[1:]:
 
135
            service_state = yield self.add_service(endpoint.service_name)
 
136
            service_states.append(service_state)
 
137
 
 
138
        # 2. And join together in a relation
 
139
        relation_state, service_relation_states = \
 
140
                        yield self.relation_manager.add_relation_state(
 
141
                            *endpoints)
 
142
        # 3. Add a service unit to only the first endpoint - we need
 
143
        #    to test what happens when service units are added to the
 
144
        #    other service state (if any), so do so separately
 
145
        relation_unit_state = yield service_relation_states[0].add_unit_state(
 
146
            unit_state)
 
147
 
 
148
        returnValue({
 
149
            "endpoints": list(endpoints),
 
150
            "service": service_states[0],
 
151
            "services": service_states,
 
152
            "unit": unit_state,
 
153
            "relation": relation_state,
 
154
            "service_relation": service_relation_states[0],
 
155
            "unit_relation": relation_unit_state,
 
156
            "service_relations": service_relation_states})
 
157
 
118
158
 
119
159
 
120
160
class HookContextTest(HookContextTestBase, CommonHookContextTestsMixin):
162
202
            set((yield new_context.get_relation_idents(None))),
163
203
            set(["db:0", "db:1", "db:2", "db-admin:3"]))
164
204
 
 
205
    @inlineCallbacks
 
206
    def test_get_relation_idents_partial_updates_to_zk(self):
 
207
        """Verify relation idents do not reflect partial updates to ZK."""
 
208
        log = self.capture_logging(level=logging.DEBUG)
 
209
 
 
210
        # 1. Partial update - no corresponding service relation unit
 
211
        yield self.add_relation_service_unit_to_another_endpoint(
 
212
            self.mysql_states,
 
213
            RelationEndpoint(
 
214
                "wordpress2", "client-server", "app", "client"))
 
215
 
 
216
        # 2. Do a complete update of adding another relation and
 
217
        # corresponding units
 
218
        yield self.add_another_blog("wordpress3")
 
219
        context = self.get_context("mysql/0")
 
220
 
 
221
        # 3. Observe only the relation ids for wordpress, wordpress3
 
222
        self.assertEqual(
 
223
            set((yield context.get_relation_idents("db"))),
 
224
            set(["db:0", "db:2"]))
 
225
        self.assertIn("Ignoring partially constructed relation: db:1",
 
226
                      log.getvalue())
 
227
 
 
228
        # 4. Finally, relation ids for a nonexistent relation are
 
229
        #    still not seen, or cause an error.
 
230
        self.assertEqual(
 
231
            set((yield context.get_relation_idents("not-a-relation"))),
 
232
            set())
 
233
 
165
234
 
166
235
class RelationHookContextTest(HookContextTestBase,
167
236
                              CommonHookContextTestsMixin):
178
247
            self.wordpress_states, "modified", "mysql/0")
179
248
 
180
249
    def get_context(self, states, change_type, unit_name):
181
 
        change = RelationChange(
182
 
            states["service_relation"].relation_ident,
183
 
            change_type,
184
 
            unit_name)
185
250
        return RelationHookContext(
186
251
            self.client, states["unit_relation"],
187
252
            states["service_relation"].relation_ident,
605
670
        self.assertEqual(
606
671
            set((yield new_context.get_relation_idents("db"))),
607
672
            set(["db:1"]))
608
 
        yield self.assertFailure( 
 
673
        yield self.assertFailure(
609
674
            new_context.get_relation_hook_context("db:0"),
610
675
            RelationStateNotFound)
611
676
        db1 = yield new_context.get_relation_hook_context("db:1")