aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_sdnc_topology.py
blob: 6ced0817339bad35bbe7baa5f976acb2c5f21ce3 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
"""Test SDNC topology creation using NETCONF-API."""
#   Copyright 2023 Deutsche Telekom AG
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
from collections.abc import Iterable
from unittest import mock

from onapsdk.sdnc.topology import Topology, Node

SDNC_TOPOLOGY_GET_NODE = {
    "network-topology:node": [
        {
            "node-id": "TEST",
            "netconf-node-topology:sleep-factor": "1.5",
            "netconf-node-topology:host": "10.1.1.41",
            "netconf-node-topology:reconnect-on-changed-schema": False,
            "netconf-node-topology:between-attempts-timeout-millis": 2000,
            "netconf-node-topology:connection-status": "connected",
            "netconf-node-topology:max-connection-attempts": 0,
            "netconf-node-topology:username": "admin",
            "netconf-node-topology:password": "admin",
            "netconf-node-topology:connection-timeout-millis": 20000,
            "netconf-node-topology:port": 32767,
            "netconf-node-topology:tcp-only": False,
            "netconf-node-topology:keepalive-delay": 120
        }
    ]
}
SDNC_TOPOLOGY_GET_NODE_KEY_ERROR = {
    "network-topology:node": [
        {
            "node-id": "TEST",
            "netconf-node-topology:host": "10.1.1.41",
            "netconf-node-topology:username": "admin",
            "netconf-node-topology:password": "admin"
        }
    ]
}

SDNC_TOPOLOGY_GET_ALL_NODES_KEY_ERROR = {
    "network-topology:topology": [
        {
            "topology-id": "topology-netconf",
            "node": [
                {
                    "node-id": "TEST1",
                    "netconf-node-topology:host": "10.1.1.41",
                    "netconf-node-topology:username": "admin",
                    "netconf-node-topology:password": "admin",
                },
                {
                    "node-id": "TEST2",
                    "netconf-node-topology:port": 830,
                    "netconf-node-topology:username": "root",
                    "netconf-node-topology:password": "password",
                    "netconf-node-topology:host": "100.70.0.53"
                }
            ]
        }
    ]
}

SDNC_TOPOLOGY_WITHOUT_NODES_GET_ALL = {
    "network-topology:network-topology": {
        "topology": [
            {
                "topology-id": "topology-netconf",
            }
        ]
    }
}

SDNC_TOPOLOGY_WITHOUT_NODES_GET = {
    "network-topology:topology": [
        {
            "topology-id": "topology-netconf",
        }
    ]
}

SDNC_TOPOLOGY_GET_ALL_NODES = {
    "network-topology:topology": [
        {
            "topology-id": "topology-netconf",
            "node": [
                {
                    "node-id": "TEST",
                    "netconf-node-topology:host": "10.1.1.41",
                    "netconf-node-topology:username": "admin",
                    "netconf-node-topology:password": "admin",
                    "netconf-node-topology:port": 32767
                }
            ]
        }
    ]
}
SDNC_TOPOLOGY = {
    "network-topology:network-topology": {
        "topology": [
            {
                "topology-id": "topology-netconf",
                "node": [
                    {
                        "node-id": "MAAS",
                        "netconf-node-topology:sleep-factor": "1.5",
                        "netconf-node-topology:host": "10.32.32.32",
                        "netconf-node-topology:reconnect-on-changed-schema": False,
                        "netconf-node-topology:clustered-connection-status": {
                            "netconf-master-node": "test"
                        },
                        "netconf-node-topology:between-attempts-timeout-millis": 2000,
                        "netconf-node-topology:connection-status": "connected",
                        "netconf-node-topology:max-connection-attempts": 0,
                        "netconf-node-topology:username": "admin",
                        "netconf-node-topology:password": "admin",
                        "netconf-node-topology:available-capabilities": {
                            "available-capability": [
                                {
                                    "capability": "urn:ietf:params:netconf:base:1.1",
                                    "capability-origin": "device-advertised"
                                }
                            ]
                        },
                        "netconf-node-topology:connection-timeout-millis": 10000,
                        "netconf-node-topology:port": 32767,
                        "netconf-node-topology:tcp-only": False,
                        "netconf-node-topology:keepalive-delay": 120
                    }
                ]
            }
        ]
    }
}
SDNC_TOPOLOGY_ID = "topology-netconf"
SDNC_NODE = {
    "network-topology:node": [
        {
            "node-id": "test-node-fff",
            "netconf-node-topology:host": "100.70.0.5",
            "netconf-node-topology:port": 830,
            "netconf-node-topology:keepalive-delay": 100,
            "netconf-node-topology:tcp-only": False,
            "netconf-node-topology:username": "root",
            "netconf-node-topology:password": "password"
        }
    ]
}

SDNC_NODE_ID = "test-node-fff"


@mock.patch.object(Topology, "send_message_json")
def test_sdnc_netconf_api_topology_get_all(mock_send_message_json):
    mock_send_message_json.return_value = SDNC_TOPOLOGY
    topology_iterable = Topology.get_all()
    assert isinstance(topology_iterable, Iterable)
    topology_list = list(topology_iterable)
    assert len(topology_list) == 1
    topology = topology_list[0]
    assert isinstance(topology, Topology)
    assert topology.topology_id == SDNC_TOPOLOGY_ID


@mock.patch.object(Topology, "send_message_json")
def test_sdnc_netconf_api_topology_get(mock_send_message_json):
    mock_send_message_json.return_value = SDNC_TOPOLOGY_GET_ALL_NODES
    topology = Topology()
    topology_new = Topology.get(topology.topology_id)
    assert isinstance(topology_new, Topology)


@mock.patch.object(Topology, "send_message_json")
def test_sdnc_netconf_api_topology_get_all_nodes(mock_send_message_json):
    mock_send_message_json.return_value = SDNC_TOPOLOGY_GET_ALL_NODES
    topology = Topology()
    nodes = topology.get_all_nodes()
    assert isinstance(nodes, Iterable)
    node_list = list(nodes)
    assert len(node_list) == 1


@mock.patch.object(Topology, "send_message_json")
def test_sdnc_netconf_api_topology_get_node(mock_send_message_json):
    mock_send_message_json.return_value = SDNC_TOPOLOGY_GET_NODE
    topology = Topology()
    node = topology.get_node("TEST")
    assert isinstance(node, Node)
    assert node.node_id == "TEST"

@mock.patch.object(Topology, "send_message_json")
def test_sdnc_netconf_api_topology_get_all_key_error(mock_send_message_json):
    mock_send_message_json.return_value = SDNC_TOPOLOGY_WITHOUT_NODES_GET_ALL
    topology_iterable = Topology.get_all()
    assert isinstance(topology_iterable, Iterable)
    topology_list = list(topology_iterable)
    assert len(topology_list) == 1
    topology = topology_list[0]
    assert topology.nodes is None

@mock.patch.object(Topology, "send_message_json")
def test_sdnc_netconf_api_topology_get_key_error(mock_send_message_json):
    mock_send_message_json.return_value = SDNC_TOPOLOGY_WITHOUT_NODES_GET
    topology = Topology.get(SDNC_TOPOLOGY_ID)
    assert isinstance(topology, Topology)
    assert topology.nodes is None

@mock.patch.object(Topology, "send_message_json")
def test_sdnc_netconf_api_topology_get_all_nodes_key_error(mock_send_message_json):
    mock_send_message_json.return_value = SDNC_TOPOLOGY_GET_ALL_NODES_KEY_ERROR
    topology = Topology()
    nodes = topology.get_all_nodes()
    assert isinstance(nodes, Iterable)
    try:
        list(nodes)
    except Exception as e:
        assert e is KeyError

@mock.patch.object(Topology, "send_message_json")
def test_sdnc_netconf_api_topology_get_node_key_error(mock_send_message_json):
    mock_send_message_json.return_value = SDNC_TOPOLOGY_GET_NODE_KEY_ERROR
    topology = Topology()
    try:
        topology.get_node("TEST")
    except Exception as e:
        assert e is KeyError