aboutsummaryrefslogtreecommitdiffstats
path: root/test/mocks/netconf-pnp-simulator/engine/tests/test_turing_machine.py
blob: 63a0c2d99e3bdf5753b3066e1e935e64499a0850 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import unittest
import nctest
import os

class TestTuringMachine(nctest.NCTestCase):
    """ Tests basic NETCONF operations on the turing-machine YANG module. """

    def __init__(self, *args, **kwargs):
        super(TestTuringMachine, self).__init__(*args, **kwargs)
        self.ns = {"nc": "urn:ietf:params:xml:ns:netconf:base:1.0", "tm": "http://example.net/turing-machine"}

    def check_deltas_in_data(self, data):
        deltas = data.xpath("/nc:rpc-reply/nc:data/tm:turing-machine/tm:transition-function/*", namespaces=self.ns)
        self.assertNotEqual(len(deltas), 0)
        for d in deltas:
            self.assertTrue(d.tag.endswith("delta"))

    def check_labels_only_in_data(self, data):
        children = data.xpath("/nc:rpc-reply/nc:data/*", namespaces=self.ns)
        self.assertNotEqual(len(children), 0)
        for child in children:
            self.assertTrue(child.tag.endswith("turing-machine"))
        children = data.xpath("/nc:rpc-reply/nc:data/tm:turing-machine/*", namespaces=self.ns)
        self.assertNotEqual(len(children), 0)
        for child in children:
            self.assertTrue(child.tag.endswith("transition-function"))
        children = data.xpath("/nc:rpc-reply/nc:data/tm:turing-machine/tm:transition-function/*", namespaces=self.ns)
        self.assertNotEqual(len(children), 0)
        for child in children:
            self.assertTrue(child.tag.endswith("delta"))
        children = data.xpath("/nc:rpc-reply/nc:data/tm:turing-machine/tm:transition-function/tm:delta/*", namespaces=self.ns)
        self.assertNotEqual(len(children), 0)
        for child in children:
            self.assertTrue(child.tag.endswith("label"))

    def test_get(self):
        reply = self.nc.get()
        self.check_reply_data(reply)
        self.check_deltas_in_data(reply.data)

    def test_get_config_startup(self):
        reply = self.nc.get_config(source="startup")
        self.check_reply_data(reply)
        self.check_deltas_in_data(reply.data)

    def test_get_config_running(self):
        reply = self.nc.get_config(source="running")
        self.check_reply_data(reply)
        self.check_deltas_in_data(reply.data)

    def test_get_subtree_filter(self):
        filter_xml = """<nc:filter xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
            <turing-machine xmlns="http://example.net/turing-machine">
                <transition-function>
                    <delta>
                        <label />
                    </delta>
                </transition-function>
            </turing-machine>
            </nc:filter>"""
        reply = self.nc.get_config(source="running", filter=filter_xml)
        self.check_reply_data(reply)
        self.check_deltas_in_data(reply.data)
        self.check_labels_only_in_data(reply.data)

    def test_get_xpath_filter(self):
        # https://github.com/ncclient/ncclient/issues/166
        filter_xml = """<nc:filter type="xpath" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"
            xmlns:tm="http://example.net/turing-machine"
            select="/tm:turing-machine/transition-function/delta/label" />
            """
        reply = self.nc.get(filter=filter_xml)
        self.check_reply_data(reply)
        self.check_deltas_in_data(reply.data)
        self.check_labels_only_in_data(reply.data)

    @unittest.skipIf(os.environ.get("DOCKER_IMG_TAG") == "latest", "bug in Netopeer2 replace operation")
    def test_edit_config(self):
        config_xml = """<nc:config xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
            <turing-machine xmlns="http://example.net/turing-machine">
                <transition-function>
                    <delta nc:operation="{}">
                        <label>test-transition-rule</label>
                        <input>
                            <symbol>{}</symbol>
                            <state>{}</state>
                        </input>
                    </delta>
                </transition-function>
            </turing-machine></nc:config>"""
        # merge
        reply = self.nc.edit_config(target='running', config=config_xml.format("merge", 9, 99))
        self.check_reply_ok(reply)
        # get
        reply = self.nc.get_config(source="running")
        self.check_reply_data(reply)
        deltas = reply.data.xpath("/nc:rpc-reply/nc:data/tm:turing-machine/tm:transition-function/tm:delta[tm:label='test-transition-rule']", namespaces=self.ns)
        self.assertEqual(len(deltas), 1)
        # create already existing - expect error
        reply = self.nc.edit_config(target='running', config=config_xml.format("create", 9, 99))
        self.check_reply_err(reply)
        # replace
        reply = self.nc.edit_config(target='running', config=config_xml.format("replace", 9, 88))
        self.check_reply_ok(reply)
        # get
        reply = self.nc.get_config(source="running")
        self.check_reply_data(reply)
        states = reply.data.xpath("/nc:rpc-reply/nc:data/tm:turing-machine/tm:transition-function/tm:delta[tm:label='test-transition-rule']/tm:input/tm:state", namespaces=self.ns)
        self.assertEqual(len(states), 1)
        self.assertEqual(states[0].text, "88")
        # delete
        reply = self.nc.edit_config(target='running', config=config_xml.format("delete", 9, 88))
        self.check_reply_ok(reply)
        # delete non-existing - expect error
        reply = self.nc.edit_config(target='running', config=config_xml.format("delete", 9, 88))
        self.check_reply_err(reply)
        # get - should be empty
        reply = self.nc.get_config(source="running")
        self.check_reply_data(reply)
        deltas = reply.data.xpath("/nc:rpc-reply/nc:data/tm:turing-machine/tm:transition-function/tm:delta[tm:label='test-transition-rule']", namespaces=self.ns)
        self.assertEqual(len(deltas), 0)

if __name__ == '__main__':
    unittest.main()