-
Notifications
You must be signed in to change notification settings - Fork 129
/
validation.go
235 lines (201 loc) · 6.39 KB
/
validation.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
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"errors"
"fmt"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/channel/v2"
"github.com/openziti/channel/v2/protobufs"
"github.com/openziti/fablab/kernel/model"
"github.com/openziti/ziti/common/pb/mgmt_pb"
"github.com/openziti/ziti/controller/rest_client/link"
"github.com/openziti/ziti/zititest/zitilab/chaos"
"github.com/openziti/ziti/zititest/zitilab/zitirest"
"google.golang.org/protobuf/proto"
"time"
)
func sowChaos(run model.Run) error {
controllers, err := chaos.SelectRandom(run, ".ctrl", chaos.RandomOfTotal())
if err != nil {
return err
}
time.Sleep(5 * time.Second)
routers, err := chaos.SelectRandom(run, ".router", chaos.PercentageRange(10, 75))
if err != nil {
return err
}
toRestart := append(routers, controllers...)
fmt.Printf("restarting %v controllers and %v routers\n", len(controllers), len(routers))
return chaos.RestartSelected(run, toRestart, 100)
}
func validateLinks(run model.Run) error {
ctrls := run.GetModel().SelectComponents(".ctrl")
errC := make(chan error, len(ctrls))
deadline := time.Now().Add(15 * time.Minute)
for _, ctrl := range ctrls {
ctrlComponent := ctrl
go validateLinksForCtrlWithChan(ctrlComponent, deadline, errC)
}
for i := 0; i < len(ctrls); i++ {
err := <-errC
if err != nil {
return err
}
}
return nil
}
func validateLinksForCtrlWithChan(c *model.Component, deadline time.Time, errC chan<- error) {
errC <- validateLinksForCtrl(c, deadline)
}
func validateLinksForCtrl(c *model.Component, deadline time.Time) error {
username := c.MustStringVariable("credentials.edge.username")
password := c.MustStringVariable("credentials.edge.password")
edgeApiBaseUrl := c.Host.PublicIp + ":1280"
var clients *zitirest.Clients
loginStart := time.Now()
for {
var err error
clients, err = zitirest.NewManagementClients(edgeApiBaseUrl)
if err != nil {
if time.Since(loginStart) > time.Minute {
return err
}
pfxlog.Logger().WithField("ctrlId", c.Id).WithError(err).Info("failed to initialize mgmt client, trying again in 1s")
time.Sleep(time.Second)
continue
}
if err = clients.Authenticate(username, password); err != nil {
if time.Since(loginStart) > time.Minute {
return err
}
pfxlog.Logger().WithField("ctrlId", c.Id).WithError(err).Info("failed to authenticate, trying again in 1s")
time.Sleep(time.Second)
} else {
break
}
}
allLinksPresent := false
start := time.Now()
logger := pfxlog.Logger().WithField("ctrl", c.Id)
var lastLog time.Time
for time.Now().Before(deadline) && !allLinksPresent {
linkCount, err := getLinkCount(clients)
if err != nil {
return nil
}
if linkCount == 79800 {
allLinksPresent = true
} else {
time.Sleep(5 * time.Second)
}
if time.Since(lastLog) > time.Minute {
logger.Infof("current link count: %v, elapsed time: %v", linkCount, time.Since(start))
lastLog = time.Now()
}
}
if allLinksPresent {
logger.Infof("all links present, elapsed time: %v", time.Since(start))
} else {
return fmt.Errorf("fail to reach expected link count of 79800 on controller %v", c.Id)
}
for {
count, err := validateRouterLinks(c.Id, clients)
if err == nil {
return nil
}
if time.Now().After(deadline) {
return err
}
logger.Infof("current link errors: %v, elapsed time: %v", count, time.Since(start))
time.Sleep(15 * time.Second)
}
}
func getLinkCount(clients *zitirest.Clients) (int64, error) {
ctx, cancelF := context.WithTimeout(context.Background(), 15*time.Second)
defer cancelF()
filter := "limit 1"
result, err := clients.Fabric.Link.ListLinks(&link.ListLinksParams{
Filter: &filter,
Context: ctx,
})
if err != nil {
return 0, err
}
linkCount := *result.Payload.Meta.Pagination.TotalCount
return linkCount, nil
}
func validateRouterLinks(id string, clients *zitirest.Clients) (int, error) {
logger := pfxlog.Logger().WithField("ctrl", id)
closeNotify := make(chan struct{})
eventNotify := make(chan *mgmt_pb.RouterLinkDetails, 1)
handleLinkResults := func(msg *channel.Message, _ channel.Channel) {
detail := &mgmt_pb.RouterLinkDetails{}
if err := proto.Unmarshal(msg.Body, detail); err != nil {
pfxlog.Logger().WithError(err).Error("unable to unmarshal router link details")
return
}
eventNotify <- detail
}
bindHandler := func(binding channel.Binding) error {
binding.AddReceiveHandlerF(int32(mgmt_pb.ContentType_ValidateRouterLinksResultType), handleLinkResults)
binding.AddCloseHandler(channel.CloseHandlerF(func(ch channel.Channel) {
close(closeNotify)
}))
return nil
}
ch, err := clients.NewWsMgmtChannel(channel.BindHandlerF(bindHandler))
if err != nil {
return 0, err
}
defer func() {
_ = ch.Close()
}()
request := &mgmt_pb.ValidateRouterLinksRequest{
Filter: "limit none",
}
responseMsg, err := protobufs.MarshalTyped(request).WithTimeout(10 * time.Second).SendForReply(ch)
response := &mgmt_pb.ValidateRouterLinksResponse{}
if err = protobufs.TypedResponse(response).Unmarshall(responseMsg, err); err != nil {
return 0, err
}
if !response.Success {
return 0, fmt.Errorf("failed to start link validation: %s", response.Message)
}
logger.Infof("started validation of %v routers", response.RouterCount)
expected := response.RouterCount
invalid := 0
for expected > 0 {
select {
case <-closeNotify:
fmt.Printf("channel closed, exiting")
return 0, errors.New("unexpected close of mgmt channel")
case routerDetail := <-eventNotify:
if !routerDetail.ValidateSuccess {
return invalid, fmt.Errorf("error: unable to validate on controller %s (%s)", routerDetail.Message, id)
}
for _, linkDetail := range routerDetail.LinkDetails {
if !linkDetail.IsValid {
invalid++
}
}
expected--
}
}
if invalid == 0 {
logger.Infof("link validation of %v routers successful", response.RouterCount)
return invalid, nil
}
return invalid, fmt.Errorf("invalid links found")
}