197
197
self.assertFalse(self.topology.has_service("m-0"))
198
198
self.assertTrue(self.topology.has_service("m-1"))
200
def test_remove_principal_service(self):
201
"""Verify that removing a principal service behaves correctly.
203
This will have to change as the implementation of remove is
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)
212
# This fails w/o a container relation in place
213
err = self.assertRaises(InternalTopologyError,
214
self.topology.add_service_unit,
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")
227
err = self.assertRaises(InternalTopologyError,
228
self.topology.remove_service, "s-0")
229
self.assertIn("Service 's-0' is associated to relations",
232
def test_remove_subordinate_service(self):
233
"""Verify that removing a principal service behaves correctly.
235
This will have to change as the implementation of remove is
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)
244
# This fails w/o a container relation in place
245
err = self.assertRaises(InternalTopologyError,
246
self.topology.add_service_unit,
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")
259
err = self.assertRaises(InternalTopologyError,
260
self.topology.remove_service, "s-1")
261
self.assertIn("Service 's-1' is associated to relations",
200
264
def test_remove_non_existent_service(self):
202
266
Attempting to remove a non-existing service should be an
222
286
self.assertEquals(self.topology.get_service_units("s-1"),
289
def test_add_service_unit_with_container(self):
290
""" validates that add_service_unit() properly handles its conatiner_id argument.
292
This test checks both the case where a container relation does and does not exist.
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)
299
# This fails w/o a container relation in place
300
err = self.assertRaises(InternalTopologyError,
301
self.topology.add_service_unit,
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")
314
self.assertEquals(self.topology.add_service_unit("s-1", "u-07",
315
container_id="u-05"), 0)
317
self.assertEquals(sorted(self.topology.get_service_units("s-0")),
319
self.assertEquals(self.topology.get_service_units("s-1"), ["u-07"])
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)
325
self.assertEquals(self.topology.get_service_unit_container("u-07"),
326
("s-0", "wordpress", 0, "u-05"))
328
self.assertEquals(self.topology.get_service_unit_container("u-05"),
225
331
def test_global_unique_service_names(self):
226
332
"""Service unit names are unique.
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"))
547
def test_remove_principal_service_unit(self):
548
"""Verify that removing a principal service unit behaves correctly.
550
This will have to change as the implementation of remove is
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)
559
# This fails w/o a container relation in place
560
err = self.assertRaises(InternalTopologyError,
561
self.topology.add_service_unit,
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")
574
self.assertEquals(self.topology.add_service_unit(
575
"s-1", "u-07", container_id="u-05"), 0)
577
self.topology.remove_service_unit("s-0", "u-05")
579
self.assertFalse(self.topology.has_service_unit("s-0", "u-05"))
580
self.assertTrue(self.topology.has_service_unit("s-0", "u-12"))
582
def test_remove_subordinate_service_unit(self):
583
"""Verify that removing a subordinate service unit behaves correctly.
585
This will have to change as the implementation of remove is
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)
594
# This fails w/o a container relation in place
595
err = self.assertRaises(InternalTopologyError,
596
self.topology.add_service_unit,
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")
609
self.assertEquals(self.topology.add_service_unit(
610
"s-1", "u-07", container_id="u-05"), 0)
612
self.topology.remove_service_unit("s-1", "u-07")
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"))
441
619
def test_remove_non_existent_service_unit(self):
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",
984
"service": {"name": "name", "role": "role"}}]
805
987
# Adding it again raises an error, even with a different name/role
806
988
self.assertRaises(InternalTopologyError,
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"}})
921
1106
def test_get_relations_for_service(self):
922
1107
"""The relations for a given service can be retrieved.
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")
1270
# Topology migration tests use these. defined at global scope to ease
1275
relation-0000000000:
1277
- service-0000000000: {name: db, role: client}
1278
service-0000000001: {name: server, role: server}
1282
TOPOLOGY_V2_EXPECTED = """
1285
relation-0000000000: {}
1288
service-0000000000: {name: db, role: client}
1289
service-0000000001: {name: server, role: server}
1294
class TestMigrations(TestCase):
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
1302
Ensure that a version 2 topology is emitted.
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())
1310
def test_migration_v1_to_unknown(self):
1311
"""Parse a fragment of a version 1 topology
1313
Ensure that a version 2 topology is emitted.
1315
topology = InternalTopology()
1317
actual_version = VERSION
1319
self.patch(juju.state.topology, "VERSION", actual_version + 1)
1321
ex = self.assertRaises(IncompatibleVersion,
1322
topology.parse, TOPOLOGY_V1)
1325
"Incompatible juju protocol versions (found 1, want %d)" % (
1326
juju.state.topology.VERSION))