Refactoring: Made complete codebase PEP8 compatible.
[osm/vim-emu.git] / src / emuvim / test / api_base_openstack.py
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).
26 import unittest
27 import os
28 import subprocess
29 import docker
30 import time
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
36
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):
67 self.api.append(OpenstackApiEndpoint("0.0.0.0", 15000 + i))
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
72 for i in range(1, nswitches + 1):
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])
78 # link switches s1, s2 and s3
79 self.net.addLink(self.s[2], self.s[0])
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}))
87 # link switches dc0.s1 with s1
88 self.net.addLink(self.dc[0].switch, self.s[0])
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):
98 self.d.append(self.net.addDocker('d%d' %
99 i, dimage="ubuntu:trusty"))
100
101 def startApi(self):
102 for i in self.api:
103 i.start(wait_for_port=True)
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 """
129 return self.getDockerCli().containers(
130 filters={"label": "com.containernet"})
131
132 @staticmethod
133 def setUp():
134 pass
135
136 def tearDown(self):
137 time.sleep(2)
138 print('->>>>>>> tear everything down ->>>>>>>>>>>>>>>')
139 self.stopApi() # stop all flask threads
140 self.stopNet() # stop some mininet and containernet stuff
141 cleanup()
142 # make sure that all pending docker containers are killed
143 # kill a possibly running docker process that blocks the open ports
144 with open(os.devnull, 'w') as devnull:
145 subprocess.call("kill $(netstat -npl | grep '15000' | grep -o -e'[0-9]\+/docker' | grep -o -e '[0-9]\+')",
146 stdout=devnull,
147 stderr=devnull,
148 shell=True)
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)
156 time.sleep(2)