Beispiel #1
0
    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)
Beispiel #2
0
    def test_sources(self):
        """
        Test to change of state with sources.
        """

        vertice = TopoNode()
        event = {}

        count = 5

        sources = [TopoNode(state=i) for i in range(count)]
        for source in sources:
            source.save(manager=tm)
        edge = TopoEdge(
            targets=[vertice.id],
            sources=list(source.id for source in sources)
        )
        edge.save(manager=tm)
        self.get_function()(
            vertice=vertice, event=event, manager=tm, ctx={},
            **self.get_kwargs()
        )
        self.assertEqual(vertice.state, self.get_new_state())
        edge.delete(manager=tm)
        for source in sources:
            source.delete(manager=tm)
Beispiel #3
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)
Beispiel #4
0
    def test_many(self):

        target = TopoNode()
        target.save(self.manager)
        count = 5
        sources = [TopoNode() for i in range(count)]
        for source in sources:
            target.save(self.manager)
        edge = TopoEdge(
            sources=[source.id for source in sources], targets=target.id
        )
        edge.save(self.manager)

        check = nok(
            event={},
            ctx={},
            vertice=target,
            min_weight=count,
            manager=self.manager
        )
        self.assertFalse(check)

        sources[0].state = Check.MINOR
        sources[0].save(self.manager)

        check = nok(event={}, ctx={}, vertice=target, manager=self.manager)
        self.assertTrue(check)
        check = nok(
            event={},
            ctx={},
            vertice=target,
            min_weight=count,
            manager=self.manager
        )
        self.assertFalse(check)

        for source in sources:
            source.state = Check.MINOR
            source.save(self.manager)

        check = nok(
            event={},
            ctx={},
            vertice=target,
            min_weight=count,
            manager=self.manager
        )
        self.assertTrue(check)
Beispiel #5
0
    def test_default(self):
        """
        Test to check default condition.
        """

        source = TopoNode()
        source.save(self.manager)
        target = TopoNode()
        target.save(self.manager)
        edge = TopoEdge(sources=source.id, targets=target.id)
        edge.save(self.manager)

        check = at_least(
            event={}, ctx={}, vertice=target, manager=self.manager
        )

        self.assertTrue(check)
Beispiel #6
0
    def test_one(self):
        """
        Test one source.
        """

        source = TopoNode()
        source.save(self.manager)
        target = TopoNode()
        target.save(self.manager)
        edge = TopoEdge(sources=source.id, targets=target.id)
        edge.save(self.manager)

        check = nok(event={}, ctx={}, vertice=target, manager=self.manager)
        self.assertFalse(check)

        source.state = Check.MINOR
        source.save(self.manager)

        check = nok(event={}, ctx={}, vertice=target, manager=self.manager)
        self.assertTrue(check)
Beispiel #7
0
    def test_false(self):
        """
        Test to check if there are at least one
        """

        source = TopoNode()
        source.save(self.manager)
        target = TopoNode()
        target.save(self.manager)
        edge = TopoEdge(sources=source.id, targets=target.id)
        edge.save(self.manager)

        check = at_least(
            event={},
            ctx={},
            state=Check.MINOR,
            vertice=target,
            manager=self.manager
        )
        self.assertFalse(check)

        edge.weight = 0.5
        edge.save(self.manager)
        source.state = Check.MINOR
        source.save(self.manager)

        check = at_least(
            event={},
            ctx={},
            state=Check.MINOR,
            vertice=target,
            manager=self.manager
        )
        self.assertFalse(check)

        edge.weight = 1.5
        edge.save(self.manager)

        check = at_least(
            event={},
            ctx={},
            state=Check.MINOR,
            vertice=target,
            manager=self.manager
        )
        self.assertTrue(check)
Beispiel #8
0
    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)