forked from fdio-stack/netplugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer.py
More file actions
144 lines (120 loc) · 5.54 KB
/
Copy pathcontainer.py
File metadata and controls
144 lines (120 loc) · 5.54 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
import tutils
import time
# This class represents a docker Container
class Container:
def __init__(self, node, cid, cntName=""):
self.node = node
self.cid = cid
self.cntName = cntName
# Read interface ip
out, err, exitCode = self.execCmd('ip addr show dev eth0 | grep "inet "')
if err != [] or exitCode != 0:
print "Error during docker exec"
print err
tutils.exit("Failed to get IP address")
self.eth0Addr = out[0].strip().split(' ')[1].split('/')[0]
# Return container identifier
def myId(self):
if self.cntName != "":
return self.node.hostname + "(" + self.node.addr + ")/" + self.cntName
else:
return self.node.hostname + "(" + self.node.addr + ")/" + self.cid
def errorExit(self, str, out, err):
print str + " " + self.myId()
print "Output: " + ''.join(out)
print "Error: " + ''.join(err)
tutils.exit(str)
# Execute a command inside a container
def execCmd(self, cmd):
out, err, exitCode = self.node.runCmd("docker exec " + self.cid + " " + cmd)
# Retry failures once to workaround docker issue #15713
if out == [] and err == [] and exitCode == 255:
time.sleep(1)
return self.node.runCmd("docker exec " + self.cid + " " + cmd)
return out, err, exitCode
# Execute a command inside a container in backgroud
def execBgndCmd(self, cmd):
out, err, exitCode = self.node.runCmd("docker exec -d " + self.cid + " " + cmd)
# Retry failures once to workaround docker issue #15713
if out == [] and err == [] and exitCode == 255:
time.sleep(1)
return self.node.runCmd("docker exec -d " + self.cid + " " + cmd)
return out, err, exitCode
# start the container
def start(self):
out, err, exitCode = self.node.runCmd("docker start " + self.cid)
if exitCode != 0:
self.errorExit("Error starting container", out, err)
# start the container
def stop(self):
out, err, exitCode = self.node.runCmd("docker stop " + self.cid)
if exitCode != 0:
self.errorExit("Error stopping container", out, err)
# remove the container
def remove(self):
# force remove the container
out, err, exitCode = self.node.runCmd("docker rm -f " + self.cid)
if exitCode != 0:
self.errorExit("Error removing container", out, err)
# Get IP address of the container
def getIpAddr(self, intfName="eth0"):
if intfName == "eth0":
return self.eth0Addr
# Read interface ip
out, err, exitCode = self.execCmd("ifconfig " + intfName)
if err != [] or exitCode != 0:
print "Error during docker exec"
print err
tutils.exit("Failed to get IP address")
return out[1].split('addr')[1].split(' ')[0].split(':')[1]
def checkPing(self, ipAddr):
tutils.log("Checking ping from " + self.myId() + " to " + ipAddr)
out, err, exitCode = self.execCmd("ping -c 5 -i 0.2 " + ipAddr)
if err != [] or exitCode != 0:
print "ping exit(" + str(exitCode) + ") Output: " + ''.join(out)
print "Error during ping"
print err
tutils.exit("Ping failed")
# Check if ping succeded
pingOutput = ''.join(out)
if "0 received, 100% packet loss" in pingOutput:
print "Ping failed. Output: " + pingOutput
tutils.exit("Ping failed")
tutils.log("ping from " + self.myId() + " to " + ipAddr + " Successful!")
return True
def checkPingFailure(self, ipAddr):
tutils.log("Checking ping failure from " + self.myId() + " to " + ipAddr)
out, err, exitCode = self.execCmd("ping -c 5 -i 0.5 -W 1 " + ipAddr)
pingOutput = ''.join(out)
if err != [] or exitCode != 0:
print "ping exit(" + str(exitCode) + ") Output: " + pingOutput
tutils.log("Ping failed as expected.")
return True
# Check if ping succeded
if "0 received, 100% packet loss" in pingOutput:
tutils.log("Ping failed as expected. Output: " + pingOutput)
return True
tutils.log("ping from " + self.myId() + " to " + ipAddr + " succeeded while expecting failure")
tutils.exit("Ping succeeded while expecting failure")
# Start netcast listener on container
def startListener(self, port, protocol="tcp"):
protoStr = "-u " if protocol == "udp" else " "
out, err, exitCode = self.execBgndCmd("netcat -k -l " + protoStr + "-p " + str(port))
if exitCode != 0:
self.errorExit("Error starting netcat", out, err)
def stopListener(self):
out, err, exitCode = self.execCmd("pkill netcat")
if exitCode != 0:
tutils.info("Error stopping netcat".join(out).join(err))
# Check if this container can connect to destination port
def checkConnection(self, ipAddr, port, protocol="tcp"):
tutils.log("Checking connection from " + self.myId() + " to " + ipAddr + " port " + str(port))
protoStr = "-u " if protocol == "udp" else " "
out, err, exitCode = self.execCmd("netcat -z -n -v -w 1 " + protoStr + ipAddr + " " + str(port))
print "checkConnection Output(" + str(exitCode) + "): " + ''.join(out)
print "checkConnection Err: " + ''.join(err)
ncOut = ''.join(out) + ''.join(err)
if "succeeded" in ncOut and exitCode == 0:
return True
else:
return False