1 # Copyright (c) 2015 SONATA-NFV and Paderborn University
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
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).
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
37 class ApiBaseOpenStack(unittest
.TestCase
):
39 Helper class to do basic test setups.
40 s1 -- s2 -- s3 -- ... -- sN
43 def __init__(self
, *args
, **kwargs
):
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
)
55 nswitches
=0, ndatacenter
=0, nhosts
=0, ndockers
=0,
56 autolinkswitches
=False, controller
=Controller
, **kwargs
):
58 Creates a Mininet instance and automatically adds some
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.
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
))
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
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])
81 # add some data centers
82 for i
in range(0, ndatacenter
):
84 self
.net
.addDatacenter(
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
)
94 for i
in range(0, nhosts
):
95 self
.h
.append(self
.net
.addHost('h%d' % i
))
97 for i
in range(0, ndockers
):
98 self
.d
.append(self
.net
.addDocker('d%d' %
99 i
, dimage
="ubuntu:trusty"))
103 i
.start(wait_for_port
=True)
107 i
.manage
.stop_floating_network()
116 def getDockerCli(self
):
118 Helper to interact with local docker instance.
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
125 def getContainernetContainers(self
):
127 List the containers managed by containernet
129 return self
.getDockerCli().containers(
130 filters
={"label": "com.containernet"})
138 print('->>>>>>> tear everything down ->>>>>>>>>>>>>>>')
139 self
.stopApi() # stop all flask threads
140 self
.stopNet() # stop some mininet and containernet stuff
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]\+')",
150 with
open(os
.devnull
, 'w') as devnull
:
152 "sudo docker rm -f $(sudo docker ps --filter 'label=com.containernet' -a -q)",