From 975b397d2115fdec2a1bc350e4f5f2a203386161 Mon Sep 17 00:00:00 2001 From: Jingyuan Liang Date: Wed, 21 Feb 2024 15:29:30 +0000 Subject: [PATCH] Add calico test case Contributed by Marek Chodor in #269 --- scripts/testcase/testcase-calico.sh | 69 ++++++++++++++++++++++ scripts/testdata/calico-spec-template.json | 39 ++++++++++++ scripts/testdata/expected-calico.json | 39 ++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 scripts/testcase/testcase-calico.sh create mode 100644 scripts/testdata/calico-spec-template.json create mode 100644 scripts/testdata/expected-calico.json diff --git a/scripts/testcase/testcase-calico.sh b/scripts/testcase/testcase-calico.sh new file mode 100644 index 000000000..6be3ae84d --- /dev/null +++ b/scripts/testcase/testcase-calico.sh @@ -0,0 +1,69 @@ +export KUBERNETES_SERVICE_HOST=kubernetes.default.svc +export KUBERNETES_SERVICE_PORT=443 + +export ENABLE_CALICO_NETWORK_POLICY=true +export ENABLE_BANDWIDTH_PLUGIN=false +export ENABLE_CILIUM_PLUGIN=false +export ENABLE_MASQUERADE=false +export ENABLE_IPV6=true +export CNI_SPEC_IPV6_ROUTE="{\"dst\": \"2600:1900:4000::/42\"}" +export CALICO_CNI_SPEC_TEMPLATE_FILE="/host/etc/cni/net.d/10-calico.conflist.template" +export CALICO_CNI_SERVICE_ACCOUNT="calico-cni-sa" +export WRITE_CALICO_CONFIG_FILE=true +export IPV6_FORWARDING_CONF=/tmp/mock-calico-enable-forwarding + +CALICO_CNI_SPEC_TEMPLATE=$(cat testdata/calico-spec-template.json) +export CALICO_CNI_SPEC_TEMPLATE + +function before_test() { + + function curl() { + # shellcheck disable=SC2317 + case "$*" in + *http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0*) + echo '{"ipv6s": ["2600:1900:4000:318:0:7:0:0"]}' + ;; + *https://kubernetes.default.svc:443/api/v1/nodes/*) + echo '{ + "metadata": { + "labels": { + }, + "creationTimestamp": "2024-01-03T11:54:01Z", + "name": "gke-my-cluster-default-pool-128bc25d-9c94", + "resourceVersion": "891003", + "uid": "f2353a2f-ca8c-4ca0-8dd3-ad1f964a54f0" + }, + "spec": { + "podCIDR": "10.52.1.0/24", + "podCIDRs": [ + "10.52.1.0/24" + ], + "providerID": "gce://my-gke-project/us-central1-c/gke-my-cluster-default-pool-128bc25d-9c94" + } + }' + ;; + *) + #unsupported + exit 1 + esac + } + export -f curl + +} + +function verify() { + local expected + local actual + + expected=$(jq -S . <"testdata/expected-calico.json") + actual=$(jq -S . <"/host/etc/cni/net.d/10-calico.conflist.template") + + if [ "$expected" != "$actual" ] ; then + echo "Expected cni_spec value:" + echo "$expected" + echo "but actual was" + echo "$actual" + return 1 + fi + +} diff --git a/scripts/testdata/calico-spec-template.json b/scripts/testdata/calico-spec-template.json new file mode 100644 index 000000000..b9347cfe0 --- /dev/null +++ b/scripts/testdata/calico-spec-template.json @@ -0,0 +1,39 @@ +{ + "name": "gke-pod-network", + "cniVersion": "0.3.1", + "plugins": [ + { + "type": "calico", + "mtu": 1460, + "log_level": "warning", + "log_file_path": "/var/log/calico/cni/cni.log", + "datastore_type": "kubernetes", + "nodename": "__KUBERNETES_NODE_NAME__", + "nodename_file_optional": true, + "ipam": { + "type": "host-local", + "ranges": [ + [ { "subnet": "usePodCidr" } ]@ipv6SubnetOptional + ], + "routes": [ + {"dst": "0.0.0.0/0"}@ipv6RouteOptional + ] + }, + "policy": { + "type": "k8s" + }, + "kubernetes": { + "kubeconfig": "__KUBECONFIG_FILEPATH__" + } + }, + { + "type": "portmap", + "capabilities": {"portMappings": true}, + "snat": true + }, + { + "type": "bandwidth", + "capabilities": {"bandwidth": true} + } + ] +} \ No newline at end of file diff --git a/scripts/testdata/expected-calico.json b/scripts/testdata/expected-calico.json new file mode 100644 index 000000000..59453d7dd --- /dev/null +++ b/scripts/testdata/expected-calico.json @@ -0,0 +1,39 @@ +{ + "name": "gke-pod-network", + "cniVersion": "0.3.1", + "plugins": [ + { + "type": "calico", + "mtu": 1460, + "log_level": "warning", + "log_file_path": "/var/log/calico/cni/cni.log", + "datastore_type": "kubernetes", + "nodename": "__KUBERNETES_NODE_NAME__", + "nodename_file_optional": true, + "ipam": { + "type": "host-local", + "ranges": [ + [ { "subnet": "usePodCidr" } ], [{"subnet": "2600:1900:4000:318:0:7:0:0/112"}] + ], + "routes": [ + {"dst": "0.0.0.0/0"}, {"dst": "2600:1900:4000::/42"} + ] + }, + "policy": { + "type": "k8s" + }, + "kubernetes": { + "kubeconfig": "__KUBECONFIG_FILEPATH__" + } + }, + { + "type": "portmap", + "capabilities": {"portMappings": true}, + "snat": true + }, + { + "type": "bandwidth", + "capabilities": {"bandwidth": true} + } + ] +} \ No newline at end of file