aboutsummaryrefslogtreecommitdiffstats
path: root/test/mocks/netconf-pnp-simulator/engine/tests/test_turing_machine.py
blob: 8ac38b0f5116be0f7063fa92f1fffa6850f263d6 (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
125
126
127
128
129
130
import nctest

_NAMESPACES = {
    "nc": "urn:ietf:params:xml:ns:netconf:base:1.0",
    "tm": "http://example.net/turing-machine"
}


def check_labels_only_in_data(data):
    children = data.xpath("/nc:rpc-reply/nc:data/*", namespaces=_NAMESPACES)
    assert children
    for child in children:
        assert child.tag.endswith("turing-machine")
    children = data.xpath("/nc:rpc-reply/nc:data/tm:turing-machine/*", namespaces=_NAMESPACES)
    assert children
    for child in children:
        assert child.tag.endswith("transition-function")
    children = data.xpath("/nc:rpc-reply/nc:data/tm:turing-machine/tm:transition-function/*", namespaces=_NAMESPACES)
    assert children
    for child in children:
        assert child.tag.endswith("delta")
    children = data.xpath("/nc:rpc-reply/nc:data/tm:turing-machine/tm:transition-function/tm:delta/*",
                          namespaces=_NAMESPACES)
    assert children
    for child in children:
        assert child.tag.endswith("label")


def check_deltas_in_data(data):
    deltas = data.xpath("/nc:rpc-reply/nc:data/tm:turing-machine/tm:transition-function/*", namespaces=_NAMESPACES)
    assert deltas
    for d in deltas:
        assert d.tag.endswith("delta")


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

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

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

    def test_get_config_running(self):
        reply = self.nc.get_config(source="running")
        nctest.check_reply_data(reply)
        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)
        nctest.check_reply_data(reply)
        check_deltas_in_data(reply.data)
        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)
        nctest.check_reply_data(reply)
        check_deltas_in_data(reply.data)
        check_labels_only_in_data(reply.data)

    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))
        nctest.check_reply_ok(reply)
        # get
        reply = self.nc.get_config(source="running")
        nctest.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=_NAMESPACES)
        assert len(deltas) == 1
        # create already existing - expect error
        reply = self.nc.edit_config(target='running', config=config_xml.format("create", 9, 99))
        nctest.check_reply_err(reply)
        # replace
        reply = self.nc.edit_config(target='running', config=config_xml.format("replace", 9, 88))
        nctest.check_reply_ok(reply)
        # get
        reply = self.nc.get_config(source="running")
        nctest.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=_NAMESPACES)
        assert len(states) == 1
        assert states[0].text == "88"
        # delete
        reply = self.nc.edit_config(target='running', config=config_xml.format("delete", 9, 88))
        nctest.check_reply_ok(reply)
        # delete non-existing - expect error
        reply = self.nc.edit_config(target='running', config=config_xml.format("delete", 9, 88))
        nctest.check_reply_err(reply)
        # get - should be empty
        reply = self.nc.get_config(source="running")
        nctest.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=_NAMESPACES)
        assert not deltas