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