From 2d86c333f5762d0b4c32149d75f2cb8c7a232b1e Mon Sep 17 00:00:00 2001 From: Paco Xu Date: Sun, 10 Sep 2023 20:06:03 +0800 Subject: [PATCH] add test case for generating etcd manifests --- cmd/kubeadm/app/phases/etcd/local_test.go | 100 +++++++++++++++++++++- 1 file changed, 98 insertions(+), 2 deletions(-) diff --git a/cmd/kubeadm/app/phases/etcd/local_test.go b/cmd/kubeadm/app/phases/etcd/local_test.go index 4aa181a51cc..5ef1a1be86f 100644 --- a/cmd/kubeadm/app/phases/etcd/local_test.go +++ b/cmd/kubeadm/app/phases/etcd/local_test.go @@ -22,11 +22,13 @@ package etcd import ( "fmt" "os" + "path" "path/filepath" "reflect" "sort" "testing" + "github.com/google/go-cmp/cmp" "github.com/lithammer/dedent" v1 "k8s.io/api/core/v1" @@ -71,8 +73,9 @@ func TestCreateLocalEtcdStaticPodManifestFile(t *testing.T) { defer os.RemoveAll(tmpdir) var tests = []struct { - cfg *kubeadmapi.ClusterConfiguration - expectedError bool + cfg *kubeadmapi.ClusterConfiguration + expectedError bool + expectedManifest string }{ { cfg: &kubeadmapi.ClusterConfiguration{ @@ -84,6 +87,89 @@ func TestCreateLocalEtcdStaticPodManifestFile(t *testing.T) { }, }, expectedError: false, + expectedManifest: fmt.Sprintf(`apiVersion: v1 +kind: Pod +metadata: + annotations: + kubeadm.kubernetes.io/etcd.advertise-client-urls: https://:2379 + creationTimestamp: null + labels: + component: etcd + tier: control-plane + name: etcd + namespace: kube-system +spec: + containers: + - command: + - etcd + - --advertise-client-urls=https://:2379 + - --cert-file=etcd/server.crt + - --client-cert-auth=true + - --data-dir=%s/etcd + - --experimental-initial-corrupt-check=true + - --experimental-watch-progress-notify-interval=5s + - --initial-advertise-peer-urls=https://:2380 + - --initial-cluster==https://:2380 + - --key-file=etcd/server.key + - --listen-client-urls=https://127.0.0.1:2379,https://:2379 + - --listen-metrics-urls=http://127.0.0.1:2381 + - --listen-peer-urls=https://:2380 + - --name= + - --peer-cert-file=etcd/peer.crt + - --peer-client-cert-auth=true + - --peer-key-file=etcd/peer.key + - --peer-trusted-ca-file=etcd/ca.crt + - --snapshot-count=10000 + - --trusted-ca-file=etcd/ca.crt + image: /etcd:%s + imagePullPolicy: IfNotPresent + livenessProbe: + failureThreshold: 8 + httpGet: + host: 127.0.0.1 + path: /health?exclude=NOSPACE&serializable=true + port: 2381 + scheme: HTTP + initialDelaySeconds: 10 + periodSeconds: 10 + timeoutSeconds: 15 + name: etcd + resources: + requests: + cpu: 100m + memory: 100Mi + startupProbe: + failureThreshold: 24 + httpGet: + host: 127.0.0.1 + path: /health?serializable=false + port: 2381 + scheme: HTTP + initialDelaySeconds: 10 + periodSeconds: 10 + timeoutSeconds: 15 + volumeMounts: + - mountPath: %s/etcd + name: etcd-data + - mountPath: /etcd + name: etcd-certs + hostNetwork: true + priority: 2000001000 + priorityClassName: system-node-critical + securityContext: + seccompProfile: + type: RuntimeDefault + volumes: + - hostPath: + path: /etcd + type: DirectoryOrCreate + name: etcd-certs + - hostPath: + path: %s/etcd + type: DirectoryOrCreate + name: etcd-data +status: {} +`, tmpdir, kubeadmconstants.DefaultEtcdVersion, tmpdir, tmpdir), }, { cfg: &kubeadmapi.ClusterConfiguration{ @@ -115,6 +201,16 @@ func TestCreateLocalEtcdStaticPodManifestFile(t *testing.T) { // Assert expected files are there testutil.AssertFilesCount(t, manifestPath, 1) testutil.AssertFileExists(t, manifestPath, kubeadmconstants.Etcd+".yaml") + manifestBytes, err := os.ReadFile(path.Join(manifestPath, kubeadmconstants.Etcd+".yaml")) + if err != nil { + t.Errorf("failed to load generated manifest file: %v", err) + } + if test.expectedManifest != string(manifestBytes) { + t.Errorf( + "File created by CreateLocalEtcdStaticPodManifestFile is not as expected. Diff: \n%s", + cmp.Diff(string(manifestBytes), test.expectedManifest), + ) + } } else { testutil.AssertError(t, err, "etcd static pod manifest cannot be generated for cluster using external etcd") }