| 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 | f27a592 | 2017-05-17 08:48:54 +0200 | [diff] [blame] | 26 | import unittest |
| 27 | import os |
| 28 | import subprocess |
| 29 | import docker |
| peusterm | 861bad7 | 2017-05-18 14:55:27 +0200 | [diff] [blame] | 30 | import time |
| peusterm | f27a592 | 2017-05-17 08:48:54 +0200 | [diff] [blame] | 31 | from emuvim.dcemulator.net import DCNetwork |
| 32 | from emuvim.api.openstack.openstack_api_endpoint import OpenstackApiEndpoint |
| 33 | from mininet.clean import cleanup |
| 34 | from mininet.node import Controller |
| 35 | |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame^] | 36 | |
| peusterm | f27a592 | 2017-05-17 08:48:54 +0200 | [diff] [blame] | 37 | class ApiBaseOpenStack(unittest.TestCase): |
| 38 | """ |
| 39 | Helper class to do basic test setups. |
| 40 | s1 -- s2 -- s3 -- ... -- sN |
| 41 | """ |
| 42 | |
| 43 | def __init__(self, *args, **kwargs): |
| 44 | self.net = None |
| 45 | self.api = [] |
| 46 | self.s = [] # list of switches |
| 47 | self.h = [] # list of hosts |
| 48 | self.d = [] # list of docker containers |
| 49 | self.dc = [] # list of data centers |
| 50 | self.docker_cli = None |
| 51 | super(ApiBaseOpenStack, self).__init__(*args, **kwargs) |
| 52 | |
| 53 | def createNet( |
| 54 | self, |
| 55 | nswitches=0, ndatacenter=0, nhosts=0, ndockers=0, |
| 56 | autolinkswitches=False, controller=Controller, **kwargs): |
| 57 | """ |
| 58 | Creates a Mininet instance and automatically adds some |
| 59 | nodes to it. |
| 60 | |
| 61 | Attention, we should always use Mininet's default controller |
| 62 | for our tests. Only use other controllers if you want to test |
| 63 | specific controller functionality. |
| 64 | """ |
| 65 | self.net = DCNetwork(controller=controller, **kwargs) |
| 66 | for i in range(0, ndatacenter): |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame^] | 67 | self.api.append(OpenstackApiEndpoint("0.0.0.0", 15000 + i)) |
| peusterm | f27a592 | 2017-05-17 08:48:54 +0200 | [diff] [blame] | 68 | |
| 69 | # add some switches |
| 70 | # start from s1 because ovs does not like to have dpid = 0 |
| 71 | # and switch name-number is being used by mininet to set the dpid |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame^] | 72 | for i in range(1, nswitches + 1): |
| peusterm | f27a592 | 2017-05-17 08:48:54 +0200 | [diff] [blame] | 73 | self.s.append(self.net.addSwitch('s%d' % i)) |
| 74 | # if specified, chain all switches |
| 75 | if autolinkswitches: |
| 76 | for i in range(0, len(self.s) - 1): |
| 77 | self.net.addLink(self.s[i], self.s[i + 1]) |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame^] | 78 | # link switches s1, s2 and s3 |
| 79 | self.net.addLink(self.s[2], self.s[0]) |
| peusterm | f27a592 | 2017-05-17 08:48:54 +0200 | [diff] [blame] | 80 | |
| 81 | # add some data centers |
| 82 | for i in range(0, ndatacenter): |
| 83 | self.dc.append( |
| 84 | self.net.addDatacenter( |
| 85 | 'dc%d' % i, |
| 86 | metadata={"unittest_dc": i})) |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame^] | 87 | # link switches dc0.s1 with s1 |
| 88 | self.net.addLink(self.dc[0].switch, self.s[0]) |
| peusterm | f27a592 | 2017-05-17 08:48:54 +0200 | [diff] [blame] | 89 | # connect data centers to the endpoint |
| 90 | for i in range(0, ndatacenter): |
| 91 | self.api[i].connect_datacenter(self.dc[i]) |
| 92 | self.api[i].connect_dc_network(self.net) |
| 93 | # add some hosts |
| 94 | for i in range(0, nhosts): |
| 95 | self.h.append(self.net.addHost('h%d' % i)) |
| 96 | # add some dockers |
| 97 | for i in range(0, ndockers): |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame^] | 98 | self.d.append(self.net.addDocker('d%d' % |
| 99 | i, dimage="ubuntu:trusty")) |
| peusterm | f27a592 | 2017-05-17 08:48:54 +0200 | [diff] [blame] | 100 | |
| 101 | def startApi(self): |
| 102 | for i in self.api: |
| peusterm | f4ac4f2 | 2017-05-18 15:51:57 +0200 | [diff] [blame] | 103 | i.start(wait_for_port=True) |
| peusterm | f27a592 | 2017-05-17 08:48:54 +0200 | [diff] [blame] | 104 | |
| 105 | def stopApi(self): |
| 106 | for i in self.api: |
| 107 | i.manage.stop_floating_network() |
| 108 | i.stop() |
| 109 | |
| 110 | def startNet(self): |
| 111 | self.net.start() |
| 112 | |
| 113 | def stopNet(self): |
| 114 | self.net.stop() |
| 115 | |
| 116 | def getDockerCli(self): |
| 117 | """ |
| 118 | Helper to interact with local docker instance. |
| 119 | """ |
| 120 | if self.docker_cli is None: |
| 121 | self.docker_cli = docker.Client( |
| 122 | base_url='unix://var/run/docker.sock') |
| 123 | return self.docker_cli |
| 124 | |
| 125 | def getContainernetContainers(self): |
| 126 | """ |
| 127 | List the containers managed by containernet |
| 128 | """ |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame^] | 129 | return self.getDockerCli().containers( |
| 130 | filters={"label": "com.containernet"}) |
| peusterm | f27a592 | 2017-05-17 08:48:54 +0200 | [diff] [blame] | 131 | |
| 132 | @staticmethod |
| 133 | def setUp(): |
| 134 | pass |
| 135 | |
| peusterm | f27a592 | 2017-05-17 08:48:54 +0200 | [diff] [blame] | 136 | def tearDown(self): |
| peusterm | 861bad7 | 2017-05-18 14:55:27 +0200 | [diff] [blame] | 137 | time.sleep(2) |
| peusterm | f27a592 | 2017-05-17 08:48:54 +0200 | [diff] [blame] | 138 | print('->>>>>>> tear everything down ->>>>>>>>>>>>>>>') |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame^] | 139 | self.stopApi() # stop all flask threads |
| 140 | self.stopNet() # stop some mininet and containernet stuff |
| peusterm | f27a592 | 2017-05-17 08:48:54 +0200 | [diff] [blame] | 141 | cleanup() |
| 142 | # make sure that all pending docker containers are killed |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame^] | 143 | # kill a possibly running docker process that blocks the open ports |
| 144 | with open(os.devnull, 'w') as devnull: |
| peusterm | e22979a | 2017-05-18 13:28:38 +0200 | [diff] [blame] | 145 | subprocess.call("kill $(netstat -npl | grep '15000' | grep -o -e'[0-9]\+/docker' | grep -o -e '[0-9]\+')", |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame^] | 146 | stdout=devnull, |
| 147 | stderr=devnull, |
| 148 | shell=True) |
| peusterm | f27a592 | 2017-05-17 08:48:54 +0200 | [diff] [blame] | 149 | |
| 150 | with open(os.devnull, 'w') as devnull: |
| 151 | subprocess.call( |
| 152 | "sudo docker rm -f $(sudo docker ps --filter 'label=com.containernet' -a -q)", |
| 153 | stdout=devnull, |
| 154 | stderr=devnull, |
| 155 | shell=True) |
| peusterm | 861bad7 | 2017-05-18 14:55:27 +0200 | [diff] [blame] | 156 | time.sleep(2) |