| 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). |
| schillinge | 68d0421 | 2019-03-11 17:57:41 +0100 | [diff] [blame] | 26 | |
| 27 | from gevent import monkey |
| 28 | monkey.patch_all() # noqa: because otherwise pep complains about code before imports |
| 29 | |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 30 | import unittest |
| 31 | import os |
| 32 | import subprocess |
| 33 | import docker |
| 34 | from emuvim.dcemulator.net import DCNetwork |
| 35 | from mininet.clean import cleanup |
| peusterm | ef6629e | 2016-03-14 17:21:56 +0100 | [diff] [blame] | 36 | from mininet.node import Controller |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 37 | |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 38 | |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 39 | class SimpleTestTopology(unittest.TestCase): |
| 40 | """ |
| 41 | Helper class to do basic test setups. |
| 42 | s1 -- s2 -- s3 -- ... -- sN |
| 43 | """ |
| 44 | |
| 45 | def __init__(self, *args, **kwargs): |
| 46 | self.net = None |
| 47 | self.s = [] # list of switches |
| 48 | self.h = [] # list of hosts |
| 49 | self.d = [] # list of docker containers |
| 50 | self.dc = [] # list of data centers |
| 51 | self.docker_cli = None |
| 52 | super(SimpleTestTopology, self).__init__(*args, **kwargs) |
| 53 | |
| 54 | def createNet( |
| 55 | self, |
| 56 | nswitches=0, ndatacenter=0, nhosts=0, ndockers=0, |
| stevenvanrossem | 7cd3c25 | 2016-05-11 22:55:15 +0200 | [diff] [blame] | 57 | autolinkswitches=False, controller=Controller, **kwargs): |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 58 | """ |
| 59 | Creates a Mininet instance and automatically adds some |
| 60 | nodes to it. |
| peusterm | de14f33 | 2016-03-15 16:14:21 +0100 | [diff] [blame] | 61 | |
| 62 | Attention, we should always use Mininet's default controller |
| 63 | for our tests. Only use other controllers if you want to test |
| 64 | specific controller functionality. |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 65 | """ |
| stevenvanrossem | 7cd3c25 | 2016-05-11 22:55:15 +0200 | [diff] [blame] | 66 | self.net = DCNetwork(controller=controller, **kwargs) |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 67 | |
| 68 | # add some switches |
| stevenvanrossem | e3e034e | 2016-05-11 23:51:06 +0200 | [diff] [blame] | 69 | # start from s1 because ovs does not like to have dpid = 0 |
| 70 | # and switch name-number is being used by mininet to set the dpid |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 71 | for i in range(1, nswitches + 1): |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 72 | self.s.append(self.net.addSwitch('s%d' % i)) |
| 73 | # if specified, chain all switches |
| 74 | if autolinkswitches: |
| 75 | for i in range(0, len(self.s) - 1): |
| 76 | self.net.addLink(self.s[i], self.s[i + 1]) |
| 77 | # add some data centers |
| 78 | for i in range(0, ndatacenter): |
| 79 | self.dc.append( |
| 80 | self.net.addDatacenter( |
| 81 | 'datacenter%d' % i, |
| 82 | metadata={"unittest_dc": i})) |
| 83 | # add some hosts |
| 84 | for i in range(0, nhosts): |
| 85 | self.h.append(self.net.addHost('h%d' % i)) |
| 86 | # add some dockers |
| 87 | for i in range(0, ndockers): |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 88 | self.d.append(self.net.addDocker('d%d' % |
| 89 | i, dimage="ubuntu:trusty")) |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 90 | |
| 91 | def startNet(self): |
| 92 | self.net.start() |
| 93 | |
| 94 | def stopNet(self): |
| 95 | self.net.stop() |
| 96 | |
| 97 | def getDockerCli(self): |
| 98 | """ |
| 99 | Helper to interact with local docker instance. |
| 100 | """ |
| 101 | if self.docker_cli is None: |
| stevenvanrossem | e8d8628 | 2017-01-28 00:52:22 +0100 | [diff] [blame] | 102 | self.docker_cli = docker.APIClient( |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 103 | base_url='unix://var/run/docker.sock') |
| 104 | return self.docker_cli |
| 105 | |
| peusterm | 5877ea2 | 2016-05-11 13:44:59 +0200 | [diff] [blame] | 106 | def getContainernetContainers(self): |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 107 | """ |
| peusterm | 5877ea2 | 2016-05-11 13:44:59 +0200 | [diff] [blame] | 108 | List the containers managed by containernet |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 109 | """ |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 110 | return self.getDockerCli().containers( |
| 111 | filters={"label": "com.containernet"}) |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 112 | |
| 113 | @staticmethod |
| 114 | def setUp(): |
| 115 | pass |
| 116 | |
| 117 | @staticmethod |
| 118 | def tearDown(): |
| 119 | cleanup() |
| 120 | # make sure that all pending docker containers are killed |
| 121 | with open(os.devnull, 'w') as devnull: |
| 122 | subprocess.call( |
| peusterm | 5877ea2 | 2016-05-11 13:44:59 +0200 | [diff] [blame] | 123 | "sudo docker rm -f $(sudo docker ps --filter 'label=com.containernet' -a -q)", |
| peusterm | 41006b7 | 2016-03-12 12:13:06 +0100 | [diff] [blame] | 124 | stdout=devnull, |
| 125 | stderr=devnull, |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 126 | shell=True) |