/
Nightly.go
503 lines (408 loc) · 15.6 KB
/
Nightly.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// Copyright 2017-2019 Authors of Cilium
//
// 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
//
// http://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 k8sTest
import (
"context"
"fmt"
"path"
"strings"
"time"
"github.com/cilium/cilium/api/v1/models"
. "github.com/cilium/cilium/test/ginkgo-ext"
"github.com/cilium/cilium/test/helpers"
"github.com/cilium/cilium/test/helpers/policygen"
"github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/sirupsen/logrus"
)
var (
endpointTimeout = 1 * time.Minute
timeout = 5 * time.Minute
netcatDsManifest = "netcat-ds.yaml"
)
var _ = Describe("NightlyEpsMeasurement", func() {
var kubectl *helpers.Kubectl
var ciliumFilename string
endpointCount := 45
endpointsTimeout := endpointTimeout * time.Duration(endpointCount)
manifestPath := "tmp.yaml"
vagrantManifestPath := ""
var err error
BeforeAll(func() {
kubectl = helpers.CreateKubectl(helpers.K8s1VMName(), logger)
vagrantManifestPath = path.Join(kubectl.BasePath(), manifestPath)
ciliumFilename = helpers.TimestampFilename("cilium.yaml")
DeployCiliumAndDNS(kubectl, ciliumFilename)
})
deleteAll := func() {
ctx, cancel := context.WithTimeout(context.Background(), endpointsTimeout)
defer cancel()
kubectl.ExecInBackground(ctx, fmt.Sprintf(
"%s delete --all pods,svc,cnp -n %s --grace-period=0 --force",
helpers.KubectlCmd, helpers.DefaultNamespace))
select {
case <-ctx.Done():
logger.Errorf("DeleteAll: delete all pods,services failed after %s", helpers.HelperTimeout)
}
}
AfterAll(func() {
deleteAll()
kubectl.DeleteCiliumDS()
ExpectAllPodsTerminated(kubectl)
kubectl.CloseSSHClient()
})
AfterFailed(func() {
kubectl.CiliumReport(helpers.CiliumNamespace,
"cilium service list",
"cilium endpoint list")
})
JustAfterEach(func() {
kubectl.ValidateNoErrorsInLogs(CurrentGinkgoTestDescription().Duration)
})
AfterEach(func() {
ExpectAllPodsTerminated(kubectl)
kubectl.Delete(vagrantManifestPath)
ExpectAllPodsTerminated(kubectl)
})
deployEndpoints := func() {
_, _, err = helpers.GenerateManifestForEndpoints(endpointCount, manifestPath)
ExpectWithOffset(1, err).Should(BeNil(), "Manifest cannot be created correctly")
// This is equivalent to res := kubectl.Apply(vagrantManifestPath) but we
// need a longer timeout than helpers.ShortCommandTimeout
ctx, cancel := context.WithTimeout(context.Background(), endpointsTimeout)
defer cancel()
res := kubectl.ExecContext(ctx, fmt.Sprintf("%s apply -f %s", helpers.KubectlCmd, vagrantManifestPath))
res.ExpectSuccess("cannot apply eps manifest :%s", res.GetDebugMessage())
}
getServices := func() map[string]string {
// getServices returns a map of services, where service name is the key
// and the ClusterIP is the value.
services, err := kubectl.Get(helpers.DefaultNamespace, fmt.Sprintf("services -l zgroup=testapp")).Filter(
`{range .items[*]}{.metadata.name}{"="}{.spec.clusterIP}{"\n"}{end}`)
ExpectWithOffset(1, err).To(BeNil(), "cannot retrieve testapp services")
result := make(map[string]string)
for _, line := range strings.Split(services.String(), "\n") {
vals := strings.Split(line, "=")
if len(vals) == 2 {
result[vals[0]] = vals[1]
}
}
return result
}
Measure("The endpoint creation", func(b ginkgo.Benchmarker) {
desiredState := string(models.EndpointStateReady)
deployEndpoints()
waitForPodsTime := b.Time("Wait for pods", func() {
err := kubectl.WaitforPods(helpers.DefaultNamespace, "-l zgroup=testapp", endpointsTimeout)
Expect(err).Should(BeNil(),
"Cannot retrieve %d pods in %d seconds", endpointCount, endpointsTimeout.Seconds())
})
log.WithFields(logrus.Fields{"pod creation time": waitForPodsTime}).Info("")
ciliumPods, err := kubectl.GetCiliumPods(helpers.CiliumNamespace)
Expect(err).To(BeNil(), "Cannot retrieve cilium pods")
runtime := b.Time("Endpoint creation", func() {
Eventually(func() bool {
count := 0
for _, pod := range ciliumPods {
status := kubectl.CiliumEndpointsStatus(pod)
result := map[string]int{}
for _, state := range status {
result[state]++
}
count += result[desiredState]
logger.WithFields(logrus.Fields{
"status": result,
"pod": pod,
}).Info("Cilium endpoint status")
}
return count >= endpointCount
}, endpointsTimeout, 3*time.Second).Should(BeTrue())
})
log.WithFields(logrus.Fields{"endpoint creation time": runtime}).Info("")
services := getServices()
Expect(len(services)).To(BeNumerically(">", 0), "Was not able to get services")
pods, err := kubectl.GetPodNames(helpers.DefaultNamespace, "zgroup=testapp")
Expect(err).To(BeNil(), "cannot retrieve pods names")
err = kubectl.WaitforPods(helpers.DefaultNamespace, "", helpers.HelperTimeout)
Expect(err).Should(BeNil(), "Pods are not ready after timeout")
By("Testing if http requests to multiple endpoints do not timeout")
for i := 0; i < 5; i++ {
for _, pod := range pods {
for service, ip := range services {
b.Time("Curl to service", func() {
res := kubectl.ExecPodCmd(
helpers.DefaultNamespace, pod,
helpers.CurlFail(fmt.Sprintf("http://%s:80/", ip)))
res.ExpectSuccess(
"Cannot curl from %s to service %s on ip %s", pod, service, ip)
})
}
}
}
}, 1)
Context("Nightly Policies", func() {
numPods := 20
bunchPods := 5
podsCreated := 0
AfterEach(func() {
deleteAll()
ExpectAllPodsTerminated(kubectl)
})
Measure(fmt.Sprintf("Applying policies to %d pods in a group of %d", numPods, bunchPods), func(b ginkgo.Benchmarker) {
testDef := func() {
logger.Errorf("Creating %d new pods, total created are %d", numPods, podsCreated)
testSpecGroup := policygen.TestSpecsGroup{}
for i := 0; i < bunchPods; i++ {
testSpec := policygen.GetBasicTestSpec()
testSpecGroup = append(testSpecGroup, &testSpec)
}
By("Creating endpoints")
endpoints := b.Time("Runtime", func() {
testSpecGroup.CreateAndApplyManifests(kubectl)
err := kubectl.WaitforPods(helpers.DefaultNamespace, "-l test=policygen", longTimeout)
Expect(err).To(BeNil(), "Pods are not ready after timeout")
})
b.RecordValue("Endpoint Creation in seconds", endpoints.Seconds())
By("Apply Policies")
policy := b.Time("policy", func() {
testSpecGroup.CreateAndApplyCNP(kubectl)
})
b.RecordValue("Policy Creation in seconds", policy.Seconds())
By("Connectivity Test")
conn := b.Time("connTest", func() {
testSpecGroup.ConnectivityTest()
})
b.RecordValue("Connectivity test in seconds", conn.Seconds())
}
for podsCreated < numPods {
testDef()
podsCreated = podsCreated + bunchPods
}
}, 1)
})
Context("Test long live connections", func() {
getServer := func(port string) string {
return fmt.Sprintf("nc -p %s -lk -v", port)
}
getClient := func(ip, port, filePipe string) string {
return fmt.Sprintf(
"rm %[1]s; touch %[1]s; tail -f %[1]s 2>&1 | nc -v %[2]s %[3]s",
filePipe, ip, port)
}
HTTPRequest := func(uid, host string) string {
request := `GET /public HTTP/1.1\r\n` +
`host: %s:8888\r\n` +
`user-agent: curl/7.54.0\r\n` +
`accept: */*\r\n` +
`UID: %s\r\n` +
`content-length: 0\r\n`
return fmt.Sprintf(request, host, uid)
}
// testConnectivity check that nc is running across the k8s nodes
testConnectivity := func() {
pipePath := "/tmp/nc_pipe.txt"
listeningString := "listening on [::]:8888"
err := kubectl.WaitforPods(helpers.DefaultNamespace, "-l zgroup=netcatds", helpers.HelperTimeout)
Expect(err).To(BeNil(), "Pods are not ready after timeout")
netcatPods, err := kubectl.GetPodNames(helpers.DefaultNamespace, "zgroup=netcatds")
Expect(err).To(BeNil(), "Cannot get pods names for netcatds")
Expect(len(netcatPods)).To(BeNumerically(">", 0), "Pods are not ready")
server := netcatPods[0]
client := netcatPods[1]
ips, err := kubectl.GetPodsIPs(helpers.DefaultNamespace, "zgroup=netcatds")
Expect(err).To(BeNil(), "Cannot get netcat ips")
ncServer := getServer("8888")
ncClient := getClient(ips[server], "8888", pipePath)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
serverctx := kubectl.ExecPodCmdBackground(ctx, helpers.DefaultNamespace, server, ncServer)
err = serverctx.WaitUntilMatch(listeningString)
Expect(err).To(BeNil(), "netcat server did not start correctly")
_ = kubectl.ExecPodCmdBackground(ctx, helpers.DefaultNamespace, client, ncClient)
testNcConnectivity := func(sleep time.Duration) {
helpers.Sleep(sleep)
uid := helpers.MakeUID()
_ = kubectl.ExecPodCmd(helpers.DefaultNamespace, client,
fmt.Sprintf(`echo -e "%s" >> %s`, HTTPRequest(uid, ips[client]), pipePath))
Expect(serverctx.WaitUntilMatch(uid)).To(BeNil(),
"%q is not in the server output after timeout", uid)
serverctx.ExpectContains(uid, "Cannot get server UUID")
}
By("Testing that simple nc works")
testNcConnectivity(1)
By("Sleeping for a minute to check tcp-keepalive")
testNcConnectivity(60)
By("Sleeping for six minutes to check tcp-keepalive")
testNcConnectivity(360)
}
It("Test TCP Keepalive with L7 Policy", func() {
kubectl.ValidateNoErrorsInLogs(CurrentGinkgoTestDescription().Duration)
manifest := helpers.ManifestGet(kubectl.BasePath(), netcatDsManifest)
kubectl.ApplyDefault(manifest).ExpectSuccess("Cannot apply netcat ds")
defer kubectl.Delete(manifest)
testConnectivity()
})
It("Test TCP Keepalive without L7 Policy", func() {
manifest := helpers.ManifestGet(kubectl.BasePath(), netcatDsManifest)
kubectl.ApplyDefault(manifest).ExpectSuccess("Cannot apply netcat ds")
defer kubectl.Delete(manifest)
kubectl.Exec(fmt.Sprintf(
"%s delete --all cnp -n %s", helpers.KubectlCmd, helpers.DefaultNamespace))
testConnectivity()
})
})
})
var _ = Describe("NightlyExamples", func() {
var kubectl *helpers.Kubectl
var l3Policy string
var ciliumFilename string
BeforeAll(func() {
kubectl = helpers.CreateKubectl(helpers.K8s1VMName(), logger)
demoPath = helpers.ManifestGet(kubectl.BasePath(), "demo.yaml")
l3Policy = helpers.ManifestGet(kubectl.BasePath(), "l3-l4-policy.yaml")
l7Policy = helpers.ManifestGet(kubectl.BasePath(), "l7-policy.yaml")
ciliumFilename = helpers.TimestampFilename("cilium.yaml")
})
AfterFailed(func() {
kubectl.CiliumReport(helpers.CiliumNamespace,
"cilium service list",
"cilium endpoint list")
})
JustAfterEach(func() {
kubectl.ValidateNoErrorsInLogs(CurrentGinkgoTestDescription().Duration)
})
AfterEach(func() {
kubectl.Delete(demoPath)
kubectl.Delete(l3Policy)
kubectl.Delete(l7Policy)
ExpectAllPodsTerminated(kubectl)
})
AfterAll(func() {
kubectl.CloseSSHClient()
})
Context("Upgrade test", func() {
var (
kubectl *helpers.Kubectl
cleanupCallback = func() { return }
)
BeforeAll(func() {
kubectl = helpers.CreateKubectl(helpers.K8s1VMName(), logger)
demoPath = helpers.ManifestGet(kubectl.BasePath(), "demo.yaml")
l7Policy = helpers.ManifestGet(kubectl.BasePath(), "l7-policy.yaml")
migrateSVCClient = helpers.ManifestGet(kubectl.BasePath(), "migrate-svc-client.yaml")
migrateSVCServer = helpers.ManifestGet(kubectl.BasePath(), "migrate-svc-server.yaml")
_ = kubectl.Delete(helpers.DNSDeployment(kubectl.BasePath()))
kubectl.Delete(migrateSVCClient)
kubectl.Delete(migrateSVCServer)
kubectl.Delete(l7Policy)
kubectl.Delete(demoPath)
// Delete kube-dns because if not will be a restore the old endpoints
// from master instead of create the new ones.
_ = kubectl.DeleteResource(
"deploy", fmt.Sprintf("-n %s kube-dns", helpers.KubeSystemNamespace))
_ = kubectl.DeleteResource(
"deploy", fmt.Sprintf("-n %s cilium-operator", helpers.CiliumNamespace))
// Sometimes PolicyGen has a lot of pods running around without delete
// it. Using this we are sure that we delete before this test start
kubectl.Exec(fmt.Sprintf(
"%s delete --all pods,svc,cnp -n %s", helpers.KubectlCmd, helpers.DefaultNamespace))
kubectl.DeleteETCDOperator()
ExpectAllPodsTerminated(kubectl)
})
AfterAll(func() {
kubectl.CloseSSHClient()
})
AfterFailed(func() {
kubectl.CiliumReport(helpers.CiliumNamespace, "cilium endpoint list")
})
JustAfterEach(func() {
kubectl.ValidateNoErrorsInLogs(CurrentGinkgoTestDescription().Duration)
})
AfterEach(func() {
cleanupCallback()
ExpectAllPodsTerminated(kubectl)
})
for imageVersion, chartVersion := range helpers.NightlyStableUpgradesFrom {
func(imageVersion, chartVersion string) {
SkipItIf(func() bool { return !helpers.RunsWithKubeProxy() },
fmt.Sprintf("Update Cilium from %s to master", imageVersion), func() {
var assertUpgradeSuccessful func()
assertUpgradeSuccessful, cleanupCallback = InstallAndValidateCiliumUpgrades(
kubectl,
chartVersion,
imageVersion,
helpers.CiliumLatestHelmChartVersion,
helpers.CiliumLatestImageVersion,
)
assertUpgradeSuccessful()
})
}(imageVersion, chartVersion)
}
})
Context("Getting started guides", func() {
var (
GRPCManifest = "../examples/kubernetes-grpc/cc-door-app.yaml"
GRPCPolicy = "../examples/kubernetes-grpc/cc-door-ingress-security.yaml"
AppManifest = ""
PolicyManifest = ""
)
BeforeAll(func() {
AppManifest = kubectl.GetFilePath(GRPCManifest)
PolicyManifest = kubectl.GetFilePath(GRPCPolicy)
ciliumFilename = helpers.TimestampFilename("cilium.yaml")
DeployCiliumAndDNS(kubectl, ciliumFilename)
})
AfterAll(func() {
kubectl.Delete(AppManifest)
kubectl.Delete(PolicyManifest)
ExpectAllPodsTerminated(kubectl)
})
It("GRPC example", func() {
clientPod := "terminal-87"
By("Testing the example config")
kubectl.ApplyDefault(AppManifest).ExpectSuccess("cannot install the GRPC application")
err := kubectl.WaitforPods(helpers.DefaultNamespace, "-l zgroup=grpcExample", helpers.HelperTimeout)
Expect(err).Should(BeNil(), "Pods are not ready after timeout")
res := kubectl.ExecPodCmd(
helpers.DefaultNamespace, clientPod,
"python3 /cloudcity/cc_door_client.py GetName 1")
res.ExpectSuccess("Client cannot get Name")
res = kubectl.ExecPodCmd(
helpers.DefaultNamespace, clientPod,
"python3 /cloudcity/cc_door_client.py GetLocation 1")
res.ExpectSuccess("Client cannot get Location")
res = kubectl.ExecPodCmd(
helpers.DefaultNamespace, clientPod,
"python3 /cloudcity/cc_door_client.py SetAccessCode 1 999")
res.ExpectSuccess("Client cannot set Accesscode")
By("Testing with L7 policy")
_, err = kubectl.CiliumPolicyAction(
helpers.DefaultNamespace, PolicyManifest,
helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).To(BeNil(), "Cannot import GPRC policy")
res = kubectl.ExecPodCmd(
helpers.DefaultNamespace, clientPod,
"python3 /cloudcity/cc_door_client.py GetName 1")
res.ExpectSuccess("Client cannot get Name")
res = kubectl.ExecPodCmd(
helpers.DefaultNamespace, clientPod,
"python3 /cloudcity/cc_door_client.py GetLocation 1")
res.ExpectSuccess("Client cannot get Location")
res = kubectl.ExecPodCmd(
helpers.DefaultNamespace, clientPod,
"python3 /cloudcity/cc_door_client.py SetAccessCode 1 999")
res.ExpectFail("Client can set Accesscode and it shoud not")
})
})
})