-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.go
324 lines (300 loc) · 7.73 KB
/
utils.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package main
import (
"errors"
"fmt"
log "github.com/platinasystems/go-common/logs"
pcc "github.com/platinasystems/pcc-blackbox/lib"
"github.com/platinasystems/pcc-blackbox/models"
"os"
"testing"
"time"
)
func IsInvader(node *pcc.NodeDetailed) bool {
for i := 0; i < len(Env.Invaders); i++ {
if Env.Invaders[i].HostIp == node.Host {
return true
}
}
return false
}
func ConvertToMillis(startTime time.Time) uint64 {
return uint64(startTime.UnixNano()) / uint64(time.Millisecond)
}
func idInSlice(idToFind uint64, list []uint64) bool {
for _, str := range list {
if str == idToFind {
return true
}
}
return false
}
func getNodeFromEnv(id uint64) *node {
for i := range Env.Invaders {
node := Env.Invaders[i].node
if node.Id == id {
return &node
}
}
for i := range Env.Servers {
node := Env.Servers[i].node
if node.Id == id {
return &node
}
}
return nil
}
func checkError(t *testing.T, res *models.TestResult, err error) {
if err != nil {
msg := err.Error()
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
t.FailNow()
}
}
func CheckDependencies(t *testing.T, res *models.TestResult, dep ...func() error) {
for _, fn := range dep {
if err := fn(); err != nil {
log.AuctaLogger.Errorf("%s", err.Error())
res.SetTestSkipped(err.Error())
t.SkipNow()
}
}
}
func CheckInvaders(nodes *[]pcc.NodeDetailed) (err error) {
for _, node := range Env.Invaders {
ok := false
for i := 0; i < len(*nodes); i++ {
remoteNode := (*nodes)[i]
if node.HostIp == remoteNode.Host && remoteNode.Status == "OK" && remoteNode.NodeStatus.ConnectionStatus == "online" {
ok = true
break
}
}
if !ok {
msg := fmt.Sprintf("%s not found in PCC or not online", node.HostIp)
err = errors.New(msg)
return
}
}
return
}
func CountInvadersMatching(nodes *[]pcc.NodeDetailed) (numInvaders int) {
for _, node := range Env.Invaders {
for i := 0; i < len(*nodes); i++ {
remoteNode := (*nodes)[i]
if node.HostIp == remoteNode.Host && remoteNode.Status == "OK" && remoteNode.NodeStatus.ConnectionStatus == "online" {
numInvaders++
}
}
}
return
}
func CheckServers(nodes *[]pcc.NodeDetailed) (err error) {
for _, node := range Env.Servers {
ok := false
for i := 0; i < len(*nodes); i++ {
remoteNode := (*nodes)[i]
if node.HostIp == remoteNode.Host && remoteNode.Status == "OK" && remoteNode.NodeStatus.ConnectionStatus == "online" {
ok = true
break
}
}
if !ok {
msg := fmt.Sprintf("%s not found in PCC or not online", node.HostIp)
err = errors.New(msg)
return
}
}
return
}
func CountServersMatching(nodes *[]pcc.NodeDetailed) (numServers int) {
for _, node := range Env.Servers {
for i := 0; i < len(*nodes); i++ {
remoteNode := (*nodes)[i]
if node.HostIp == remoteNode.Host && remoteNode.Status == "OK" && remoteNode.NodeStatus.ConnectionStatus == "online" {
numServers++
}
}
}
return
}
func CheckNodes() (err error) {
var nodes *[]pcc.NodeDetailed
if nodes, err = Pcc.GetNodes(); err == nil {
if err = CheckInvaders(nodes); err != nil {
return
}
if err = CheckServers(nodes); err != nil {
return
}
} else {
msg := fmt.Sprintf("Error getting nodes: %v", err)
err = errors.New(msg)
return
}
return
}
func CheckNumNodes(numNodes int) (err error) {
var nodes *[]pcc.NodeDetailed
if nodes, err = Pcc.GetNodes(); err == nil {
if remoteNumNodes := len(*nodes); remoteNumNodes < numNodes {
msg := fmt.Sprintf("%d nodes present,but %d nodes required", remoteNumNodes, numNodes)
err = errors.New(msg)
return
}
numInvaders := CountInvadersMatching(nodes)
numServers := CountServersMatching(nodes)
if (numServers + numInvaders) < numNodes {
msg := fmt.Sprintf("%d matching nodes ,but %d nodes required", numServers+numInvaders, numNodes)
err = errors.New(msg)
return
}
} else {
msg := fmt.Sprintf("Error getting nodes: %v", err)
err = errors.New(msg)
return
}
return
}
func CheckNodesNetCluster() (err error) {
var envCluster netCluster
for _, cluster := range Env.NetCluster {
if cluster.Name == netClusterName {
envCluster = cluster
break
}
}
if envCluster.Name == "" {
err = errors.New("Can't find a Network Cluster with the provided ClusterName in the env file")
return
}
for _, envNode := range envCluster.Nodes {
nodeId, ok := Pcc.FindNodeAddress(envNode.IpAddr)
if ok != nil {
err = ok
return
}
node, ok := Pcc.GetNode(nodeId)
if ok != nil {
err = ok
return
}
if node.NodeStatus.ConnectionStatus != "online" {
msg := fmt.Sprintf("%s connection status not online", node.Host)
err = errors.New(msg)
return
}
}
return
}
func CheckCephClusterExists() (err error) {
cephCluster, ok := Pcc.GetCephCluster(Env.CephConfiguration.ClusterName)
if ok != nil {
err = errors.New("Can't find a CephCluster with the provided ClusterName")
return
}
if cephCluster.CephClusterConfig.ClusterNetwork != Env.CephConfiguration.ClusterNetwork ||
cephCluster.CephClusterConfig.PublicNetwork != Env.CephConfiguration.PublicNetwork ||
len(cephCluster.Nodes) != Env.CephConfiguration.NumberOfNodes {
err = errors.New("The CephCluster does not match the specified parameters")
return
}
if status, _ := Pcc.GetCephHealthStatusById(cephCluster.Id); status.Health == "HEALTH_ERR" {
err = errors.New("The CephCluster status is not OK")
return
}
return
}
func CheckCephClusterRGWExists() (err error) {
cephCluster, ok := Pcc.GetCephCluster(Env.RGWConfiguration.ClusterName)
if ok != nil {
err = errors.New("Can't find a CephCluster with the provided ClusterName")
return
}
if status, _ := Pcc.GetCephHealthStatusById(cephCluster.Id); status.Health == "HEALTH_ERR" {
err = errors.New("The CephCluster status is not OK")
return
}
return
}
func CheckK8sClusterExists() (err error) {
k8sCluster, ok := Pcc.GetKubernetesClusterByName(k8sname)
if ok != nil {
err = errors.New("Can't find a k8sCluster with the provided ClusterName")
return
}
if k8sCluster.HealthStatus != "good" {
err = errors.New("The K8sCluster status is not OK")
return
}
return
}
func CheckNetClusterExists() (err error) {
networkCluster, ok := Pcc.FindNetClusterName(netClusterName)
if ok != nil {
err = errors.New("Can't find a Network Cluster with the provided ClusterName")
return
}
var envCluster netCluster
for _, cluster := range Env.NetCluster {
if cluster.Name == netClusterName {
envCluster = cluster
break
}
}
if envCluster.Name == "" {
err = errors.New("Can't find a Network Cluster with the provided ClusterName in the env file")
return
}
controlSubnet, ok := Pcc.FindSubnetObj(envCluster.ControlCIDR)
if ok != nil {
err = ok
return
}
dataSubnet, ok := Pcc.FindSubnetObj(envCluster.DataCIDR)
if ok != nil {
err = ok
return
}
if string(controlSubnet.Subnet) != networkCluster.ControlCIDR ||
string(dataSubnet.Subnet) != networkCluster.DataCIDR ||
envCluster.IgwPolicy != networkCluster.IgwPolicy {
err = errors.New("The network Cluster does not match the specified parameters")
return
}
for _, envNode := range envCluster.Nodes {
found := false
for _, node := range networkCluster.Nodes {
remoteNode, ok := Pcc.GetNode(node.NodeId)
if ok != nil {
err = ok
return
}
if envNode.IpAddr == remoteNode.Host {
found = true
break
}
}
if !found {
msg := fmt.Sprintf("Node %s not found in the Network %s", envNode.IpAddr, netClusterName)
err = errors.New(msg)
return
}
}
if networkCluster.Health != "OK" {
err = errors.New("The Network Cluster status is not OK")
return
}
return
}
func createFile(name string, size int64) (f *os.File, err error) {
f, err = os.Create(name)
if err != nil {
log.AuctaLogger.Errorf("%v", err)
return
}
if err = f.Truncate(size); err != nil {
log.AuctaLogger.Errorf("%v", err)
}
return
}