Skip to content

Commit

Permalink
Rework etcd test to use new test framework
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Nola <derek.nola@suse.com>
  • Loading branch information
dereknola committed Feb 9, 2024
1 parent ec3c394 commit 5128ae1
Show file tree
Hide file tree
Showing 2 changed files with 237 additions and 41 deletions.
69 changes: 44 additions & 25 deletions tests/integration/etcdsnapshot/etcd_int_test.go
Original file line number Diff line number Diff line change
@@ -1,104 +1,123 @@
package tests

import (
"os"
"regexp"
"strings"
"testing"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
util "github.com/rancher/rke2/tests/integration"
testutil "github.com/rancher/rke2/tests/integration"
)

var serverArgs = []string{"server"}
var serverArgs = []string{""}
var serverLog *os.File
var testLock int

var _ = BeforeSuite(func() {
var err error
testLock, err = testutil.AcquireTestLock()
Expect(err).ToNot(HaveOccurred())
serverLog, err = testutil.StartServer(serverArgs...)
Expect(err).ToNot(HaveOccurred())
})

var _ = Describe("etcd snapshots", func() {
BeforeEach(func() {
if !util.ServerArgsPresent(serverArgs) {
Skip("Test needs rke2 server with: " + strings.Join(serverArgs, " "))
}
})
When("a new etcd is created", func() {
When("a new etcd snapshot is created", func() {
It("starts up with no problems", func() {
Eventually(util.Rke2Ready(), "90s", "1s").Should(BeTrue())
Eventually(testutil.ServerReady(), "90s", "1s").Should(BeTrue())
})
It("saves an etcd snapshot", func() {
Expect(util.Rke2Cmd("etcd-snapshot", "save")).
Expect(testutil.RKE2Cmd("etcd-snapshot", "save")).
To(ContainSubstring("Saving current etcd snapshot set to rke2-etcd-snapshots"))
})
It("list snapshots", func() {
Expect(util.Rke2Cmd("etcd-snapshot", "ls")).
Expect(testutil.RKE2Cmd("etcd-snapshot", "ls")).
To(MatchRegexp(`:///var/lib/rancher/rke2/server/db/snapshots/on-demand`))
})
It("deletes a snapshot", func() {
lsResult, err := util.Rke2Cmd("etcd-snapshot", "ls")
lsResult, err := testutil.RKE2Cmd("etcd-snapshot", "ls")
Expect(err).ToNot(HaveOccurred())
reg, err := regexp.Compile(`on-demand[^\s]+`)
Expect(err).ToNot(HaveOccurred())
snapshotName := reg.FindString(lsResult)
Expect(util.Rke2Cmd("etcd-snapshot", "delete", snapshotName)).
Expect(testutil.RKE2Cmd("etcd-snapshot", "delete", snapshotName)).
To(ContainSubstring("Removing the given locally stored etcd snapshot"))
})
})
When("saving a custom name", func() {
It("saves an etcd snapshot with a custom name", func() {
Expect(util.Rke2Cmd("etcd-snapshot", "save", "--name", "ALIVEBEEF")).
Expect(testutil.RKE2Cmd("etcd-snapshot", "save", "--name", "ALIVEBEEF")).
To(ContainSubstring("Saving etcd snapshot to /var/lib/rancher/rke2/server/db/snapshots/ALIVEBEEF"))
})
It("deletes that snapshot", func() {
lsResult, err := util.Rke2Cmd("etcd-snapshot", "ls")
lsResult, err := testutil.RKE2Cmd("etcd-snapshot", "ls")
Expect(err).ToNot(HaveOccurred())
reg, err := regexp.Compile(`ALIVEBEEF[^\s]+`)
Expect(err).ToNot(HaveOccurred())
snapshotName := reg.FindString(lsResult)
Expect(util.Rke2Cmd("etcd-snapshot", "delete", snapshotName)).
Expect(testutil.RKE2Cmd("etcd-snapshot", "delete", snapshotName)).
To(ContainSubstring("Removing the given locally stored etcd snapshot"))
})
})
When("using etcd snapshot prune", func() {
It("saves 3 different snapshots", func() {
Expect(util.Rke2Cmd("etcd-snapshot", "save", "--name", "PRUNE_TEST")).
Expect(testutil.RKE2Cmd("etcd-snapshot", "save", "--name", "PRUNE_TEST")).
To(ContainSubstring("Saving current etcd snapshot set to rke2-etcd-snapshots"))
time.Sleep(1 * time.Second)
Expect(util.Rke2Cmd("etcd-snapshot", "save", "--name", "PRUNE_TEST")).
Expect(testutil.RKE2Cmd("etcd-snapshot", "save", "--name", "PRUNE_TEST")).
To(ContainSubstring("Saving current etcd snapshot set to rke2-etcd-snapshots"))
time.Sleep(1 * time.Second)
Expect(util.Rke2Cmd("etcd-snapshot", "save", "--name", "PRUNE_TEST")).
Expect(testutil.RKE2Cmd("etcd-snapshot", "save", "--name", "PRUNE_TEST")).
To(ContainSubstring("Saving current etcd snapshot set to rke2-etcd-snapshots"))
time.Sleep(1 * time.Second)
})
It("lists all 3 snapshots", func() {
lsResult, err := util.Rke2Cmd("etcd-snapshot", "ls")
lsResult, err := testutil.RKE2Cmd("etcd-snapshot", "ls")
Expect(err).ToNot(HaveOccurred())
reg, err := regexp.Compile(`:///var/lib/rancher/rke2/server/db/snapshots/PRUNE_TEST`)
Expect(err).ToNot(HaveOccurred())
sepLines := reg.FindAllString(lsResult, -1)
Expect(sepLines).To(HaveLen(3))
})
It("prunes snapshots down to 2", func() {
Expect(util.Rke2Cmd("etcd-snapshot", "prune", "--snapshot-retention", "2", "--name", "PRUNE_TEST")).
Expect(testutil.RKE2Cmd("etcd-snapshot", "prune", "--snapshot-retention", "2", "--name", "PRUNE_TEST")).
To(BeEmpty())
lsResult, err := util.Rke2Cmd("etcd-snapshot", "ls")
lsResult, err := testutil.RKE2Cmd("etcd-snapshot", "ls")
Expect(err).ToNot(HaveOccurred())
reg, err := regexp.Compile(`:///var/lib/rancher/rke2/server/db/snapshots/PRUNE_TEST`)
Expect(err).ToNot(HaveOccurred())
sepLines := reg.FindAllString(lsResult, -1)
Expect(sepLines).To(HaveLen(2))
})
It("cleans up remaining snapshots", func() {
lsResult, err := util.Rke2Cmd("etcd-snapshot", "ls")
lsResult, err := testutil.RKE2Cmd("etcd-snapshot", "ls")
Expect(err).ToNot(HaveOccurred())
reg, err := regexp.Compile(`PRUNE_TEST[^\s]+`)
Expect(err).ToNot(HaveOccurred())
for _, snapshotName := range reg.FindAllString(lsResult, -1) {
Expect(util.Rke2Cmd("etcd-snapshot", "delete", snapshotName)).
Expect(testutil.RKE2Cmd("etcd-snapshot", "delete", snapshotName)).
To(ContainSubstring("Removing the given locally stored etcd snapshot"))
}
})
})
})

var failed bool
var _ = AfterEach(func() {
failed = failed || CurrentSpecReport().Failed()
})

var _ = AfterSuite(func() {
if failed {
testutil.SaveLog(serverLog, false)
serverLog = nil
}
Expect(testutil.KillServer(serverLog)).To(Succeed())
Expect(testutil.Cleanup(testLock)).To(Succeed())
})

func Test_IntegrationEtcd(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Etcd Suite")
Expand Down
Loading

0 comments on commit 5128ae1

Please sign in to comment.