Removed properitary monitoring (used for student demo). Only keep OpenStack fakes.
[osm/vim-emu.git] / src / emuvim / test / api_base_openstack.py
1 """
2 Copyright (c) 2015 SONATA-NFV
3 ALL RIGHTS RESERVED.
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16
17 Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION]
18 nor the names of its contributors may be used to endorse or promote
19 products derived from this software without specific prior written
20 permission.
21
22 This work has been performed in the framework of the SONATA project,
23 funded by the European Commission under Grant number 671517 through
24 the Horizon 2020 and 5G-PPP programmes. The authors would like to
25 acknowledge the contributions of their colleagues of the SONATA
26 partner consortium (www.sonata-nfv.eu).
27 """
28
29 """
30 Helper module that implements helpers for test implementations.
31 """
32
33 import unittest
34 import os
35 import subprocess
36 import docker
37 from emuvim.dcemulator.net import DCNetwork
38 from emuvim.api.openstack.openstack_api_endpoint import OpenstackApiEndpoint
39 from mininet.clean import cleanup
40 from mininet.node import Controller
41
42 class ApiBaseOpenStack(unittest.TestCase):
43 """
44 Helper class to do basic test setups.
45 s1 -- s2 -- s3 -- ... -- sN
46 """
47
48 def __init__(self, *args, **kwargs):
49 self.net = None
50 self.api = []
51 self.s = [] # list of switches
52 self.h = [] # list of hosts
53 self.d = [] # list of docker containers
54 self.dc = [] # list of data centers
55 self.docker_cli = None
56 super(ApiBaseOpenStack, self).__init__(*args, **kwargs)
57
58 def createNet(
59 self,
60 nswitches=0, ndatacenter=0, nhosts=0, ndockers=0,
61 autolinkswitches=False, controller=Controller, **kwargs):
62 """
63 Creates a Mininet instance and automatically adds some
64 nodes to it.
65
66 Attention, we should always use Mininet's default controller
67 for our tests. Only use other controllers if you want to test
68 specific controller functionality.
69 """
70 self.net = DCNetwork(controller=controller, **kwargs)
71 for i in range(0, ndatacenter):
72 self.api.append(OpenstackApiEndpoint("0.0.0.0", 15000+i))
73
74 # add some switches
75 # start from s1 because ovs does not like to have dpid = 0
76 # and switch name-number is being used by mininet to set the dpid
77 for i in range(1, nswitches+1):
78 self.s.append(self.net.addSwitch('s%d' % i))
79 # if specified, chain all switches
80 if autolinkswitches:
81 for i in range(0, len(self.s) - 1):
82 self.net.addLink(self.s[i], self.s[i + 1])
83 self.net.addLink(self.s[2], self.s[0]) # link switches s1, s2 and s3
84
85 # add some data centers
86 for i in range(0, ndatacenter):
87 self.dc.append(
88 self.net.addDatacenter(
89 'dc%d' % i,
90 metadata={"unittest_dc": i}))
91 self.net.addLink(self.dc[0].switch, self.s[0]) # link switches dc0.s1 with s1
92 # connect data centers to the endpoint
93 for i in range(0, ndatacenter):
94 self.api[i].connect_datacenter(self.dc[i])
95 self.api[i].connect_dc_network(self.net)
96 # add some hosts
97 for i in range(0, nhosts):
98 self.h.append(self.net.addHost('h%d' % i))
99 # add some dockers
100 for i in range(0, ndockers):
101 self.d.append(self.net.addDocker('d%d' % i, dimage="ubuntu:trusty"))
102
103 def startApi(self):
104 for i in self.api:
105 i.start()
106
107 def stopApi(self):
108 for i in self.api:
109 i.manage.stop_floating_network()
110 i.stop()
111
112 def startNet(self):
113 self.net.start()
114
115 def stopNet(self):
116 self.net.stop()
117
118 def getDockerCli(self):
119 """
120 Helper to interact with local docker instance.
121 """
122 if self.docker_cli is None:
123 self.docker_cli = docker.Client(
124 base_url='unix://var/run/docker.sock')
125 return self.docker_cli
126
127 def getContainernetContainers(self):
128 """
129 List the containers managed by containernet
130 """
131 return self.getDockerCli().containers(filters={"label": "com.containernet"})
132
133 @staticmethod
134 def setUp():
135 pass
136
137
138 def tearDown(self):
139 print('->>>>>>> tear everything down ->>>>>>>>>>>>>>>')
140 self.stopApi() # stop all flask threads
141 self.stopNet() # stop some mininet and containernet stuff
142 cleanup()
143 # make sure that all pending docker containers are killed
144 with open(os.devnull, 'w') as devnull: # kill a possibly running docker process that blocks the open ports
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
157
158
159
160