-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcheckTunnel.go
283 lines (244 loc) · 8.53 KB
/
checkTunnel.go
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package main
import (
"fmt"
"github.com/go-test/deep"
log "github.com/platinasystems/go-common/logs"
pcc "github.com/platinasystems/pcc-blackbox/lib"
"github.com/platinasystems/pcc-blackbox/models"
"strings"
"testing"
"time"
)
// Check if server is reaching the PCC through the tunnel
func checkInvaderTunnels(t *testing.T) {
res := models.InitTestResult(runID)
defer res.CheckTestAndSave(t, time.Now())
log.AuctaLogger.Info("TUNNEL: checking the tunnel addresses for the invaders")
if nodes, err := Pcc.GetInvaders(); err == nil {
for i := range *nodes {
node := (*nodes)[i]
nodeId := node.Id
checkEnvironmentForNode(t, &node) // Check for the environment address
if node, err := Pcc.GetNodeFromDB(nodeId); err == nil { // Check the DB
address := node.TunnelServerAddress
if len(strings.TrimSpace(address)) == 0 {
msg := fmt.Sprintf("The tunnel address for the invader %d:%s is blank", nodeId, node.Name)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
} else {
log.AuctaLogger.Info(fmt.Sprintf("The invader %d:%s is reaching the PCC through the tunnel %s", nodeId, node.Name, address))
}
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
}
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
}
// Check if server is reaching the PCC through the cluster-head tunnel
func checkServerTunnels(t *testing.T) {
res := models.InitTestResult(runID)
defer res.CheckTestAndSave(t, time.Now())
log.AuctaLogger.Info("TUNNEL: checking the tunnel addresses for the servers")
if invaders, err := Pcc.GetInvadersFromDB(); err == nil {
if nodes, err := Pcc.GetServers(); err == nil {
loopServer:
for i := range *nodes {
node := (*nodes)[i]
nodeId := node.Id
address := checkEnvironmentForNode(t, &node) // Check for the environment address
if node, err := Pcc.GetNodeFromDB(nodeId); err == nil { // Check the DB. The address should be blank
address := node.TunnelServerAddress
if len(strings.TrimSpace(address)) != 0 {
msg := fmt.Sprintf("The tunnel address for the server %d:%s should be blank instead of %s", nodeId, node.Name, node.TunnelServerAddress)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
for j := range *invaders { // Check if the address is associated to one Invader
invader := (*invaders)[j]
if strings.Compare(address, invader.Host) == 0 {
log.AuctaLogger.Info(fmt.Sprintf("The node %d:%s is reaching the pcc through the invader %d:%s:%s", nodeId, node.Name, invader.Id, invader.Name, invader.Host))
continue loopServer
}
}
msg := fmt.Sprintf("Unable to find the invader associated to the server %d:%s", nodeId, node.Name)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
}
// Check data received from the environment endpoint
func checkEnvironmentForNode(t *testing.T, node *pcc.NodeDetailed) (address string) {
res := models.InitTestResult(runID)
defer res.CheckTestAndSave(t, time.Now())
log.AuctaLogger.Info(fmt.Sprintf("Tunnel: checking the tunnel address for the node %d:%s:%s", node.Id, node.Name, node.Host))
if defaultEnv, err := Pcc.GetEnvironment(nil); err == nil {
defaultAddress := defaultEnv["servicePublicHost"]
nodeId := node.Id
if env, err := Pcc.GetEnvironment(&nodeId); err == nil { // Check the environment
hAddress := env["servicePublicHost"]
if diff := deep.Equal(defaultAddress, hAddress); diff == nil {
msg := fmt.Sprintf("The tunnel address should be different from the PCC address %v", defaultAddress)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
} else {
log.AuctaLogger.Info(fmt.Sprintf("The node %d:%s is getting the address %v", nodeId, node.Name, hAddress))
address = fmt.Sprintf("%v", hAddress)
}
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
return
}
// Check if invader is reaching the PCC through the tunnel
func checkTunnelConnection(t *testing.T) {
res := models.InitTestResult(runID)
defer res.CheckTestAndSave(t, time.Now())
log.AuctaLogger.Info("TUNNEL: checking invaders connection")
if nodes, err := Pcc.GetInvadersFromDB(); err == nil {
for i := range *nodes {
node := (*nodes)[i]
if err = tunnelPing(&node, true); err != nil {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
}
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
}
func tunnelPing(node *pcc.NodeDetailed, logs bool) error {
nodeId := node.Id
if stdout, stderr, err := Pcc.SSHHandler().Run(node.Host, fmt.Sprintf("ping -c 3 %s", node.TunnelServerAddress)); err == nil {
if logs {
log.AuctaLogger.Info(fmt.Sprintf("The node %d:%s is pinging the address %v%s", nodeId, node.Name, node.TunnelServerAddress, stdout))
}
return nil
} else {
if logs {
log.AuctaLogger.Error(fmt.Sprintf("Error pinging from the node %d:%s %v%s", nodeId, node.Name, err, stderr))
}
return err
}
}
// Check if invader is running the iptables rules
func checkTunnelForwardingRules(t *testing.T) {
res := models.InitTestResult(runID)
defer res.CheckTestAndSave(t, time.Now())
log.AuctaLogger.Info("TUNNEL: checking forwarding rules")
if nodes, err := Pcc.GetInvadersFromDB(); err == nil {
for i := range *nodes {
node := (*nodes)[i]
nodeId := node.Id
if stdout, stderr, err := Pcc.SSHHandler().Run(node.Host, "sudo iptables --list-rules PREROUTING -t nat"); err == nil {
lines := strings.Split(stdout, `-A`)
loopPort:
for _, port := range []string{"8081", "9092", "9999"} {
rule := fmt.Sprintf("PREROUTING -p tcp -m tcp --dport %s -j DNAT --to-destination %s:%s", port, node.TunnelServerAddress, port)
for _, line := range lines {
if strings.Compare(strings.TrimSpace(line), rule) == 0 {
log.AuctaLogger.Info(fmt.Sprintf("Found the forward rule for port %s on invader %d:%s", port, nodeId, node.Name))
continue loopPort
}
}
msg := fmt.Sprintf("Unable to find the forward rule for port %s on invader %d:%s. Rules are:%s", port, nodeId, node.Name, stdout)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
} else {
msg := fmt.Sprintf("Error getting iptables tule from the node %d:%s %v%s", nodeId, node.Name, err, stderr)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
}
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
}
// Check if the PCC is able to restore the tunnel
func checkTunnelRestore(t *testing.T) {
res := models.InitTestResult(runID)
defer res.CheckTestAndSave(t, time.Now())
log.AuctaLogger.Info("TUNNEL: checking the restore for the invaders")
if nodes, err := Pcc.GetInvadersFromDB(); err == nil {
for i := range *nodes {
node := (*nodes)[i]
nodeId := node.Id
tun := fmt.Sprintf("tun%d", node.Id)
if _, _, err := Pcc.SSHHandler().Run(node.Host, fmt.Sprintf("sudo ip link delete tun%d", node.Id)); err == nil {
fmt.Println(fmt.Sprintf("The node %d:%s tun device %s has been removed. Waiting for the restore", nodeId, node.Name, tun))
restoreLoop:
for i := 1; i <= 10; i++ {
time.Sleep(time.Second * time.Duration(15))
if err = tunnelPing(&node, false); err == nil {
log.AuctaLogger.Warn(fmt.Sprintf("The PCC restored the tunnel %s on the invader %d:%s", tun, nodeId, node.Name))
break restoreLoop
}
}
if err != nil {
msg := fmt.Sprintf("The PCC was not able to restore the tunnel for the invader %d:%s", nodeId, node.Name)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
}
} else {
msg := fmt.Sprintf("%v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
}