From 82dadc50173127fd288a0e7321d235c49794d467 Mon Sep 17 00:00:00 2001 From: shunki-fujita Date: Tue, 6 Aug 2024 05:24:07 +0000 Subject: [PATCH] wip --- e2e/backup_test.go | 66 ++++++++++++++++---- e2e/testdata/{restore.yaml => restore1.yaml} | 2 +- e2e/testdata/restore2.yaml | 40 ++++++++++++ 3 files changed, 94 insertions(+), 14 deletions(-) rename e2e/testdata/{restore.yaml => restore1.yaml} (98%) create mode 100644 e2e/testdata/restore2.yaml diff --git a/e2e/backup_test.go b/e2e/backup_test.go index aa0772e50..8fe1afca4 100644 --- a/e2e/backup_test.go +++ b/e2e/backup_test.go @@ -23,8 +23,11 @@ var makeBucketYAML string //go:embed testdata/backup.yaml var backupYAML string -//go:embed testdata/restore.yaml -var restoreYAML string +//go:embed testdata/restore1.yaml +var restore1YAML string + +//go:embed testdata/restore2.yaml +var restore2YAML string var _ = Context("backup", func() { if doUpgrade { @@ -58,11 +61,17 @@ var _ = Context("backup", func() { }).Should(Succeed()) kubectlSafe(nil, "moco", "-n", "backup", "mysql", "-u", "moco-writable", "source", "--", - "-e", "CREATE DATABASE test") + "-e", "CREATE DATABASE test1") + kubectlSafe(nil, "moco", "-n", "backup", "mysql", "-u", "moco-writable", "source", "--", + "-D", "test1", "-e", "CREATE TABLE t (id INT NOT NULL AUTO_INCREMENT, data VARCHAR(32) NOT NULL, PRIMARY KEY (id), KEY key1 (data), KEY key2 (data, id)) ENGINE=InnoDB") + kubectlSafe(nil, "moco", "-n", "backup", "mysql", "-u", "moco-writable", "source", "--", + "-D", "test1", "--init_command=SET autocommit=1", "-e", "INSERT INTO t (data) VALUES ('aaa')") + kubectlSafe(nil, "moco", "-n", "backup", "mysql", "-u", "moco-writable", "source", "--", + "-e", "CREATE DATABASE test2") kubectlSafe(nil, "moco", "-n", "backup", "mysql", "-u", "moco-writable", "source", "--", - "-D", "test", "-e", "CREATE TABLE t (id INT NOT NULL AUTO_INCREMENT, data VARCHAR(32) NOT NULL, PRIMARY KEY (id), KEY key1 (data), KEY key2 (data, id)) ENGINE=InnoDB") + "-D", "test2", "-e", "CREATE TABLE t (id INT NOT NULL AUTO_INCREMENT, data VARCHAR(32) NOT NULL, PRIMARY KEY (id), KEY key1 (data), KEY key2 (data, id)) ENGINE=InnoDB") kubectlSafe(nil, "moco", "-n", "backup", "mysql", "-u", "moco-writable", "source", "--", - "-D", "test", "--init_command=SET autocommit=1", "-e", "INSERT INTO t (data) VALUES ('aaa')") + "-D", "test2", "--init_command=SET autocommit=1", "-e", "INSERT INTO t (data) VALUES ('aaa')") }) It("should take a full dump", func() { @@ -81,14 +90,14 @@ var _ = Context("backup", func() { It("should take an incremental backup", func() { kubectlSafe(nil, "moco", "-n", "backup", "mysql", "-u", "moco-writable", "source", "--", - "-D", "test", "--init_command=SET autocommit=1", "-e", "INSERT INTO t (data) VALUES ('bbb')") + "-D", "test1", "--init_command=SET autocommit=1", "-e", "INSERT INTO t (data) VALUES ('bbb')") time.Sleep(1100 * time.Millisecond) restorePoint = time.Now().UTC() time.Sleep(1100 * time.Millisecond) kubectlSafe(nil, "moco", "-n", "backup", "mysql", "-u", "moco-admin", "source", "--", - "-D", "test", "--init_command=SET autocommit=1", "-e", "FLUSH LOCAL BINARY LOGS") + "-D", "test1", "--init_command=SET autocommit=1", "-e", "FLUSH LOCAL BINARY LOGS") kubectlSafe(nil, "moco", "-n", "backup", "mysql", "-u", "moco-writable", "source", "--", - "-D", "test", "--init_command=SET autocommit=1", "-e", "INSERT INTO t (data) VALUES ('ccc')") + "-D", "test1", "--init_command=SET autocommit=1", "-e", "INSERT INTO t (data) VALUES ('ccc')") time.Sleep(100 * time.Millisecond) kubectlSafe(nil, "-n", "backup", "create", "job", "--from=cronjob/moco-backup-source", "backup-2") @@ -111,7 +120,38 @@ var _ = Context("backup", func() { It("should destroy the source then restore the backup data", func() { kubectlSafe(nil, "-n", "backup", "delete", "mysqlclusters", "source") - tmpl, err := template.New("").Parse(restoreYAML) + tmpl, err := template.New("").Parse(restore1YAML) + Expect(err).NotTo(HaveOccurred()) + buf := new(bytes.Buffer) + err = tmpl.Execute(buf, struct { + MySQLVersion string + RestorePoint string + }{ + mysqlVersion, + restorePoint.Format(time.RFC3339), + }) + Expect(err).NotTo(HaveOccurred()) + + kubectlSafe(buf.Bytes(), "apply", "-f", "-") + Eventually(func(g Gomega) { + cluster, err := getCluster("backup", "target1") + g.Expect(err).NotTo(HaveOccurred()) + condHealthy, err := getClusterCondition(cluster, mocov1beta2.ConditionHealthy) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(condHealthy.Status).To(Equal(metav1.ConditionTrue), "target1 is not healthy") + }).Should(Succeed()) + + out := kubectlSafe(nil, "moco", "-n", "backup", "mysql", "target1", "--", + "-N", "-D", "test1", "-e", "SELECT COUNT(*) FROM t") + count, err := strconv.Atoi(strings.TrimSpace(string(out))) + Expect(err).NotTo(HaveOccurred()) + Expect(count).To(Equal(2)) + }) + + It("should restore only test2 schema", func() { + kubectlSafe(nil, "-n", "backup", "delete", "mysqlclusters", "source") + + tmpl, err := template.New("").Parse(restore2YAML) Expect(err).NotTo(HaveOccurred()) buf := new(bytes.Buffer) err = tmpl.Execute(buf, struct { @@ -125,15 +165,15 @@ var _ = Context("backup", func() { kubectlSafe(buf.Bytes(), "apply", "-f", "-") Eventually(func(g Gomega) { - cluster, err := getCluster("backup", "target") + cluster, err := getCluster("backup", "target2") g.Expect(err).NotTo(HaveOccurred()) condHealthy, err := getClusterCondition(cluster, mocov1beta2.ConditionHealthy) g.Expect(err).NotTo(HaveOccurred()) - g.Expect(condHealthy.Status).To(Equal(metav1.ConditionTrue), "target is not healthy") + g.Expect(condHealthy.Status).To(Equal(metav1.ConditionTrue), "target2 is not healthy") }).Should(Succeed()) - out := kubectlSafe(nil, "moco", "-n", "backup", "mysql", "target", "--", - "-N", "-D", "test", "-e", "SELECT COUNT(*) FROM t") + out := kubectlSafe(nil, "moco", "-n", "backup", "mysql", "target2", "--", + "-N", "-D", "test1", "-e", "SELECT COUNT(*) FROM t") count, err := strconv.Atoi(strings.TrimSpace(string(out))) Expect(err).NotTo(HaveOccurred()) Expect(count).To(Equal(2)) diff --git a/e2e/testdata/restore.yaml b/e2e/testdata/restore1.yaml similarity index 98% rename from e2e/testdata/restore.yaml rename to e2e/testdata/restore1.yaml index 79835305b..d77c0182a 100644 --- a/e2e/testdata/restore.yaml +++ b/e2e/testdata/restore1.yaml @@ -2,7 +2,7 @@ apiVersion: moco.cybozu.com/v1beta2 kind: MySQLCluster metadata: namespace: backup - name: target + name: target1 spec: mysqlConfigMapName: mycnf replicas: 1 diff --git a/e2e/testdata/restore2.yaml b/e2e/testdata/restore2.yaml new file mode 100644 index 000000000..7083daa82 --- /dev/null +++ b/e2e/testdata/restore2.yaml @@ -0,0 +1,40 @@ +apiVersion: moco.cybozu.com/v1beta2 +kind: MySQLCluster +metadata: + namespace: backup + name: target2 +spec: + mysqlConfigMapName: mycnf + replicas: 1 + restore: + sourceName: source + sourceNamespace: backup + restorePoint: "{{ .RestorePoint }}" + jobConfig: + serviceAccountName: backup-owner + env: + - name: AWS_ACCESS_KEY_ID + value: minioadmin + - name: AWS_SECRET_ACCESS_KEY + value: minioadmin + - name: AWS_REGION + value: us-east-1 + bucketConfig: + bucketName: moco + endpointURL: http://minio.default.svc:9000 + usePathStyle: true + workVolume: + emptyDir: {} + podTemplate: + spec: + containers: + - name: mysqld + image: ghcr.io/cybozu-go/moco/mysql:{{ .MySQLVersion }} + volumeClaimTemplates: + - metadata: + name: mysql-data + spec: + accessModes: ["ReadWriteOnce"] + resources: + requests: + storage: 1Gi