forked from MFALHI/netplugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcTrigger.py
More file actions
253 lines (202 loc) · 9.21 KB
/
Copy pathtcTrigger.py
File metadata and controls
253 lines (202 loc) · 9.21 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import api.tutils
import time
import sys
import api.objmodel
import random
# Check ping between all containers in the same network
def checkPingContainersInNetworks(containers, networks):
cntrIpList = []
# Read all IP addresses
for cnt in containers:
cntrIp = cnt.getIpAddr()
cntrIpList.append(cntrIp)
nidx = random.randint(0, (len(networks) - 1))
# Test ping from each container to other container in same network
# We expect ping success for each container in same network
for cidx, cnt in enumerate(containers):
for ipidx, ipAddr in enumerate(cntrIpList):
if (ipidx % len(networks)) == (cidx % len(networks)) and (cidx % len(networks) == nidx):
cnt.checkPing(ipAddr)
# Check full mesh connection to all containers within a group
def checkConnectionsWithinGroup(containers, groups, port, success):
cntrIpList = []
# Read all IP addresses
for cnt in containers:
cntrIp = cnt.getIpAddr()
cntrIpList.append(cntrIp)
gidx = random.randint(0, (len(groups) - 1))
# Check connection to all containers
for cidx, cnt in enumerate(containers):
for aidx, ipAddr in enumerate(cntrIpList):
if (cidx % len(groups)) == (aidx % len(groups)) and (cidx % len(groups) == gidx):
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 connection to all containers between two neighboring groups
def checkConnectionsAcrossGroup(containers, groups, port, success):
cntrIpList = []
# Read all IP addresses
for cnt in containers:
cntrIp = cnt.getIpAddr()
cntrIpList.append(cntrIp)
gidx = random.randint(0, (len(groups) - 1))
# Check connection to all containers
for cidx, cnt in enumerate(containers):
for aidx, ipAddr in enumerate(cntrIpList):
cgrp = groups[cidx % len(groups)]
agrp = groups[aidx % len(groups)]
if ((cidx + 1) % len(groups)) == (aidx % len(groups)) and cgrp.networkName == agrp.networkName and (cidx % len(groups) == gidx):
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
# Checks all datapth connections
def checkAllConnection(testbed, netContainers, networks, grpContainers, groups):
# Check ping between containes in same network
checkPingContainersInNetworks(netContainers, networks)
# Check port 8000 and 8001 connecting succeedes within group
if checkConnectionsWithinGroup(grpContainers, groups, 8000, True) != True:
api.tutils.exit("Connection failed")
if checkConnectionsWithinGroup(grpContainers, groups, 8001, True) != True:
api.tutils.exit("Connection failed")
if checkConnectionsAcrossGroup(grpContainers, groups, 8000, True) != True:
api.tutils.exit("Connection failed")
if checkConnectionsAcrossGroup(grpContainers, groups, 8001, False) != False:
api.tutils.exit("Connection succeeded while expecting to fail")
# Check for errors
testbed.chekForNetpluginErrors()
# remove all containers
def removeAllContainers(netContainers, grpContainers):
for cnt in netContainers:
cnt.remove()
for cnt in grpContainers:
cnt.remove()
# Start all containers
def startAllContainers(testbed, netNames, groupNames):
# start containers in each network
numCntr = len(netNames) * testbed.numNodes() * 2
netContainers = testbed.runContainersInNetworks(numCntr, netNames)
# start containers in each group
numCntr = len(groupNames) * testbed.numNodes() * 2
grpContainers = testbed.runContainersInGroups(numCntr, groupNames)
# start netcat listeners on epg containers
testbed.startListeners(grpContainers, [8000, 8001])
# Return newly created containers
return netContainers, grpContainers
# Trigger netplugin restart
def triggerNetpluginRestart(testbed):
for node in testbed.nodes:
api.tutils.info("Restarting netplugin on " + node.hostname)
node.stopNetplugin()
time.sleep(1)
# Move old log file
currTime = time.strftime("%H:%M:%S", time.localtime())
node.runCmd("mv /tmp/netplugin.log /tmp/netplugin-" + currTime + ".log")
node.startNetplugin()
# Wait a little
time.sleep(5)
# Trigger netmaster restart
def triggerNetmasterRestart(testbed):
api.tutils.info("Restarting netmaster on " + testbed.nodes[0].hostname)
testbed.nodes[0].stopNetmaster()
time.sleep(1)
currTime = time.strftime("%H:%M:%S", time.localtime())
testbed.nodes[0].runCmd("mv /tmp/netmaster.log /tmp/netmaster-" + currTime + ".log")
testbed.nodes[0].startNetmaster()
# Wait a little
time.sleep(10)
# Trigger removal/add of all containers
def triggerRestartContainers(testbed, netContainers, grpContainers, netNames, groupNames):
# Remove all containers
removeAllContainers(netContainers, grpContainers)
# Start all containers
return startAllContainers(testbed, netNames, groupNames)
# Tests multiple triggers and verifies datapath after each trigger
def testMultiTrigger(testbed, numIter, numTenants=1, numNetworksPerTenant=1, numGroupsPerNetwork=2):
# create tenants and networks
tenants = []
networks = []
netNames = []
groups = []
groupNames = []
policies = []
# Setup tenants, networks and policies
for tidx in range(numTenants):
tenantName = "tenant" + str(tidx)
tenant = api.objmodel.tenant(tenantName)
tenants.append(tenant)
# Create multiple networks per tenant
for nidx in range(numNetworksPerTenant):
netName = "net" + str(nidx)
subnet = "10." + str(tidx) + "." + str(nidx) + ".0/24"
gateway = "10." + str(tidx) + "." + str(nidx) + ".254"
pktTag = 1001 + (tidx * numNetworksPerTenant) + nidx
network = tenant.newNetwork(netName, pktTag=pktTag, subnet=subnet, gateway=gateway, encap="vxlan")
networks.append(network)
netNames.append(netName + "/" + tenantName)
# Create multiple EPGs and associated policies
for pidx in range(numGroupsPerNetwork):
srvName = "srv" + str(pidx)
policyName = "srv" + str(pidx) + "_" + netName
# Create policy for each service
policy = tenant.newPolicy(policyName)
policies.append(policy)
# create default deny Rule
policy.addRule('1', direction="in", protocol="tcp", action="deny")
# Create allow port 8000 Rule
policy.addRule('2', direction="in", priority=100, protocol="tcp", port=8000, action="accept")
# create the EPG
group = network.newGroup(srvName, policies=[policyName])
groups.append(group)
groupNames.append(srvName + "." + netName + "/" + tenantName)
# Create allow from this epg rule
policy.addRule('3', direction="in", priority=100, endpointGroup=srvName, network=netName, protocol="tcp", port=8001, action="accept")
# start containers in each network and each group
netContainers, grpContainers = startAllContainers(testbed, netNames, groupNames)
# Check all datapaths
checkAllConnection(testbed, netContainers, networks, grpContainers, groups)
# Run a random trigger and verify all connections
for iter in range(numIter):
triggerIdx = random.randint(1, 3)
# Run the specific trigger
if triggerIdx == 1:
triggerName = "triggerNetpluginRestart"
api.tutils.log("Performing trigger " + triggerName)
# restart netplugin
triggerNetpluginRestart(testbed)
elif triggerIdx == 2:
triggerName = "triggerNetmasterRestart"
api.tutils.log("Performing trigger " + triggerName)
# restart netmaster
triggerNetmasterRestart(testbed)
elif triggerIdx == 3:
triggerName = "triggerRestartContainers"
api.tutils.log("Performing trigger " + triggerName)
# restart containers
netContainers, grpContainers = triggerRestartContainers(testbed, netContainers, grpContainers, netNames, groupNames)
else:
api.tutils.exit("Unexpected value")
api.tutils.log("Performed trigger " + triggerName + " verifying datapath")
# Check all datapaths after each trigger
checkAllConnection(testbed, netContainers, networks, grpContainers, groups)
api.tutils.info("testMultiTrigger Iteration " + str(iter) + " trigger " + triggerName + " Passed")
# Cleanup all containers
removeAllContainers(netContainers, grpContainers)
# Cleanup all state
for group in groups:
group.delete()
for policy in policies:
policy.delete()
for net in networks:
net.delete()
for tenant in tenants:
tenant.delete()
# Check for cleanup errors
testbed.chekForNetpluginErrors()
# Done
api.tutils.info("testMultiTrigger PASSED")