-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathend_to_end_test.py
executable file
·355 lines (301 loc) · 16.9 KB
/
end_to_end_test.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
#!/usr/bin/python
################################################################################
# The Frenetic Project #
################################################################################
# Licensed to the Frenetic Project by one or more contributors. See the #
# NOTICE file distributed with this work for additional information #
# regarding copyright and ownership. The Frenetic Project licenses this #
# file to you under the following license. #
# #
# Redistribution and use in source and binary forms, with or without #
# modification, are permitted provided the following conditions are met: #
# - Redistributions of source code must retain the above copyright #
# notice, this list of conditions and the following disclaimer. #
# - Redistributions in binary form must reproduce the above copyright #
# notice, this list of conditions and the following disclaimer in #
# the documentation or other materials provided with the distribution. #
# - The names of the copyright holds and contributors may not be used to #
# endorse or promote products derived from this work without specific #
# prior written permission. #
# #
# 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 file distributed with this work for specific language governing #
# permissions and limitations under the License. #
################################################################################
# /slices/end_to_end_test.py #
# Test slice compiler correctness with the SAT solver #
################################################################################
"""Test slice compiler with SAT solver.
This tests both the compiler output and the SAT solver's verification, so a
failed test could be from either.
It's probably wise to not modify both in the same commit.
There are some tests in this file that take 30 or so minutes to run. They are
diabled unless the EXPENSIVE_TESTS enviroment variable is set.
`export EXPENSIVE_TESTS=1`
to set it, and
`unset EXPENSIVE_TESTS`
to unset it.
To print out finer-grained progress information on tests, set VERBOSE_TESTS.
"""
from examples import topology_gen, policy_gen
import sat, nxtopo, slicing
import compile as cp
import edge_compile as ec
import networkx as nx
import netcore as nc
from netcore import then
import os
import unittest
from test_util import linear, linear_all_ports, linear_hosts
from test_util import k10_nodes, k10, k4_nodes, k4, k4hosts
verbose = 'VERBOSE_TESTS' in os.environ
class TestVerify(unittest.TestCase):
def testBasicOverlap(self):
topo, policies = linear((0, 1, 2), (1, 2, 3))
self.assertIsNotNone(sat.shared_io(topo, policies[0], policies[1]))
def testBasicIso(self):
topo, policies = linear((0, 1), (2, 3))
self.assertIsNone(sat.shared_io(topo, policies[0], policies[1]))
topo, policies = linear((0, 1, 2), (2, 3))
self.assertIsNone(sat.shared_io(topo, policies[0], policies[1]))
def testForwarding(self):
topo, policies = linear((0, 1), (2, 3))
self.assertIsNone(sat.not_empty(policies[0]))
self.assertIsNone(sat.not_empty(policies[1]))
topo, policies = linear((0, 1, 2), (1, 2, 3))
self.assertIsNotNone(sat.not_empty(policies[0]))
self.assertIsNotNone(sat.not_empty(policies[1]))
topo, policies = linear_all_ports((0, 1), (2, 3))
self.assertIsNotNone(sat.not_empty(policies[0]))
self.assertIsNotNone(sat.not_empty(policies[1]))
class TestCompile(unittest.TestCase):
def testBasicCompile(self):
topo, policies = linear((0, 1, 2, 3), (0, 1, 2, 3))
slices = [slicing.ident_map_slice(topo, {}) for p in policies]
combined = zip(slices, policies)
compiled = cp.compile_slices(combined)
self.assertIsNotNone(sat.shared_io(topo, policies[0], policies[1]))
self.assertIsNone(sat.shared_io(topo, compiled[0], compiled[1]))
self.assertTrue(sat.compiled_correctly(topo, policies[0], compiled[0]))
self.assertTrue(sat.compiled_correctly(topo, policies[1], compiled[1]))
def testHostsCompile(self):
topo, combined = linear_hosts((0, 1, 2, 3), (0, 1, 2, 3))
policies = [p for _, p in combined]
compiled = cp.compile_slices(combined)
self.assertIsNotNone(sat.shared_io(topo, policies[0], policies[1]))
self.assertIsNone(sat.shared_io(topo, compiled[0], compiled[1]))
self.assertTrue(sat.compiled_correctly(topo, policies[0], compiled[0]))
self.assertTrue(sat.compiled_correctly(topo, policies[1], compiled[1]))
class TestEdgeCompile(unittest.TestCase):
def testBasicCompile(self):
topo, policies = linear((0, 1, 2, 3), (0, 1, 2, 3))
slices = [slicing.ident_map_slice(topo, {}) for p in policies]
combined = zip(slices, policies)
compiled = ec.compile_slices(topo, combined)
self.assertIsNotNone(sat.shared_io(topo, policies[0], policies[1]))
self.assertIsNone(sat.shared_io(topo, compiled[0], compiled[1]))
self.assertTrue(sat.compiled_correctly(topo, policies[0], compiled[0]))
self.assertTrue(sat.compiled_correctly(topo, policies[1], compiled[1]))
topo, policies = linear((0, 1, 2), (2, 3))
slices = [slicing.ident_map_slice(topo, {}) for p in policies]
combined = zip(slices, policies)
compiled = ec.compile_slices(topo, combined)
self.assertIsNone(sat.shared_io(topo, policies[0], policies[1]))
self.assertIsNone(sat.shared_io(topo, compiled[0], compiled[1]))
self.assertTrue(sat.compiled_correctly(topo, policies[0], compiled[0]))
self.assertTrue(sat.compiled_correctly(topo, policies[1], compiled[1]))
topo, policies = linear((0, 1, 2), (1, 2, 3))
slices = [slicing.ident_map_slice(topo, {}) for p in policies]
combined = zip(slices, policies)
compiled = ec.compile_slices(topo, combined)
self.assertIsNotNone(sat.shared_io(topo, policies[0], policies[1]))
self.assertIsNone(sat.shared_io(topo, compiled[0], compiled[1]))
self.assertTrue(sat.compiled_correctly(topo, policies[0], compiled[0]))
self.assertTrue(sat.compiled_correctly(topo, policies[1], compiled[1]))
topo, policies = linear((0, 1), (2, 3))
slices = [slicing.ident_map_slice(topo, {}) for p in policies]
combined = zip(slices, policies)
compiled = ec.compile_slices(topo, combined)
self.assertIsNone(sat.shared_io(topo, policies[0], policies[1]))
self.assertIsNone(sat.shared_io(topo, compiled[0], compiled[1]))
self.assertTrue(sat.compiled_correctly(topo, policies[0], compiled[0]))
self.assertTrue(sat.compiled_correctly(topo, policies[1], compiled[1]))
topo, policies = linear_all_ports((0, 1), (2, 3))
slices = [slicing.ident_map_slice(topo, {}) for p in policies]
combined = zip(slices, policies)
compiled = ec.compile_slices(topo, combined)
self.assertIsNone(sat.shared_io(topo, policies[0], policies[1]))
self.assertIsNone(sat.shared_io(topo, compiled[0], compiled[1]))
self.assertTrue(sat.compiled_correctly(topo, policies[0], compiled[0]))
self.assertTrue(sat.compiled_correctly(topo, policies[1], compiled[1]))
def testHostsCompileSmall(self):
topo, combined = linear_hosts((0, 1, 2, 3), (0, 1, 2, 3))
slices = [s for s, _ in combined]
policy = ((nc.inport(3, 3) |then| nc.forward(3, 1)) +
(nc.inport(3, 3) |then| nc.forward(3, 2)) +
(nc.inport(2, 3) |then| nc.forward(2, 2)))
combined = [(slices[0], policy), (slices[1], nc.BottomPolicy())]
compiled, _ = ec.compile_slices(topo, combined)
self.assertIsNone(sat.one_per_edge(topo, compiled))
self.assertTrue(sat.compiled_correctly(topo, policy, compiled))
def testHostsCompile(self):
topo, combined = linear_hosts((0, 1, 2, 3), (0, 1, 2, 3))
policies = [p for _, p in combined]
compiled = ec.compile_slices(topo, combined)
self.assertIsNotNone(sat.shared_io(topo, policies[0], policies[1]))
self.assertIsNone(sat.shared_io(topo, compiled[0], compiled[1]))
self.assertTrue(sat.compiled_correctly(topo, policies[0], compiled[0]))
self.assertTrue(sat.compiled_correctly(topo, policies[1], compiled[1]))
class TestSeparation(unittest.TestCase):
def testInputDisjoint(self):
p1 = nc.Header({'switch': 0, 'port': 1, 'vlan': 0}) |then|\
nc.Action(0, [2])
p2 = nc.Header({'switch': 0, 'port': 2, 'vlan': 0}) |then|\
nc.Action(0, [3])
self.assertIsNone(sat.shared_inputs(p1, p2))
def testInputOverlap(self):
p1 = nc.Header({'switch': 0, 'port': 1, 'vlan': 0}) |then|\
nc.Action(0, [2])
p2 = nc.Header({'switch': 0, 'port': 1, 'vlan': 0}) |then|\
nc.Action(0, [3])
self.assertIsNotNone(sat.shared_inputs(p1, p2))
def testOutputDisjoint(self):
p1 = nc.inport(0, 1) |then| nc.Action(0, [2], {'vlan': 0, 'srcmac': 1})
p2 = nc.inport(0, 1) |then| nc.Action(0, [2], {'vlan': 0, 'srcmac': 2})
self.assertIsNone(sat.shared_outputs(p1, p2))
def testOutputOverlap(self):
p1 = nc.inport(0, 1) |then| nc.Action(0, [2], {'vlan': 0, 'srcmac': 1})
p2 = nc.inport(0, 1) |then| nc.Action(0, [2], {'vlan': 0, 'srcmac': 1})
self.assertIsNotNone(sat.shared_outputs(p1, p2))
def testBasic(self):
topo, policies = linear((0, 1, 2, 3), (0, 1, 2, 3))
slices = [slicing.ident_map_slice(topo, {}) for p in policies]
combined = zip(slices, policies)
compiled = cp.compile_slices(combined)
self.assertTrue(sat.separate(topo, compiled[0], compiled[1]))
def testBasicFail(self):
topo, combined = linear_hosts((0, 1, 2, 3), (0, 1, 2, 3))
policies = [p for _, p in combined]
compiled = ec.compile_slices(topo, combined)
self.assertIsNone(sat.shared_io(topo, compiled[0], compiled[1]))
self.assertIsNone(sat.shared_io(topo, compiled[1], compiled[0]))
self.assertIsNotNone(sat.shared_inputs(compiled[0], compiled[1]))
self.assertIsNotNone(sat.shared_inputs(compiled[1], compiled[0]))
self.assertIsNotNone(sat.shared_outputs(compiled[0], compiled[1]))
self.assertIsNotNone(sat.shared_outputs(compiled[1], compiled[0]))
self.assertFalse(sat.separate(topo, compiled[0], compiled[1]))
def testEdgePredicates(self):
topo, combined = linear_hosts((0, 1, 2, 3), (0, 1, 2, 3))
for key in combined[0][0].edge_policy:
combined[0][0].edge_policy[key] = nc.Header({'srcmac': 100})
for key in combined[1][0].edge_policy:
combined[1][0].edge_policy[key] = nc.Header({'srcmac': 101})
policies = [p for _, p in combined]
compiled = ec.compile_slices(topo, combined)
self.assertIsNone(sat.shared_io(topo, compiled[0], compiled[1]))
self.assertIsNone(sat.shared_io(topo, compiled[1], compiled[0]))
self.assertIsNone(sat.shared_inputs(compiled[0], compiled[1]))
self.assertIsNone(sat.shared_inputs(compiled[1], compiled[0]))
# If we had outbound predicates, these would be None
self.assertIsNotNone(sat.shared_outputs(compiled[0], compiled[1]))
self.assertIsNotNone(sat.shared_outputs(compiled[1], compiled[0]))
self.assertFalse(sat.separate(topo, compiled[0], compiled[1]))
class TestCompleteGraph(unittest.TestCase):
def setUp(self):
self.k10topo, self.k10combined = k10()
self.k10policies = [p for _, p in self.k10combined]
self.k4topo, self.k4combined = k4()
self.k4policies = [p for _, p in self.k4combined]
self.k4hosts_topo, self.k4hosts_combined = k4hosts()
self.k4hosts_policies = [p for _, p in self.k4hosts_combined]
@unittest.skipIf('EXPENSIVE_TESTS' not in os.environ, 'expensive')
def testExpensivePhysEquiv(self):
self.physEquiv(k10_nodes, self.k10policies)
def testCheapPhysEquiv(self):
self.physEquiv(k4_nodes, self.k4policies)
def testHostsPhysEquiv(self):
self.physEquiv(k4_nodes, self.k4hosts_policies)
@unittest.skipIf('EXPENSIVE_TESTS' not in os.environ, 'expensive')
def testExpensivePhysSep(self):
self.physSep(k10_nodes, self.k10topo, self.k10policies)
def testCheapPhysSep(self):
self.physSep(k4_nodes, self.k4topo, self.k4policies)
def testHostsPhysSep(self):
self.physSep(k4_nodes, self.k4hosts_topo, self.k4hosts_policies)
@unittest.skipIf('EXPENSIVE_TESTS' not in os.environ, 'expensive')
def testExpensiveCompile(self):
self.sliceCompile(k10_nodes, self.k10topo, self.k10combined)
def testCheapCompile(self):
self.sliceCompile(k4_nodes, self.k4topo, self.k4combined)
def testHostsCompile(self):
self.sliceCompile(k4_nodes, self.k4hosts_topo, self.k4hosts_combined)
@unittest.skipIf('EXPENSIVE_TESTS' not in os.environ, 'expensive')
def testExpensiveEdgeCompile(self):
self.edgeCompile(k10_nodes, self.k10topo, self.k10combined)
def testCheapEdgeCompile(self):
self.edgeCompile(k4_nodes, self.k4topo, self.k4combined)
def testHostsCompile(self):
self.edgeCompile(k4_nodes, self.k4hosts_topo, self.k4hosts_combined)
def physEquiv(self, nodes, policies):
for i in range(0, len(policies)):
for j in range(len(policies)):
if verbose:
print "testing %s equiv %s." % (nodes[i], nodes[j])
result = sat.equivalent(policies[i],
nc.PolicyUnion(policies[i],
policies[j]))
if i is not j and\
len(set(nodes[i]).intersection(nodes[j])) > 1:
self.assertIsNotNone(result)
else:
self.assertIsNone(result)
def physSep(self, nodes, topo, policies):
for i in range(0, len(policies)):
for j in range(len(policies)):
if verbose:
print "testing %s with %s." % (nodes[i], nodes[j])
print str(policies[i])
print str(policies[j])
result = sat.shared_io(topo, policies[i], policies[j])
if len(set(nodes[i]).intersection(nodes[j])) > 1:
self.assertIsNotNone(result)
else:
self.assertIsNone(result)
def sliceCompile(self, nodes, topo, combined):
compiled = cp.compile_slices(combined)
for i in range(len(compiled)):
self.assertTrue(sat.compiled_correctly(topo, combined[i][1],
compiled[i]))
for j in range(len(compiled)):
if verbose:
print "testing compiled %s with %s."\
% (nodes[i], nodes[j])
result = sat.shared_io(topo, compiled[i], compiled[j])
if i == j:
self.assertIsNotNone(result)
else:
cc = sat.compiled_correctly(topo, combined[i][1], compiled[j])
self.assertIsNotNone(cc)
self.assertIsNone(result)
def edgeCompile(self, nodes, topo, combined):
compiled = ec.compile_slices(topo, combined, verbose=verbose)
for i in range(len(compiled)):
self.assertTrue(sat.compiled_correctly(topo, combined[i][1],
compiled[i]))
self.assertIsNotNone(sat.shared_io(topo, compiled[i], compiled[i]))
for j in range(len(compiled)):
if verbose:
print "testing edge compiled %d:%s with %d:%s."\
% (i, nodes[i], j, nodes[j])
if i != j:
self.assertFalse(sat.compiled_correctly(topo, combined[i][1],
compiled[j]))
self.assertIsNone(sat.shared_io(topo, compiled[i],
compiled[j]))
if __name__ == '__main__':
#suite = unittest.TestLoader().loadTestsFromTestCase(TestSeparation)
#unittest.TextTestRunner(verbosity=2).run(suite)
unittest.main()