-
Notifications
You must be signed in to change notification settings - Fork 432
/
util.go
233 lines (206 loc) · 8.04 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
package kube2e
import (
"context"
"io/ioutil"
"net/http"
"os"
"os/exec"
"regexp"
"strconv"
"time"
"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 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
replaceInvalidRoutes: 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 := strconv.Itoa(9091)
portFwd := exec.Command("kubectl", "port-forward", "-n", installNamespace,
"deployment/gloo", metricsPort)
portFwd.Stdout = os.Stderr
portFwd.Stderr = os.Stderr
err := portFwd.Start()
Expect(err).ToNot(HaveOccurred())
defer func() {
if portFwd.Process != nil {
portFwd.Process.Kill()
}
}()
// make sure we eventually reach an eventually consistent state
lastSnapOut := getSnapOut(metricsPort)
eventuallyConsistentPollingInterval := 7 * time.Second // >= 5s for metrics reporting, which happens every 5s
time.Sleep(eventuallyConsistentPollingInterval)
Eventually(func() bool {
currentSnapOut := getSnapOut(metricsPort)
consistent := lastSnapOut == currentSnapOut
lastSnapOut = currentSnapOut
return consistent
}, "30s", eventuallyConsistentPollingInterval).Should(Equal(true))
Consistently(func() string {
currentSnapOut := getSnapOut(metricsPort)
return currentSnapOut
}, "30s", eventuallyConsistentPollingInterval).Should(Equal(lastSnapOut))
}
// needs a port-forward of the metrics port before a call to this will work
func getSnapOut(metricsPort string) string {
var bodyResp string
Eventually(func() string {
res, err := http.Post("http://localhost:"+metricsPort+"/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())
Expect(bodyResp).To(ContainSubstring("api_gloo_solo_io_emitter_snap_out"))
findSnapOut := regexp.MustCompile("api_gloo_solo_io_emitter_snap_out ([\\d]+)")
matches := findSnapOut.FindAllStringSubmatch(bodyResp, -1)
Expect(matches).To(HaveLen(1))
snapOut := matches[0][1]
return snapOut
}
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()
}
// 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
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>`