blob: 3909e1ae4eef1931e93f91c6c4af214196a21d7a [file] [log] [blame]
peusterm72f09882018-05-15 17:10:27 +02001# 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).
peustermf27a5922017-05-17 08:48:54 +020026import unittest
27import os
28import subprocess
29import docker
peusterm861bad72017-05-18 14:55:27 +020030import time
peustermf27a5922017-05-17 08:48:54 +020031from emuvim.dcemulator.net import DCNetwork
32from emuvim.api.openstack.openstack_api_endpoint import OpenstackApiEndpoint
33from mininet.clean import cleanup
34from mininet.node import Controller
35
peusterm72f09882018-05-15 17:10:27 +020036
peustermf27a5922017-05-17 08:48:54 +020037class 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):
peusterm72f09882018-05-15 17:10:27 +020067 self.api.append(OpenstackApiEndpoint("0.0.0.0", 15000 + i))
peustermf27a5922017-05-17 08:48:54 +020068
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
peusterm72f09882018-05-15 17:10:27 +020072 for i in range(1, nswitches + 1):
peustermf27a5922017-05-17 08:48:54 +020073 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])
peusterm72f09882018-05-15 17:10:27 +020078 # link switches s1, s2 and s3
79 self.net.addLink(self.s[2], self.s[0])
peustermf27a5922017-05-17 08:48:54 +020080
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}))
peusterm72f09882018-05-15 17:10:27 +020087 # link switches dc0.s1 with s1
88 self.net.addLink(self.dc[0].switch, self.s[0])
peustermf27a5922017-05-17 08:48:54 +020089 # 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):
peusterm72f09882018-05-15 17:10:27 +020098 self.d.append(self.net.addDocker('d%d' %
99 i, dimage="ubuntu:trusty"))
peustermf27a5922017-05-17 08:48:54 +0200100
101 def startApi(self):
102 for i in self.api:
peustermf4ac4f22017-05-18 15:51:57 +0200103 i.start(wait_for_port=True)
peustermf27a5922017-05-17 08:48:54 +0200104
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 """
peusterm72f09882018-05-15 17:10:27 +0200129 return self.getDockerCli().containers(
130 filters={"label": "com.containernet"})
peustermf27a5922017-05-17 08:48:54 +0200131
132 @staticmethod
133 def setUp():
134 pass
135
peustermf27a5922017-05-17 08:48:54 +0200136 def tearDown(self):
peusterm861bad72017-05-18 14:55:27 +0200137 time.sleep(2)
peustermf27a5922017-05-17 08:48:54 +0200138 print('->>>>>>> tear everything down ->>>>>>>>>>>>>>>')
peusterm72f09882018-05-15 17:10:27 +0200139 self.stopApi() # stop all flask threads
140 self.stopNet() # stop some mininet and containernet stuff
peustermf27a5922017-05-17 08:48:54 +0200141 cleanup()
142 # make sure that all pending docker containers are killed
peusterm72f09882018-05-15 17:10:27 +0200143 # kill a possibly running docker process that blocks the open ports
144 with open(os.devnull, 'w') as devnull:
peusterme22979a2017-05-18 13:28:38 +0200145 subprocess.call("kill $(netstat -npl | grep '15000' | grep -o -e'[0-9]\+/docker' | grep -o -e '[0-9]\+')",
peusterm72f09882018-05-15 17:10:27 +0200146 stdout=devnull,
147 stderr=devnull,
148 shell=True)
peustermf27a5922017-05-17 08:48:54 +0200149
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)
peusterm861bad72017-05-18 14:55:27 +0200156 time.sleep(2)