def test_proccess_task_with_propagation(self): """Process a node and check if node and edge states have changed. """ state = 0 new_state = state + 1 operation = { 'id': 'canopsis.topology.rule.action.change_state', 'params': {'state': new_state} } toponode = TopoNode(state=state, operation=operation) toponode.save(manager=self.manager) edge = TopoEdge(sources=toponode) edge.save(manager=self.manager) self.assertEqual(toponode.state, state) self.assertEqual(edge.state, state) toponode.process(event={}, manager=self.manager) elts = self.manager.get_elts(ids=[toponode.id, edge.id]) toponode = elts[0] edge = elts[1] self.assertEqual(toponode.state, new_state) self.assertEqual(edge.state, new_state)
def test_default(self): """Test to process a toponode without default task. """ toponode = TopoNode() self.assertEqual(toponode.state, 0) event = {'state': 1} result = toponode.process(event=event, manager=self.manager) self.assertIsNone(result) self.assertEqual(toponode.state, 1)
def test_process_task(self): """Process a task which returns all toponode data. """ @register_task('process') def process_node( vertice, ctx, event=None, publisher=None, source=None, manager=None, logger=None, **kwargs ): return vertice, ctx, kwargs ctx, entity, state, operation = {'b': 1}, 'e', 0, 'process' toponode = TopoNode(state=state, entity=entity, operation=operation) _node, _ctx, _kwargs = toponode.process( ctx=ctx, event=None, manager=self.manager ) self.assertIs(_node, toponode) self.assertIs(_ctx, ctx) self.assertFalse(_kwargs)
class ProcessingTest(TestCase): """ Test event processing function. """ class _Amqp(object): """ In charge of processing publishing of test. """ def __init__(self, processingTest): self.exchange_name_events = None self.processingTest = processingTest self.event = None def publish(self, event, rk, exchange): """ Called when an event process publishes an event. """ self.event = event self.processingTest.count += 1 event_processing( event=event, engine=self.processingTest, manager=self.processingTest.manager ) def setUp(self): self.context = Context(data_scope='test_context') self.manager = TopologyManager(data_scope='test_topology') self.check = { 'type': 'check', 'event_type': 'check', 'connector': 'c', 'connector_name': 'c', 'component': 'c', 'source_type': 'component', 'state': Check.OK } entity = self.context.get_entity(self.check) entity_id = self.context.get_entity_id(entity) self.node = TopoNode(entity=entity_id) self.node.save(self.manager) self.count = 0 self.amqp = ProcessingTest._Amqp(self) def tearDown(self): self.manager.del_elts() def test_no_bound(self): """ Test in case of not bound nodes. """ event_processing(event=self.check, engine=self, manager=self.manager) self.assertEqual(self.count, 0) def test_one_node(self): """ Test in case of one bound node """ source = TopoNode() source.save(self.manager) edge = TopoEdge(sources=source.id, targets=self.node.id) edge.save(self.manager) event_processing(event=self.check, engine=self, manager=self.manager) self.assertEqual(self.count, 0) def test_change_state(self): """ Test in case of change state. """ # create a change state operation with minor state change_state_conf = new_conf( change_state, state=Check.MINOR ) self.node.operation = change_state_conf self.node.save(self.manager) self.node.process(event=self.check, manager=self.manager) event_processing(event=self.check, engine=self, manager=self.manager) target = self.manager.get_elts(ids=self.node.id) self.assertEqual(target.state, Check.MINOR) def test_chain_change_state(self): """ Test to change of state in a chain of nodes. This test consists to link three node in such way: self.node(state=0) -> node(state=0) -> root(state=0) And to propagate the change state task with state = 1 in order to check if root state equals 1. """ # create a simple task which consists to change of state change_state_conf = new_conf( change_state, state=Check.MINOR ) # create a root node with the change state task root = TopoNode(operator=change_state_conf) root.save(self.manager) # create a node with the change state task node = TopoNode(operator=change_state_conf) node.save(self.manager) # create a leaf with the change state task self.node.operation = change_state_conf self.node.save(self.manager) # link node to root rootnode = TopoEdge(targets=root.id, sources=node.id) rootnode.save(self.manager) # link self.node to node self_node = TopoEdge(targets=node.id, sources=self.node.id) self_node.save(self.manager) event_processing(event=self.check, engine=self, manager=self.manager) self.assertEqual(self.count, 3) self.node = self.manager.get_elts(ids=self.node.id) self.assertEqual(self.node.state, Check.MINOR)