Closed chris3torek closed 6 years ago
Once we have a wide range of such helper functions, it would be beneficial to switch to the Python unittest framework, rather than using our custom one. At the moment writing a module test is somewhat arcane and exotic.
I am taking a look. Letting me know if you have any wishlist added to it.
Some module tests work by injecting some packet(s) using a port-and-socket pair generated by
gen_socket_and_port
. See, e.g.,bessctl/conf/testing/module_tests/nat.py
orbessctl/conf/testing/module_tests/etherencap.py
. As long as the code has the form:or similar, we're fine. But suppose we want to specifically test, e.g., that a packet with some attributes gets routed to some particular output gate:
Now there is a potential problem: the
sock.send
call in Python waits for the socket data to get to the kernel, but getting into the kernel does not mean that the data have even arrived at the receivingUnixSocketPort
in bessd (much less been dispatched).What's needed here is a way to wait for the
UnixSocketPort
module's igate packet count to go up. This means we need to sample it once before thesock.send
, and in a loop afterward. This code should probably be in the test framework so that any test can use it easily.While at it, the test framework probably should define a function similar to
get_pkts_for_ogate
above, but generalized to handle both input and output gates. Rather than getting just the packet count it should get the IGate or OGate data from <module, direction, gate_num>. Thenget_pkts_for_ogate(module, 0)
becomesget_gate_stats(module, 'ogate', 0).pkts
. But that's less important than hiding the "sample the count, then inject packet(s), then wait for packet to get dispatched" test case.(If there's a better way overall to do this, that would be even better)