|
| 1 | +/* |
| 2 | +Copyright 2021 RadonDB. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package backup |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "math/rand" |
| 22 | + "regexp" |
| 23 | + "strings" |
| 24 | + "time" |
| 25 | + |
| 26 | + . "github.com/onsi/ginkgo" |
| 27 | + . "github.com/onsi/gomega" |
| 28 | + apiv1alpha1 "github.com/radondb/radondb-mysql-kubernetes/api/v1alpha1" |
| 29 | + "github.com/radondb/radondb-mysql-kubernetes/test/e2e/framework" |
| 30 | + "github.com/radondb/radondb-mysql-kubernetes/utils" |
| 31 | + corev1 "k8s.io/api/core/v1" |
| 32 | + "k8s.io/apimachinery/pkg/types" |
| 33 | +) |
| 34 | + |
| 35 | +var _ = Describe("MySQL Backup E2E Tests", func() { |
| 36 | + f := framework.NewFramework("mcbackup-1") |
| 37 | + _ = f |
| 38 | + |
| 39 | + var ( |
| 40 | + cluster *apiv1alpha1.MysqlCluster |
| 41 | + clusterKey types.NamespacedName |
| 42 | + name string |
| 43 | + backupSecret *corev1.Secret |
| 44 | + //timeout time.Duration |
| 45 | + POLLING = 2 * time.Second |
| 46 | + backupDir string |
| 47 | + leader string |
| 48 | + follower string |
| 49 | + ) |
| 50 | + |
| 51 | + BeforeEach(func() { |
| 52 | + // be careful, mysql allowed hostname lenght is <63 |
| 53 | + name = fmt.Sprintf("bk-%d", rand.Int31()/1000) |
| 54 | + |
| 55 | + //timeout = 350 * time.Second |
| 56 | + |
| 57 | + By("create a new backup secret") |
| 58 | + backupSecret = f.NewBackupSecret() |
| 59 | + Expect(f.Client.Create(context.TODO(), backupSecret)).To(Succeed(), "create backup secret failed") |
| 60 | + By("creating a new cluster") |
| 61 | + cluster = framework.NewCluster(name, f.Namespace.Name) |
| 62 | + clusterKey = types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace} |
| 63 | + cluster.Spec.BackupSecretName = backupSecret.Name |
| 64 | + Expect(f.Client.Create(context.TODO(), cluster)).To(Succeed(), |
| 65 | + "failed to create cluster '%s'", cluster.Name) |
| 66 | + By("waiting the cluster readiness") |
| 67 | + framework.WaitClusterReadiness(f, cluster) |
| 68 | + //get leader |
| 69 | + for _, node := range cluster.Status.Nodes { |
| 70 | + if node.RaftStatus.Role == string(utils.Leader) { |
| 71 | + leader = strings.Split(node.Name, ".")[0] |
| 72 | + } else if node.RaftStatus.Role == string(utils.Follower) { |
| 73 | + follower = strings.Split(node.Name, ".")[0] |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + Expect(f.Client.Get(context.TODO(), clusterKey, cluster)).To(Succeed(), "failed to get cluster %s", cluster.Name) |
| 78 | + |
| 79 | + Eventually(f.RefreshClusterFn(cluster), f.Timeout, POLLING).Should( |
| 80 | + framework.HaveClusterReplicas(2)) |
| 81 | + Eventually(f.RefreshClusterFn(cluster), f.Timeout, POLLING).Should( |
| 82 | + framework.HaveClusterCond(apiv1alpha1.ConditionReady, corev1.ConditionTrue)) |
| 83 | + |
| 84 | + // refresh cluster |
| 85 | + Expect(f.Client.Get(context.TODO(), clusterKey, cluster)).To(Succeed(), |
| 86 | + "failed to get cluster %s", cluster.Name) |
| 87 | + |
| 88 | + }) |
| 89 | + |
| 90 | + It("backup to object store", func() { |
| 91 | + //exectute sql command in mysql pod |
| 92 | + By("executing insert data") |
| 93 | + _, err := f.ExecSQLOnNode(*cluster, leader, "create table testtable (id int)") |
| 94 | + Expect(err).To(BeNil()) |
| 95 | + _, err = f.ExecSQLOnNode(*cluster, leader, "insert into testtable values (1),(2),(3)") |
| 96 | + Expect(err).To(BeNil()) |
| 97 | + rows, err := f.ExecSQLOnNode(*cluster, leader, "select * from testtable") |
| 98 | + Expect(err).To(BeNil(), "failed to execute sql") |
| 99 | + defer rows.Close() |
| 100 | + var id int |
| 101 | + ids := make([]int, 0) |
| 102 | + for rows.Next() { |
| 103 | + if err := rows.Scan(&id); err != nil { |
| 104 | + Fail(err.Error()) |
| 105 | + } |
| 106 | + ids = append(ids, id) |
| 107 | + } |
| 108 | + Expect(ids).To(Equal([]int{1, 2, 3})) |
| 109 | + Eventually(func() []int { |
| 110 | + rows, _ := |
| 111 | + f.ExecSQLOnNode(*cluster, follower, "select * from testtable ") |
| 112 | + if rows == nil { |
| 113 | + return nil |
| 114 | + } |
| 115 | + defer rows.Close() |
| 116 | + var id int |
| 117 | + ids := make([]int, 0) |
| 118 | + for rows.Next() { |
| 119 | + if err := rows.Scan(&id); err != nil { |
| 120 | + Fail(err.Error()) |
| 121 | + } |
| 122 | + ids = append(ids, id) |
| 123 | + } |
| 124 | + return ids |
| 125 | + }, f.Timeout, POLLING).Should(Equal([]int{1, 2, 3})) |
| 126 | + By("executing a backup ") |
| 127 | + // do the backup |
| 128 | + backup := framework.NewBackup(cluster, leader) |
| 129 | + Expect(f.Client.Create(context.TODO(), backup)).To(Succeed(), |
| 130 | + "failed to create backup '%s'", backup.Name) |
| 131 | + |
| 132 | + Eventually(f.RefreshBackupFn(backup), f.Timeout, POLLING).Should( |
| 133 | + framework.HaveBackupComplete()) |
| 134 | + |
| 135 | + if str, err := framework.GetPodLogs(f.ClientSet, f.Namespace.Name, leader, "backup"); err == nil { |
| 136 | + r, _ := regexp.Compile("backup_[0-9]+") |
| 137 | + backupDir = r.FindString(str) |
| 138 | + } |
| 139 | + nameRestore := fmt.Sprintf("rs-%d", rand.Int31()/1000) |
| 140 | + By("creating a new cluster from backup") |
| 141 | + clusterRestore := framework.NewCluster(nameRestore, f.Namespace.Name) |
| 142 | + clusterKeyRestore := types.NamespacedName{Name: clusterRestore.Name, Namespace: clusterRestore.Namespace} |
| 143 | + clusterRestore.Spec.BackupSecretName = backupSecret.Name |
| 144 | + clusterRestore.Spec.RestoreFrom = backupDir |
| 145 | + Expect(f.Client.Create(context.TODO(), clusterRestore)).To(Succeed(), |
| 146 | + "failed to create clusterRestore '%s'", clusterRestore.Name) |
| 147 | + By("waiting the clusterRestore readiness") |
| 148 | + framework.WaitClusterReadiness(f, clusterRestore) |
| 149 | + Eventually(f.RefreshClusterFn(clusterRestore), f.Timeout, POLLING).Should( |
| 150 | + framework.HaveClusterReplicas(2)) |
| 151 | + Eventually(f.RefreshClusterFn(clusterRestore), f.Timeout, POLLING).Should( |
| 152 | + framework.HaveClusterCond(apiv1alpha1.ConditionReady, corev1.ConditionTrue)) |
| 153 | + Eventually(func() []int { |
| 154 | + rows, _ := f.ExecSQLOnNode(*clusterRestore, fmt.Sprintf("%s-mysql-0", nameRestore), "select * from testtable ") |
| 155 | + if rows == nil { |
| 156 | + return nil |
| 157 | + } |
| 158 | + defer rows.Close() |
| 159 | + var id int |
| 160 | + ids := make([]int, 0) |
| 161 | + for rows.Next() { |
| 162 | + if err := rows.Scan(&id); err != nil { |
| 163 | + Fail(err.Error()) |
| 164 | + } |
| 165 | + ids = append(ids, id) |
| 166 | + } |
| 167 | + return ids |
| 168 | + }, f.Timeout, POLLING).Should(Equal([]int{1, 2, 3})) |
| 169 | + |
| 170 | + // refresh clusterRestore |
| 171 | + Expect(f.Client.Get(context.TODO(), clusterKeyRestore, clusterRestore)).To(Succeed(), |
| 172 | + "failed to get clusterRestore %s", clusterRestore.Name) |
| 173 | + Expect(f.Client.Delete(context.TODO(), clusterRestore)).To(Succeed(), |
| 174 | + "failed to delete clusterRestore '%s'", clusterRestore.Name) |
| 175 | + }) |
| 176 | + |
| 177 | +}) |
0 commit comments