~hazmat/pyjuju/proposed-support

« back to all changes in this revision

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

  • Committer: kapil.thangavelu at canonical
  • Date: 2012-05-22 22:08:15 UTC
  • mfrom: (484.1.53 trunk)
  • Revision ID: kapil.thangavelu@canonical.com-20120522220815-acyt8m89i9ybe0w1
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
197
197
        self.assertFalse(self.topology.has_service("m-0"))
198
198
        self.assertTrue(self.topology.has_service("m-1"))
199
199
 
 
200
    def test_remove_principal_service(self):
 
201
        """Verify that removing a principal service behaves correctly.
 
202
 
 
203
        This will have to change as the implementation of remove is
 
204
        still pending.
 
205
 
 
206
        """
 
207
        self.topology.add_service("s-0", "wordpress")
 
208
        self.topology.add_service("s-1", "mysql")
 
209
        self.assertEquals(self.topology.add_service_unit("s-0", "u-05"), 0)
 
210
        self.assertEquals(self.topology.add_service_unit("s-0", "u-12"), 1)
 
211
 
 
212
        # This fails w/o a container relation in place
 
213
        err = self.assertRaises(InternalTopologyError,
 
214
                          self.topology.add_service_unit,
 
215
                                "s-1", "u-07",
 
216
                                container_id="u-05")
 
217
        self.assertEquals(str(err),
 
218
                          "Attempted to add subordinate unit without "
 
219
                          "container relation")
 
220
        # now add the relationship and try again
 
221
        self.topology.add_relation("r-1", "client-server", "container")
 
222
        self.topology.assign_service_to_relation(
 
223
            "r-1", "s-0", "juju-info", "server")
 
224
        self.topology.assign_service_to_relation(
 
225
            "r-1", "s-1", "juju-info", "client")
 
226
 
 
227
        err = self.assertRaises(InternalTopologyError,
 
228
                                self.topology.remove_service, "s-0")
 
229
        self.assertIn("Service 's-0' is associated to relations",
 
230
                      str(err))
 
231
 
 
232
    def test_remove_subordinate_service(self):
 
233
        """Verify that removing a principal service behaves correctly.
 
234
 
 
235
        This will have to change as the implementation of remove is
 
236
        still pending.
 
237
 
 
238
        """
 
239
        self.topology.add_service("s-0", "wordpress")
 
240
        self.topology.add_service("s-1", "mysql")
 
241
        self.assertEquals(self.topology.add_service_unit("s-0", "u-05"), 0)
 
242
        self.assertEquals(self.topology.add_service_unit("s-0", "u-12"), 1)
 
243
 
 
244
        # This fails w/o a container relation in place
 
245
        err = self.assertRaises(InternalTopologyError,
 
246
                          self.topology.add_service_unit,
 
247
                                "s-1", "u-07",
 
248
                                container_id="u-05")
 
249
        self.assertEquals(str(err),
 
250
                          "Attempted to add subordinate unit without "
 
251
                          "container relation")
 
252
        # now add the relationship and try again
 
253
        self.topology.add_relation("r-1", "client-server", "container")
 
254
        self.topology.assign_service_to_relation(
 
255
            "r-1", "s-0", "juju-info", "server")
 
256
        self.topology.assign_service_to_relation(
 
257
            "r-1", "s-1", "juju-info", "client")
 
258
 
 
259
        err = self.assertRaises(InternalTopologyError,
 
260
                                self.topology.remove_service, "s-1")
 
261
        self.assertIn("Service 's-1' is associated to relations",
 
262
                      str(err))
 
263
 
200
264
    def test_remove_non_existent_service(self):
201
265
        """
202
266
        Attempting to remove a non-existing service should be an
222
286
        self.assertEquals(self.topology.get_service_units("s-1"),
223
287
                          ["u-07"])
224
288
 
 
289
    def test_add_service_unit_with_container(self):
 
290
        """ validates that add_service_unit() properly handles its conatiner_id argument.
 
291
 
 
292
        This test checks both the case where a container relation does and does not exist.
 
293
        """
 
294
        self.topology.add_service("s-0", "wordpress")
 
295
        self.topology.add_service("s-1", "mysql")
 
296
        self.assertEquals(self.topology.add_service_unit("s-0", "u-05"), 0)
 
297
        self.assertEquals(self.topology.add_service_unit("s-0", "u-12"), 1)
 
298
 
 
299
        # This fails w/o a container relation in place
 
300
        err = self.assertRaises(InternalTopologyError,
 
301
                          self.topology.add_service_unit,
 
302
                                "s-1", "u-07",
 
303
                                container_id="u-05")
 
304
        self.assertEquals(str(err),
 
305
                          "Attempted to add subordinate unit without "
 
306
                          "container relation")
 
307
        # now add the relationship and try again
 
308
        self.topology.add_relation("r-1", "client-server", "container")
 
309
        self.topology.assign_service_to_relation("r-1", "s-0",
 
310
                                                 "juju-info", "server")
 
311
        self.topology.assign_service_to_relation("r-1", "s-1",
 
312
                                                 "juju-info", "client")
 
313
 
 
314
        self.assertEquals(self.topology.add_service_unit("s-1", "u-07",
 
315
                                                         container_id="u-05"), 0)
 
316
 
 
317
        self.assertEquals(sorted(self.topology.get_service_units("s-0")),
 
318
                          ["u-05", "u-12"])
 
319
        self.assertEquals(self.topology.get_service_units("s-1"), ["u-07"])
 
320
 
 
321
        self.assertEquals(self.topology.get_service_unit_principal("u-07"), "u-05")
 
322
        self.assertEquals(self.topology.get_service_unit_principal("u-12"), None)
 
323
        self.assertEquals(self.topology.get_service_unit_principal("u-05"), None)
 
324
 
 
325
        self.assertEquals(self.topology.get_service_unit_container("u-07"),
 
326
                          ("s-0", "wordpress", 0, "u-05"))
 
327
 
 
328
        self.assertEquals(self.topology.get_service_unit_container("u-05"),
 
329
                          None)
 
330
 
225
331
    def test_global_unique_service_names(self):
226
332
        """Service unit names are unique.
227
333
 
438
544
        self.assertFalse(self.topology.has_service_unit("s-0", "m-0"))
439
545
        self.assertTrue(self.topology.has_service_unit("s-0", "m-1"))
440
546
 
 
547
    def test_remove_principal_service_unit(self):
 
548
        """Verify that removing a principal service unit behaves correctly.
 
549
 
 
550
        This will have to change as the implementation of remove is
 
551
        still pending.
 
552
 
 
553
        """
 
554
        self.topology.add_service("s-0", "wordpress")
 
555
        self.topology.add_service("s-1", "mysql")
 
556
        self.assertEquals(self.topology.add_service_unit("s-0", "u-05"), 0)
 
557
        self.assertEquals(self.topology.add_service_unit("s-0", "u-12"), 1)
 
558
 
 
559
        # This fails w/o a container relation in place
 
560
        err = self.assertRaises(InternalTopologyError,
 
561
                          self.topology.add_service_unit,
 
562
                                "s-1", "u-07",
 
563
                                container_id="u-05")
 
564
        self.assertEquals(str(err),
 
565
                          "Attempted to add subordinate unit without "
 
566
                          "container relation")
 
567
        # now add the relationship and try again
 
568
        self.topology.add_relation("r-1", "client-server", "container")
 
569
        self.topology.assign_service_to_relation(
 
570
            "r-1", "s-0", "juju-info", "server")
 
571
        self.topology.assign_service_to_relation(
 
572
            "r-1", "s-1", "juju-info", "client")
 
573
 
 
574
        self.assertEquals(self.topology.add_service_unit(
 
575
            "s-1", "u-07", container_id="u-05"), 0)
 
576
 
 
577
        self.topology.remove_service_unit("s-0", "u-05")
 
578
 
 
579
        self.assertFalse(self.topology.has_service_unit("s-0", "u-05"))
 
580
        self.assertTrue(self.topology.has_service_unit("s-0", "u-12"))
 
581
 
 
582
    def test_remove_subordinate_service_unit(self):
 
583
        """Verify that removing a subordinate service unit behaves correctly.
 
584
 
 
585
        This will have to change as the implementation of remove is
 
586
        still pending.
 
587
 
 
588
        """
 
589
        self.topology.add_service("s-0", "wordpress")
 
590
        self.topology.add_service("s-1", "mysql")
 
591
        self.assertEquals(self.topology.add_service_unit("s-0", "u-05"), 0)
 
592
        self.assertEquals(self.topology.add_service_unit("s-0", "u-12"), 1)
 
593
 
 
594
        # This fails w/o a container relation in place
 
595
        err = self.assertRaises(InternalTopologyError,
 
596
                          self.topology.add_service_unit,
 
597
                                "s-1", "u-07",
 
598
                                container_id="u-05")
 
599
        self.assertEquals(str(err),
 
600
                          "Attempted to add subordinate unit without "
 
601
                          "container relation")
 
602
        # now add the relationship and try again
 
603
        self.topology.add_relation("r-1", "client-server", "container")
 
604
        self.topology.assign_service_to_relation(
 
605
            "r-1", "s-0", "juju-info", "server")
 
606
        self.topology.assign_service_to_relation(
 
607
            "r-1", "s-1", "juju-info", "client")
 
608
 
 
609
        self.assertEquals(self.topology.add_service_unit(
 
610
            "s-1", "u-07", container_id="u-05"), 0)
 
611
 
 
612
        self.topology.remove_service_unit("s-1", "u-07")
 
613
 
 
614
        self.assertTrue(self.topology.has_service_unit("s-0", "u-05"))
 
615
        self.assertTrue(self.topology.has_service_unit("s-0", "u-12"))
 
616
        # The subordinate unit can be removed
 
617
        self.assertFalse(self.topology.has_service_unit("s-1", "u-07"))
 
618
 
441
619
    def test_remove_non_existent_service_unit(self):
442
620
        """
443
621
        Attempting to remove a non-existing service unit or a unit
800
978
        self.assertFalse(self.topology.relation_has_service("r-1", "s-0"))
801
979
        self.topology.assign_service_to_relation("r-1", "s-0", "name", "role")
802
980
        self.assertEqual(self.topology.get_relations_for_service("s-0"),
803
 
                         [("r-1", "type", {"name": "name", "role": "role"})])
 
981
                         [{"interface": "type",
 
982
                           "relation_id": "r-1",
 
983
                           "scope": "global",
 
984
                           "service": {"name": "name", "role": "role"}}]
 
985
                         )
804
986
 
805
987
        # Adding it again raises an error, even with a different name/role
806
988
        self.assertRaises(InternalTopologyError,
842
1024
 
843
1025
        self.topology.assign_service_to_relation("r-1", "s-0", "name", "role")
844
1026
        self.assertEqual(self.topology.get_relations_for_service("s-0"),
845
 
                         [("r-1", "type", {"name": "name", "role": "role"})])
 
1027
                         [{"interface": "type",
 
1028
                           "relation_id": "r-1",
 
1029
                           "scope": "global",
 
1030
                           "service": {"name": "name", "role": "role"}}])
846
1031
 
847
1032
        self.topology.unassign_service_from_relation("r-1", "s-0")
848
1033
        self.assertFalse(self.topology.get_relations_for_service("s-0"))
915
1100
        self.topology.assign_service_to_relation("r-1", "s-1", "name", "role2")
916
1101
        self.assertEqual(
917
1102
            self.topology.get_relation_services("r-1"),
918
 
            {'s-1': {'role': 'role2', 'name': 'name'},
919
 
             's-0': {'role': 'role', 'name': 'name'}})
 
1103
            {"s-1": {"role": "role2", "name": "name"},
 
1104
             "s-0": {"role": "role", "name": "name"}})
920
1105
 
921
1106
    def test_get_relations_for_service(self):
922
1107
        """The relations for a given service can be retrieved.
938
1123
 
939
1124
        self.assertEqual(
940
1125
            sorted(self.topology.get_relations_for_service("s-0")),
941
 
            [("r-1", "type", {"name": "name", "role": "role"}),
942
 
             ("r-2", "type", {"name": "name", "role": "role"})])
 
1126
            [{"interface": "type",
 
1127
              "relation_id": "r-1",
 
1128
              "scope": "global",
 
1129
              "service": {"name": "name", "role": "role"}},
 
1130
             {"interface": "type",
 
1131
              "relation_id": "r-2",
 
1132
              "scope": "global",
 
1133
              "service": {"name": "name", "role": "role"}}])
943
1134
 
944
1135
        self.topology.unassign_service_from_relation("r-2", "s-0")
945
1136
 
1070
1261
        riak_ep = RelationEndpoint("riak", "riak", "riak", "peer")
1071
1262
        self.topology.add_service("s-0", "riak")
1072
1263
        self.topology.add_relation("r-0", "riak")
1073
 
        self.topology.assign_service_to_relation("r-0", "s-0", "riak", "peer")
 
1264
        self.topology.assign_service_to_relation(
 
1265
            "r-0", "s-0", "riak", "peer")
1074
1266
        self.assertEqual(self.topology.get_relation_between_endpoints(
1075
1267
            [riak_ep]), "r-0")
 
1268
 
 
1269
 
 
1270
# Topology migration tests use these. defined at global scope to ease
 
1271
# string formatting
 
1272
 
 
1273
TOPOLOGY_V1 = """
 
1274
relations:
 
1275
  relation-0000000000:
 
1276
    - mysql
 
1277
    - service-0000000000: {name: db, role: client}
 
1278
      service-0000000001: {name: server, role: server}
 
1279
version: 1
 
1280
"""
 
1281
 
 
1282
TOPOLOGY_V2_EXPECTED = """
 
1283
relations:
 
1284
  interface: mysql
 
1285
  relation-0000000000: {}
 
1286
  scope: global
 
1287
  services:
 
1288
    service-0000000000: {name: db, role: client}
 
1289
    service-0000000001: {name: server, role: server}
 
1290
version: 2
 
1291
"""
 
1292
 
 
1293
 
 
1294
class TestMigrations(TestCase):
 
1295
 
 
1296
    # DISABLED: We don't do transparent data migrations, till we
 
1297
    # have explicit juju core code upgrades, else older agents
 
1298
    # will die on new topology formats.
 
1299
    def xtest_migration_v1_to_v2(self):
 
1300
        """Parse a fragment of a version 1 topology
 
1301
 
 
1302
        Ensure that a version 2 topology is emitted.
 
1303
        """
 
1304
        topology = InternalTopology()
 
1305
        topology.parse(TOPOLOGY_V1)
 
1306
        self.assertEqual(topology.get_version(), 2)
 
1307
        self.assertEqual(topology.dump().strip(),
 
1308
                         TOPOLOGY_V2_EXPECTED.strip())
 
1309
 
 
1310
    def test_migration_v1_to_unknown(self):
 
1311
        """Parse a fragment of a version 1 topology
 
1312
 
 
1313
        Ensure that a version 2 topology is emitted.
 
1314
        """
 
1315
        topology = InternalTopology()
 
1316
 
 
1317
        actual_version = VERSION
 
1318
        import juju
 
1319
        self.patch(juju.state.topology, "VERSION", actual_version + 1)
 
1320
 
 
1321
        ex = self.assertRaises(IncompatibleVersion,
 
1322
                               topology.parse, TOPOLOGY_V1)
 
1323
        self.assertEqual(
 
1324
            str(ex),
 
1325
            "Incompatible juju protocol versions (found 1, want %d)" % (
 
1326
                    juju.state.topology.VERSION))