forked from Azure/aks-engine
/
kubernetes_test.go
1414 lines (1278 loc) · 60.7 KB
/
kubernetes_test.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
package kubernetes
import (
"fmt"
"log"
"math/rand"
"net"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/Azure/aks-engine/pkg/api/common"
"github.com/Azure/aks-engine/test/e2e/config"
"github.com/Azure/aks-engine/test/e2e/engine"
"github.com/Azure/aks-engine/test/e2e/kubernetes/deployment"
"github.com/Azure/aks-engine/test/e2e/kubernetes/hpa"
"github.com/Azure/aks-engine/test/e2e/kubernetes/job"
"github.com/Azure/aks-engine/test/e2e/kubernetes/namespace"
"github.com/Azure/aks-engine/test/e2e/kubernetes/networkpolicy"
"github.com/Azure/aks-engine/test/e2e/kubernetes/node"
"github.com/Azure/aks-engine/test/e2e/kubernetes/persistentvolume"
"github.com/Azure/aks-engine/test/e2e/kubernetes/persistentvolumeclaims"
"github.com/Azure/aks-engine/test/e2e/kubernetes/pod"
"github.com/Azure/aks-engine/test/e2e/kubernetes/service"
"github.com/Azure/aks-engine/test/e2e/kubernetes/storageclass"
"github.com/Azure/aks-engine/test/e2e/kubernetes/util"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
const (
WorkloadDir = "workloads"
PolicyDir = "workloads/policies"
deleteResourceRetries = 10
retryCommandsTimeout = 5 * time.Minute
kubeSystemPodsReadinessChecks = 6
)
var (
cfg config.Config
eng engine.Engine
masterSSHPort string
masterSSHPrivateKeyFilepath string
longRunningApacheDeploymentName string
)
var _ = BeforeSuite(func() {
cwd, _ := os.Getwd()
rootPath := filepath.Join(cwd, "../../..") // The current working dir of these tests is down a few levels from the root of the project. We should traverse up that path so we can find the _output dir
c, err := config.ParseConfig()
c.CurrentWorkingDir = rootPath
Expect(err).NotTo(HaveOccurred())
cfg = *c // We have to do this because golang anon functions and scoping and stuff
engCfg, err := engine.ParseConfig(c.CurrentWorkingDir, c.ClusterDefinition, c.Name)
Expect(err).NotTo(HaveOccurred())
csInput, err := engine.ParseInput(engCfg.ClusterDefinitionTemplate)
Expect(err).NotTo(HaveOccurred())
csGenerated, err := engine.ParseOutput(engCfg.GeneratedDefinitionPath + "/apimodel.json")
Expect(err).NotTo(HaveOccurred())
eng = engine.Engine{
Config: engCfg,
ClusterDefinition: csInput,
ExpandedDefinition: csGenerated,
}
masterNodes, err := node.GetByPrefix("k8s-master")
Expect(err).NotTo(HaveOccurred())
masterName := masterNodes[0].Metadata.Name
if strings.Contains(masterName, "vmss") {
masterSSHPort = "50001"
} else {
masterSSHPort = "22"
}
masterSSHPrivateKeyFilepath = cfg.GetSSHKeyPath()
longRunningApacheDeploymentName = "php-apache-long-running"
})
var _ = Describe("Azure Container Cluster using the Kubernetes Orchestrator", func() {
Describe("regardless of agent pool type", func() {
It("should display the installed Ubuntu version on the master node", func() {
kubeConfig, err := GetConfig()
Expect(err).NotTo(HaveOccurred())
master := fmt.Sprintf("azureuser@%s", kubeConfig.GetServerName())
lsbReleaseCmd := fmt.Sprintf("lsb_release -a && uname -r")
cmd := exec.Command("ssh", "-i", masterSSHPrivateKeyFilepath, "-p", masterSSHPort, "-o", "ConnectTimeout=10", "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", master, lsbReleaseCmd)
util.PrintCommand(cmd)
out, err := cmd.CombinedOutput()
log.Printf("%s\n", out)
if err != nil {
log.Printf("Error while getting Ubuntu image version: %s\n", err)
}
kernelVerCmd := fmt.Sprintf("cat /proc/version")
cmd = exec.Command("ssh", "-i", masterSSHPrivateKeyFilepath, "-p", masterSSHPort, "-o", "ConnectTimeout=10", "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", master, kernelVerCmd)
util.PrintCommand(cmd)
out, err = cmd.CombinedOutput()
log.Printf("%s\n", out)
if err != nil {
log.Printf("Error while getting LinuxKernel version: %s\n", err)
}
})
It("should display the installed docker runtime on the master node", func() {
if eng.ExpandedDefinition.Properties.OrchestratorProfile.KubernetesConfig.RequiresDocker() {
kubeConfig, err := GetConfig()
Expect(err).NotTo(HaveOccurred())
master := fmt.Sprintf("azureuser@%s", kubeConfig.GetServerName())
cmd := exec.Command("ssh-add", "-D")
util.PrintCommand(cmd)
out, err := cmd.CombinedOutput()
log.Printf("%s\n", out)
if err != nil {
log.Printf("Error while cleaning ssh agent keychain: %s\n", err)
}
cmd = exec.Command("ssh-add", masterSSHPrivateKeyFilepath)
util.PrintCommand(cmd)
out, err = cmd.CombinedOutput()
log.Printf("%s\n", out)
if err != nil {
log.Printf("Error while adding private key to ssh agent keychain for forwarding: %s\n", err)
}
nodeList, err := node.Get()
Expect(err).NotTo(HaveOccurred())
dockerVersionCmd := fmt.Sprintf("\"docker version\"")
for _, node := range nodeList.Nodes {
cmd = exec.Command("ssh", "-A", "-i", masterSSHPrivateKeyFilepath, "-p", masterSSHPort, "-o", "ConnectTimeout=10", "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", master, "ssh", "-o", "ConnectTimeout=10", "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", node.Metadata.Name, dockerVersionCmd)
util.PrintCommand(cmd)
out, err = cmd.CombinedOutput()
log.Printf("%s\n", out)
if err != nil {
log.Printf("Error while getting docker version on node %s: %s\n", node.Metadata.Name, err)
}
}
} else {
Skip("Skip docker validations on non-docker-backed clusters")
}
})
It("should report all nodes in a Ready state", func() {
nodeCount := eng.NodeCount()
log.Printf("Checking for %d Ready nodes\n", nodeCount)
ready := node.WaitOnReady(nodeCount, 10*time.Second, cfg.Timeout)
cmd := exec.Command("kubectl", "get", "nodes", "-o", "wide")
out, _ := cmd.CombinedOutput()
log.Printf("%s\n", out)
if !ready {
log.Printf("Error: Not all nodes in a healthy state\n")
}
Expect(ready).To(Equal(true))
})
It("should print all pods", func() {
cmd := exec.Command("kubectl", "get", "pods", "--all-namespaces", "-o", "wide")
out, err := cmd.CombinedOutput()
log.Printf("%s\n", out)
if err != nil {
log.Printf("Error: Unable to print all pods\n")
}
})
It("should have DNS pod running", func() {
var err error
var running bool
if common.IsKubernetesVersionGe(eng.ExpandedDefinition.Properties.OrchestratorProfile.OrchestratorVersion, "1.12.0") {
By("Ensuring that coredns is running")
running, err = pod.WaitOnReady("coredns", "kube-system", kubeSystemPodsReadinessChecks, 1*time.Second, cfg.Timeout)
} else {
By("Ensuring that kube-dns is running")
running, err = pod.WaitOnReady("kube-dns", "kube-system", kubeSystemPodsReadinessChecks, 1*time.Second, cfg.Timeout)
}
Expect(err).NotTo(HaveOccurred())
Expect(running).To(Equal(true))
})
It("should have core kube-system componentry running", func() {
coreComponents := []string{"kube-proxy", "kube-addon-manager", "kube-apiserver", "kube-controller-manager", "kube-scheduler"}
if !common.IsKubernetesVersionGe(eng.ExpandedDefinition.Properties.OrchestratorProfile.OrchestratorVersion, "1.13.0") {
coreComponents = append(coreComponents, "heapster")
}
for _, componentName := range coreComponents {
By(fmt.Sprintf("Ensuring that %s is Running", componentName))
running, err := pod.WaitOnReady(componentName, "kube-system", kubeSystemPodsReadinessChecks, 1*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(running).To(Equal(true))
}
})
It("should have the correct IP address for the apiserver", func() {
pods, err := pod.GetAllByPrefix("kube-apiserver", "kube-system")
Expect(err).NotTo(HaveOccurred())
By("Ensuring that the correct IP address has been applied to the apiserver")
expectedIPAddress := eng.ExpandedDefinition.Properties.MasterProfile.FirstConsecutiveStaticIP
if eng.ExpandedDefinition.Properties.MasterProfile.Count > 1 {
firstMasterIP := net.ParseIP(eng.ExpandedDefinition.Properties.MasterProfile.FirstConsecutiveStaticIP).To4()
expectedIP := net.IP{firstMasterIP[0], firstMasterIP[1], firstMasterIP[2], firstMasterIP[3] + byte(common.DefaultInternalLbStaticIPOffset)}
if eng.ExpandedDefinition.Properties.MasterProfile.IsVirtualMachineScaleSets() {
expectedIP = net.IP{firstMasterIP[0], firstMasterIP[1], byte(255), byte(common.DefaultInternalLbStaticIPOffset)}
}
expectedIPAddress = expectedIP.String()
}
actualIPAddress, err := pods[0].Spec.Containers[0].GetArg("--advertise-address")
Expect(err).NotTo(HaveOccurred())
Expect(actualIPAddress).To(Equal(expectedIPAddress))
})
It("should have addons running", func() {
for _, addonName := range []string{"tiller", "aci-connector", "cluster-autoscaler", "blobfuse-flexvolume", "smb-flexvolume", "keyvault-flexvolume", "kubernetes-dashboard", "rescheduler", "metrics-server", "nvidia-device-plugin", "container-monitoring", "azure-cni-networkmonitor", "azure-npm-daemonset", "ip-masq-agent"} {
var addonPods = []string{addonName}
var addonNamespace = "kube-system"
switch addonName {
case "blobfuse-flexvolume":
addonPods = []string{"blobfuse-flexvol-installer"}
case "smb-flexvolume":
addonPods = []string{"smb-flexvol-installer"}
case "container-monitoring":
addonPods = []string{"omsagent"}
case "azure-npm-daemonset":
addonPods = []string{"azure-npm"}
}
if hasAddon, addon := eng.HasAddon(addonName); hasAddon {
for _, addonPod := range addonPods {
By(fmt.Sprintf("Ensuring that the %s addon is Running", addonName))
running, err := pod.WaitOnReady(addonPod, addonNamespace, kubeSystemPodsReadinessChecks, 1*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(running).To(Equal(true))
By(fmt.Sprintf("Ensuring that the correct resources have been applied for %s", addonPod))
pods, err := pod.GetAllByPrefix(addonPod, addonNamespace)
Expect(err).NotTo(HaveOccurred())
for i, c := range addon.Containers {
err := pods[0].Spec.Containers[i].ValidateResources(c)
Expect(err).NotTo(HaveOccurred())
}
}
} else {
fmt.Printf("%s disabled for this cluster, will not test\n", addonName)
}
}
})
It("should have the correct tiller configuration", func() {
if hasTiller, tillerAddon := eng.HasAddon("tiller"); hasTiller {
running, err := pod.WaitOnReady("tiller", "kube-system", kubeSystemPodsReadinessChecks, 1*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(running).To(Equal(true))
pods, err := pod.GetAllByPrefix("tiller-deploy", "kube-system")
Expect(err).NotTo(HaveOccurred())
By("Ensuring that the correct max-history has been applied")
maxHistory := tillerAddon.Config["max-history"]
// There is only one tiller pod and one container in that pod
actualTillerMaxHistory, err := pods[0].Spec.Containers[0].GetEnvironmentVariable("TILLER_HISTORY_MAX")
Expect(err).NotTo(HaveOccurred())
Expect(actualTillerMaxHistory).To(Equal(maxHistory))
} else {
Skip("tiller disabled for this cluster, will not test")
}
})
It("should have the expected omsagent cluster footprint", func() {
if hasContainerMonitoring, _ := eng.HasAddon("container-monitoring"); hasContainerMonitoring {
By("Validating the omsagent replicaset")
running, err := pod.WaitOnReady("omsagent-rs", "kube-system", kubeSystemPodsReadinessChecks, 1*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(running).To(Equal(true))
pods, err := pod.GetAllByPrefix("omsagent-rs", "kube-system")
Expect(err).NotTo(HaveOccurred())
By("Ensuring that the kubepodinventory plugin is writing data successfully")
pass, err := pods[0].ValidateOmsAgentLogs("kubePodInventoryEmitStreamSuccess", 1*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(pass).To(BeTrue())
By("Ensuring that the kubenodeinventory plugin is writing data successfully")
pass, err = pods[0].ValidateOmsAgentLogs("kubeNodeInventoryEmitStreamSuccess", 1*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(pass).To(BeTrue())
By("Validating the omsagent daemonset")
running, err = pod.WaitOnReady("omsagent", "kube-system", kubeSystemPodsReadinessChecks, 1*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(running).To(Equal(true))
pods, err = pod.GetAllByPrefix("omsagent", "kube-system")
Expect(err).NotTo(HaveOccurred())
By("Ensuring that the cadvisor_perf plugin is writing data successfully")
pass, err = pods[0].ValidateOmsAgentLogs("cAdvisorPerfEmitStreamSuccess", 1*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(pass).To(BeTrue())
By("Ensuring that the containerinventory plugin is writing data successfully")
pass, err = pods[0].ValidateOmsAgentLogs("containerInventoryEmitStreamSuccess", 1*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(pass).To(BeTrue())
} else {
Skip("container monitoring disabled for this cluster, will not test")
}
})
It("should be able to launch a long-running container networking DNS liveness pod", func() {
if !eng.HasNetworkPolicy("calico") {
var err error
var p *pod.Pod
p, err = pod.CreatePodFromFile(filepath.Join(WorkloadDir, "dns-liveness.yaml"), "dns-liveness", "default", 1*time.Second, cfg.Timeout)
if cfg.SoakClusterName == "" {
Expect(err).NotTo(HaveOccurred())
} else {
if err != nil {
p, err = pod.Get("dns-liveness", "default")
Expect(err).NotTo(HaveOccurred())
}
}
running, err := p.WaitOnReady(5*time.Second, 2*time.Minute)
Expect(err).NotTo(HaveOccurred())
Expect(running).To(Equal(true))
} else {
Skip("We don't run DNS liveness checks on calico clusters ( //TODO )")
}
})
It("should be able to launch a long running HTTP listener and svc endpoint", func() {
By("Creating a php-apache deployment")
var phpApacheDeploy *deployment.Deployment
d, _ := deployment.Get(longRunningApacheDeploymentName, "default")
if d == nil {
var err error
phpApacheDeploy, err = deployment.CreateLinuxDeploy("k8s.gcr.io/hpa-example", longRunningApacheDeploymentName, "default", "--requests=cpu=10m,memory=10M")
if err != nil {
fmt.Println(err)
}
Expect(err).NotTo(HaveOccurred())
} else {
phpApacheDeploy = d
}
By("Ensuring that php-apache pod is running")
running, err := pod.WaitOnReady(longRunningApacheDeploymentName, "default", 3, 1*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(running).To(Equal(true))
By("Ensuing that the php-apache pod has outbound internet access")
pods, err := phpApacheDeploy.Pods()
Expect(err).NotTo(HaveOccurred())
for _, p := range pods {
p.CheckLinuxOutboundConnection(5*time.Second, cfg.Timeout)
}
By("Exposing TCP 80 internally on the php-apache deployment")
s, _ := service.Get(longRunningApacheDeploymentName, "default")
if s == nil {
err := phpApacheDeploy.Expose("ClusterIP", 80, 80)
Expect(err).NotTo(HaveOccurred())
_, err = service.Get(longRunningApacheDeploymentName, "default")
Expect(err).NotTo(HaveOccurred())
}
})
It("should have stable external container networking as we recycle a bunch of pods", func() {
name := fmt.Sprintf("alpine-%s", cfg.Name)
command := fmt.Sprintf("nc -vz 8.8.8.8 53 || nc -vz 8.8.4.4 53")
successes, err := pod.RunCommandMultipleTimes(pod.RunLinuxPod, "alpine", name, command, cfg.StabilityIterations, 1*time.Second, retryCommandsTimeout)
Expect(err).NotTo(HaveOccurred())
Expect(successes).To(Equal(cfg.StabilityIterations))
})
It("should have stable internal container networking as we recycle a bunch of pods", func() {
name := fmt.Sprintf("alpine-%s", cfg.Name)
var command string
if common.IsKubernetesVersionGe(eng.ExpandedDefinition.Properties.OrchestratorProfile.OrchestratorVersion, "1.12.0") {
command = fmt.Sprintf("nc -vz kubernetes 443 && nc -vz kubernetes.default.svc 443 && nc -vz kubernetes.default.svc.cluster.local 443")
} else {
command = fmt.Sprintf("nc -vz kubernetes 443")
}
successes, err := pod.RunCommandMultipleTimes(pod.RunLinuxPod, "alpine", name, command, cfg.StabilityIterations, 1*time.Second, retryCommandsTimeout)
Expect(err).NotTo(HaveOccurred())
Expect(successes).To(Equal(cfg.StabilityIterations))
})
It("should have stable pod-to-pod networking", func() {
if eng.HasLinuxAgents() {
By("Creating a test php-apache deployment")
r := rand.New(rand.NewSource(time.Now().UnixNano()))
By("Creating another pod that will connect to the php-apache pod")
commandString := fmt.Sprintf("nc -vz %s.default.svc.cluster.local 80", longRunningApacheDeploymentName)
consumerPodName := fmt.Sprintf("consumer-pod-%s-%v", cfg.Name, r.Intn(99999))
successes, err := pod.RunCommandMultipleTimes(pod.RunLinuxPod, "busybox", consumerPodName, commandString, cfg.StabilityIterations, 1*time.Second, retryCommandsTimeout)
Expect(err).NotTo(HaveOccurred())
Expect(successes).To(Equal(cfg.StabilityIterations))
} else {
Skip("Pod-to-pod network tests only valid on Linux clusters")
}
})
It("should have functional host OS DNS", func() {
kubeConfig, err := GetConfig()
Expect(err).NotTo(HaveOccurred())
master := fmt.Sprintf("azureuser@%s", kubeConfig.GetServerName())
ifconfigCmd := fmt.Sprintf("ifconfig -a -v")
cmd := exec.Command("ssh", "-i", masterSSHPrivateKeyFilepath, "-p", masterSSHPort, "-o", "ConnectTimeout=10", "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", master, ifconfigCmd)
util.PrintCommand(cmd)
out, err := cmd.CombinedOutput()
log.Printf("%s\n", out)
if err != nil {
log.Printf("Error while querying DNS: %s\n", err)
}
resolvCmd := fmt.Sprintf("cat /etc/resolv.conf")
cmd = exec.Command("ssh", "-i", masterSSHPrivateKeyFilepath, "-p", masterSSHPort, "-o", "ConnectTimeout=10", "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", master, resolvCmd)
util.PrintCommand(cmd)
out, err = cmd.CombinedOutput()
log.Printf("%s\n", out)
if err != nil {
log.Printf("Error while querying DNS: %s\n", err)
}
By("Ensuring that we have a valid connection to our resolver")
digCmd := fmt.Sprintf("dig +short +search +answer `hostname`")
cmd = exec.Command("ssh", "-i", masterSSHPrivateKeyFilepath, "-p", masterSSHPort, "-o", "ConnectTimeout=10", "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", master, digCmd)
util.PrintCommand(cmd)
out, err = cmd.CombinedOutput()
log.Printf("%s\n", out)
if err != nil {
log.Printf("Error while querying DNS: %s\n", err)
}
nodeList, err := node.Get()
Expect(err).NotTo(HaveOccurred())
for _, node := range nodeList.Nodes {
By("Ensuring that we get a DNS lookup answer response for each node hostname")
digCmd := fmt.Sprintf("dig +short +search +answer %s | grep -v -e '^$'", node.Metadata.Name)
cmd = exec.Command("ssh", "-i", masterSSHPrivateKeyFilepath, "-p", masterSSHPort, "-o", "ConnectTimeout=10", "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", master, digCmd)
util.PrintCommand(cmd)
out, err = cmd.CombinedOutput()
log.Printf("%s\n", out)
if err != nil {
log.Printf("Error while querying DNS: %s\n", err)
}
Expect(err).NotTo(HaveOccurred())
}
By("Ensuring that we get a DNS lookup answer response for external names")
digCmd = fmt.Sprintf("dig +short +search www.bing.com | grep -v -e '^$'")
cmd = exec.Command("ssh", "-i", masterSSHPrivateKeyFilepath, "-p", masterSSHPort, "-o", "ConnectTimeout=10", "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", master, digCmd)
util.PrintCommand(cmd)
out, err = cmd.CombinedOutput()
if err != nil {
log.Printf("Error while querying DNS: %s\n", out)
}
digCmd = fmt.Sprintf("dig +short +search google.com | grep -v -e '^$'")
cmd = exec.Command("ssh", "-i", masterSSHPrivateKeyFilepath, "-p", masterSSHPort, "-o", "ConnectTimeout=10", "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", master, digCmd)
util.PrintCommand(cmd)
out, err = cmd.CombinedOutput()
log.Printf("%s\n", out)
if err != nil {
log.Printf("Error while querying DNS: %s\n", err)
}
By("Ensuring that we get a DNS lookup answer response for external names using external resolver")
digCmd = fmt.Sprintf("dig +short +search www.bing.com @8.8.8.8 | grep -v -e '^$'")
cmd = exec.Command("ssh", "-i", masterSSHPrivateKeyFilepath, "-p", masterSSHPort, "-o", "ConnectTimeout=10", "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", master, digCmd)
util.PrintCommand(cmd)
out, err = cmd.CombinedOutput()
log.Printf("%s\n", out)
if err != nil {
log.Printf("Error while querying DNS: %s\n", err)
}
digCmd = fmt.Sprintf("dig +short +search google.com @8.8.8.8 | grep -v -e '^$'")
cmd = exec.Command("ssh", "-i", masterSSHPrivateKeyFilepath, "-p", masterSSHPort, "-o", "ConnectTimeout=10", "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", master, digCmd)
util.PrintCommand(cmd)
out, err = cmd.CombinedOutput()
log.Printf("%s\n", out)
if err != nil {
log.Printf("Error while querying DNS: %s\n", err)
}
})
It("should have functional container networking DNS", func() {
By("Ensuring that we have functional DNS resolution from a container")
// "Pre"-delete the job in case a prior delete attempt failed, for long-running cluster scenarios
j, err := job.Get("validate-dns", "default")
if err == nil {
j.Delete(deleteResourceRetries)
// Wait a minute before proceeding to create a new job w/ the same name
time.Sleep(1 * time.Minute)
}
j, err = job.CreateJobFromFile(filepath.Join(WorkloadDir, "validate-dns.yaml"), "validate-dns", "default")
Expect(err).NotTo(HaveOccurred())
ready, err := j.WaitOnReady(5*time.Second, cfg.Timeout)
delErr := j.Delete(deleteResourceRetries)
if delErr != nil {
fmt.Printf("could not delete job %s\n", j.Metadata.Name)
fmt.Println(delErr)
}
Expect(err).NotTo(HaveOccurred())
Expect(ready).To(Equal(true))
By("Ensuring that we have stable external DNS resolution as we recycle a bunch of pods")
name := fmt.Sprintf("alpine-%s", cfg.Name)
command := fmt.Sprintf("nc -vz bbc.co.uk 80 || nc -vz google.com 443 || nc -vz microsoft.com 80")
successes, err := pod.RunCommandMultipleTimes(pod.RunLinuxPod, "alpine", name, command, cfg.StabilityIterations, 1*time.Second, retryCommandsTimeout)
Expect(err).NotTo(HaveOccurred())
Expect(successes).To(Equal(cfg.StabilityIterations))
})
It("should be able to access the dashboard from each node", func() {
if hasDashboard, dashboardAddon := eng.HasAddon("kubernetes-dashboard"); hasDashboard {
By("Ensuring that the kubernetes-dashboard service is Running")
s, err := service.Get("kubernetes-dashboard", "kube-system")
Expect(err).NotTo(HaveOccurred())
if !eng.HasWindowsAgents() {
By("Gathering connection information to determine whether or not to connect via HTTP or HTTPS")
dashboardPort := 443
version, err := node.Version()
Expect(err).NotTo(HaveOccurred())
re := regexp.MustCompile("1.(5|6|7|8).")
if re.FindString(version) != "" {
dashboardPort = 80
}
port := s.GetNodePort(dashboardPort)
kubeConfig, err := GetConfig()
Expect(err).NotTo(HaveOccurred())
master := fmt.Sprintf("azureuser@%s", kubeConfig.GetServerName())
if dashboardPort == 80 {
By("Ensuring that we can connect via HTTP to the dashboard on any one node")
} else {
By("Ensuring that we can connect via HTTPS to the dashboard on any one node")
}
nodeList, err := node.Get()
Expect(err).NotTo(HaveOccurred())
for _, node := range nodeList.Nodes {
success := false
for i := 0; i < 60; i++ {
address := node.Status.GetAddressByType("InternalIP")
if address == nil {
log.Printf("One of our nodes does not have an InternalIP value!: %s\n", node.Metadata.Name)
}
Expect(address).NotTo(BeNil())
dashboardURL := fmt.Sprintf("http://%s:%v", address.Address, port)
curlCMD := fmt.Sprintf("curl --max-time 60 %s", dashboardURL)
cmd := exec.Command("ssh", "-i", masterSSHPrivateKeyFilepath, "-p", masterSSHPort, "-o", "ConnectTimeout=10", "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", master, curlCMD)
util.PrintCommand(cmd)
out, err := cmd.CombinedOutput()
if err == nil {
success = true
break
}
if i > 58 {
log.Printf("Error while connecting to Windows dashboard:%s\n", err)
log.Println(string(out))
}
time.Sleep(10 * time.Second)
}
Expect(success).To(BeTrue())
}
By("Ensuring that the correct resources have been applied")
// Assuming one dashboard pod
pods, err := pod.GetAllByPrefix("kubernetes-dashboard", "kube-system")
Expect(err).NotTo(HaveOccurred())
for i, c := range dashboardAddon.Containers {
err := pods[0].Spec.Containers[i].ValidateResources(c)
Expect(err).NotTo(HaveOccurred())
}
}
} else {
Skip("kubernetes-dashboard disabled for this cluster, will not test")
}
})
})
Describe("with a linux agent pool", func() {
It("should be able to produce a working ILB connection", func() {
if eng.HasLinuxAgents() {
By("Creating a nginx deployment")
r := rand.New(rand.NewSource(time.Now().UnixNano()))
serviceName := "ingress-nginx"
deploymentName := fmt.Sprintf("ingress-nginx-%s-%v", cfg.Name, r.Intn(99999))
d, _ := deployment.Get(deploymentName, "default")
if d != nil {
err := d.Delete(deleteResourceRetries)
Expect(err).NotTo(HaveOccurred())
}
deploy, err := deployment.CreateLinuxDeploy("library/nginx:latest", deploymentName, "default", "--labels=app="+serviceName)
Expect(err).NotTo(HaveOccurred())
s, _ := service.Get(serviceName, "default")
if s != nil {
err := s.Delete(deleteResourceRetries)
Expect(err).NotTo(HaveOccurred())
}
s, err = service.CreateServiceFromFile(filepath.Join(WorkloadDir, "ingress-nginx-ilb.yaml"), serviceName, "default")
Expect(err).NotTo(HaveOccurred())
svc, err := s.WaitForExternalIP(cfg.Timeout, 5*time.Second)
Expect(err).NotTo(HaveOccurred())
By("Ensuring the ILB IP is assigned to the service")
curlDeploymentName := fmt.Sprintf("ilb-test-deployment-%s", cfg.Name)
curlDeploy, err := deployment.CreateLinuxDeployIfNotExist("library/nginx:latest", curlDeploymentName, "default", "")
Expect(err).NotTo(HaveOccurred())
running, err := pod.WaitOnReady(curlDeploymentName, "default", 3, 1*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(running).To(Equal(true))
curlPods, err := curlDeploy.Pods()
Expect(err).NotTo(HaveOccurred())
for i, curlPod := range curlPods {
if i < 1 {
pass, err := curlPod.ValidateCurlConnection(svc.Status.LoadBalancer.Ingress[0]["ip"], 5*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(pass).To(BeTrue())
}
}
By("Cleaning up after ourselves")
err = curlDeploy.Delete(deleteResourceRetries)
Expect(err).NotTo(HaveOccurred())
err = deploy.Delete(deleteResourceRetries)
Expect(err).NotTo(HaveOccurred())
err = s.Delete(deleteResourceRetries)
Expect(err).NotTo(HaveOccurred())
} else {
Skip("No linux agent was provisioned for this Cluster Definition")
}
})
It("should be able to get nodes metrics", func() {
if eng.ExpandedDefinition.Properties.OrchestratorProfile.KubernetesConfig.IsRBACEnabled() {
success := false
for i := 0; i < 30; i++ {
cmd := exec.Command("kubectl", "top", "nodes")
util.PrintCommand(cmd)
out, err := cmd.CombinedOutput()
if err == nil {
success = true
break
}
if i > 28 {
log.Printf("Error while running kubectl top nodes:%s\n", err)
pods, _ := pod.GetAllByPrefix("metrics-server", "kube-system")
if len(pods) != 0 {
for _, p := range pods {
p.Logs()
}
}
log.Println(string(out))
}
}
Expect(success).To(BeTrue())
}
})
It("should be able to autoscale", func() {
if eng.HasLinuxAgents() && eng.ExpandedDefinition.Properties.OrchestratorProfile.KubernetesConfig.EnableAggregatedAPIs {
// "Pre"-delete the hpa in case a prior delete attempt failed, for long-running cluster scenarios
h, err := hpa.Get(longRunningApacheDeploymentName, "default")
if err == nil {
h.Delete(deleteResourceRetries)
// Wait a minute before proceeding to create a new hpa w/ the same name
time.Sleep(1 * time.Minute)
}
By("Getting the long-running php-apache deployment")
// Inspired by http://blog.kubernetes.io/2016/07/autoscaling-in-kubernetes.html
r := rand.New(rand.NewSource(time.Now().UnixNano()))
phpApacheDeploy, err := deployment.Get(longRunningApacheDeploymentName, "default")
if err != nil {
fmt.Println(err)
}
Expect(err).NotTo(HaveOccurred())
By("Ensuring that one php-apache pod is running before autoscale configuration or load applied")
running, err := pod.WaitOnReady(longRunningApacheDeploymentName, "default", 3, 1*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(running).To(Equal(true))
phpPods, err := phpApacheDeploy.Pods()
Expect(err).NotTo(HaveOccurred())
// We should have exactly 1 pod to begin
Expect(len(phpPods)).To(Equal(1))
By("Assigning hpa configuration to the php-apache deployment")
// Apply autoscale characteristics to deployment
err = phpApacheDeploy.CreateDeploymentHPA(5, 1, 10)
Expect(err).NotTo(HaveOccurred())
By("Sending load to the php-apache service by creating a 3 replica deployment")
// Launch a simple busybox pod that wget's continuously to the apache serviceto simulate load
commandString := fmt.Sprintf("while true; do wget -q -O- http://%s.default.svc.cluster.local; done", longRunningApacheDeploymentName)
loadTestName := fmt.Sprintf("load-test-%s-%v", cfg.Name, r.Intn(99999))
numLoadTestPods := 3
loadTestDeploy, err := deployment.RunLinuxDeploy("busybox", loadTestName, "default", commandString, numLoadTestPods)
Expect(err).NotTo(HaveOccurred())
By("Ensuring there are 3 load test pods")
running, err = pod.WaitOnReady(loadTestName, "default", 3, 1*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(running).To(Equal(true))
// We should have three load tester pods running
loadTestPods, err := loadTestDeploy.Pods()
Expect(err).NotTo(HaveOccurred())
Expect(len(loadTestPods)).To(Equal(numLoadTestPods))
By("Ensuring we have more than 1 apache-php pods due to hpa enforcement")
_, err = phpApacheDeploy.WaitForReplicas(2, -1, 5*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
By("Stopping load")
err = loadTestDeploy.Delete(deleteResourceRetries)
Expect(err).NotTo(HaveOccurred())
By("Ensuring we only have 1 apache-php pod after stopping load")
_, err = phpApacheDeploy.WaitForReplicas(-1, 1, 5*time.Second, 20*time.Minute)
Expect(err).NotTo(HaveOccurred())
h, err = hpa.Get(longRunningApacheDeploymentName, "default")
Expect(err).NotTo(HaveOccurred())
By("Deleting HPA configuration")
err = h.Delete(deleteResourceRetries)
Expect(err).NotTo(HaveOccurred())
} else {
Skip("This flavor/version of Kubernetes doesn't support hpa autoscale")
}
})
It("should be able to deploy an nginx service", func() {
if eng.HasLinuxAgents() {
By("Creating a nginx deployment")
r := rand.New(rand.NewSource(time.Now().UnixNano()))
deploymentName := fmt.Sprintf("nginx-%s-%v", cfg.Name, r.Intn(99999))
nginxDeploy, err := deployment.CreateLinuxDeploy("library/nginx:latest", deploymentName, "default", "")
Expect(err).NotTo(HaveOccurred())
By("Ensure there is a Running nginx pod")
running, err := pod.WaitOnReady(deploymentName, "default", 3, 1*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(running).To(Equal(true))
By("Exposing TCP 80 LB on the nginx deployment")
err = nginxDeploy.Expose("LoadBalancer", 80, 80)
Expect(err).NotTo(HaveOccurred())
By("Ensuring we can connect to the service")
s, err := service.Get(deploymentName, "default")
Expect(err).NotTo(HaveOccurred())
By("Ensuring the service root URL returns the expected payload")
valid := s.Validate("(Welcome to nginx)", 5, 30*time.Second, cfg.Timeout)
Expect(valid).To(BeTrue())
By("Cleaning up after ourselves")
err = nginxDeploy.Delete(deleteResourceRetries)
Expect(err).NotTo(HaveOccurred())
err = s.Delete(deleteResourceRetries)
Expect(err).NotTo(HaveOccurred())
} else {
Skip("No linux agent was provisioned for this Cluster Definition")
}
})
It("should be able to schedule a pod to a master node", func() {
By("Creating a pod with master nodeSelector")
p, err := pod.CreatePodFromFile(filepath.Join(WorkloadDir, "nginx-master.yaml"), "nginx-master", "default", 1*time.Second, cfg.Timeout)
if err != nil {
p, err = pod.Get("nginx-master", "default")
Expect(err).NotTo(HaveOccurred())
}
running, err := p.WaitOnReady(5*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(running).To(Equal(true))
By("validating that master-scheduled pod has outbound internet connectivity")
pass, err := p.CheckLinuxOutboundConnection(5*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(pass).To(BeTrue())
By("Cleaning up after ourselves")
err = p.Delete(deleteResourceRetries)
Expect(err).NotTo(HaveOccurred())
})
})
Describe("with a GPU-enabled agent pool", func() {
It("should be able to run a nvidia-gpu job", func() {
if eng.ExpandedDefinition.Properties.HasNSeriesSKU() {
version := common.RationalizeReleaseAndVersion(
common.Kubernetes,
eng.ClusterDefinition.Properties.OrchestratorProfile.OrchestratorRelease,
eng.ClusterDefinition.Properties.OrchestratorProfile.OrchestratorVersion,
false,
eng.HasWindowsAgents())
if common.IsKubernetesVersionGe(version, "1.10.0") {
j, err := job.CreateJobFromFile(filepath.Join(WorkloadDir, "cuda-vector-add.yaml"), "cuda-vector-add", "default")
Expect(err).NotTo(HaveOccurred())
ready, err := j.WaitOnReady(30*time.Second, cfg.Timeout)
delErr := j.Delete(deleteResourceRetries)
if delErr != nil {
fmt.Printf("could not delete job %s\n", j.Metadata.Name)
fmt.Println(delErr)
}
Expect(err).NotTo(HaveOccurred())
Expect(ready).To(Equal(true))
} else {
j, err := job.CreateJobFromFile(filepath.Join(WorkloadDir, "nvidia-smi.yaml"), "nvidia-smi", "default")
Expect(err).NotTo(HaveOccurred())
ready, err := j.WaitOnReady(30*time.Second, cfg.Timeout)
delErr := j.Delete(deleteResourceRetries)
if delErr != nil {
fmt.Printf("could not delete job %s\n", j.Metadata.Name)
fmt.Println(delErr)
}
Expect(err).NotTo(HaveOccurred())
Expect(ready).To(Equal(true))
}
} else {
Skip("This is not a GPU-enabled cluster")
}
})
})
Describe("with zoned master profile", func() {
It("should be labeled with zones for each masternode", func() {
if eng.ExpandedDefinition.Properties.MasterProfile.HasAvailabilityZones() {
nodeList, err := node.Get()
Expect(err).NotTo(HaveOccurred())
for _, node := range nodeList.Nodes {
role := node.Metadata.Labels["kubernetes.io/role"]
if role == "master" {
By("Ensuring that we get zones for each master node")
zones := node.Metadata.Labels["failure-domain.beta.kubernetes.io/zone"]
contains := strings.Contains(zones, "-")
Expect(contains).To(Equal(true))
}
}
} else {
Skip("Availability zones was not configured for master profile for this Cluster Definition")
}
})
})
Describe("with all zoned agent pools", func() {
It("should be labeled with zones for each node", func() {
if eng.ExpandedDefinition.Properties.HasZonesForAllAgentPools() {
nodeList, err := node.Get()
Expect(err).NotTo(HaveOccurred())
for _, node := range nodeList.Nodes {
role := node.Metadata.Labels["kubernetes.io/role"]
if role == "agent" {
By("Ensuring that we get zones for each agent node")
zones := node.Metadata.Labels["failure-domain.beta.kubernetes.io/zone"]
contains := strings.Contains(zones, "-")
Expect(contains).To(Equal(true))
}
}
} else {
Skip("Availability zones was not configured for this Cluster Definition")
}
})
It("should create pv with zone labels and node affinity", func() {
if eng.ExpandedDefinition.Properties.HasZonesForAllAgentPools() {
By("Creating a persistent volume claim")
pvcName := "azure-managed-disk" // should be the same as in pvc-premium.yaml
pvc, err := persistentvolumeclaims.CreatePersistentVolumeClaimsFromFile(filepath.Join(WorkloadDir, "pvc-premium.yaml"), pvcName, "default")
Expect(err).NotTo(HaveOccurred())
ready, err := pvc.WaitOnReady("default", 5*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(ready).To(Equal(true))
pvList, err := persistentvolume.Get()
Expect(err).NotTo(HaveOccurred())
pvZone := ""
for _, pv := range pvList.PersistentVolumes {
By("Ensuring that we get zones for the pv")
// zone is chosen by round-robin across all zones
pvZone = pv.Metadata.Labels["failure-domain.beta.kubernetes.io/zone"]
fmt.Printf("pvZone: %s\n", pvZone)
contains := strings.Contains(pvZone, "-")
Expect(contains).To(Equal(true))
// VolumeScheduling feature gate is set to true by default starting v1.10+
for _, expression := range pv.Spec.NodeAffinity.Required.NodeSelectorTerms[0].MatchExpressions {
if expression.Key == "failure-domain.beta.kubernetes.io/zone" {
By("Ensuring that we get nodeAffinity for each pv")
value := expression.Values[0]
fmt.Printf("NodeAffinity value: %s\n", value)
contains := strings.Contains(value, "-")
Expect(contains).To(Equal(true))
}
}
}
By("Launching a pod using the volume claim")
podName := "zone-pv-pod" // should be the same as in pod-pvc.yaml
testPod, err := pod.CreatePodFromFile(filepath.Join(WorkloadDir, "pod-pvc.yaml"), podName, "default", 1*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
ready, err = testPod.WaitOnReady(5*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(ready).To(Equal(true))
By("Checking that the pod can access volume")
valid, err := testPod.ValidatePVC("/mnt/azure", 10, 10*time.Second)
Expect(valid).To(BeTrue())
Expect(err).NotTo(HaveOccurred())
By("Ensuring that attached volume pv has the same zone as the zone of the node")
nodeName := testPod.Spec.NodeName
nodeList, err := node.GetByPrefix(nodeName)
Expect(err).NotTo(HaveOccurred())
nodeZone := nodeList[0].Metadata.Labels["failure-domain.beta.kubernetes.io/zone"]
fmt.Printf("pvZone: %s\n", pvZone)
fmt.Printf("nodeZone: %s\n", nodeZone)
Expect(nodeZone == pvZone).To(Equal(true))
By("Cleaning up after ourselves")
err = testPod.Delete(deleteResourceRetries)
Expect(err).NotTo(HaveOccurred())
err = pvc.Delete(deleteResourceRetries)
Expect(err).NotTo(HaveOccurred())
} else {
Skip("Availability zones was not configured for this Cluster Definition")
}
})
})
Describe("with NetworkPolicy enabled", func() {
It("should apply various network policies and enforce access to nginx pod", func() {
if eng.HasNetworkPolicy("calico") || eng.HasNetworkPolicy("azure") || eng.HasNetworkPolicy("cilium") {
nsClientOne, nsClientTwo, nsServer := "client-one", "client-two", "server"
By("Creating namespaces")
namespaceClientOne, err := namespace.CreateIfNotExist(nsClientOne)
Expect(err).NotTo(HaveOccurred())
namespaceClientTwo, err := namespace.CreateIfNotExist(nsClientTwo)
Expect(err).NotTo(HaveOccurred())
namespaceServer, err := namespace.CreateIfNotExist(nsServer)
Expect(err).NotTo(HaveOccurred())
By("Creating client and server nginx deployments")
r := rand.New(rand.NewSource(time.Now().UnixNano()))
randInt := r.Intn(99999)
clientOneDeploymentName := fmt.Sprintf("nginx-%s-%v", cfg.Name, randInt)
clientTwoDeploymentName := fmt.Sprintf("nginx-%s-%v", cfg.Name, randInt+100000)
serverDeploymentName := fmt.Sprintf("nginx-%s-%v", cfg.Name, randInt+200000)
clientOneDeploy, err := deployment.CreateLinuxDeploy("library/nginx:latest", clientOneDeploymentName, nsClientOne, "--labels=role=client-one")
Expect(err).NotTo(HaveOccurred())
clientTwoDeploy, err := deployment.CreateLinuxDeploy("library/nginx:latest", clientTwoDeploymentName, nsClientTwo, "--labels=role=client-two")
Expect(err).NotTo(HaveOccurred())
serverDeploy, err := deployment.CreateLinuxDeploy("library/nginx:latest", serverDeploymentName, nsServer, "--labels=role=server")
Expect(err).NotTo(HaveOccurred())
By("Ensure there is a Running nginx client one pod")
running, err := pod.WaitOnReady(clientOneDeploymentName, nsClientOne, 3, 1*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(running).To(Equal(true))
By("Ensure there is a Running nginx client two pod")
running, err = pod.WaitOnReady(clientTwoDeploymentName, nsClientTwo, 3, 1*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(running).To(Equal(true))
By("Ensure there is a Running nginx server pod")
running, err = pod.WaitOnReady(serverDeploymentName, nsServer, 3, 1*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(running).To(Equal(true))
By("Ensuring we have outbound internet access from the nginx client one pods")
clientOnePods, err := clientOneDeploy.Pods()
Expect(err).NotTo(HaveOccurred())
Expect(len(clientOnePods)).ToNot(BeZero())
for _, clientOnePod := range clientOnePods {
pass, err := clientOnePod.CheckLinuxOutboundConnection(5*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(pass).To(BeTrue())
}
By("Ensuring we have outbound internet access from the nginx client one pods")
clientTwoPods, err := clientTwoDeploy.Pods()
Expect(err).NotTo(HaveOccurred())
Expect(len(clientTwoPods)).ToNot(BeZero())
for _, clientTwoPod := range clientTwoPods {
pass, err := clientTwoPod.CheckLinuxOutboundConnection(5*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(pass).To(BeTrue())
}
By("Ensuring we have outbound internet access from the nginx server pods")
serverPods, err := serverDeploy.Pods()
Expect(err).NotTo(HaveOccurred())
Expect(len(serverPods)).ToNot(BeZero())
for _, serverPod := range serverPods {
pass, err := serverPod.CheckLinuxOutboundConnection(5*time.Second, cfg.Timeout)
Expect(err).NotTo(HaveOccurred())
Expect(pass).To(BeTrue())
}
var (
networkPolicyName string
namespace string