| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 1 | # Copyright (c) 2015 SONATA-NFV and Paderborn University |
| 2 | # ALL RIGHTS RESERVED. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | # Neither the name of the SONATA-NFV, Paderborn University |
| 17 | # nor the names of its contributors may be used to endorse or promote |
| 18 | # products derived from this software without specific prior written |
| 19 | # permission. |
| 20 | # |
| 21 | # This work has been performed in the framework of the SONATA project, |
| 22 | # funded by the European Commission under Grant number 671517 through |
| 23 | # the Horizon 2020 and 5G-PPP programmes. The authors would like to |
| 24 | # acknowledge the contributions of their colleagues of the SONATA |
| 25 | # partner consortium (www.sonata-nfv.eu). |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 26 | import unittest |
| 27 | import os |
| 28 | import subprocess |
| 29 | import docker |
| 30 | from emuvim.dcemulator.net import DCNetwork |
| 31 | from mininet.clean import cleanup |
| peusterm | ef6629e | 2016-03-14 17:21:56 +0100 | [diff] [blame] | 32 | from mininet.node import Controller |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 33 | |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 34 | |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 35 | class SimpleTestTopology(unittest.TestCase): |
| 36 | """ |
| 37 | Helper class to do basic test setups. |
| 38 | s1 -- s2 -- s3 -- ... -- sN |
| 39 | """ |
| 40 | |
| 41 | def __init__(self, *args, **kwargs): |
| 42 | self.net = None |
| 43 | self.s = [] # list of switches |
| 44 | self.h = [] # list of hosts |
| 45 | self.d = [] # list of docker containers |
| 46 | self.dc = [] # list of data centers |
| 47 | self.docker_cli = None |
| 48 | super(SimpleTestTopology, self).__init__(*args, **kwargs) |
| 49 | |
| 50 | def createNet( |
| 51 | self, |
| 52 | nswitches=0, ndatacenter=0, nhosts=0, ndockers=0, |
| stevenvanrossem | 7cd3c25 | 2016-05-11 22:55:15 +0200 | [diff] [blame] | 53 | autolinkswitches=False, controller=Controller, **kwargs): |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 54 | """ |
| 55 | Creates a Mininet instance and automatically adds some |
| 56 | nodes to it. |
| peusterm | de14f33 | 2016-03-15 16:14:21 +0100 | [diff] [blame] | 57 | |
| 58 | Attention, we should always use Mininet's default controller |
| 59 | for our tests. Only use other controllers if you want to test |
| 60 | specific controller functionality. |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 61 | """ |
| stevenvanrossem | 7cd3c25 | 2016-05-11 22:55:15 +0200 | [diff] [blame] | 62 | self.net = DCNetwork(controller=controller, **kwargs) |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 63 | |
| 64 | # add some switches |
| stevenvanrossem | e3e034e | 2016-05-11 23:51:06 +0200 | [diff] [blame] | 65 | # start from s1 because ovs does not like to have dpid = 0 |
| 66 | # and switch name-number is being used by mininet to set the dpid |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 67 | for i in range(1, nswitches + 1): |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 68 | self.s.append(self.net.addSwitch('s%d' % i)) |
| 69 | # if specified, chain all switches |
| 70 | if autolinkswitches: |
| 71 | for i in range(0, len(self.s) - 1): |
| 72 | self.net.addLink(self.s[i], self.s[i + 1]) |
| 73 | # add some data centers |
| 74 | for i in range(0, ndatacenter): |
| 75 | self.dc.append( |
| 76 | self.net.addDatacenter( |
| 77 | 'datacenter%d' % i, |
| 78 | metadata={"unittest_dc": i})) |
| 79 | # add some hosts |
| 80 | for i in range(0, nhosts): |
| 81 | self.h.append(self.net.addHost('h%d' % i)) |
| 82 | # add some dockers |
| 83 | for i in range(0, ndockers): |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 84 | self.d.append(self.net.addDocker('d%d' % |
| 85 | i, dimage="ubuntu:trusty")) |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 86 | |
| 87 | def startNet(self): |
| 88 | self.net.start() |
| 89 | |
| 90 | def stopNet(self): |
| 91 | self.net.stop() |
| 92 | |
| 93 | def getDockerCli(self): |
| 94 | """ |
| 95 | Helper to interact with local docker instance. |
| 96 | """ |
| 97 | if self.docker_cli is None: |
| stevenvanrossem | e8d8628 | 2017-01-28 00:52:22 +0100 | [diff] [blame] | 98 | self.docker_cli = docker.APIClient( |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 99 | base_url='unix://var/run/docker.sock') |
| 100 | return self.docker_cli |
| 101 | |
| peusterm | 5877ea2 | 2016-05-11 13:44:59 +0200 | [diff] [blame] | 102 | def getContainernetContainers(self): |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 103 | """ |
| peusterm | 5877ea2 | 2016-05-11 13:44:59 +0200 | [diff] [blame] | 104 | List the containers managed by containernet |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 105 | """ |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 106 | return self.getDockerCli().containers( |
| 107 | filters={"label": "com.containernet"}) |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 108 | |
| 109 | @staticmethod |
| 110 | def setUp(): |
| 111 | pass |
| 112 | |
| 113 | @staticmethod |
| 114 | def tearDown(): |
| 115 | cleanup() |
| 116 | # make sure that all pending docker containers are killed |
| 117 | with open(os.devnull, 'w') as devnull: |
| 118 | subprocess.call( |
| peusterm | 5877ea2 | 2016-05-11 13:44:59 +0200 | [diff] [blame] | 119 | "sudo docker rm -f $(sudo docker ps --filter 'label=com.containernet' -a -q)", |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 120 | stdout=devnull, |
| 121 | stderr=devnull, |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 122 | shell=True) |