Left: | ||
Right: |
OLD | NEW |
---|---|
1 import StringIO | 1 import StringIO |
2 import itertools | 2 import itertools |
3 import logging | 3 import logging |
4 import os | 4 import os |
5 import shutil | 5 import shutil |
6 import stat | 6 import stat |
7 import sys | 7 import sys |
8 | 8 |
9 import yaml | 9 import yaml |
10 import zookeeper | 10 import zookeeper |
(...skipping 16 matching lines...) Expand all Loading... | |
27 from juju.state.machine import MachineStateManager | 27 from juju.state.machine import MachineStateManager |
28 from juju.state.relation import ClientServerUnitWatcher | 28 from juju.state.relation import ClientServerUnitWatcher |
29 from juju.state.service import NO_HOOKS | 29 from juju.state.service import NO_HOOKS |
30 from juju.state.tests.test_relation import RelationTestBase | 30 from juju.state.tests.test_relation import RelationTestBase |
31 from juju.state.hook import RelationChange | 31 from juju.state.hook import RelationChange |
32 from juju.unit.lifecycle import ( | 32 from juju.unit.lifecycle import ( |
33 UnitLifecycle, UnitRelationLifecycle, RelationInvoker) | 33 UnitLifecycle, UnitRelationLifecycle, RelationInvoker) |
34 from juju.unit.tests.test_charm import CharmPublisherTestBase | 34 from juju.unit.tests.test_charm import CharmPublisherTestBase |
35 | 35 |
36 from juju.lib.testing import TestCase | 36 from juju.lib.testing import TestCase |
37 from juju.lib.mocker import (ANY, MATCH, Mocker) | 37 from juju.lib.mocker import MATCH |
38 | 38 |
39 | 39 |
40 class UnwriteablePath(object): | 40 class UnwriteablePath(object): |
41 | 41 |
42 def __init__(self, path): | 42 def __init__(self, path): |
43 self.path = path | 43 self.path = path |
44 | 44 |
45 def __enter__(self): | 45 def __enter__(self): |
46 self.mode = os.stat(self.path).st_mode | 46 self.mode = os.stat(self.path).st_mode |
47 os.chmod(self.path, 0000) | 47 os.chmod(self.path, 0000) |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
351 self.assertEqual("up", (yield workflow.get_state())) | 351 self.assertEqual("up", (yield workflow.get_state())) |
352 with (yield workflow.lock()): | 352 with (yield workflow.lock()): |
353 yield workflow.transition_state("down") | 353 yield workflow.transition_state("down") |
354 | 354 |
355 resolved = self.wait_on_state(workflow, "up") | 355 resolved = self.wait_on_state(workflow, "up") |
356 | 356 |
357 # Set the relation to resolved | 357 # Set the relation to resolved |
358 yield self.states["unit"].set_relation_resolved( | 358 yield self.states["unit"].set_relation_resolved( |
359 {self.states["unit_relation"].internal_relation_id: NO_HOOKS}) | 359 {self.states["unit_relation"].internal_relation_id: NO_HOOKS}) |
360 | 360 |
361 | |
362 # Wait for the relation to come back up | 361 # Wait for the relation to come back up |
363 value = yield self.states["unit"].get_relation_resolved() | 362 value = yield self.states["unit"].get_relation_resolved() |
364 | 363 |
365 yield resolved | 364 yield resolved |
366 | 365 |
367 # Verify state | 366 # Verify state |
368 value = yield workflow.get_state() | 367 value = yield workflow.get_state() |
369 self.assertEqual(value, "up") | 368 self.assertEqual(value, "up") |
370 | 369 |
371 self.assertIn( | 370 self.assertIn( |
(...skipping 510 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
882 going_service_relation.get_unit_state(self.states["unit"]), | 881 going_service_relation.get_unit_state(self.states["unit"]), |
883 UnitRelationStateNotFound) | 882 UnitRelationStateNotFound) |
884 | 883 |
885 # The workflow, which we grabbed earlier from the original lifecycle, | 884 # The workflow, which we grabbed earlier from the original lifecycle, |
886 # should now be "departed" (rather than "up", which it was when we | 885 # should now be "departed" (rather than "up", which it was when we |
887 # stopped the original lifecycle). | 886 # stopped the original lifecycle). |
888 self.assertEqual("departed", (yield going_workflow.get_state())) | 887 self.assertEqual("departed", (yield going_workflow.get_state())) |
889 self.assertEqual("up", (yield staying_workflow.get_state())) | 888 self.assertEqual("up", (yield staying_workflow.get_state())) |
890 yield new_lifecycle.stop() | 889 yield new_lifecycle.stop() |
891 | 890 |
891 @inlineCallbacks | |
892 def test_start_invoker(self): | |
893 """Verify the invoker is started by the unit relation lifecycle""" | |
894 # Setup three different wordpress services, wordpress, | |
895 # wordpress-1, wordpress-2 with one service unit each. Each of | |
896 # these will be on the relation with ident app:0, app:1, app:2 | |
897 self.write_hook( | |
898 "start", | |
899 '#!/bin/sh\n echo "hello world"\n') | |
900 yield self.add_opposite_service_unit(self.states) | |
901 for i in xrange(1, 3): | |
hazmat
2012/05/03 22:34:14
don't use xrange, its of little value and dropped
jimbaker
2012/05/04 03:48:08
Pure habit here, fixed by using range instead
| |
902 yield self.add_opposite_service_unit( | |
903 (yield self.add_relation_service_unit_to_another_endpoint( | |
904 self.states, | |
905 RelationEndpoint( | |
906 "wordpress-%d" % i, | |
907 "client-server", "db", "client")))) | |
908 | |
909 finished = self.wait_on_hook("app-relation-changed", 3) | |
910 yield self.lifecycle.start() | |
911 yield finished | |
912 | |
913 # This will verify that config-changed and start both are | |
914 # cached properly. The remaining hook caching will be for the | |
915 # relation hook contexts, along some permutation based on how | |
916 # the hooks are actually scheduled. However, this is tested in | |
917 # UnitRelationLifecycleTest.test_start_invoker so need not be | |
918 # covered here. | |
919 self.assertLogLines( | |
920 self.hook_log.getvalue(), | |
921 ["Cached relation hook contexts: ['app:0', 'app:1', 'app:2']"] * 2) | |
922 | |
892 | 923 |
893 class UnitLifecycleUpgradeTest( | 924 class UnitLifecycleUpgradeTest( |
894 LifecycleTestBase, CharmPublisherTestBase, CharmUpgradeTestBase): | 925 LifecycleTestBase, CharmPublisherTestBase, CharmUpgradeTestBase): |
895 | 926 |
896 @inlineCallbacks | 927 @inlineCallbacks |
897 def setUp(self): | 928 def setUp(self): |
898 yield super(UnitLifecycleUpgradeTest, self).setUp() | 929 yield super(UnitLifecycleUpgradeTest, self).setUp() |
899 yield self.setup_default_test_relation() | 930 yield self.setup_default_test_relation() |
900 self.lifecycle = UnitLifecycle( | 931 self.lifecycle = UnitLifecycle( |
901 self.client, self.states["unit"], self.states["service"], | 932 self.client, self.states["unit"], self.states["service"], |
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1283 self.assertFalse(self.lifecycle.executing) | 1314 self.assertFalse(self.lifecycle.executing) |
1284 | 1315 |
1285 # Start the scheduler only, without the watches | 1316 # Start the scheduler only, without the watches |
1286 yield self.lifecycle.start(start_watches=False) | 1317 yield self.lifecycle.start(start_watches=False) |
1287 self.assertFalse(self.lifecycle.watching) | 1318 self.assertFalse(self.lifecycle.watching) |
1288 self.assertTrue(self.lifecycle.executing) | 1319 self.assertTrue(self.lifecycle.executing) |
1289 | 1320 |
1290 # The scheduler should run the hooks it queued up earlier | 1321 # The scheduler should run the hooks it queued up earlier |
1291 yield hooks_complete | 1322 yield hooks_complete |
1292 | 1323 |
1324 @inlineCallbacks | |
1325 def test_start_invoker(self): | |
1326 """Verify the invoker is started by the unit relation lifecycle""" | |
1327 # Setup 5 different wordpress services, wordpress, wordpress-1 | |
1328 # through wordpress-4 with one service unit each. Each of these | |
1329 # will be on the relation app:0 ... app:4 | |
1330 yield self.add_opposite_service_unit(self.states) | |
1331 for i in xrange(1, 5): | |
hazmat
2012/05/03 22:34:14
ditto re xrange
jimbaker
2012/05/04 03:48:08
Ditto fix
| |
1332 yield self.add_opposite_service_unit( | |
1333 (yield self.add_relation_service_unit_to_another_endpoint( | |
1334 self.states, | |
1335 RelationEndpoint( | |
1336 "wordpress-%d" % i, | |
1337 "client-server", "db", "client")))) | |
1338 yield self.lifecycle.start() | |
1339 yield self.wait_on_hook( | |
1340 sequence=["app-relation-joined", "app-relation-changed"]) | |
1341 | |
1342 # Currently the only action taken by the invoker.start is to | |
1343 # cache the relation hook context; app:0 is excluded because | |
1344 # it is the parent | |
1345 self.assertIn( | |
1346 ("Cached relation hook contexts on 'app:0': " | |
1347 "['app:1', 'app:2', 'app:3', 'app:4']"), | |
1348 self.hook_log.getvalue()) | |
1349 | |
1293 | 1350 |
1294 class SubordinateUnitLifecycleTest(LifecycleTestBase, CharmPublisherTestBase): | 1351 class SubordinateUnitLifecycleTest(LifecycleTestBase, CharmPublisherTestBase): |
1295 | 1352 |
1296 @inlineCallbacks | 1353 @inlineCallbacks |
1297 def setUp(self): | 1354 def setUp(self): |
1298 yield super(SubordinateUnitLifecycleTest, self).setUp() | 1355 yield super(SubordinateUnitLifecycleTest, self).setUp() |
1299 self.output = self.capture_logging(level=logging.DEBUG) | 1356 self.output = self.capture_logging(level=logging.DEBUG) |
1300 environment = self.config.get_default() | 1357 environment = self.config.get_default() |
1301 self.provider = environment.get_machine_provider() | 1358 self.provider = environment.get_machine_provider() |
1302 self.storage = self.provider.get_file_storage() | 1359 self.storage = self.provider.get_file_storage() |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1377 | 1434 |
1378 mock_unit_deploy = self.mocker.patch(lifecycle) | 1435 mock_unit_deploy = self.mocker.patch(lifecycle) |
1379 mock_unit_deploy._do_unit_deploy("logging/0", 0, | 1436 mock_unit_deploy._do_unit_deploy("logging/0", 0, |
1380 MATCH(lambda x: os.path.exists(x))) | 1437 MATCH(lambda x: os.path.exists(x))) |
1381 self.mocker.call(test_complete) | 1438 self.mocker.call(test_complete) |
1382 self.mocker.replay() | 1439 self.mocker.replay() |
1383 | 1440 |
1384 yield lifecycle.start() | 1441 yield lifecycle.start() |
1385 yield test_deferred | 1442 yield test_deferred |
1386 # mocker has already verified the call signature, we're happy | 1443 # mocker has already verified the call signature, we're happy |
OLD | NEW |