Skip to content

Commit

Permalink
issue-725: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
shunki-fujita committed Aug 7, 2024
1 parent e866c56 commit af19522
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 20 deletions.
1 change: 1 addition & 0 deletions api/v1beta2/mysqlcluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ var _ = Describe("MySQLCluster Webhook", func() {
EndpointURL: "https://foo.bar.svc:9000",
},
},
IncludeSchemas: []string{"db1", "db2"},
}
err := k8sClient.Create(ctx, r)
Expect(err).NotTo(HaveOccurred())
Expand Down
2 changes: 1 addition & 1 deletion backup/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (o *getUUIDSetMockOp) PrepareRestore(_ context.Context) error {
panic("not implemented")
}

func (o *getUUIDSetMockOp) LoadDump(ctx context.Context, dir string) error {
func (o *getUUIDSetMockOp) LoadDump(ctx context.Context, dir string, includeSchemas []string) error {
panic("not implemented")
}

Expand Down
6 changes: 3 additions & 3 deletions backup/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ var _ = Describe("Backup/Restore", func() {
Expect(bs.WorkDirUsage).To(BeNumerically(">", 0))
Expect(bs.Warnings).To(BeEmpty())

rm, err := NewRestoreManager(cfg, bc, workDir2, "test", "single", "restore", "target", "", 3, bs.Time.Time)
rm, err := NewRestoreManager(cfg, bc, workDir2, "test", "single", "restore", "target", "", 3, bs.Time.Time, []string{})
Expect(err).NotTo(HaveOccurred())

ctx2, cancel := context.WithTimeout(ctx, 3*time.Second)
Expand Down Expand Up @@ -240,7 +240,7 @@ var _ = Describe("Backup/Restore", func() {
Expect(bs.WorkDirUsage).To(BeNumerically(">", 0))
Expect(bs.Warnings).To(BeEmpty())

rm, err := NewRestoreManager(cfg, bc, workDir2, "test", "single", "restore", "target", "", 3, restorePoint)
rm, err := NewRestoreManager(cfg, bc, workDir2, "test", "single", "restore", "target", "", 3, restorePoint, []string{})
Expect(err).NotTo(HaveOccurred())

err = rm.Restore(ctx)
Expand Down Expand Up @@ -292,7 +292,7 @@ var _ = Describe("Backup/Restore", func() {
Expect(err).NotTo(HaveOccurred())
Expect(bc.contents).To(HaveLen(3))

rm, err := NewRestoreManager(cfg, bc, workDir2, "test", "single", "restore", "target", "", 3, bt)
rm, err := NewRestoreManager(cfg, bc, workDir2, "test", "single", "restore", "target", "", 3, bt, []string{})
Expect(err).NotTo(HaveOccurred())

err = rm.Restore(ctx)
Expand Down
2 changes: 1 addition & 1 deletion backup/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (o *mockOperator) PrepareRestore(_ context.Context) error {
return nil
}

func (o *mockOperator) LoadDump(ctx context.Context, dir string) error {
func (o *mockOperator) LoadDump(ctx context.Context, dir string, includeSchemas []string) error {
if !o.prepared {
return errors.New("not prepared")
}
Expand Down
1 change: 1 addition & 0 deletions controllers/mysqlcluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,7 @@ var _ = Describe("MySQLCluster reconciler", func() {
"test",
"test",
now.UTC().Format(constants.BackupTimeFormat),
"",
}))
Expect(c.EnvFrom).To(HaveLen(1))
Expect(c.Env).To(HaveLen(2))
Expand Down
82 changes: 69 additions & 13 deletions e2e/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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", "--",
"-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")
"-e", "CREATE DATABASE test2")
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", "-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", "test2", "--init_command=SET autocommit=1", "-e", "INSERT INTO t (data) VALUES ('aaa')")
})

It("should take a full dump", func() {
Expand All @@ -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")
Expand All @@ -111,7 +120,42 @@ 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))

out = kubectlSafe(nil, "moco", "-n", "backup", "mysql", "target1", "--",
"-N", "-e", "SHOW DATABASES LIKE 'test%'")
count, err = strconv.Atoi(strings.TrimSpace(string(out)))
Expect(err).NotTo(HaveOccurred())
Expect(count).To(Equal(2))
})

It("should restore only test2 schema", func() {
tmpl, err := template.New("").Parse(restore2YAML)
Expect(err).NotTo(HaveOccurred())
buf := new(bytes.Buffer)
err = tmpl.Execute(buf, struct {
Expand All @@ -125,18 +169,30 @@ 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", "test2", "-e", "SELECT COUNT(*) FROM t")
count, err := strconv.Atoi(strings.TrimSpace(string(out)))
Expect(err).NotTo(HaveOccurred())
Expect(count).To(Equal(2))

out = kubectlSafe(nil, "moco", "-n", "backup", "mysql", "target1", "--",
"-N", "-e", "SHOW DATABASES LIKE 'test%'")
count, err = strconv.Atoi(strings.TrimSpace(string(out)))
Expect(err).NotTo(HaveOccurred())
Expect(count).To(Equal(1))

out = kubectlSafe(nil, "moco", "-n", "backup", "mysql", "target1", "--",
"-N", "-e", "SHOW DATABASES WHERE `Database` = 'test2'")
count, err = strconv.Atoi(strings.TrimSpace(string(out)))
Expect(err).NotTo(HaveOccurred())
Expect(count).To(Equal(1))
})

It("should delete clusters", func() {
Expand Down
2 changes: 1 addition & 1 deletion e2e/testdata/restore.yaml → e2e/testdata/restore1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: moco.cybozu.com/v1beta2
kind: MySQLCluster
metadata:
namespace: backup
name: target
name: target1
spec:
mysqlConfigMapName: mycnf
replicas: 1
Expand Down
41 changes: 41 additions & 0 deletions e2e/testdata/restore2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
apiVersion: moco.cybozu.com/v1beta2
kind: MySQLCluster
metadata:
namespace: backup
name: target2
spec:
mysqlConfigMapName: mycnf
replicas: 1
restore:
sourceName: source
sourceNamespace: backup
restorePoint: "{{ .RestorePoint }}"
IncludeSchemas: ["test2"]
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
2 changes: 1 addition & 1 deletion pkg/bkop/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ var _ = Describe("Operator", func() {

err = opRe.PrepareRestore(ctx)
Expect(err).NotTo(HaveOccurred())
err = opRe.LoadDump(ctx, dumpDir)
err = opRe.LoadDump(ctx, dumpDir, []string{})
Expect(err).NotTo(HaveOccurred())

var restoredGTID string
Expand Down

0 comments on commit af19522

Please sign in to comment.