~hazmat/pyjuju/security-specification

« back to all changes in this revision

Viewing changes to ensemble/agents/tests/test_unit.py

  • Committer: kapil.thangavelu at canonical
  • Date: 2011-05-26 17:06:12 UTC
  • mfrom: (226.4.27 watching-godot-redux)
  • Revision ID: kapil.thangavelu@canonical.com-20110526170612-0op8p202usfullvs
merge waiting-for-godot [r=niemeyer][f=778628]

Improve watch apis to wait for their initial invocation against the
current state before returning. This allows for much better
synchronization betewen the invoker and background activity. Also
fixes some broken tests from trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
295
295
        self.agent.set_watch_enabled(True)
296
296
        yield self.agent.startService()
297
297
        yield self.states["unit"].enable_hook_debug(["*"])
 
298
        # Wait for watch to fire invoke callback and reset
298
299
        yield self.sleep(0.1)
299
300
        # Check the propogation to the executor
300
301
        self.assertNotEquals(
319
320
            "started", (yield self.agent.workflow.get_state()))
320
321
 
321
322
        yield self.agent.unit_state.set_resolved(RETRY_HOOKS)
 
323
        # Wait for watch to fire and reset
322
324
        yield self.sleep(0.1)
323
325
 
324
326
        self.assertEqual(
343
345
        self.assertEqual(
344
346
            "install_error", (yield self.agent.workflow.get_state()))
345
347
 
346
 
        install_deferred = self.wait_on_hook(
347
 
            "install", executor=self.agent.executor)
 
348
        install_deferred = self.wait_on_state(self.agent.workflow, "installed")
348
349
        self.write_hook("install", "#!/bin/sh\nexit 0")
349
350
        yield self.agent.unit_state.set_resolved(RETRY_HOOKS)
350
351
        yield install_deferred
351
 
        yield self.poke_zk()
352
352
        self.assertEqual("installed", (yield self.agent.workflow.get_state()))
 
353
        # Ensure we clear out background activity from the watch firing
 
354
        yield self.poke_zk()
353
355
 
354
356
    @inlineCallbacks
355
357
    def test_resolved_start_error(self):
369
371
            "start_error", (yield self.agent.workflow.get_state()))
370
372
 
371
373
        state_deferred = self.wait_on_state(self.agent.workflow, "started")
372
 
        hook_deferred = self.wait_on_hook(
373
 
            "start", executor=self.agent.executor)
374
374
        self.write_hook("start", "#!/bin/sh\nexit 0")
375
375
        yield self.agent.unit_state.set_resolved(NO_HOOKS)
376
376
        yield state_deferred
377
 
 
378
 
        # let background async prcoessing finish..
379
 
        yield self.sleep(0.1)
380
 
 
381
377
        self.assertEqual("started", (yield self.agent.workflow.get_state()))
 
378
        # Resolving to the started state from the resolved watch will cause the
 
379
        # lifecycle start to execute in the background context, wait
 
380
        # for it to finish.
 
381
        yield self.sleep(0.1)
382
382
 
383
383
    @inlineCallbacks
384
384
    def test_resolved_stopped(self):
400
400
 
401
401
        self.assertEqual("stop_error", (yield self.agent.workflow.get_state()))
402
402
 
403
 
        hook_deferred = self.wait_on_hook("stop", executor=self.agent.executor)
 
403
        state_deferred = self.wait_on_state(self.agent.workflow, "stopped")
404
404
        self.write_hook("stop", "#!/bin/sh\nexit 0")
405
405
        yield self.agent.unit_state.set_resolved(RETRY_HOOKS)
406
 
        yield hook_deferred
407
 
        yield self.poke_zk()
408
 
        yield self.poke_zk()
 
406
        yield state_deferred
409
407
        self.assertEqual("stopped", (yield self.agent.workflow.get_state()))
 
408
        # Ensure we clear out background activity from the watch firing
 
409
        yield self.poke_zk()
410
410
 
411
411
    @inlineCallbacks
412
412
    def test_hook_error_on_resolved_retry_remains_in_error_state(self):
431
431
        hook_deferred = self.wait_on_hook("stop", executor=self.agent.executor)
432
432
        yield self.agent.unit_state.set_resolved(RETRY_HOOKS)
433
433
        yield hook_deferred
 
434
        # Ensure we clear out background activity from the watch firing
434
435
        yield self.poke_zk()
435
436
        self.assertEqual("stop_error", (yield self.agent.workflow.get_state()))
436
437
 
466
467
        hook_done = self.wait_on_hook(
467
468
            "upgrade-formula", executor=self.agent.executor)
468
469
        yield self.agent.startService()
469
 
 
470
470
        yield hook_done
471
 
 
472
 
        # Stop also verifies started state.
473
 
        yield self.agent.workflow.fire_transition("stop")
 
471
        yield self.assertState(self.agent.workflow, "started")
474
472
 
475
473
    @inlineCallbacks
476
474
    def test_agent_upgrade_watch_continues_on_unexpected_error(self):
554
552
 
555
553
        # Verify the upgrade.
556
554
        self.assertIdentical(value, False)
557
 
        self.assertIn("Unit not in an upgradeable state:start_error",
 
555
        self.assertIn("Unit not in an upgradeable state: start_error",
558
556
                      output.getvalue())
559
557
        self.assertIdentical(
560
558
            (yield self.states["unit"].get_upgrade_flag()),