2 Copyright (c) 2015 SONATA-NFV
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
9 http://www.apache.org/licenses/LICENSE-2.0
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.
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
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).
30 Helper module that implements helpers for test implementations.
37 from emuvim
.dcemulator
.net
import DCNetwork
38 from mininet
.clean
import cleanup
39 from mininet
.node
import Controller
41 class SimpleTestTopology(unittest
.TestCase
):
43 Helper class to do basic test setups.
44 s1 -- s2 -- s3 -- ... -- sN
47 def __init__(self
, *args
, **kwargs
):
49 self
.s
= [] # list of switches
50 self
.h
= [] # list of hosts
51 self
.d
= [] # list of docker containers
52 self
.dc
= [] # list of data centers
53 self
.docker_cli
= None
54 super(SimpleTestTopology
, self
).__init
__(*args
, **kwargs
)
58 nswitches
=0, ndatacenter
=0, nhosts
=0, ndockers
=0,
59 autolinkswitches
=False, controller
=Controller
, **kwargs
):
61 Creates a Mininet instance and automatically adds some
64 Attention, we should always use Mininet's default controller
65 for our tests. Only use other controllers if you want to test
66 specific controller functionality.
68 self
.net
= DCNetwork(controller
=controller
, **kwargs
)
71 # start from s1 because ovs does not like to have dpid = 0
72 # and switch name-number is being used by mininet to set the dpid
73 for i
in range(1, nswitches
+1):
74 self
.s
.append(self
.net
.addSwitch('s%d' % i
))
75 # if specified, chain all switches
77 for i
in range(0, len(self
.s
) - 1):
78 self
.net
.addLink(self
.s
[i
], self
.s
[i
+ 1])
79 # add some data centers
80 for i
in range(0, ndatacenter
):
82 self
.net
.addDatacenter(
84 metadata
={"unittest_dc": i
}))
86 for i
in range(0, nhosts
):
87 self
.h
.append(self
.net
.addHost('h%d' % i
))
89 for i
in range(0, ndockers
):
90 self
.d
.append(self
.net
.addDocker('d%d' % i
, dimage
="ubuntu:trusty"))
98 def getDockerCli(self
):
100 Helper to interact with local docker instance.
102 if self
.docker_cli
is None:
103 self
.docker_cli
= docker
.Client(
104 base_url
='unix://var/run/docker.sock')
105 return self
.docker_cli
107 def getContainernetContainers(self
):
109 List the containers managed by containernet
111 return self
.getDockerCli().containers(filters
={"label": "com.containernet"})
120 # make sure that all pending docker containers are killed
121 with
open(os
.devnull
, 'w') as devnull
:
123 "sudo docker rm -f $(sudo docker ps --filter 'label=com.containernet' -a -q)",