def test_configcluagent_call_before_rexecute_after(self):
    cluagent = ConfigurableCluAgent({})
    before_execute=Mock()
    after_execute=Mock()
    execute=Mock()

    cluagent.before_execute=before_execute
    cluagent.execute=execute
    cluagent.after_execute=after_execute
    
    cluagent.run()
    
    before_execute.assert_called_once_with()
    execute.assert_called_once_with()
    after_execute.assert_called_once_with()
  def test_cluagent_call_ensure_afterexecute_on_run_exception(self):
    """ Ensure that ensure_after_execute is called when execute() raise an Exception """
    cluagent = ConfigurableCluAgent({})
    before_execute=Mock()
    after_execute=Mock()
    ensure_after_execute=Mock()
    execute=Mock(side_effect=Exception("In your face"))

    cluagent.before_execute=before_execute
    cluagent.execute=execute
    cluagent.after_execute=after_execute
    cluagent.ensure_after_execute=ensure_after_execute
    
    with(self.assertRaises(CluAgentException)) as ex:
      cluagent.run()
    
    before_execute.assert_called_once_with()
    execute.assert_called_once_with()
    ensure_after_execute.assert_called_once_with()
  def test_cluagent_call_ensure_afterexecute_on_after_execute_exception_is_first(self):
    """ Ensure that when execute() and after_execute() raises an Exception, the first is raised"""
    ex_ensure_after=Exception("In your face")
    ensure_after_execute=Mock(side_effect=ex_ensure_after)
    
    cluagent = ConfigurableCluAgent({})

    before_execute=Mock()
    execute=Mock()
    after_execute=Mock()
    
    cluagent.before_execute=before_execute
    cluagent.execute=execute
    cluagent.after_execute=after_execute
    cluagent.ensure_after_execute=ensure_after_execute
    
    with(self.assertRaises(CluAgentException)) as e:
      cluagent.run()

    before_execute.assert_called_once_with()
    execute.assert_called_once_with()
    after_execute.assert_called_once_with()
    ensure_after_execute.assert_called_once_with()