forked from kubernetes-retired/kpng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_e2e.sh
executable file
·1117 lines (945 loc) · 47.2 KB
/
test_e2e.sh
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
#!/bin/bash
# shellcheck disable=SC2181,SC2155,SC2128
#
# Copyright 2021 The Kubernetes Authors.
#
# 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.
shopt -s expand_aliases
: "${E2E_GO_VERSION:="1.17.3"}"
: "${E2E_K8S_VERSION:="v1.23.3"}"
: "${E2E_TIMEOUT_MINUTES:=100}"
: "${KPNG_DEBUG_LEVEL:=4}"
OS=$(uname| tr '[:upper:]' '[:lower:]')
CONTAINER_ENGINE="docker"
KPNG_IMAGE_TAG_NAME="kpng:test"
KUBECONFIG_TESTS="kubeconfig_tests.conf"
# kind
KIND_VERSION="v0.11.1"
# system data
NAMESPACE="kube-system"
CONFIG_MAP_NAME="kpng"
SERVICE_ACCOUNT_NAME="kpng"
CLUSTER_ROLE_NAME="system:node-proxier"
CLUSTER_ROLE_BINDING_NAME="kpng"
# ginkgo
GINKGO_NUMBER_OF_NODES=25
GINKGO_FOCUS="\[Conformance\]|\[sig-network\]"
GINKGO_SKIP_TESTS="machinery|Feature|Federation|PerformanceDNS|Disruptive|Serial|LoadBalancer|KubeProxy|GCE|Netpol|NetworkPolicy"
GINKGO_REPORT_DIR="artifacts/reports"
GINKGO_DUMP_LOGS_ON_FAILURE=false
GINKGO_DISABLE_LOG_DUMP=true
GINKGO_PROVIDER="local"
# Users can specify docker.io, quay.io registry
KINDEST_NODE_IMAGE="docker.io/kindest/node"
function if_error_exit {
###########################################################################
# Description: #
# Validate if previous command failed and show an error msg (if provided) #
# #
# Arguments: #
# $1 - error message if not provided, it will just exit #
###########################################################################
if [ "$?" != "0" ]; then
if [ -n "$1" ]; then
RED="\e[31m"
ENDCOLOR="\e[0m"
echo -e "[ ${RED}FAILED${ENDCOLOR} ] ${1}"
fi
exit 1
fi
}
function if_error_warning {
###########################################################################
# Description: #
# Validate if previous command failed and show an error msg (if provided) #
# #
# Arguments: #
# $1 - error message if not provided, it will just exit #
###########################################################################
if [ "$?" != "0" ]; then
if [ -n "$1" ]; then
RED="\e[31m"
ENDCOLOR="\e[0m"
echo -e "[ ${RED}FAILED${ENDCOLOR} ] ${1}"
fi
fi
}
function pass_message {
###########################################################################
# Description: #
# show [PASSED] in green and a message as the validation passed. #
# #
# Arguments: #
# $1 - message to output #
###########################################################################
if [ -z "${1}" ]; then
echo "pass_message() requires a message"
exit 1
fi
GREEN="\e[32m"
ENDCOLOR="\e[0m"
echo -e "[ ${GREEN}PASSED${ENDCOLOR} ] ${1}"
}
function detect_container_engine {
###########################################################################
# Description: #
# Detect Container Engine, by default it is docker but developers might #
# use real alternatives like podman. The project welcome both. #
# #
# Arguments: #
# None #
###########################################################################
# If docker is not available, let's check if podman exists
${CONTAINER_ENGINE} &> /dev/null
if [ "$?" != "0" ]; then
CONTAINER_ENGINE="podman"
${CONTAINER_ENGINE} --help &> /dev/null
if_error_exit "the e2e tests currently support docker and podman as the container engine. Please install either of them"
fi
pass_message "Detected Container Engine: ${CONTAINER_ENGINE}"
}
function container_build {
###########################################################################
# Description: #
# build a container image for KPNG #
# #
# Arguments: #
# arg1: Path for E2E installation directory, or the empty string #
# arg2: ci_mode #
###########################################################################
[ $# -eq 2 ]
if_error_exit "Wrong number of arguments to ${FUNCNAME[0]}"
local CONTAINER_FILE=${1}
local ci_mode=${2}
# Running locally it's not necessary to show all info
QUIET_MODE="--quiet"
if [ "${ci_mode}" = true ] ; then
QUIET_MODE=""
fi
[ -f "${CONTAINER_FILE}" ]
if_error_exit "cannot find ${CONTAINER_FILE}"
CMD_BUILD_IMAGE=("${CONTAINER_ENGINE} build ${QUIET_MODE} -t ${KPNG_IMAGE_TAG_NAME} -f ${CONTAINER_FILE} .")
pushd "${0%/*}/.." > /dev/null || exit
if [ -z "${QUIET_MODE}" ]; then
${CMD_BUILD_IMAGE}
else
${CMD_BUILD_IMAGE} &> /dev/null
fi
if_error_exit "Failed to build kpng, command was: ${CMD_BUILD_IMAGE}"
popd > /dev/null || exit
pass_message "Image build and tag ${KPNG_IMAGE_TAG_NAME} is set."
}
function setup_kind {
###########################################################################
# Description: #
# setup kind if not available in the system #
# #
# Arguments: #
# arg1: installation directory, path to where kind will be installed #
###########################################################################
[ $# -eq 1 ]
if_error_exit "Wrong number of arguments to ${FUNCNAME[0]}"
local install_directory=$1
[ -d "${install_directory}" ]
if_error_exit "Directory \"${install_directory}\" does not exist"
if ! [ -f "${install_directory}"/kind ] ; then
echo -e "\nDownloading kind ..."
local tmp_file=$(mktemp -q)
if_error_exit "Could not create temp file, mktemp failed"
curl -L https://kind.sigs.k8s.io/dl/"${KIND_VERSION}"/kind-"${OS}"-amd64 -o "${tmp_file}"
if_error_exit "cannot download kind"
sudo mv "${tmp_file}" "${install_directory}"/kind
sudo chmod +rx "${install_directory}"/kind
sudo chown root.root "${install_directory}"/kind
fi
pass_message "The kind tool is set."
}
function setup_kubectl {
###########################################################################
# Description: #
# setup kubectl if not available in the system #
# #
# Arguments: #
# arg1: installation directory, path to where kubectl will be installed #
###########################################################################
[ $# -eq 1 ]
if_error_exit "Wrong number of arguments to ${FUNCNAME[0]}"
local install_directory=$1
[ -d "${install_directory}" ]
if_error_exit "Directory \"${install_directory}\" does not exist"
if ! [ -f "${install_directory}"/kubectl ] ; then
echo -e "\nDownloading kubectl ..."
local tmp_file=$(mktemp -q)
if_error_exit "Could not create temp file, mktemp failed"
curl -L https://dl.k8s.io/"${E2E_K8S_VERSION}"/bin/"${OS}"/amd64/kubectl -o "${tmp_file}"
if_error_exit "cannot download kubectl"
sudo mv "${tmp_file}" "${install_directory}"/kubectl
sudo chmod +rx "${install_directory}"/kubectl
sudo chown root.root "${install_directory}"/kubectl
fi
pass_message "The kubectl tool is set."
}
function setup_ginkgo {
###########################################################################
# Description: #
# setup ginkgo and e2e.test #
# #
# # Arguments: #
# arg1: binary directory, path to where ginko will be installed #
# arg2: Kubernetes version #
# arg3: OS, name of the operating system #
###########################################################################
[ $# -eq 3 ]
if_error_exit "Wrong number of arguments to ${FUNCNAME[0]}"
local bin_directory=${1}
local k8s_version=${2}
local os=${3}
local temp_directory=$(mktemp -qd)
if ! [ -f "${bin_directory}"/ginkgo ] || ! [ -f "${bin_directory}"/e2e.test ] ; then
echo -e "\nDownloading ginkgo and e2e.test ..."
curl -L https://dl.k8s.io/"${k8s_version}"/kubernetes-test-"${os}"-amd64.tar.gz \
-o "${temp_directory}"/kubernetes-test-"${os}"-amd64.tar.gz
if_error_exit "cannot download kubernetes-test package"
tar xvzf "${temp_directory}"/kubernetes-test-"${os}"-amd64.tar.gz \
--directory "${bin_directory}" \
--strip-components=3 kubernetes/test/bin/ginkgo kubernetes/test/bin/e2e.test &> /dev/null
rm -rf "${temp_directory}"
sudo chmod +rx "${bin_directory}/ginkgo"
sudo chmod +rx "${bin_directory}/e2e.test"
fi
pass_message "The tools ginko and e2e.test have been set up."
}
function create_cluster {
###########################################################################
# Description: #
# Create kind cluster #
# #
# Arguments: #
# arg1: cluster name #
# arg2: IP family #
# arg3: artifacts directory #
# arg4: ci_mode #
###########################################################################
[ $# -eq 4 ]
if_error_exit "Wrong number of arguments to ${FUNCNAME[0]}"
local cluster_name=${1}
local ip_family=${2}
local artifacts_directory=${3}
local ci_mode=${4}
# Get rid of any old cluster with the same name.
if kind get clusters | grep -q "${cluster_name}" &> /dev/null; then
kind delete cluster --name "${cluster_name}" &> /dev/null
if_error_exit "cannot delete cluster ${cluster_name}"
pass_message "Previous cluster ${cluster_name} deleted."
fi
# Default Log level for all components in test clusters
local kind_cluster_log_level=${KIND_CLUSTER_LOG_LEVEL:-4}
local kind_log_level="-v3"
if [ "${ci_mode}" = true ] ; then
kind_log_level="-v7"
fi
# potentially enable --logging-format
local scheduler_extra_args=" \"v\": \"${kind_cluster_log_level}\""
local controllerManager_extra_args=" \"v\": \"${kind_cluster_log_level}\""
local apiServer_extra_args=" \"v\": \"${kind_cluster_log_level}\""
# local kubelet_extra_args=" \"v\": \"${kind_cluster_log_level}\""
# JSON map injected into featureGates config
# local feature_gates='{"AllAlpha":false,"AllBeta":false}'
# --runtime-config argument value passed to the API server
# local runtime_config='{"api/alpha":"false", "api/beta":"false"}'
if [ -n "$CLUSTER_LOG_FORMAT" ]; then
scheduler_extra_args="${scheduler_extra_args}\"logging-format\": \"${CLUSTER_LOG_FORMAT}\""
controllerManager_extra_args="${controllerManager_extra_args}\"logging-format\": \"${CLUSTER_LOG_FORMAT}\""
apiServer_extra_args="${apiServer_extra_args}\"logging-format\": \"${CLUSTER_LOG_FORMAT}\""
fi
echo -e "\nPreparing to setup ${cluster_name} cluster ..."
# create cluster
# create the config file
cat <<EOF > "${artifacts_directory}/kind-config.yaml"
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
ipFamily: "${ip_family}"
nodes:
- role: control-plane
- role: worker
- role: worker
EOF
kind create cluster \
--name "${cluster_name}" \
--image "${KINDEST_NODE_IMAGE}":"${E2E_K8S_VERSION}" \
--retain \
--wait=1m \
"${kind_log_level}" \
"--config=${artifacts_directory}/kind-config.yaml"
if_error_exit "cannot create kind cluster ${cluster_name}"
# Patch kube-proxy to set the verbosity level
kubectl patch -n kube-system daemonset/kube-proxy \
--type='json' -p='[{"op": "add", "path": "/spec/template/spec/containers/0/command/-", "value": "--v='"${kind_cluster_log_level}"'" }]'
kind get kubeconfig --internal --name "${cluster_name}" > "${artifacts_directory}/kubeconfig.conf"
kind get kubeconfig --name "${cluster_name}" > "${artifacts_directory}/${KUBECONFIG_TESTS}"
# IPv6 clusters need some CoreDNS changes in order to work in k8s CI:
# 1. k8s CI doesn´t offer IPv6 connectivity, so CoreDNS should be configured
# to work in an offline environment:
# https://github.com/coredns/coredns/issues/2494#issuecomment-457215452
# 2. k8s CI adds following domains to resolv.conf search field:
# c.k8s-prow-builds.internal google.internal.
# CoreDNS should handle those domains and answer with NXDOMAIN instead of SERVFAIL
# otherwise pods stops trying to resolve the domain.
#
if [ "${ip_family}" = "ipv6" ]; then
local k8s_context="kind-${cluster_name}"
# Get the current config
local original_coredns=$(kubectl --context "${k8s_context}" get -oyaml -n=kube-system configmap/coredns)
echo "Original CoreDNS config:"
echo "${original_coredns}"
# Patch it
local fixed_coredns=$(
printf '%s' "${original_coredns}" | sed \
-e 's/^.*kubernetes cluster\.local/& internal/' \
-e '/^.*upstream$/d' \
-e '/^.*fallthrough.*$/d' \
-e '/^.*forward . \/etc\/resolv.conf$/d' \
-e '/^.*loop$/d' \
)
echo "Patched CoreDNS config:"
echo "${fixed_coredns}"
printf '%s' "${fixed_coredns}" | kubectl --context "${k8s_context}" apply -f -
fi
pass_message "Cluster ${cluster_name} is created."
}
function wait_until_cluster_is_ready {
###########################################################################
# Description: #
# Wait pods with selector k8s-app=kube-dns be ready and operational #
# #
# Arguments: #
# arg1: cluster name #
# arg2: ci_mode #
###########################################################################
[ $# -eq 2 ]
if_error_exit "Wrong number of arguments to ${FUNCNAME[0]}"
local cluster_name=${1}
local ci_mode=${2}
local k8s_context="kind-${cluster_name}"
kubectl --context "${k8s_context}" wait \
--for=condition=ready \
pods \
--namespace="${NAMESPACE}" \
--selector k8s-app=kube-dns 1> /dev/null
if [ "${ci_mode}" = true ] ; then
kubectl --context "${k8s_context}" get nodes -o wide
if_error_exit "unable to show nodes"
kubectl --context "${k8s_context}" get pods --all-namespaces
if_error_exit "error getting pods from all namespaces"
fi
pass_message "${cluster_name} is operational."
}
function delete_kind_cluster {
###########################################################################
# Description: #
# delete kind cluster #
# #
# Arguments: #
# arg1: cluster name #
###########################################################################
[ $# -eq 1 ]
if_error_exit "Wrong number of arguments to ${FUNCNAME[0]}"
local cluster_name="${1}"
if kind get clusters | grep -q "${cluster_name}" &> /dev/null; then
kind delete cluster --name "${cluster_name}" &> /dev/null
if_error_warning "cannot delete cluster ${cluster_name}"
pass_message "Cluster ${cluster_name} deleted."
fi
}
function install_kpng {
###########################################################################
# Description: #
# Install KPNG following these steps: #
# - removes existing kube-proxy #
# - load kpng container image #
# - create service account, clusterbinding and configmap for kpng #
# - deploy kpng from the template generated #
# #
# Arguments: #
# arg1: cluster name #
###########################################################################
[ $# -eq 1 ]
if_error_exit "Wrong number of arguments to ${FUNCNAME[0]}"
local cluster_name=$1
local k8s_context="kind-${cluster_name}"
# remove kube-proxy
kubectl --context "${k8s_context}" delete \
--namespace "${NAMESPACE}" \
daemonset.apps/kube-proxy 1> /dev/null
if_error_exit "cannot delete delete daemonset.apps kube-proxy"
pass_message "Removed daemonset.apps/kube-proxy."
# preload kpng image
# TODO move this to ci:
# docker load --input kpng-image.tar
CMD_KIND_LOAD_KPNG_TEST_IMAGE=("kind load docker-image ${KPNG_IMAGE_TAG_NAME} --name ${cluster_name}")
${CMD_KIND_LOAD_KPNG_TEST_IMAGE} &> /dev/null
if_error_exit "error loading image to kind, command was: ${CMD_KIND_LOAD_KPNG_TEST_IMAGE}"
pass_message "Loaded ${KPNG_IMAGE_TAG_NAME} container image."
# TODO this should be part of the template
kubectl --context "${k8s_context}" create serviceaccount \
--namespace "${NAMESPACE}" \
"${SERVICE_ACCOUNT_NAME}" 1> /dev/null
if_error_exit "error creating serviceaccount ${SERVICE_ACCOUNT_NAME}"
pass_message "Created service account ${SERVICE_ACCOUNT_NAME}."
kubectl --context "${k8s_context}" create clusterrolebinding \
"${CLUSTER_ROLE_BINDING_NAME}" \
--clusterrole="${CLUSTER_ROLE_NAME}" \
--serviceaccount="${NAMESPACE}":"${SERVICE_ACCOUNT_NAME}" 1> /dev/null
if_error_exit "error creating clusterrolebinding ${CLUSTER_ROLE_BINDING_NAME}"
pass_message "Created clusterrolebinding ${CLUSTER_ROLE_BINDING_NAME}."
kubectl --context "${k8s_context}" create configmap \
"${CONFIG_MAP_NAME}" \
--namespace "${NAMESPACE}" \
--from-file "${artifacts_directory}/kubeconfig.conf" 1> /dev/null
if_error_exit "error creating configmap ${CONFIG_MAP_NAME}"
pass_message "Created configmap ${CONFIG_MAP_NAME}."
# Setting vars for generate the kpng deployment based on template
export IMAGE="${KPNG_IMAGE_TAG_NAME}"
export PULL=IfNotPresent
export E2E_BACKEND
export CONFIG_MAP_NAME
export SERVICE_ACCOUNT_NAME
export NAMESPACE
export KPNG_DEBUG_LEVEL
envsubst <"${0%/*}"/kpng-deployment-ds.yaml.tmpl > "${artifacts_directory}"/kpng-deployment-ds.yaml
if_error_exit "error generating kpng deployment YAML"
kubectl --context "${k8s_context}" create -f "${artifacts_directory}"/kpng-deployment-ds.yaml 1> /dev/null
if_error_exit "error creating kpng deployment"
kubectl --context "${k8s_context}" --namespace="${NAMESPACE}" rollout status daemonset kpng -w --request-timeout=3m 1> /dev/null
if_error_exit "timeout waiting kpng rollout"
pass_message "Installation of kpng is done.\n"
}
function run_tests {
###########################################################################
# Description: #
# Execute the tests with ginkgo #
# #
# Arguments: #
# arg1: e2e directory #
# arg2: e2e_test, path to test binary #
# arg3: parallel ginkgo tests boolean #
###########################################################################
[ $# -eq 3 ]
if_error_exit "Wrong number of arguments to ${FUNCNAME[0]}"
local e2e_dir="${1}"
local e2e_test="${2}"
local parallel="${3}"
local artifacts_directory="${e2e_dir}/artifacts"
[ -f "${artifacts_directory}/${KUBECONFIG_TESTS}" ]
if_error_exit "Directory \"${artifacts_directory}/${KUBECONFIG_TESTS}\" does not exist"
[ -f "${e2e_test}" ]
if_error_exit "File \"${e2e_test}\" does not exist"
# ginkgo regexes
local ginkgo_skip="${GINKGO_SKIP_TESTS:-}"
local ginkgo_focus=${GINKGO_FOCUS:-"\\[Conformance\\]"}
# if we set PARALLEL=true, skip serial tests set --ginkgo-parallel
if [ "${parallel}" = "true" ]; then
export GINKGO_PARALLEL=y
if [ -z "${skip}" ]; then
ginkgo_skip="\\[Serial\\]"
else
ginkgo_skip="\\[Serial\\]|${ginkgo_skip}"
fi
fi
# setting this env prevents ginkgo e2e from trying to run provider setup
export KUBERNETES_CONFORMANCE_TEST='y'
# setting these is required to make RuntimeClass tests work ... :/
export KUBE_CONTAINER_RUNTIME=remote
export KUBE_CONTAINER_RUNTIME_ENDPOINT=unix:///run/containerd/containerd.sock
export KUBE_CONTAINER_RUNTIME_NAME=containerd
ginkgo --nodes="${GINKGO_NUMBER_OF_NODES}" \
--focus="${ginkgo_focus}" \
--skip="${ginkgo_skip}" \
"${e2e_test}" \
-- \
--kubeconfig="${artifacts_directory}/${KUBECONFIG_TESTS}" \
--provider="${GINKGO_PROVIDER}" \
--dump-logs-on-failure="${GINKGO_DUMP_LOGS_ON_FAILURE}" \
--report-dir="${GINKGO_REPORT_DIR}" \
--disable-log-dump="${GINKGO_DISABLE_LOG_DUMP}"
}
function clean_artifacts {
###########################################################################
# Description: #
# Clean all artifacts and export kind logs #
# #
# Arguments: #
# arg1: Path for E2E installation directory #
###########################################################################
[ $# -eq 1 ]
if_error_exit "Wrong number of arguments to ${FUNCNAME[0]}"
local e2e_dir="${1}"
local log_dir="${E2E_LOGS:-${e2e_dir}/artifacts/logs}"
kind export \
logs \
--name="$(cat "${e2e_dir}/clustername")" \
"${log_dir}"
if_error_exit "cannot export kind logs"
#TODO in local mode to avoid the overwriting of artifacts from test to test
# add logic to this function that moves the content of artifacts
# to a dir named clustername+date+time
pass_message "make sure to safe your result before the next run."
}
function verify_sysctl_setting {
###########################################################################
# Description: #
# Verify that a sysctl attribute setting has a value #
# #
# Arguments: #
# arg1: attribute #
# arg2: value #
###########################################################################
[ $# -eq 2 ]
if_error_exit "Wrong number of arguments to ${FUNCNAME[0]}"
local attribute="${1}"
local value="${2}"
local result=$(sysctl -n "${attribute}")
if_error_exit "\"sysctl -n ${attribute}\" failed}"
if [ ! "${value}" -eq "${result}" ] ; then
echo "Failure: \"sysctl -n ${attribute}\" returned \"${result}\", not \"${value}\" as expected."
exit
fi
}
function set_sysctl {
###########################################################################
# Description: #
# Set a sysctl attribute to value #
# #
# Arguments: #
# arg1: attribute #
# arg2: value #
###########################################################################
[ $# -eq 2 ]
if_error_exit "Wrong number of arguments to ${FUNCNAME[0]}"
local attribute="${1}"
local value="${2}"
local result=$(sysctl -n "${attribute}")
if_error_exit "\"sysctl -n ${attribute}\" failed"
if [ ! "${value}" -eq "${result}" ] ; then
echo "Setting: \"sysctl -w ${attribute}=${value}\""
sudo sysctl -w "${attribute}"="${value}"
if_error_exit "\"sudo sysctl -w ${attribute} = ${value}\" failed"
fi
}
function verify_host_network_settings {
###########################################################################
# Description: #
# Verify hosts network settings #
# #
# Arguments: #
# arg1: ip_family #
###########################################################################
[ $# -eq 1 ]
if_error_exit "Wrong number of arguments to ${FUNCNAME[0]}"
local ip_family="${1}"
verify_sysctl_setting net.ipv4.ip_forward 1
if [ "${ip_family}" = "ipv6" ]; then
verify_sysctl_setting net.ipv6.conf.all.forwarding 1
verify_sysctl_setting net.bridge.bridge-nf-call-arptables 0
verify_sysctl_setting net.bridge.bridge-nf-call-ip6tables 0
verify_sysctl_setting net.bridge.bridge-nf-call-iptables 0
fi
}
function set_host_network_settings {
###########################################################################
# Description: #
# prepare hosts network settings #
# #
# Arguments: #
# arg1: ip_family #
###########################################################################
[ $# -eq 1 ]
if_error_exit "Wrong number of arguments to ${FUNCNAME[0]}"
local ip_family="${1}"
set_sysctl net.ipv6.conf.all.forwarding 1
if [ "${ip_family}" = "ipv6" ]; then
set_sysctl net.ipv4.ip_forward 1
set_sysctl net.bridge.bridge-nf-call-arptables 0
set_sysctl net.bridge.bridge-nf-call-ip6tables 0
set_sysctl net.bridge.bridge-nf-call-iptables 0
fi
}
function add_to_path {
###########################################################################
# Description: #
# Add directory to path #
# #
# Arguments: #
# arg1: directory #
###########################################################################
[ $# -eq 1 ]
if_error_exit "Wrong number of arguments to ${FUNCNAME[0]}"
local directory="${1}"
[ -d "${directory}" ]
if_error_exit "Directory \"${directory}\" does not exist"
case ":${PATH:-}:" in
*:${directory}:*) ;;
*) PATH="${directory}${PATH:+:$PATH}" ;;
esac
}
function install_binaries {
###########################################################################
# Description: #
# Copy binaries from the net to binaries directory #
# #
# Arguments: #
# arg1: binary directory, path to where ginko will be installed #
# arg2: Kubernetes version #
# arg3: OS, name of the operating system #
###########################################################################
[ $# -eq 3 ]
if_error_exit "Wrong number of arguments to ${FUNCNAME[0]}"
local bin_directory="${1}"
local k8s_version="${2}"
local os="${3}"
pushd "${0%/*}" > /dev/null || exit
mkdir -p "${bin_directory}"
popd > /dev/null || exit
add_to_path "${bin_directory}"
setup_kind "${bin_directory}"
setup_kubectl "${bin_directory}"
setup_ginkgo "${bin_directory}" "${k8s_version}" "${os}"
}
function set_e2e_dir {
###########################################################################
# Description: #
# Set E2E directory #
# #
# Arguments: #
# arg1: Path for E2E installation directory #
# arg2: binary directory, path to where ginko will be installed #
###########################################################################
[ $# -eq 2 ]
if_error_exit "Wrong number of arguments to ${FUNCNAME[0]}"
local e2e_dir="${1}"
local bin_dir="${2}"
[ -d "${bin_dir}" ]
if_error_exit "Directory \"${bin_dir}\" does not exist"
pushd "${0%/*}" > /dev/null || exit
mkdir -p "${e2e_dir}"
mkdir -p "${e2e_dir}/artifacts"
popd > /dev/null || exit
}
function prepare_container {
###########################################################################
# Description: #
# Prepare container #
# #
# Arguments: #
# arg1: Path of dockerfile #
# arg2: ci_mode #
###########################################################################
[ $# -eq 2 ]
if_error_exit "Wrong number of arguments to ${FUNCNAME[0]}"
local dockerfile="${1}"
local ci_mode="${2}"
# Detect container engine
detect_container_engine
container_build "${dockerfile}" "${ci_mode}"
}
function create_infrastructure_and_run_tests {
###########################################################################
# Description: #
# create_infrastructure_and_run_tests #
# #
# Arguments: #
# arg1: Path for E2E installation directory #
# arg2: ip_family #
# arg3: backend #
# arg4: e2e_test #
# arg5: suffix #
# arg6: developer_mode #
# arg7: <ci_mode> #
###########################################################################
[ $# -eq 7 ]
if_error_exit "Wrong number of arguments to ${FUNCNAME[0]}"
local e2e_dir="${1}"
local ip_family="${2}"
local backend="${3}"
local e2e_test="${4}"
local suffix="${5}"
local devel_mode="${6}"
local ci_mode="${7}"
local artifacts_directory="${e2e_dir}/artifacts"
local cluster_name="kpng-e2e-${ip_family}-${backend}${suffix}"
export E2E_DIR="${e2e_dir}"
export E2E_ARTIFACTS="${artifacts_directory}"
export E2E_CLUSTER_NAME="${cluster_name}"
export E2E_IP_FAMILY="${ip_family}"
export E2E_BACKEND="${backend}"
export E2E_DIR="${e2e_dir}"
export E2E_ARTIFACTS="${artifacts_directory}"
[ -d "${artifacts_directory}" ]
if_error_exit "Directory \"${artifacts_directory}\" does not exist"
[ -f "${e2e_test}" ]
if_error_exit "File \"${e2e_test}\" does not exist"
echo "${cluster_name}"
create_cluster "${cluster_name}" "${ip_family}" "${artifacts_directory}" "${ci_mode}"
wait_until_cluster_is_ready "${cluster_name}" "${ci_mode}"
echo "${cluster_name}" > "${e2e_dir}"/clustername
if [ "${backend}" != "not-kpng" ] ; then
install_kpng "${cluster_name}"
fi
if ! ${devel_mode} ; then
run_tests "${e2e_dir}" "${e2e_test}" "false"
#need to clean this up
if [ "${ci_mode}" = false ] ; then
clean_artifacts "${e2e_dir}"
fi
fi
}
function delete_kind_clusters {
###########################################################################
# Description: #
# create_infrastructure_and_run_tests #
# #
# Arguments: #
# arg1: bin_directory #
# arg2: ip_family #
# arg3: backend #
# arg4: suffix #
# arg5: cluser_count #
###########################################################################
echo "+==================================================================+"
echo -e "\t\tErasing kind clusters"
echo "+==================================================================+"
[ $# -eq 5 ]
if_error_exit "Wrong number of arguments to ${FUNCNAME[0]}"
# setting up variables
local bin_directory="${1}"
local ip_family="${2}"
local backend="${3}"
local suffix="${4}"
local cluster_count="${5}"
add_to_path "${bin_directory}"
[ "${cluster_count}" -ge "1" ]
if_error_exit "cluster_count must be larger or equal to one"
local cluster_name_base="kpng-e2e-${ip_family}-${backend}"
if [ "${cluster_count}" -eq "1" ] ; then
local tmp_suffix=${suffix:+"-${suffix}"}
delete_kind_cluster "${cluster_name_base}${tmp_suffix}"
else
for i in $(seq "${cluster_count}"); do
local tmp_suffix="-${suffix}${i}"
delete_kind_cluster "${cluster_name_base}${tmp_suffix}"
done
fi
}
function print_reports {
###########################################################################
# Description: #
# create_infrastructure_and_run_tests #
# #
# Arguments: #
# arg1: ip_family #
# arg2: backend #
# arg3: e2e_directory #
# arg4: suffix #
# arg5: cluster_count #
###########################################################################
[ $# -eq 5 ]
if_error_exit "Wrong number of arguments to ${FUNCNAME[0]}"
# setting up variables
local ip_family="${1}"
local backend="${2}"
local e2e_directory="${3}"
local suffix="${4}"
local cluster_count="${5}"
echo "+==========================================================================================+"
echo -e "\t\tTest Report from running test \"-i ${ip_family} -b ${backend}\" on ${cluster_count} clusters."
echo "+==========================================================================================+"
local combined_output_file=$(mktemp -q)
if_error_exit "Could not create temp file, mktemp failed"
for i in $(seq "${cluster_count}"); do
local test_directory="${e2e_directory}${suffix}${i}"
if ! [ -d "${test_directory}" ] ; then
echo "directory \"${test_directory}\" not found, skipping"
continue
fi
echo -e "Summary report from cluster \"${i}\" in directory: \"${test_directory}\""
local output_file="${test_directory}/output.log"
cat "${output_file}" >> "${combined_output_file}"
sed -nE '/Ran[[:space:]]+[[:digit:]]+[[:space:]]+of[[:space:]]+[[:digit:]]/{N;p}' "${output_file}"
done
echo -e "\nOccurence\tFailure"
awk '/Summarizing/,0' "${combined_output_file}" | awk 'ORS=/\[Fail\]/?", ":RS' | awk '/\[Fail\]/' | \
sed 's/\x1b\[90m//g' |sort | uniq -c | sort -nr | sed 's/\,/\n\t\t/g'
rm -f "${combined_output_file}"
}
function main {
###########################################################################
# Description: #
# Starting E2E process #
# #
# Arguments: #
# None #
###########################################################################
[ $# -eq 11 ]
if_error_exit "Wrong number of arguments to ${FUNCNAME[0]}"
# setting up variables
local ip_family="${1}"
local backend="${2}"
local ci_mode="${3}"
local e2e_dir="${4}"
local bin_dir="${5}"
local dockerfile="${6}"
local suffix="${7}"
local cluster_count="${8}"
local erase_clusters="${9}"
local print_report="${10}"
local devel_mode="${11}"
[ "${cluster_count}" -ge "1" ]
if_error_exit "cluster_count must be larger or equal to one"
e2e_dir=${e2e_dir:="$(pwd)/temp/e2e"}
bin_dir=${bin_dir:="${e2e_dir}/bin"}
if ${erase_clusters} ; then
delete_kind_clusters "${bin_dir}" "${ip_family}" "${backend}" "${suffix}" "${cluster_count}"
exit 1
fi
if ${print_report} ; then
print_reports "${ip_family}" "${backend}" "${e2e_dir}" "-${suffix}" "${cluster_count}"
exit 1
fi
echo "+==================================================================+"
echo -e "\t\tStarting KPNG E2E testing"
echo "+==================================================================+"
# in ci this should fail
if [ "${ci_mode}" = true ] ; then
# REMOVE THIS comment out ON THE REPO WITH A PR WHEN LOCAL TESTS ARE ALL GREEN
# set -e
echo "this tests can't fail now in ci"
set_host_network_settings "${ip_family}"
fi
verify_host_network_settings "${ip_family}"
prepare_container "${dockerfile}" "${ci_mode}"
install_binaries "${bin_dir}" "${E2E_K8S_VERSION}" "${OS}"
if [ "${cluster_count}" -eq "1" ] ; then
local tmp_suffix=${suffix:+"-${suffix}"}
set_e2e_dir "${e2e_dir}${tmp_suffix}" "${bin_dir}"
else
for i in $(seq "${cluster_count}"); do
local tmp_suffix="-${suffix}${i}"
set_e2e_dir "${e2e_dir}${tmp_suffix}" "${bin_dir}"