示例#1
0
class TopologyFunder(CTXPropRegistry):
    """In charge of ctx topology.
    """

    __datatype__ = 'topology'  #: default datatype name

    def __init__(self, *args, **kwargs):

        super(TopologyFunder, self).__init__(*args, **kwargs)

        self.manager = TopologyManager()

    def _get_documents(self, ids, query, *args, **kwargs):

        result = []

        ctx_id_field = self._ctx_id_field()

        entity_cst = TopoVertice.ENTITY
        info_cst = Vertice.INFO

        entity = (
            {'$exists': True} if ids is None else {'$in': ids}
        )
        info = {entity_cst: entity}

        docs = self.manager.get_elts(info=info, serialize=False, query=query)

        for doc in docs:
            doc[ctx_id_field] = doc[info_cst][entity_cst]
            result.append(doc)

        return result

    def _get(self, ids, query, *args, **kwargs):

        return self._get_documents(ids=ids, query=query)

    def _delete(self, ids, query, *args, **kwargs):

        result = self._get_documents(ids=ids, query=query)

        ids = [doc['_id'] for doc in result]
        self.manager.del_elts(ids=ids)

        return result

    def ids(self, query=None):

        result = set()

        elts = self.manager.get_elts(query=query)

        for elt in elts:
            result.add(elt.entity)

        return result
示例#2
0
class TopologyGraphTest(TestCase):
    """Test topology element.
    """

    def setUp(self):

        self.context = Context(data_scope='test')
        self.manager = TopologyManager(data_scope='test')

    def tearDown(self):

        self.context.remove()
        self.manager.del_elts()

    def test_save(self):
        """Test if an entity exists after saving a topology.
        """
        id = 'test'

        topology = Topology(id=id)
        topology.save(manager=self.manager, context=self.context)

        topology = self.context.get(_type=topology.type, names=id)

        self.assertEqual(topology[Context.NAME], id)

    def test_delete(self):
        """Test if topology nodes exist after deleting a topology.
        """

        topology = Topology()
        node = TopoNode()
        topology.add_elts(node)

        topology.save(manager=self.manager)

        node = self.manager.get_elts(node.id)
        self.assertIsNotNone(node)

        topology.delete(manager=self.manager)
        node = self.manager.get_elts(node.id)
        self.assertIsNone(node)
示例#3
0
class TopoNodeTest(TestCase):
    """Test event processing function.
    """

    def setUp(self):

        self.manager = TopologyManager(data_scope='test')

    def tearDown(self):

        self.manager.del_elts()

    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)

    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)
示例#4
0
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)