blob: bf0f4c08308f3985c4aa99745b4c7b98b12d681f [file] [log] [blame]
peusterm72f09882018-05-15 17:10:27 +02001# Copyright (c) 2015 SONATA-NFV and Paderborn University
2# ALL RIGHTS RESERVED.
3#
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
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
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.
15#
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
19# permission.
20#
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).
hadik3r237d3f52016-06-27 17:57:49 +020026import unittest
27import os
28import subprocess
29import docker
30from emuvim.dcemulator.net import DCNetwork
stevenvanrossem73efd192016-06-29 01:44:07 +020031from emuvim.api.rest.rest_api_endpoint import RestApiEndpoint
hadik3r237d3f52016-06-27 17:57:49 +020032from mininet.clean import cleanup
33from mininet.node import Controller
34
peusterm72f09882018-05-15 17:10:27 +020035
hadik3r237d3f52016-06-27 17:57:49 +020036class SimpleTestTopology(unittest.TestCase):
37 """
38 Helper class to do basic test setups.
39 s1 -- s2 -- s3 -- ... -- sN
40 """
41
42 def __init__(self, *args, **kwargs):
43 self.net = None
44 self.api = None
45 self.s = [] # list of switches
46 self.h = [] # list of hosts
47 self.d = [] # list of docker containers
48 self.dc = [] # list of data centers
49 self.docker_cli = None
50 super(SimpleTestTopology, self).__init__(*args, **kwargs)
51
52 def createNet(
53 self,
54 nswitches=0, ndatacenter=0, nhosts=0, ndockers=0,
55 autolinkswitches=False, controller=Controller, **kwargs):
56 """
57 Creates a Mininet instance and automatically adds some
58 nodes to it.
59
60 Attention, we should always use Mininet's default controller
61 for our tests. Only use other controllers if you want to test
62 specific controller functionality.
63 """
64 self.net = DCNetwork(controller=controller, **kwargs)
stevenvanrossemae588012017-06-06 10:33:19 +020065 self.api = RestApiEndpoint("127.0.0.1", 5001, self.net)
hadik3r237d3f52016-06-27 17:57:49 +020066 # add some switches
67 # start from s1 because ovs does not like to have dpid = 0
68 # and switch name-number is being used by mininet to set the dpid
peusterm72f09882018-05-15 17:10:27 +020069 for i in range(1, nswitches + 1):
hadik3r237d3f52016-06-27 17:57:49 +020070 self.s.append(self.net.addSwitch('s%d' % i))
71 # if specified, chain all switches
72 if autolinkswitches:
73 for i in range(0, len(self.s) - 1):
74 self.net.addLink(self.s[i], self.s[i + 1])
75 # add some data centers
76 for i in range(0, ndatacenter):
77 self.dc.append(
78 self.net.addDatacenter(
79 'datacenter%d' % i,
80 metadata={"unittest_dc": i}))
81 # connect data centers to the endpoint
82 for i in range(0, ndatacenter):
83 self.api.connectDatacenter(self.dc[i])
84 # add some hosts
85 for i in range(0, nhosts):
86 self.h.append(self.net.addHost('h%d' % i))
87 # add some dockers
88 for i in range(0, ndockers):
peusterm72f09882018-05-15 17:10:27 +020089 self.d.append(self.net.addDocker('d%d' %
90 i, dimage="ubuntu:trusty"))
hadik3r237d3f52016-06-27 17:57:49 +020091
92 def startApi(self):
93 self.api.start()
94
peusterm25074142017-08-18 10:10:27 +020095 def stopApi(self):
96 self.api.stop()
peusterm72f09882018-05-15 17:10:27 +020097
hadik3r237d3f52016-06-27 17:57:49 +020098 def startNet(self):
99 self.net.start()
100
101 def stopNet(self):
102 self.net.stop()
103
104 def getDockerCli(self):
105 """
106 Helper to interact with local docker instance.
107 """
108 if self.docker_cli is None:
stevenvanrosseme8d86282017-01-28 00:52:22 +0100109 self.docker_cli = docker.APIClient(
hadik3r237d3f52016-06-27 17:57:49 +0200110 base_url='unix://var/run/docker.sock')
111 return self.docker_cli
112
113 def getContainernetContainers(self):
114 """
115 List the containers managed by containernet
116 """
peusterm72f09882018-05-15 17:10:27 +0200117 return self.getDockerCli().containers(
118 filters={"label": "com.containernet"})
hadik3r237d3f52016-06-27 17:57:49 +0200119
120 @staticmethod
121 def setUp():
122 pass
123
124 @staticmethod
125 def tearDown():
126 cleanup()
127 # make sure that all pending docker containers are killed
128 with open(os.devnull, 'w') as devnull:
129 subprocess.call(
130 "sudo docker rm -f $(sudo docker ps --filter 'label=com.containernet' -a -q)",
131 stdout=devnull,
132 stderr=devnull,
peusterm25074142017-08-18 10:10:27 +0200133 shell=True)