blob: 0d994ca85fba1a0659260a51dec8455231501a68 [file] [log] [blame]
hadik3r237d3f52016-06-27 17:57:49 +02001"""
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"""
hadik3r237d3f52016-06-27 17:57:49 +020030Helper module that implements helpers for test implementations.
31"""
32
33import unittest
34import os
35import subprocess
36import docker
37from emuvim.dcemulator.net import DCNetwork
stevenvanrossem73efd192016-06-29 01:44:07 +020038from emuvim.api.rest.rest_api_endpoint import RestApiEndpoint
hadik3r237d3f52016-06-27 17:57:49 +020039from mininet.clean import cleanup
40from mininet.node import Controller
41
42class SimpleTestTopology(unittest.TestCase):
43 """
44 Helper class to do basic test setups.
45 s1 -- s2 -- s3 -- ... -- sN
46 """
47
48 def __init__(self, *args, **kwargs):
49 self.net = None
50 self.api = None
51 self.s = [] # list of switches
52 self.h = [] # list of hosts
53 self.d = [] # list of docker containers
54 self.dc = [] # list of data centers
55 self.docker_cli = None
56 super(SimpleTestTopology, self).__init__(*args, **kwargs)
57
58 def createNet(
59 self,
60 nswitches=0, ndatacenter=0, nhosts=0, ndockers=0,
61 autolinkswitches=False, controller=Controller, **kwargs):
62 """
63 Creates a Mininet instance and automatically adds some
64 nodes to it.
65
66 Attention, we should always use Mininet's default controller
67 for our tests. Only use other controllers if you want to test
68 specific controller functionality.
69 """
70 self.net = DCNetwork(controller=controller, **kwargs)
stevenvanrossemae588012017-06-06 10:33:19 +020071 self.api = RestApiEndpoint("127.0.0.1", 5001, self.net)
hadik3r237d3f52016-06-27 17:57:49 +020072 # add some switches
73 # start from s1 because ovs does not like to have dpid = 0
74 # and switch name-number is being used by mininet to set the dpid
75 for i in range(1, nswitches+1):
76 self.s.append(self.net.addSwitch('s%d' % i))
77 # if specified, chain all switches
78 if autolinkswitches:
79 for i in range(0, len(self.s) - 1):
80 self.net.addLink(self.s[i], self.s[i + 1])
81 # add some data centers
82 for i in range(0, ndatacenter):
83 self.dc.append(
84 self.net.addDatacenter(
85 'datacenter%d' % i,
86 metadata={"unittest_dc": i}))
87 # connect data centers to the endpoint
88 for i in range(0, ndatacenter):
89 self.api.connectDatacenter(self.dc[i])
90 # add some hosts
91 for i in range(0, nhosts):
92 self.h.append(self.net.addHost('h%d' % i))
93 # add some dockers
94 for i in range(0, ndockers):
95 self.d.append(self.net.addDocker('d%d' % i, dimage="ubuntu:trusty"))
96
97 def startApi(self):
98 self.api.start()
99
peusterm25074142017-08-18 10:10:27 +0200100 def stopApi(self):
101 self.api.stop()
102
hadik3r237d3f52016-06-27 17:57:49 +0200103 def startNet(self):
104 self.net.start()
105
106 def stopNet(self):
107 self.net.stop()
108
109 def getDockerCli(self):
110 """
111 Helper to interact with local docker instance.
112 """
113 if self.docker_cli is None:
stevenvanrosseme8d86282017-01-28 00:52:22 +0100114 self.docker_cli = docker.APIClient(
hadik3r237d3f52016-06-27 17:57:49 +0200115 base_url='unix://var/run/docker.sock')
116 return self.docker_cli
117
118 def getContainernetContainers(self):
119 """
120 List the containers managed by containernet
121 """
122 return self.getDockerCli().containers(filters={"label": "com.containernet"})
123
124 @staticmethod
125 def setUp():
126 pass
127
128 @staticmethod
129 def tearDown():
130 cleanup()
131 # make sure that all pending docker containers are killed
132 with open(os.devnull, 'w') as devnull:
133 subprocess.call(
134 "sudo docker rm -f $(sudo docker ps --filter 'label=com.containernet' -a -q)",
135 stdout=devnull,
136 stderr=devnull,
peusterm25074142017-08-18 10:10:27 +0200137 shell=True)