2 Helper module that implements helpers for test implementations.
9 from emuvim
.dcemulator
.net
import DCNetwork
10 from mininet
.clean
import cleanup
11 from mininet
.node
import Controller
13 class SimpleTestTopology(unittest
.TestCase
):
15 Helper class to do basic test setups.
16 s1 -- s2 -- s3 -- ... -- sN
19 def __init__(self
, *args
, **kwargs
):
21 self
.s
= [] # list of switches
22 self
.h
= [] # list of hosts
23 self
.d
= [] # list of docker containers
24 self
.dc
= [] # list of data centers
25 self
.docker_cli
= None
26 super(SimpleTestTopology
, self
).__init
__(*args
, **kwargs
)
30 nswitches
=0, ndatacenter
=0, nhosts
=0, ndockers
=0,
31 autolinkswitches
=False, controller
=Controller
):
33 Creates a Mininet instance and automatically adds some
36 Attention, we should always use Mininet's default controller
37 for our tests. Only use other controllers if you want to test
38 specific controller functionality.
40 self
.net
= DCNetwork(controller
=controller
)
43 for i
in range(0, nswitches
):
44 self
.s
.append(self
.net
.addSwitch('s%d' % i
))
45 # if specified, chain all switches
47 for i
in range(0, len(self
.s
) - 1):
48 self
.net
.addLink(self
.s
[i
], self
.s
[i
+ 1])
49 # add some data centers
50 for i
in range(0, ndatacenter
):
52 self
.net
.addDatacenter(
54 metadata
={"unittest_dc": i
}))
56 for i
in range(0, nhosts
):
57 self
.h
.append(self
.net
.addHost('h%d' % i
))
59 for i
in range(0, ndockers
):
60 self
.d
.append(self
.net
.addDocker('d%d' % i
, dimage
="ubuntu:trusty"))
68 def getDockerCli(self
):
70 Helper to interact with local docker instance.
72 if self
.docker_cli
is None:
73 self
.docker_cli
= docker
.Client(
74 base_url
='unix://var/run/docker.sock')
75 return self
.docker_cli
77 def getContainernetContainers(self
):
79 List the containers managed by containernet
81 return self
.getDockerCli().containers(filters
={"label": "com.containernet"})
90 # make sure that all pending docker containers are killed
91 with
open(os
.devnull
, 'w') as devnull
:
93 "sudo docker rm -f $(sudo docker ps --filter 'label=com.containernet' -a -q)",