Skip to content

Instantly share code, notes, and snippets.

@lantz
Last active July 1, 2019 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lantz/804b2f764e9573b3cac46b795b43dd14 to your computer and use it in GitHub Desktop.
Save lantz/804b2f764e9573b3cac46b795b43dd14 to your computer and use it in GitHub Desktop.
Simple sanity test for Mininet host ordering
#!/usr/bin/env python3
"""
Mininet host order sanity test
We make sure that net.topo.hosts(sorted=False)
returns hosts in the order they were added.
We also make sure that net.hosts is in canonical
order, which should also be different from repr()
order.
"""
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.cli import CLI
from mininet.node import Host
from mininet.util import natural
# Alternate host class and prefix names in strange order
class AHost(Host): pass
class ZHost(Host): pass
class MHost(Host): pass
def hostClass(n): return (AHost, ZHost, MHost)[n % 3]
def hostPrefix(n): return 'lkij'[n % 4] + '%s'
def space(strs): return ' '.join(strs)
class RevTopo( Topo ):
"Reversed topo with weird host names and classes"
def build( self, k=4):
global topoHosts
topoHosts = []
switch = self.addSwitch( 's1' )
print("Adding hosts: ", end='')
for h in range(k, 0, -1):
host = self.addHost( hostPrefix(h) % h, cls=hostClass(h) )
topoHosts.append(host)
print(host, end=' ')
self.addLink( host, switch)
print()
net = Mininet(topo=RevTopo(k=4))
net.start()
# Compute various host orderings
orderedHosts = net.topo.hosts(sort=False)
sortedHosts = net.topo.hosts(sort=True)
resortedHosts = sorted([h.name for h in net.hosts], key=natural)
mininetHosts = [h.name for h in net.hosts]
reprHosts = [h.name for h in sorted(net.hosts, key=repr)]
print("Topo hosts: ", space(topoHosts))
print("Ordered hosts: ", space(orderedHosts))
assert topoHosts == orderedHosts, "topo.hosts(sort=False) should be in addHost order"
print("Sorted hosts: ", space(sortedHosts))
assert orderedHosts != sortedHosts, "topo.hosts(sort=True/False) doesn't work"
print("Resorted hosts: ", space(resortedHosts))
assert resortedHosts == sortedHosts, "Mininet hosts should be sorted properly"
print("Mininet hosts: ", space(mininetHosts))
assert sortedHosts == mininetHosts, "Mininet hosts should match sorted topo hosts"
print("Repr hosts: ", space(reprHosts))
assert sortedHosts != reprHosts, "Mininet hosts shouldn't be in repr order"
# CLI(net)
net.stop()
print("Test complete.")
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment