-
Notifications
You must be signed in to change notification settings - Fork 52
/
test_ank.py
359 lines (331 loc) · 14.3 KB
/
test_ank.py
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import unittest
import netaddr
import networkx as nx
import autonetkit
import autonetkit.ank as ank_utils
class TestAnk(unittest.TestCase):
def test_sn_preflen_to_network(self):
result = ank_utils.sn_preflen_to_network('1', '2')
expected_result = netaddr.IPNetwork('1.0.0.0/2')
self.assertEqual(expected_result, result)
result = ank_utils.sn_preflen_to_network('1', '4')
expected_result = netaddr.IPNetwork('1.0.0.0/4')
self.assertEqual(expected_result, result)
result = ank_utils.sn_preflen_to_network('5', '4')
expected_result = netaddr.IPNetwork('5.0.0.0/4')
self.assertEqual(expected_result, result)
def test_fqdn(self):
anm = autonetkit.topos.house()
r1 = anm['phy'].node("r1")
self.assertEqual(ank_utils.fqdn(r1), 'r1.1')
r2 = anm['phy'].node("r2")
self.assertEqual(ank_utils.fqdn(r2), 'r2.1')
def test_name_folder_safe(self):
result = ank_utils.name_folder_safe('ank')
self.assertEqual(result, 'ank')
result = ank_utils.name_folder_safe('ank/repo')
self.assertEqual(result ,'ank_repo')
result = ank_utils.name_folder_safe('auto.net.kit.repo')
self.assertEqual(result, 'auto_net_kit_repo')
result = ank_utils.name_folder_safe('ank.folder')
self.assertEqual(result, 'ank_folder')
result = ank_utils.name_folder_safe('ank__repo')
self.assertEqual(result, 'ank_repo')
def test_set_node_default(self):
anm = autonetkit.topos.house()
g_phy = anm['phy']
r1 = g_phy.node("r1")
r2 = g_phy.node("r2")
r3 = g_phy.node("r3")
r4 = g_phy.node("r4")
r5 = g_phy.node("r5")
r1.set("color", "blue")
result = [(n, n.get("color")) for n in g_phy]
expected_result = [(r4, None), (r5, None), (r1, 'blue'), (r2, None), (r3, None)]
self.assertListEqual(expected_result, result)
ank_utils.set_node_default(g_phy, color="red")
result = [(n, n.get("color")) for n in g_phy]
expected_result = [(r4, 'red'), (r5, 'red'), (r1, 'blue'), (r2, 'red'), (r3, 'red')]
self.assertListEqual(expected_result, result)
# Can also set for a specific bunch of nodes
nodes = ["r1", "r2", "r3"]
ank_utils.set_node_default(g_phy, nodes, role="core")
result = [(n, n.get("role")) for n in g_phy]
expected_result = [(r4, None), (r5, None), (r1, 'core'), (r2, 'core'), (r3, 'core')]
self.assertListEqual(expected_result, result)
def test_copy_attr_from(self):
anm = autonetkit.topos.house()
g_in = anm['input']
g_phy = anm['phy']
result = [n.get("color") for n in g_phy]
expected_result = [None, None, None, None, None]
self.assertEqual(expected_result, result)
ank_utils.set_node_default(g_in, color="red")
ank_utils.copy_attr_from(g_in, g_phy, "color")
result = [n.get("color") for n in g_phy]
expected_result = ['red', 'red', 'red', 'red', 'red']
self.assertListEqual(expected_result, result)
# Can specify a default value if unset
nodes = ["r1", "r2", "r3"]
r1 = g_in.node('r1')
r2 = g_in.node('r2')
r3 = g_in.node('r3')
r4 = g_in.node('r4')
r5 = g_in.node('r5')
ank_utils.set_node_default(g_in, nodes, role="core")
ank_utils.copy_attr_from(g_in, g_phy, "role", default="edge")
result = [(n, n.get("role")) for n in g_phy]
expected_result = [(r4, 'edge'), (r5, 'edge'), (r1, 'core'),
(r2, 'core'), (r3, 'core')]
self.assertListEqual(expected_result, result)
# Can specify the remote attribute to set
ank_utils.copy_attr_from(g_in, g_phy, "role",
"device_role", default="edge")
result = [(n, n.get('device_role')) for n in g_phy]
expected_result = [(n, n.get('role') if n.get('role') else 'edge')
for n in g_in]
self.assertListEqual(expected_result, result)
# Can specify the type to cast to
g_in.update(memory = "32")
ank_utils.copy_attr_from(g_in, g_phy, "memory", type=int)
result = [n.get("memory") for n in g_phy]
expected_result = [32, 32, 32, 32, 32]
self.assertEqual(expected_result, result)
def test_copy_int_attr_from(self):
anm = autonetkit.topos.house()
g_in = anm['input']
g_phy = anm['phy']
result = [iface.get('ospf_cost') for node in g_phy for iface in node]
expected_result = [None, None, None, None, None, None, None, None,
None, None, None, None]
self.assertListEqual(expected_result, result)
for node in g_in:
for interface in node:
interface.set('ospf_cost', 10)
ank_utils.copy_int_attr_from(g_in, g_phy, "ospf_cost")
result = [iface.get('ospf_cost') for node in g_phy for iface in node]
expected_result = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
self.assertListEqual(expected_result, result)
def test_copy_edge_attr_from(self):
anm = autonetkit.topos.house()
g_in = anm['input']
g_phy = anm['phy']
r1 = g_in.node('r1')
r2 = g_in.node('r2')
r3 = g_in.node('r3')
r4 = g_in.node('r4')
r5 = g_in.node('r5')
expected_result = [(r4, r5), (r4, r2), (r5, r3),
(r1, r2), (r1, r3), (r2, r3)]
self.assertListEqual(expected_result, g_in.edges())
self.assertListEqual(expected_result, g_phy.edges())
g_in.edge("r1", "r2").set('color', "red")
result = g_in.edges(color = "red")
expected_result = [(r1, r2)]
self.assertListEqual(expected_result, result)
ank_utils.copy_edge_attr_from(g_in, g_phy, 'color')
result = g_phy.edges(color = "red")
expected_result = [(r1, r2)]
self.assertListEqual(expected_result, result)
def test_wrap_edges(self):
anm = autonetkit.topos.house()
g_phy = anm['phy']
r1 = g_phy.node('r1')
r2 = g_phy.node('r2')
r3 = g_phy.node('r3')
r4 = g_phy.node('r4')
r5 = g_phy.node('r5')
elist = [("r1", "r2"), ("r2", "r3")]
edges = ank_utils.wrap_edges(g_phy, elist)
# The edges are now NetworkModel edge objects
expected_result = [(r1, r2), (r2, r3)]
self.assertListEqual(expected_result, edges)
def test_wrap_nodes(self):
anm = autonetkit.topos.house()
g_phy = anm['phy']
r1 = g_phy.node('r1')
r2 = g_phy.node('r2')
r3 = g_phy.node('r3')
r4 = g_phy.node('r4')
r5 = g_phy.node('r5')
nlist = ["r1", "r2", "r3"]
nodes = ank_utils.wrap_nodes(g_phy, nlist)
# The nodes are now NetworkModel node objects
self.assertListEqual(nodes, [r1, r2, r3])
# This is generally used in internal functions.
# An alternative method is:f
result = [g_phy.node(n) for n in nlist]
self.assertListEqual(result, [r1, r2, r3])
def test_in_edges(self):
g = nx.MultiDiGraph()
g.add_edges_from([(1,2),(3,4),(1,6)])
self.assertListEqual(g.out_edges(1), [(1, 2), (1, 6)])
self.assertListEqual(g.in_edges(1), [])
self.assertListEqual(g.in_edges(4), [(3, 4)])
def test_split(self):
anm = autonetkit.topos.house()
g_phy = anm['phy']
edge = g_phy.edge("r1", "r2")
new_nodes = ank_utils.split_edge(g_phy, edge)
r1 = g_phy.node('r1')
r2 = g_phy.node('r2')
r1_r2 = g_phy.node('r1_r2')
self.assertListEqual(new_nodes, [r1_r2])
result = [n.neighbors() for n in new_nodes]
self.assertListEqual([[r1, r2]], result)
# For multiple edges and specifying a prepend for the new nodes
anm = autonetkit.topos.house()
g_phy = anm['phy']
edges = g_phy.node("r2").edges()
new_nodes = ank_utils.split_edges(g_phy, edges, id_prepend="split_")
split_r2_r4 = g_phy.node('split_r2_r4')
split_r1_r2 = g_phy.node('split_r1_r2')
split_r2_r3 = g_phy.node('split_r2_r3')
expected_result = [split_r2_r4, split_r1_r2, split_r2_r3]
self.assertListEqual(expected_result, new_nodes)
result = [n.neighbors() for n in new_nodes]
r3 = g_phy.node('r3')
r4 = g_phy.node('r4')
expected_result = [[r4, r2], [r1, r2], [r2, r3]]
self.assertListEqual(expected_result, result)
def test_explode_nodes(self):
anm = autonetkit.topos.mixed()
g_phy = anm['phy']
r1 = g_phy.node('r1')
r2 = g_phy.node('r2')
switches = g_phy.switches()
exploded_edges = ank_utils.explode_nodes(g_phy, switches)
self.assertListEqual(exploded_edges, [(r1, r2)])
# Or to explode a specific node
anm = autonetkit.topos.house()
g_phy = anm['phy']
r1 = g_phy.node('r1')
r2 = g_phy.node('r2')
r3 = g_phy.node('r3')
r4 = g_phy.node('r4')
r5 = g_phy.node('r5')
self.assertListEqual(g_phy.nodes(), [r4, r5, r1, r2, r3])
result = sorted(g_phy.edges())
expected_result = [(r1, r2), (r1, r3), (r2, r3), (r4, r2),
(r4, r5), (r5, r3)]
self.assertListEqual(expected_result, result)
exploded_edges = ank_utils.explode_node(g_phy, r2)
expected_result = [(r1, r4), (r3, r4), (r1, r3)]
self.assertListEqual(exploded_edges, expected_result)
self.assertListEqual(g_phy.nodes(), [r4, r5, r1, r3])
result = sorted(g_phy.edges())
expected_result = [(r1, r3), (r4, r1), (r4, r3), (r4, r5), (r5, r3)]
self.assertListEqual(expected_result, result)
def test_label(self):
anm = autonetkit.topos.house()
g_phy = anm['phy']
r1 = g_phy.node("r1")
r2 = g_phy.node("r2")
r5 = g_phy.node("r5")
result = ank_utils.label(g_phy, [r1, r2, r5])
self.assertListEqual(['r1', 'r2', 'r5'], result)
def test_connected_subgraphs(self):
anm = autonetkit.topos.house()
g_phy = anm['phy']
r1 = g_phy.node('r1')
r2 = g_phy.node('r2')
r3 = g_phy.node('r3')
r4 = g_phy.node('r4')
r5 = g_phy.node('r5')
result = ank_utils.connected_subgraphs(g_phy)
expected_result = [[r4, r5, r1, r2, r3]]
self.assertListEqual(expected_result, result)
edges = [("r2", "r4"), ("r3", "r5")]
g_phy.remove_edges_from(edges)
result = ank_utils.connected_subgraphs(g_phy)
expected_result = [[r1, r2, r3], [r4, r5]]
self.assertListEqual(expected_result, result)
def test_aggregate_nodes(self):
anm = autonetkit.topos.house()
g_phy = anm['phy']
r2 = g_phy.node('r2')
r3 = g_phy.node('r3')
r5 = g_phy.node('r5')
rlist = ['r4', 'r2']
result = ank_utils.aggregate_nodes(g_phy, rlist)
self.assertListEqual(result, [(r2, r5)])
alist = ['r1', 'r2', 'r3', 'r4']
result = ank_utils.aggregate_nodes(g_phy, alist)
self.assertListEqual(result, [(r3, r5)])
def test_most_frequent(self):
rlist= ['r1', 'r2', 'r3', 'r1']
self.assertEqual(ank_utils.most_frequent(rlist), 'r1')
rlist= ['r1', 'r2', 'r3', 'r1', 'r3', 'r3']
self.assertEqual(ank_utils.most_frequent(rlist), 'r3')
def test_neigh_most_frequent(self):
anm = autonetkit.topos.house()
g_phy = anm['phy']
self.assertEqual(ank_utils.neigh_most_frequent(g_phy, "r2", "asn"), 1)
self.assertEqual(ank_utils.neigh_most_frequent(g_phy, "r5", "asn"), 1)
def test_neigh_average(self):
anm = autonetkit.topos.house()
g_phy = anm['phy']
self.assertEqual(ank_utils.neigh_average(g_phy, "r5", "asn"), 1.5)
self.assertEqual(ank_utils.neigh_average(g_phy, "r3", "asn"), 1.3333333333333333)
def test_neigh_equal(self):
anm = autonetkit.topos.house()
g_phy = anm['phy']
self.assertFalse(ank_utils.neigh_equal(g_phy, "r2", "asn"))
self.assertTrue(ank_utils.neigh_equal(g_phy, "r1", "asn"))
def test_unique_attr(self):
from sets import Set
anm = autonetkit.topos.house()
g_phy = anm['phy']
self.assertSetEqual(ank_utils.unique_attr(g_phy, "asn"), Set([1, 2]))
def test_groupby(self):
anm = autonetkit.topos.house()
g_phy = anm['phy']
r1 = g_phy.node('r1')
r2 = g_phy.node('r2')
r3 = g_phy.node('r3')
r4 = g_phy.node('r4')
r5 = g_phy.node('r5')
expected_result = {1: [r1, r2, r3], 2: [r4, r5]}
self.assertDictEqual(expected_result, g_phy.groupby("asn"))
def test_shortest_path(self):
anm = autonetkit.topos.house()
g_phy = anm['phy']
r1 = g_phy.node('r1')
r2 = g_phy.node('r2')
r3 = g_phy.node('r3')
r4 = g_phy.node('r4')
r5 = g_phy.node('r5')
self.assertListEqual(ank_utils.shortest_path(g_phy,'r1','r2'), [r1, r2])
self.assertListEqual(ank_utils.shortest_path(g_phy,'r1','r4'), [r1, r2, r4])
self.assertListEqual(ank_utils.shortest_path(g_phy,'r1','r5'), [r1, r3, r5])
def test_boundary_nodes(self):
anm = autonetkit.topos.house()
g_phy = anm['phy']
r4 = g_phy.node('r4')
r2 = g_phy.node('r2')
rlist = ["r4", "r2"]
self.assertListEqual(ank_utils.boundary_nodes(g_phy, rlist), [r4, r2, r2])
def test_shallow_copy_nx_graph(self):
G = nx.Graph()
H = ank_utils.shallow_copy_nx_graph(G)
self.assertIsInstance(H, nx.Graph)
self.assertNotIsInstance(H, nx.DiGraph)
self.assertNotIsInstance(H, nx.MultiGraph)
self.assertNotIsInstance(H, nx.MultiDiGraph)
G = nx.DiGraph()
H = ank_utils.shallow_copy_nx_graph(G)
self.assertIsInstance(H, nx.DiGraph)
G = nx.MultiGraph()
H = ank_utils.shallow_copy_nx_graph(G)
self.assertIsInstance(H, nx.MultiGraph)
G = nx.MultiDiGraph()
H = ank_utils.shallow_copy_nx_graph(G)
self.assertIsInstance(H, nx.MultiDiGraph)
def test_neigh_attr(self):
anm = autonetkit.topos.house()
g_phy = anm['phy']
genexp = ank_utils.neigh_attr(g_phy, "r2", "asn")
result = [item for item in genexp]
self.assertListEqual([2,1,1], result)
if __name__ == '__main__':
unittest.main()