def topology(): "Create a network with different node types, also utilizing Libvirt" net = Containernet(controller=Controller) net.addController("c0") n1 = net.addHost("h1", ip='10.0.0.1') n2 = net.addLibvirthost("vm1", ip='10.0.0.2', domain_name="ubuntu16.04") n3 = net.addDocker('d1', ip='10.0.0.3', dimage="ubuntu:trusty") n4 = net.addLibvirthost("vm2", ip='10.0.0.4', disk_image="/var/libvirt/images/ubuntu16.04.qcow2") info('*** Starting Switches and Links\n') s1 = net.addSwitch("s1") net.addLink(n1, s1) net.addLink(n2, s1) net.addLink(n3, s1) net.addLink(n4, s1) info('*** Starting network\n') net.start() CLI(net) info('*** Stopping network') net.stop()
class simpleTestTopology(unittest.TestCase): """ Helper class to do basic test setups. s1 -- s2 -- s3 -- ... -- sN """ def __init__(self, *args, **kwargs): self.net = None self.s = [] # list of switches self.h = [] # list of hosts self.d = [] # list of docker containers self.l = [] self.docker_cli = None self.lv_conn_qemu = libvirt.open('qemu:///system') self.vm_names = ["vm1", "vm2", "vm3"] super(simpleTestTopology, self).__init__(*args, **kwargs) def createNet(self, nswitches=1, nhosts=0, ndockers=0, nlibvirt=0, autolinkswitches=False, use_running=False): """ Creates a Mininet instance and automatically adds some nodes to it. """ self.net = Containernet(controller=Controller, mgmt_net={'mac': '00:AA:BB:CC:DD:EE'}, cmd_endpoint="qemu:///system") self.net.addController('c0') # add some switches for i in range(0, nswitches): self.s.append(self.net.addSwitch('s%d' % i)) # if specified, chain all switches if autolinkswitches: for i in range(0, len(self.s) - 1): self.net.addLink(self.s[i], self.s[i + 1]) # add some hosts for i in range(0, nhosts): self.h.append(self.net.addHost('h%d' % i)) # add some dockers for i in range(0, ndockers): self.d.append(self.net.addDocker('d%d' % i, dimage="ubuntu:trusty")) for i in range(1, nlibvirt + 1): self.l.append( self.net.addLibvirthost('vm%d' % i, disk_image=DISK_IMAGE, use_existing_vm=use_running)) def startNet(self): self.net.start() def stopNet(self): self.net.stop() def getDockerCli(self): """ Helper to interact with local docker instance. """ if self.docker_cli is None: self.docker_cli = docker.APIClient( base_url='unix://var/run/docker.sock') return self.docker_cli def setUp(self): print "\nTesting: ", self._testMethodName @staticmethod def tearDown(): cleanup() # make sure that all pending docker containers are killed with open(os.devnull, 'w') as devnull: subprocess.call( "docker rm -f $(docker ps --filter 'label=com.containernet' -a -q)", stdout=devnull, stderr=devnull, shell=True) def getContainernetContainers(self): """ List the containers managed by containernet """ return self.getDockerCli().containers( filters={"label": "com.containernet"}) def getContainernetLibvirtHosts(self): hosts = 0 for domain in self.lv_conn_qemu.listAllDomains(): xml = minidom.parseString(domain.XMLDesc()) title = xml.getElementsByTagName("title") if title and "com.containernet" in title[0].toxml(): hosts += 1 return hosts