2022-02-08 19:24:34 +00:00
|
|
|
package snapshot_test
|
2021-07-26 16:59:33 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-03-15 17:29:56 +00:00
|
|
|
testutil "github.com/k3s-io/k3s/tests/integration"
|
2022-02-09 16:22:53 +00:00
|
|
|
. "github.com/onsi/ginkgo/v2"
|
2021-07-26 16:59:33 +00:00
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
2021-08-10 23:22:12 +00:00
|
|
|
var server *testutil.K3sServer
|
2021-08-26 23:26:19 +00:00
|
|
|
var serverArgs = []string{"--cluster-init"}
|
2022-02-25 16:42:21 +00:00
|
|
|
var testLock int
|
|
|
|
|
2021-07-26 16:59:33 +00:00
|
|
|
var _ = BeforeSuite(func() {
|
2021-08-26 23:26:19 +00:00
|
|
|
if !testutil.IsExistingServer() {
|
|
|
|
var err error
|
2022-02-25 16:42:21 +00:00
|
|
|
testLock, err = testutil.K3sTestLock()
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-08-26 23:26:19 +00:00
|
|
|
server, err = testutil.K3sStartServer(serverArgs...)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
}
|
2021-07-26 16:59:33 +00:00
|
|
|
})
|
|
|
|
|
2022-08-31 22:40:17 +00:00
|
|
|
var _ = Describe("etcd snapshots", Ordered, func() {
|
2021-08-26 23:26:19 +00:00
|
|
|
BeforeEach(func() {
|
|
|
|
if testutil.IsExistingServer() && !testutil.ServerArgsPresent(serverArgs) {
|
|
|
|
Skip("Test needs k3s server with: " + strings.Join(serverArgs, " "))
|
|
|
|
}
|
|
|
|
})
|
2021-07-26 16:59:33 +00:00
|
|
|
When("a new etcd is created", func() {
|
|
|
|
It("starts up with no problems", func() {
|
2022-06-13 20:32:13 +00:00
|
|
|
Eventually(func() error {
|
|
|
|
return testutil.K3sDefaultDeployments()
|
|
|
|
}, "180s", "10s").Should(Succeed())
|
2021-07-26 16:59:33 +00:00
|
|
|
})
|
|
|
|
It("saves an etcd snapshot", func() {
|
2021-08-10 18:13:26 +00:00
|
|
|
Expect(testutil.K3sCmd("etcd-snapshot", "save")).
|
2021-11-29 18:30:04 +00:00
|
|
|
To(ContainSubstring("saved"))
|
2021-07-26 16:59:33 +00:00
|
|
|
})
|
|
|
|
It("list snapshots", func() {
|
2021-08-10 18:13:26 +00:00
|
|
|
Expect(testutil.K3sCmd("etcd-snapshot", "ls")).
|
2021-07-26 16:59:33 +00:00
|
|
|
To(MatchRegexp(`:///var/lib/rancher/k3s/server/db/snapshots/on-demand`))
|
|
|
|
})
|
|
|
|
It("deletes a snapshot", func() {
|
2021-08-10 18:13:26 +00:00
|
|
|
lsResult, err := testutil.K3sCmd("etcd-snapshot", "ls")
|
2021-07-26 16:59:33 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
reg, err := regexp.Compile(`on-demand[^\s]+`)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
snapshotName := reg.FindString(lsResult)
|
2021-08-10 18:13:26 +00:00
|
|
|
Expect(testutil.K3sCmd("etcd-snapshot", "delete", snapshotName)).
|
2021-07-26 16:59:33 +00:00
|
|
|
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() {
|
2022-01-06 16:05:56 +00:00
|
|
|
Expect(testutil.K3sCmd("etcd-snapshot", "save --name ALIVEBEEF")).
|
2021-07-26 16:59:33 +00:00
|
|
|
To(ContainSubstring("Saving etcd snapshot to /var/lib/rancher/k3s/server/db/snapshots/ALIVEBEEF"))
|
|
|
|
})
|
|
|
|
It("deletes that snapshot", func() {
|
2021-08-10 18:13:26 +00:00
|
|
|
lsResult, err := testutil.K3sCmd("etcd-snapshot", "ls")
|
2021-07-26 16:59:33 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
reg, err := regexp.Compile(`ALIVEBEEF[^\s]+`)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
snapshotName := reg.FindString(lsResult)
|
2021-08-10 18:13:26 +00:00
|
|
|
Expect(testutil.K3sCmd("etcd-snapshot", "delete", snapshotName)).
|
2021-07-26 16:59:33 +00:00
|
|
|
To(ContainSubstring("Removing the given locally stored etcd snapshot"))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
When("using etcd snapshot prune", func() {
|
|
|
|
It("saves 3 different snapshots", func() {
|
2022-01-06 16:05:56 +00:00
|
|
|
Expect(testutil.K3sCmd("etcd-snapshot", "save -name PRUNE_TEST")).
|
2021-11-29 18:30:04 +00:00
|
|
|
To(ContainSubstring("saved"))
|
2021-07-26 16:59:33 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2022-01-06 16:05:56 +00:00
|
|
|
Expect(testutil.K3sCmd("etcd-snapshot", "save -name PRUNE_TEST")).
|
2021-11-29 18:30:04 +00:00
|
|
|
To(ContainSubstring("saved"))
|
2021-07-26 16:59:33 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2022-01-06 16:05:56 +00:00
|
|
|
Expect(testutil.K3sCmd("etcd-snapshot", "save -name PRUNE_TEST")).
|
2021-11-29 18:30:04 +00:00
|
|
|
To(ContainSubstring("saved"))
|
2021-07-26 16:59:33 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
})
|
|
|
|
It("lists all 3 snapshots", func() {
|
2021-08-10 18:13:26 +00:00
|
|
|
lsResult, err := testutil.K3sCmd("etcd-snapshot", "ls")
|
2021-07-26 16:59:33 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-08-26 23:26:19 +00:00
|
|
|
reg, err := regexp.Compile(`:///var/lib/rancher/k3s/server/db/snapshots/PRUNE_TEST`)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
sepLines := reg.FindAllString(lsResult, -1)
|
2021-07-26 16:59:33 +00:00
|
|
|
Expect(sepLines).To(HaveLen(3))
|
|
|
|
})
|
|
|
|
It("prunes snapshots down to 2", func() {
|
2022-01-06 16:05:56 +00:00
|
|
|
Expect(testutil.K3sCmd("etcd-snapshot", "prune --snapshot-retention 2 --name PRUNE_TEST")).
|
2021-11-29 18:30:04 +00:00
|
|
|
To(ContainSubstring("Removing local snapshot"))
|
2021-08-10 18:13:26 +00:00
|
|
|
lsResult, err := testutil.K3sCmd("etcd-snapshot", "ls")
|
2021-07-26 16:59:33 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-08-26 23:26:19 +00:00
|
|
|
reg, err := regexp.Compile(`:///var/lib/rancher/k3s/server/db/snapshots/PRUNE_TEST`)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
sepLines := reg.FindAllString(lsResult, -1)
|
2021-07-26 16:59:33 +00:00
|
|
|
Expect(sepLines).To(HaveLen(2))
|
|
|
|
})
|
|
|
|
It("cleans up remaining snapshots", func() {
|
2021-08-10 18:13:26 +00:00
|
|
|
lsResult, err := testutil.K3sCmd("etcd-snapshot", "ls")
|
2021-07-26 16:59:33 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
reg, err := regexp.Compile(`PRUNE_TEST[^\s]+`)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
for _, snapshotName := range reg.FindAllString(lsResult, -1) {
|
2021-08-10 18:13:26 +00:00
|
|
|
Expect(testutil.K3sCmd("etcd-snapshot", "delete", snapshotName)).
|
2021-07-26 16:59:33 +00:00
|
|
|
To(ContainSubstring("Removing the given locally stored etcd snapshot"))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
var _ = AfterSuite(func() {
|
2021-08-26 23:26:19 +00:00
|
|
|
if !testutil.IsExistingServer() {
|
2022-08-31 22:40:17 +00:00
|
|
|
if CurrentSpecReport().Failed() {
|
|
|
|
testutil.K3sDumpLog(server)
|
|
|
|
}
|
2022-02-25 16:42:21 +00:00
|
|
|
Expect(testutil.K3sKillServer(server)).To(Succeed())
|
|
|
|
Expect(testutil.K3sCleanup(testLock, "")).To(Succeed())
|
2021-08-26 23:26:19 +00:00
|
|
|
}
|
2021-07-26 16:59:33 +00:00
|
|
|
})
|
|
|
|
|
2022-02-09 16:22:53 +00:00
|
|
|
func Test_IntegrationEtcdSnapshot(t *testing.T) {
|
2021-07-26 16:59:33 +00:00
|
|
|
RegisterFailHandler(Fail)
|
2022-02-09 16:22:53 +00:00
|
|
|
RunSpecs(t, "Etcd Snapshot Suite")
|
2021-07-26 16:59:33 +00:00
|
|
|
}
|