blob: 397e2a184c811336c8d5e0a3d09eb0583860d0f7 [file] [log] [blame]
peusterm41006b72016-03-12 12:13:06 +01001"""
peustermc89ba382016-07-08 13:46:32 +02002Copyright (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"""
peusterm41006b72016-03-12 12:13:06 +010030Helper module that implements helpers for test implementations.
31"""
32
33import unittest
34import os
35import subprocess
36import docker
37from emuvim.dcemulator.net import DCNetwork
38from mininet.clean import cleanup
peustermef6629e2016-03-14 17:21:56 +010039from mininet.node import Controller
peusterm41006b72016-03-12 12:13:06 +010040
41class SimpleTestTopology(unittest.TestCase):
42 """
43 Helper class to do basic test setups.
44 s1 -- s2 -- s3 -- ... -- sN
45 """
46
47 def __init__(self, *args, **kwargs):
48 self.net = None
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)
55
56 def createNet(
57 self,
58 nswitches=0, ndatacenter=0, nhosts=0, ndockers=0,
stevenvanrossem7cd3c252016-05-11 22:55:15 +020059 autolinkswitches=False, controller=Controller, **kwargs):
peusterm41006b72016-03-12 12:13:06 +010060 """
61 Creates a Mininet instance and automatically adds some
62 nodes to it.
peustermde14f332016-03-15 16:14:21 +010063
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.
peusterm41006b72016-03-12 12:13:06 +010067 """
stevenvanrossem7cd3c252016-05-11 22:55:15 +020068 self.net = DCNetwork(controller=controller, **kwargs)
peusterm41006b72016-03-12 12:13:06 +010069
70 # add some switches
stevenvanrosseme3e034e2016-05-11 23:51:06 +020071 # 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):
peusterm41006b72016-03-12 12:13:06 +010074 self.s.append(self.net.addSwitch('s%d' % i))
75 # if specified, chain all switches
76 if autolinkswitches:
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):
81 self.dc.append(
82 self.net.addDatacenter(
83 'datacenter%d' % i,
84 metadata={"unittest_dc": i}))
85 # add some hosts
86 for i in range(0, nhosts):
87 self.h.append(self.net.addHost('h%d' % i))
88 # add some dockers
89 for i in range(0, ndockers):
peusterm0dc3ae02016-04-27 09:33:28 +020090 self.d.append(self.net.addDocker('d%d' % i, dimage="ubuntu:trusty"))
peusterm41006b72016-03-12 12:13:06 +010091
92 def startNet(self):
93 self.net.start()
94
95 def stopNet(self):
96 self.net.stop()
97
98 def getDockerCli(self):
99 """
100 Helper to interact with local docker instance.
101 """
102 if self.docker_cli is None:
stevenvanrosseme8d86282017-01-28 00:52:22 +0100103 self.docker_cli = docker.APIClient(
peusterm41006b72016-03-12 12:13:06 +0100104 base_url='unix://var/run/docker.sock')
105 return self.docker_cli
106
peusterm5877ea22016-05-11 13:44:59 +0200107 def getContainernetContainers(self):
peusterm41006b72016-03-12 12:13:06 +0100108 """
peusterm5877ea22016-05-11 13:44:59 +0200109 List the containers managed by containernet
peusterm41006b72016-03-12 12:13:06 +0100110 """
peusterm5877ea22016-05-11 13:44:59 +0200111 return self.getDockerCli().containers(filters={"label": "com.containernet"})
peusterm41006b72016-03-12 12:13:06 +0100112
113 @staticmethod
114 def setUp():
115 pass
116
117 @staticmethod
118 def tearDown():
119 cleanup()
120 # make sure that all pending docker containers are killed
121 with open(os.devnull, 'w') as devnull:
122 subprocess.call(
peusterm5877ea22016-05-11 13:44:59 +0200123 "sudo docker rm -f $(sudo docker ps --filter 'label=com.containernet' -a -q)",
peusterm41006b72016-03-12 12:13:06 +0100124 stdout=devnull,
125 stderr=devnull,
126 shell=True)