From 7aee1c1430b9fef8106a03fe3fe00d825bd88913 Mon Sep 17 00:00:00 2001 From: Ian Cardoso Date: Fri, 28 Apr 2023 14:07:11 -0300 Subject: [PATCH] Add integration tests for etc-snapshot server flags and refactor /tests/integration/integration.go/K3sStartServer (#7300) This adds integration tests for the following flags: "--etcd-snapshot-name","--etcd-snapshot-dir","--etcd-snapshot-retention","--etcd-snapshot-schedule-cron" and "--etcd-snapshot-compress". It also refactors K3sStartServer to stop applying strings.Fields() into inputArgs, so it can accept arguments that have space in their definition. Signed-off-by: Ian Cardoso (cherry picked from commit 1ac03aad43c813ac77236e2507329d944b391f92) --- .../custometcdargs/custometcdargs_int_test.go | 3 +- .../dualstack/dualstack_int_test.go | 4 +- .../etcdsnapshot/etcdsnapshot_int_test.go | 62 +++++++++++++++++++ tests/integration/integration.go | 6 +- 4 files changed, 67 insertions(+), 8 deletions(-) diff --git a/tests/integration/custometcdargs/custometcdargs_int_test.go b/tests/integration/custometcdargs/custometcdargs_int_test.go index 20ea9803d7e5..f61c46cffabd 100644 --- a/tests/integration/custometcdargs/custometcdargs_int_test.go +++ b/tests/integration/custometcdargs/custometcdargs_int_test.go @@ -13,8 +13,9 @@ import ( var customEtcdArgsServer *testutil.K3sServer var customEtcdArgsServerArgs = []string{ "--cluster-init", - "--etcd-arg quota-backend-bytes=858993459", + "--etcd-arg=quota-backend-bytes=858993459", } + var testLock int var _ = BeforeSuite(func() { diff --git a/tests/integration/dualstack/dualstack_int_test.go b/tests/integration/dualstack/dualstack_int_test.go index 81bbc208e9e7..c0b2c18f4124 100644 --- a/tests/integration/dualstack/dualstack_int_test.go +++ b/tests/integration/dualstack/dualstack_int_test.go @@ -13,8 +13,8 @@ import ( var dualStackServer *testutil.K3sServer var dualStackServerArgs = []string{ "--cluster-init", - "--cluster-cidr 10.42.0.0/16,2001:cafe:42:0::/56", - "--service-cidr 10.43.0.0/16,2001:cafe:42:1::/112", + "--cluster-cidr", "10.42.0.0/16,2001:cafe:42:0::/56", + "--service-cidr", "10.43.0.0/16,2001:cafe:42:1::/112", "--disable-network-policy", } var testLock int diff --git a/tests/integration/etcdsnapshot/etcdsnapshot_int_test.go b/tests/integration/etcdsnapshot/etcdsnapshot_int_test.go index 1df2a208bebb..3fe9f4152b84 100644 --- a/tests/integration/etcdsnapshot/etcdsnapshot_int_test.go +++ b/tests/integration/etcdsnapshot/etcdsnapshot_int_test.go @@ -1,6 +1,8 @@ package snapshot_test import ( + "fmt" + "path/filepath" "regexp" "strings" "testing" @@ -14,6 +16,10 @@ import ( var server *testutil.K3sServer var serverArgs = []string{"--cluster-init"} var testLock int +var populatedTestSnapshotDir string +var emptyTestSnapshotDir string +var etcdSnapshotFilePattern = "test-snapshot" +var etcdSnapshotRetention = 1 var _ = BeforeSuite(func() { if !testutil.IsExistingServer() { @@ -111,6 +117,60 @@ var _ = Describe("etcd snapshots", Ordered, func() { } }) }) + When("a new etcd is created with server start flags", func() { + It("kills previous server and start up with no problems", func() { + var err error + Expect(testutil.K3sKillServer(server)).To(Succeed()) + localServerArgs := []string{"--cluster-init", + "--etcd-snapshot-name", etcdSnapshotFilePattern, + "--etcd-snapshot-dir", populatedTestSnapshotDir, + "--etcd-snapshot-retention", fmt.Sprint(etcdSnapshotRetention), + "--etcd-snapshot-schedule-cron", `* * * * *`, + "--etcd-snapshot-compress"} + server, err = testutil.K3sStartServer(localServerArgs...) + Expect(err).ToNot(HaveOccurred()) + Eventually(func() error { + return testutil.K3sDefaultDeployments() + }, "180s", "5s").Should(Succeed()) + + }) + It("saves an etcd snapshot with specified name and it should be no more than 1 compressed file", func() { + + Eventually(func() (int, error) { + matches, err := filepath.Glob(filepath.Join(populatedTestSnapshotDir, fmt.Sprintf("%s%s%s", "*", etcdSnapshotFilePattern, "*.zip"))) + return len(matches), err + }, "180s", "30s").Should(Equal(etcdSnapshotRetention)) + Consistently(func() (int, error) { + matches, err := filepath.Glob(filepath.Join(populatedTestSnapshotDir, fmt.Sprintf("%s%s%s", "*", etcdSnapshotFilePattern, "*.zip"))) + return len(matches), err + }, "120s", "30s").Should(Equal(etcdSnapshotRetention)) + }) + It("kills previous server and start up with no problems and disabled snapshots", func() { + + var err error + Expect(testutil.K3sKillServer(server)).To(Succeed()) + localServerArgs := []string{"--cluster-init", + "--etcd-snapshot-dir", emptyTestSnapshotDir, + "--etcd-snapshot-schedule-cron", `* * * * *`, + "--etcd-disable-snapshots"} + server, err = testutil.K3sStartServer(localServerArgs...) + Expect(err).ToNot(HaveOccurred()) + Eventually(func() error { + return testutil.K3sDefaultDeployments() + }, "180s", "5s").Should(Succeed()) + + }) + It("should not save any snapshot", func() { + Consistently(func() error { + matches, err := filepath.Glob(filepath.Join(emptyTestSnapshotDir, "*")) + if matches != nil || err != nil { + return fmt.Errorf("something went wrong: err != nil (%v) or matches != nil (%v)", err, matches) + } + return nil + }, "180s", "60s").Should(Succeed()) + }) + }) + }) var failed bool @@ -130,5 +190,7 @@ var _ = AfterSuite(func() { func Test_IntegrationEtcdSnapshot(t *testing.T) { RegisterFailHandler(Fail) + populatedTestSnapshotDir = t.TempDir() + emptyTestSnapshotDir = t.TempDir() RunSpecs(t, "Etcd Snapshot Suite") } diff --git a/tests/integration/integration.go b/tests/integration/integration.go index be60e7d8cf93..c3e065beb174 100644 --- a/tests/integration/integration.go +++ b/tests/integration/integration.go @@ -210,12 +210,8 @@ func K3sStartServer(inputArgs ...string) (*K3sServer, error) { return nil, errors.New("integration tests must be run as sudo/root") } - var cmdArgs []string - for _, arg := range inputArgs { - cmdArgs = append(cmdArgs, strings.Fields(arg)...) - } k3sBin := findK3sExecutable() - k3sCmd := append([]string{"server"}, cmdArgs...) + k3sCmd := append([]string{"server"}, inputArgs...) cmd := exec.Command(k3sBin, k3sCmd...) // Give the server a new group id so we can kill it and its children later cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}