| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 1 | """ |
| 2 | Helper module that implements helpers for test implementations. |
| 3 | """ |
| 4 | |
| 5 | import unittest |
| 6 | import os |
| 7 | import subprocess |
| 8 | import docker |
| 9 | from emuvim.dcemulator.net import DCNetwork |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame^] | 10 | from emuvim.api.rest.rest_api_endpoint import RestApiEndpoint |
| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 11 | from mininet.clean import cleanup |
| 12 | from mininet.node import Controller |
| 13 | |
| 14 | class SimpleTestTopology(unittest.TestCase): |
| 15 | """ |
| 16 | Helper class to do basic test setups. |
| 17 | s1 -- s2 -- s3 -- ... -- sN |
| 18 | """ |
| 19 | |
| 20 | def __init__(self, *args, **kwargs): |
| 21 | self.net = None |
| 22 | self.api = None |
| 23 | self.s = [] # list of switches |
| 24 | self.h = [] # list of hosts |
| 25 | self.d = [] # list of docker containers |
| 26 | self.dc = [] # list of data centers |
| 27 | self.docker_cli = None |
| 28 | super(SimpleTestTopology, self).__init__(*args, **kwargs) |
| 29 | |
| 30 | def createNet( |
| 31 | self, |
| 32 | nswitches=0, ndatacenter=0, nhosts=0, ndockers=0, |
| 33 | autolinkswitches=False, controller=Controller, **kwargs): |
| 34 | """ |
| 35 | Creates a Mininet instance and automatically adds some |
| 36 | nodes to it. |
| 37 | |
| 38 | Attention, we should always use Mininet's default controller |
| 39 | for our tests. Only use other controllers if you want to test |
| 40 | specific controller functionality. |
| 41 | """ |
| 42 | self.net = DCNetwork(controller=controller, **kwargs) |
| 43 | self.api = RestApiEndpoint("127.0.0.1",5000) |
| 44 | # add some switches |
| 45 | # start from s1 because ovs does not like to have dpid = 0 |
| 46 | # and switch name-number is being used by mininet to set the dpid |
| 47 | for i in range(1, nswitches+1): |
| 48 | self.s.append(self.net.addSwitch('s%d' % i)) |
| 49 | # if specified, chain all switches |
| 50 | if autolinkswitches: |
| 51 | for i in range(0, len(self.s) - 1): |
| 52 | self.net.addLink(self.s[i], self.s[i + 1]) |
| 53 | # add some data centers |
| 54 | for i in range(0, ndatacenter): |
| 55 | self.dc.append( |
| 56 | self.net.addDatacenter( |
| 57 | 'datacenter%d' % i, |
| 58 | metadata={"unittest_dc": i})) |
| 59 | # connect data centers to the endpoint |
| 60 | for i in range(0, ndatacenter): |
| 61 | self.api.connectDatacenter(self.dc[i]) |
| 62 | # add some hosts |
| 63 | for i in range(0, nhosts): |
| 64 | self.h.append(self.net.addHost('h%d' % i)) |
| 65 | # add some dockers |
| 66 | for i in range(0, ndockers): |
| 67 | self.d.append(self.net.addDocker('d%d' % i, dimage="ubuntu:trusty")) |
| 68 | |
| 69 | def startApi(self): |
| 70 | self.api.start() |
| 71 | |
| 72 | def startNet(self): |
| 73 | self.net.start() |
| 74 | |
| 75 | def stopNet(self): |
| 76 | self.net.stop() |
| 77 | |
| 78 | def getDockerCli(self): |
| 79 | """ |
| 80 | Helper to interact with local docker instance. |
| 81 | """ |
| 82 | if self.docker_cli is None: |
| 83 | self.docker_cli = docker.Client( |
| 84 | base_url='unix://var/run/docker.sock') |
| 85 | return self.docker_cli |
| 86 | |
| 87 | def getContainernetContainers(self): |
| 88 | """ |
| 89 | List the containers managed by containernet |
| 90 | """ |
| 91 | return self.getDockerCli().containers(filters={"label": "com.containernet"}) |
| 92 | |
| 93 | @staticmethod |
| 94 | def setUp(): |
| 95 | pass |
| 96 | |
| 97 | @staticmethod |
| 98 | def tearDown(): |
| 99 | cleanup() |
| 100 | # make sure that all pending docker containers are killed |
| 101 | with open(os.devnull, 'w') as devnull: |
| 102 | subprocess.call( |
| 103 | "sudo docker rm -f $(sudo docker ps --filter 'label=com.containernet' -a -q)", |
| 104 | stdout=devnull, |
| 105 | stderr=devnull, |
| 106 | shell=True) |