From 268f45b0be36b28a6a828dcf590611acfc646765 Mon Sep 17 00:00:00 2001 From: Brad Davidson Date: Tue, 10 Oct 2023 17:03:11 +0000 Subject: [PATCH] Fix etcd snapshot integration tests Snapshot delete/prune tests were only working because the delete command would report success even when deleting a snapshot that didn't exist, and the test regex was finding the snapshot name multiple times in the list output and deleting it twice. Snapshot restore tests seem to have expected the deployment to be rolled out immediately, which is not a reasonable expectation. Signed-off-by: Brad Davidson (cherry picked from commit 7c5b69ca1d2973461cf5c4b9c4aaf75f59727d7d) Signed-off-by: Brad Davidson --- .../etcdrestore/etcd_restore_int_test.go | 17 +++++++++++------ .../etcdrestore/testdata/temp_depl.yaml | 5 ++++- .../etcdrestore/testdata/temp_depl2.yaml | 5 ++++- .../etcdsnapshot/etcdsnapshot_int_test.go | 10 +++++----- tests/integration/integration.go | 3 +++ 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/tests/integration/etcdrestore/etcd_restore_int_test.go b/tests/integration/etcdrestore/etcd_restore_int_test.go index 22bb0f2b6ee5..5ea168d53237 100644 --- a/tests/integration/etcdrestore/etcd_restore_int_test.go +++ b/tests/integration/etcdrestore/etcd_restore_int_test.go @@ -41,6 +41,11 @@ var _ = Describe("etcd snapshot restore", Ordered, func() { Expect(result).To(ContainSubstring("deployment.apps/nginx-deployment created")) Expect(err).NotTo(HaveOccurred()) }) + It("make sure workload exists", func() { + res, err := testutil.K3sCmd("kubectl", "rollout", "status", "deployment", "nginx-deployment", "--watch=true", "--timeout=360s") + Expect(res).To(ContainSubstring("successfully rolled out")) + Expect(err).ToNot(HaveOccurred()) + }) It("saves an etcd snapshot", func() { Expect(testutil.K3sCmd("etcd-snapshot", "save", "-d", tmpdDataDir, "--name", "snapshot-to-restore")). To(ContainSubstring("saved")) @@ -83,15 +88,15 @@ var _ = Describe("etcd snapshot restore", Ordered, func() { return testutil.K3sDefaultDeployments() }, "360s", "5s").Should(Succeed()) }) - It("Make sure Workload 1 exists", func() { - Eventually(func() (string, error) { - return testutil.K3sCmd("kubectl", "get", "deployment", "nginx-deployment") - }, "360s", "5s").Should(ContainSubstring("3/3")) + It("make sure workload 1 exists", func() { + res, err := testutil.K3sCmd("kubectl", "rollout", "status", "deployment", "nginx-deployment", "--watch=true", "--timeout=360s") + Expect(res).To(ContainSubstring("successfully rolled out")) + Expect(err).ToNot(HaveOccurred()) }) - It("Make sure Workload 2 does not exists", func() { + It("make sure workload 2 does not exists", func() { res, err := testutil.K3sCmd("kubectl", "get", "deployment", "nginx-deployment-post-snapshot") - Expect(err).To(HaveOccurred()) Expect(res).To(ContainSubstring("not found")) + Expect(err).To(HaveOccurred()) }) It("check if CA cert hash matches", func() { // get md5sum of the CA certs diff --git a/tests/integration/etcdrestore/testdata/temp_depl.yaml b/tests/integration/etcdrestore/testdata/temp_depl.yaml index 3649247c1bb1..8e8c564fec83 100644 --- a/tests/integration/etcdrestore/testdata/temp_depl.yaml +++ b/tests/integration/etcdrestore/testdata/temp_depl.yaml @@ -6,6 +6,9 @@ metadata: app: nginx spec: replicas: 3 + revisionHistoryLimit: 0 + strategy: + type: Recreate selector: matchLabels: app: nginx @@ -18,4 +21,4 @@ spec: - name: nginx image: nginx:1.14.2 ports: - - containerPort: 80 \ No newline at end of file + - containerPort: 80 diff --git a/tests/integration/etcdrestore/testdata/temp_depl2.yaml b/tests/integration/etcdrestore/testdata/temp_depl2.yaml index 8cea5e6f2d95..c5247a77e75d 100644 --- a/tests/integration/etcdrestore/testdata/temp_depl2.yaml +++ b/tests/integration/etcdrestore/testdata/temp_depl2.yaml @@ -6,6 +6,9 @@ metadata: app: nginx spec: replicas: 3 + revisionHistoryLimit: 0 + strategy: + type: Recreate selector: matchLabels: app: nginx @@ -18,4 +21,4 @@ spec: - name: nginx image: nginx:1.14.2 ports: - - containerPort: 80 \ No newline at end of file + - containerPort: 80 diff --git a/tests/integration/etcdsnapshot/etcdsnapshot_int_test.go b/tests/integration/etcdsnapshot/etcdsnapshot_int_test.go index ee5ec6b049a7..1d7c9b5ea21b 100644 --- a/tests/integration/etcdsnapshot/etcdsnapshot_int_test.go +++ b/tests/integration/etcdsnapshot/etcdsnapshot_int_test.go @@ -54,7 +54,7 @@ var _ = Describe("etcd snapshots", Ordered, func() { It("deletes a snapshot", func() { lsResult, err := testutil.K3sCmd("etcd-snapshot", "ls") Expect(err).ToNot(HaveOccurred()) - reg, err := regexp.Compile(`on-demand[^\s]+`) + reg, err := regexp.Compile(`(?m)^on-demand[^\s]+`) Expect(err).ToNot(HaveOccurred()) snapshotName := reg.FindString(lsResult) Expect(testutil.K3sCmd("etcd-snapshot", "delete", snapshotName)). @@ -69,7 +69,7 @@ var _ = Describe("etcd snapshots", Ordered, func() { It("deletes that snapshot", func() { lsResult, err := testutil.K3sCmd("etcd-snapshot", "ls") Expect(err).ToNot(HaveOccurred()) - reg, err := regexp.Compile(`ALIVEBEEF[^\s]+`) + reg, err := regexp.Compile(`(?m)^ALIVEBEEF[^\s]+`) Expect(err).ToNot(HaveOccurred()) snapshotName := reg.FindString(lsResult) Expect(testutil.K3sCmd("etcd-snapshot", "delete", snapshotName)). @@ -91,7 +91,7 @@ var _ = Describe("etcd snapshots", Ordered, func() { It("lists all 3 snapshots", func() { lsResult, err := testutil.K3sCmd("etcd-snapshot", "ls") Expect(err).ToNot(HaveOccurred()) - reg, err := regexp.Compile(`:///var/lib/rancher/k3s/server/db/snapshots/PRUNE_TEST`) + reg, err := regexp.Compile(`(?m):///var/lib/rancher/k3s/server/db/snapshots/PRUNE_TEST`) Expect(err).ToNot(HaveOccurred()) sepLines := reg.FindAllString(lsResult, -1) Expect(sepLines).To(HaveLen(3)) @@ -101,7 +101,7 @@ var _ = Describe("etcd snapshots", Ordered, func() { To(ContainSubstring("Removing local snapshot")) lsResult, err := testutil.K3sCmd("etcd-snapshot", "ls") Expect(err).ToNot(HaveOccurred()) - reg, err := regexp.Compile(`:///var/lib/rancher/k3s/server/db/snapshots/PRUNE_TEST`) + reg, err := regexp.Compile(`(?m):///var/lib/rancher/k3s/server/db/snapshots/PRUNE_TEST`) Expect(err).ToNot(HaveOccurred()) sepLines := reg.FindAllString(lsResult, -1) Expect(sepLines).To(HaveLen(2)) @@ -109,7 +109,7 @@ var _ = Describe("etcd snapshots", Ordered, func() { It("cleans up remaining snapshots", func() { lsResult, err := testutil.K3sCmd("etcd-snapshot", "ls") Expect(err).ToNot(HaveOccurred()) - reg, err := regexp.Compile(`PRUNE_TEST[^\s]+`) + reg, err := regexp.Compile(`(?m)^PRUNE_TEST[^\s]+`) Expect(err).ToNot(HaveOccurred()) for _, snapshotName := range reg.FindAllString(lsResult, -1) { Expect(testutil.K3sCmd("etcd-snapshot", "delete", snapshotName)). diff --git a/tests/integration/integration.go b/tests/integration/integration.go index 643104d8825e..461f1db9895d 100644 --- a/tests/integration/integration.go +++ b/tests/integration/integration.go @@ -275,6 +275,9 @@ func K3sStopServer(server *K3sServer) error { // K3sKillServer terminates the running K3s server and its children. // Equivalent to k3s-killall.sh func K3sKillServer(server *K3sServer) error { + if server == nil { + return nil + } if server.log != nil { server.log.Close() os.Remove(server.log.Name())