forked from MFALHI/netplugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtbed.py
More file actions
212 lines (175 loc) · 6.97 KB
/
Copy pathtbed.py
File metadata and controls
212 lines (175 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import tnode
import time
import sys
import tutils
# This class represents a testbed i.e, collection of nodes
class Testbed:
# Initialize a testbed
def __init__(self, addrList, username='vagrant', password='vagrant', binpath='/opt/gopath/bin'):
self.nodes = []
self.failOnError = True
# Basic error checking
if len(addrList) < 1:
print "Empty address list"
sys.exit(1)
# Create nodes
for addr in addrList:
node = tnode.Node(addr, username, password, binpath)
self.nodes.append(node)
# Cleanup all state before we can start
for node in self.nodes:
node.cleanupContainers()
node.stopNetmaster()
node.cleanupDockerNetwork()
node.stopNetplugin()
# cleanup master and slave state
for node in self.nodes:
node.cleanupMaster()
node.cleanupSlave()
# Start netplugin on all nodes
for node in self.nodes:
print "Starting netplugin on " + node.addr
node.startNetplugin()
# Wait few seconds before starting netmaster
time.sleep(3)
# Start netmaster in the end
print "Starting netmaster"
self.nodes[0].startNetmaster()
# Cleanup a testbed once test is done
def cleanup(self):
# Cleanup each node
for node in self.nodes:
print "Stopping containers on " + node.addr
node.cleanupContainers()
# Stop netmaster and remove networks
self.nodes[0].stopNetmaster()
self.nodes[0].cleanupDockerNetwork()
for node in self.nodes:
print "Cleaning up node " + node.addr
node.stopNetplugin()
node.cleanupSlave()
# cleanup master
print "Cleaning up master"
self.nodes[0].cleanupMaster()
# Number of nodes in the testbed
def numNodes(self):
return len(self.nodes)
# Start containers on the testbed
def runContainers(self, numContainer, withService=False):
containers = []
# Start the containers
for cntIdx in range(numContainer):
nodeIdx = cntIdx % self.numNodes()
srvName = "srv" + str(cntIdx)
if withService:
cnt = self.nodes[nodeIdx].runContainer("ubuntu", networkName="private", serviceName=srvName, cntName=srvName)
else:
cnt = self.nodes[nodeIdx].runContainer("ubuntu", networkName="private", cntName=srvName)
containers.append(cnt)
return containers
# Start containers in a specific service
def runContainersInService(self, numContainer, serviceName, networkName="private"):
containers = []
# Start the containers
for cntIdx in range(numContainer):
nodeIdx = cntIdx % self.numNodes()
cnt = self.nodes[nodeIdx].runContainer("ubuntu", networkName=networkName, serviceName=serviceName)
containers.append(cnt)
return containers
# Start containers in list of networks
def runContainersInNetworks(self, numContainer, networks):
containers = []
# Start the containers
for cntIdx in range(numContainer):
nodeIdx = cntIdx % self.numNodes()
netIdx = cntIdx % len(networks)
cnt = self.nodes[nodeIdx].runContainer("ubuntu", networkName=networks[netIdx])
containers.append(cnt)
return containers
# Start containers in list of groups
def runContainersInGroups(self, numContainer, groups):
containers = []
# Start the containers
for cntIdx in range(numContainer):
nodeIdx = cntIdx % self.numNodes()
gidx = cntIdx % len(groups)
netName = groups[gidx].split(".")[1]
svcName = groups[gidx].split('.')[0]
cnt = self.nodes[nodeIdx].runContainer("ubuntu", networkName=netName, serviceName=svcName)
containers.append(cnt)
return containers
# Remove containers
def removeContainers(self, containers):
# remove containers
for cnt in containers:
cnt.remove()
# start all netcast listeners
def startListeners(self, containers, ports):
# start netcast listeners
for cnt in containers:
for port in ports:
cnt.startListener(port)
def stopListeners(self, containers):
# stop netcast listeners
for cnt in containers:
cnt.stopListener()
def pingTest(self, containers):
cntrIpList = []
# Read all IP addresses
for cnt in containers:
cntrIp = cnt.getIpAddr()
cntrIpList.append(cntrIp)
# Test ping to all other containers from one container in each node
for cidx, cnt in enumerate(containers):
if (cidx < self.numNodes()):
for ipAddr in cntrIpList:
cnt.checkPing(ipAddr)
return True
# Check full mesh connection to all containers
def checkConnections(self, containers, port, success):
cntrIpList = []
# Read all IP addresses
for cnt in containers:
cntrIp = cnt.getIpAddr()
cntrIpList.append(cntrIp)
# Check connection to all containers from one container on each node
for cidx, cnt in enumerate(containers):
if (cidx < self.numNodes()):
for aidx, ipAddr in enumerate(cntrIpList):
if cidx != aidx:
ret = cnt.checkConnection(ipAddr, port)
# If connection status is not what we were expecting, we are done.
if ret != success:
return ret
# Return
return success
# Check bipartite connection between two list of containers
def checkConnectionPair(self, fromContainers, toContainers, port, success):
toIpList = []
# Read all IP addresses
for cnt in toContainers:
cntrIp = cnt.getIpAddr()
toIpList.append(cntrIp)
# Check connection from each container
for cidx, cnt in enumerate(fromContainers):
for aidx, ipAddr in enumerate(toIpList):
if cnt.getIpAddr() != ipAddr:
ret = cnt.checkConnection(ipAddr, port)
# If connection status is not what we were expecting, we are done.
if ret != success:
return ret
# Return
return success
# Look for any error logs on all nodes
def chekForNetpluginErrors(self):
for node in self.nodes:
ret = node.chekForNetpluginErrors()
if ret == False and self.failOnError:
tutils.exit("Errors in log file")
# Print netplugin errors if any and exit
def errExit(self, str):
# print erros from netplugin log file
for node in self.nodes:
node.chekForNetpluginErrors()
# exit the script
tutils.exit(str)