-
Notifications
You must be signed in to change notification settings - Fork 444
/
util.go
371 lines (329 loc) · 12.1 KB
/
util.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package kube2e
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"regexp"
"runtime"
"strconv"
"time"
"github.com/solo-io/gloo/test/helpers"
"github.com/solo-io/go-utils/testutils/goimpl"
"github.com/solo-io/solo-kit/pkg/api/v1/resources"
"go.uber.org/zap/zapcore"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/solo-io/gloo/projects/gloo/cli/pkg/cmd/check"
"github.com/solo-io/gloo/projects/gloo/cli/pkg/cmd/options"
clienthelpers "github.com/solo-io/gloo/projects/gloo/cli/pkg/helpers"
v1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1"
"github.com/solo-io/k8s-utils/kubeutils"
"github.com/solo-io/k8s-utils/testutils/helper"
"github.com/solo-io/solo-kit/pkg/api/v1/clients"
"github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
. "github.com/onsi/gomega"
errors "github.com/rotisserie/eris"
"k8s.io/client-go/kubernetes"
)
func GetHttpEchoImage() string {
httpEchoImage := "hashicorp/http-echo"
if runtime.GOARCH == "arm64" {
httpEchoImage = "gcr.io/solo-test-236622/http-echo"
}
return httpEchoImage
}
func MustKubeClient() kubernetes.Interface {
restConfig, err := kubeutils.GetConfig("", "")
ExpectWithOffset(1, err).NotTo(HaveOccurred())
kubeClient, err := kubernetes.NewForConfig(restConfig)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
return kubeClient
}
// Check that everything is OK by running `glooctl check`
func GlooctlCheckEventuallyHealthy(offset int, testHelper *helper.SoloTestHelper, timeoutInterval string) {
EventuallyWithOffset(offset, func() error {
opts := &options.Options{
Metadata: core.Metadata{
Namespace: testHelper.InstallNamespace,
},
Top: options.Top{
Ctx: context.Background(),
},
}
err := check.CheckResources(opts)
if err != nil {
return errors.Wrap(err, "glooctl check detected a problem with the installation")
}
return nil
}, timeoutInterval, "5s").Should(BeNil())
}
func GetHelmValuesOverrideFile() (filename string, cleanup func()) {
values, err := ioutil.TempFile("", "values-*.yaml")
Expect(err).NotTo(HaveOccurred())
// disabling usage statistics is not important to the functionality of the tests,
// but we don't want to report usage in CI since we only care about how our users are actually using Gloo.
// install to a single namespace so we can run multiple invocations of the regression tests against the
// same cluster in CI.
_, err = values.Write([]byte(`
global:
image:
pullPolicy: IfNotPresent
glooRbac:
namespaced: true
nameSuffix: e2e-test-rbac-suffix
settings:
singleNamespace: true
create: true
invalidConfigPolicy:
replaceInvalidRoutes: true
invalidRouteResponseCode: 404
invalidRouteResponseBody: Gloo Gateway has invalid configuration.
gateway:
persistProxySpec: true
gloo:
deployment:
replicas: 2
customEnv:
- name: LEADER_ELECTION_LEASE_DURATION
value: 4s
livenessProbeEnabled: true
gatewayProxies:
gatewayProxy:
healthyPanicThreshold: 0
`))
Expect(err).NotTo(HaveOccurred())
err = values.Close()
Expect(err).NotTo(HaveOccurred())
return values.Name(), func() { _ = os.Remove(values.Name()) }
}
func EventuallyReachesConsistentState(installNamespace string) {
metricsPort := 9091
metricsPortString := strconv.Itoa(metricsPort)
portFwd := exec.Command(
"kubectl",
"port-forward",
"-n",
installNamespace,
"deployment/gloo",
metricsPortString)
portFwd.Stdout = os.Stderr
portFwd.Stderr = os.Stderr
err := portFwd.Start()
Expect(err).ToNot(HaveOccurred())
defer func() {
if portFwd.Process != nil {
portFwd.Process.Kill()
}
}()
// Gloo components are configured to log to the Info level by default
EventuallyLogLevel(metricsPort, zapcore.InfoLevel)
EventuallyMetricsBecomeConsistent(1, metricsPort)
}
// Copied from: https://github.com/solo-io/go-utils/blob/176c4c008b4d7cde836269c7a817f657b6981236/testutils/assertions.go#L20
func ExpectEqualProtoMessages(g Gomega, a, b proto.Message, optionalDescription ...interface{}) {
if proto.Equal(a, b) {
return
}
g.Expect(a.String()).To(Equal(b.String()), optionalDescription...)
}
func EventuallyMetricsBecomeConsistent(offset int, metricsPort int) {
// make sure we eventually reach an eventually consistent state
eventuallyConsistentPollingInterval := 7 * time.Second // >= 5s for metrics reporting, which happens every 5s
// wait for the initial snapOut reading to be present
var lastSnapOut = 0
EventuallyWithOffset(offset+1, func() int {
lastSnapOut = getSnapOut(metricsPort)
return lastSnapOut
}, "30s", eventuallyConsistentPollingInterval).Should(BeNumerically(">", 0),
"expected metrics to be found")
// wait for that snapOut reading to become consistent
consistentlyInARow := 0
EventuallyWithOffset(offset+1, func() int {
currentSnapOut := getSnapOut(metricsPort)
consistent := lastSnapOut == currentSnapOut
lastSnapOut = currentSnapOut
if consistent {
consistentlyInARow += 1
} else {
consistentlyInARow = 0
}
return consistentlyInARow
}, "80s", eventuallyConsistentPollingInterval).Should(Equal(4),
"expected metrics to be consistent")
}
// needs a port-forward of the metrics port before a call to this will work
func getSnapOut(metricsPort int) int {
metricsPortString := strconv.Itoa(metricsPort)
var bodyResp string
Eventually(func() string {
res, err := http.Post("http://localhost:"+metricsPortString+"/metrics", "", nil)
if err != nil || res.StatusCode != 200 {
return ""
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
Expect(err).ToNot(HaveOccurred())
bodyResp = string(body)
return bodyResp
}, "5s", "1s").ShouldNot(BeEmpty())
findSnapOut, err := regexp.Compile("api_gloosnapshot_gloo_solo_io_emitter_snap_out ([\\d]+)")
if err != nil {
// No snapOut metrics were found, still starting up
return 0
}
matches := findSnapOut.FindAllStringSubmatch(bodyResp, -1)
Expect(matches).To(HaveLen(1))
snapOut, err := strconv.Atoi(matches[0][1])
Expect(err).NotTo(HaveOccurred())
return snapOut
}
// EventuallyLogLevel ensures that we can query the endpoint responsible for getting the current
// log level of a gloo component, and updating the log level dynamically
func EventuallyLogLevel(port int, logLevel zapcore.Level) {
url := fmt.Sprintf("http://localhost:%d/logging", port)
body := bytes.NewReader([]byte(url))
request, err := http.NewRequest(http.MethodGet, url, body)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
expectedResponse := fmt.Sprintf("{\"level\":\"%s\"}\n", logLevel.String())
EventuallyWithOffset(1, func() (string, error) {
return goimpl.ExecuteRequest(request)
}, time.Second*5, time.Millisecond*100).Should(Equal(expectedResponse))
}
func UpdateDisableTransformationValidationSetting(ctx context.Context, shouldDisable bool, installNamespace string) {
UpdateSettings(ctx, func(settings *v1.Settings) {
Expect(settings.GetGateway().GetValidation()).NotTo(BeNil())
settings.GetGateway().GetValidation().DisableTransformationValidation = &wrappers.BoolValue{Value: shouldDisable}
}, installNamespace)
}
// enable/disable strict validation
func UpdateAlwaysAcceptSetting(ctx context.Context, alwaysAccept bool, installNamespace string) {
UpdateSettings(ctx, func(settings *v1.Settings) {
Expect(settings.GetGateway().GetValidation()).NotTo(BeNil())
settings.GetGateway().GetValidation().AlwaysAccept = &wrappers.BoolValue{Value: alwaysAccept}
}, installNamespace)
}
func UpdateRestEdsSetting(ctx context.Context, enableRestEds bool, installNamespace string) {
UpdateSettings(ctx, func(settings *v1.Settings) {
Expect(settings.GetGloo()).NotTo(BeNil())
settings.GetGloo().EnableRestEds = &wrappers.BoolValue{Value: enableRestEds}
}, installNamespace)
}
func UpdateReplaceInvalidRoutes(ctx context.Context, replaceInvalidRoutes bool, installNamespace string) {
UpdateSettings(ctx, func(settings *v1.Settings) {
Expect(settings.GetGloo().GetInvalidConfigPolicy()).NotTo(BeNil())
settings.GetGloo().GetInvalidConfigPolicy().ReplaceInvalidRoutes = replaceInvalidRoutes
}, installNamespace)
}
func UpdateSettings(ctx context.Context, updateSettings func(settings *v1.Settings), installNamespace string) {
// when validation config changes, the validation server restarts -- give time for it to come up again.
// without the wait, the validation webhook may temporarily fallback to it's failurePolicy, which is not
// what we want to test.
// TODO (samheilbron) We should avoid relying on time.Sleep in our tests as these tend to cause flakes
waitForSettingsToPropagate := func() {
time.Sleep(3 * time.Second)
}
UpdateSettingsWithPropagationDelay(updateSettings, waitForSettingsToPropagate, ctx, installNamespace)
}
func UpdateSettingsWithPropagationDelay(updateSettings func(settings *v1.Settings), waitForSettingsToPropagate func(), ctx context.Context, installNamespace string) {
settingsClient := clienthelpers.MustSettingsClient(ctx)
settings, err := settingsClient.Read(installNamespace, "default", clients.ReadOpts{})
Expect(err).NotTo(HaveOccurred())
updateSettings(settings)
_, err = settingsClient.Write(settings, clients.WriteOpts{OverwriteExisting: true})
Expect(err).NotTo(HaveOccurred())
waitForSettingsToPropagate()
}
func ToFile(content string) string {
f, err := ioutil.TempFile("", "")
ExpectWithOffset(1, err).NotTo(HaveOccurred())
n, err := f.WriteString(content)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
ExpectWithOffset(1, n).To(Equal(len(content)))
_ = f.Close()
return f.Name()
}
// PatchResource mutates an existing resource, retrying if a resourceVersionError is encountered
// Deprecated: Prefer the helpers.PatchResource, which is not a Kubernetes specific package
func PatchResource(ctx context.Context, resourceRef *core.ResourceRef, mutator func(resource resources.Resource), client clients.ResourceClient) error {
return helpers.PatchResourceWithOffset(1, ctx, resourceRef, mutator, client)
}
// https://github.com/solo-io/gloo/issues/4043#issuecomment-772706604
// We should move tests away from using the testrunner, and instead depend on EphemeralContainers.
// The default response changed in later kube versions, which caused this value to change.
// Ideally the test utilities used by Gloo are maintained in the Gloo repo, so I opted to move
// this constant here.
// This response is given by the testrunner when the SimpleServer is started
func GetSimpleTestRunnerHttpResponse() string {
if runtime.GOARCH == "arm64" {
return SimpleTestRunnerHttpResponseArm
} else {
return SimpleTestRunnerHttpResponse
}
}
const SimpleTestRunnerHttpResponse = `<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>
<title>Directory listing for /</title>
<body>
<h2>Directory listing for /</h2>
<hr>
<ul>
<li><a href="bin/">bin/</a>
<li><a href="boot/">boot/</a>
<li><a href="dev/">dev/</a>
<li><a href="etc/">etc/</a>
<li><a href="home/">home/</a>
<li><a href="lib/">lib/</a>
<li><a href="lib64/">lib64/</a>
<li><a href="media/">media/</a>
<li><a href="mnt/">mnt/</a>
<li><a href="opt/">opt/</a>
<li><a href="proc/">proc/</a>
<li><a href="product_name">product_name</a>
<li><a href="product_uuid">product_uuid</a>
<li><a href="root/">root/</a>
<li><a href="root.crt">root.crt</a>
<li><a href="run/">run/</a>
<li><a href="sbin/">sbin/</a>
<li><a href="srv/">srv/</a>
<li><a href="sys/">sys/</a>
<li><a href="tmp/">tmp/</a>
<li><a href="usr/">usr/</a>
<li><a href="var/">var/</a>
</ul>
<hr>
</body>
</html>`
const SimpleTestRunnerHttpResponseArm = `<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>
<title>Directory listing for /</title>
<body>
<h2>Directory listing for /</h2>
<hr>
<ul>
<li><a href="bin/">bin/</a>
<li><a href="boot/">boot/</a>
<li><a href="dev/">dev/</a>
<li><a href="etc/">etc/</a>
<li><a href="home/">home/</a>
<li><a href="lib/">lib/</a>
<li><a href="lib64/">lib64/</a>
<li><a href="media/">media/</a>
<li><a href="mnt/">mnt/</a>
<li><a href="opt/">opt/</a>
<li><a href="proc/">proc/</a>
<li><a href="product_uuid">product_uuid</a>
<li><a href="root/">root/</a>
<li><a href="root.crt">root.crt</a>
<li><a href="run/">run/</a>
<li><a href="sbin/">sbin/</a>
<li><a href="srv/">srv/</a>
<li><a href="sys/">sys/</a>
<li><a href="tmp/">tmp/</a>
<li><a href="usr/">usr/</a>
<li><a href="var/">var/</a>
</ul>
<hr>
</body>
</html>`