|
| 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 | + |
| 17 | +package cluster |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "math/rand" |
| 23 | + "time" |
| 24 | + |
| 25 | + . "github.com/onsi/ginkgo" |
| 26 | + . "github.com/onsi/gomega" |
| 27 | + corev1 "k8s.io/api/core/v1" |
| 28 | + "k8s.io/apimachinery/pkg/types" |
| 29 | + |
| 30 | + apiv1alpha1 "github.com/radondb/radondb-mysql-kubernetes/api/v1alpha1" |
| 31 | + "github.com/radondb/radondb-mysql-kubernetes/test/e2e/framework" |
| 32 | +) |
| 33 | + |
| 34 | +const ( |
| 35 | + POLLING = 2 * time.Second |
| 36 | +) |
| 37 | + |
| 38 | +var _ = Describe("MySQL Cluster E2E Tests", func() { |
| 39 | + f := framework.NewFramework("mc-1") |
| 40 | + two := int32(2) |
| 41 | + three := int32(3) |
| 42 | + five := int32(5) |
| 43 | + |
| 44 | + var ( |
| 45 | + cluster *apiv1alpha1.MysqlCluster |
| 46 | + clusterKey types.NamespacedName |
| 47 | + name string |
| 48 | + ) |
| 49 | + |
| 50 | + BeforeEach(func() { |
| 51 | + // Be careful, mysql allowed hostname lenght is <63. |
| 52 | + name = fmt.Sprintf("cl-%d", rand.Int31()/1000) |
| 53 | + |
| 54 | + By("creating a new cluster") |
| 55 | + cluster = framework.NewCluster(name, f.Namespace.Name) |
| 56 | + clusterKey = types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace} |
| 57 | + Expect(f.Client.Create(context.TODO(), cluster)).To(Succeed(), "failed to create cluster '%s'", cluster.Name) |
| 58 | + |
| 59 | + By("testing the cluster readiness") |
| 60 | + waitClusterReadiness(f, cluster) |
| 61 | + Expect(f.Client.Get(context.TODO(), clusterKey, cluster)).To(Succeed(), "failed to get cluster %s", cluster.Name) |
| 62 | + }) |
| 63 | + |
| 64 | + It("scale out/in a cluster, 2 -> 3 -> 5 -> 3 -> 2", func() { |
| 65 | + By("test cluster is ready after scale out 2 -> 3") |
| 66 | + cluster.Spec.Replicas = &three |
| 67 | + Expect(f.Client.Update(context.TODO(), cluster)).To(Succeed()) |
| 68 | + fmt.Println("scale time: ", waitClusterReadiness(f, cluster)) |
| 69 | + |
| 70 | + By("test cluster is ready after scale out 3 -> 5") |
| 71 | + cluster.Spec.Replicas = &five |
| 72 | + Expect(f.Client.Update(context.TODO(), cluster)).To(Succeed()) |
| 73 | + fmt.Println("scale time: ", waitClusterReadiness(f, cluster)) |
| 74 | + |
| 75 | + By("test cluster is ready after scale in 5 -> 3") |
| 76 | + cluster.Spec.Replicas = &three |
| 77 | + Expect(f.Client.Update(context.TODO(), cluster)).To(Succeed()) |
| 78 | + fmt.Println("scale time: ", waitClusterReadiness(f, cluster)) |
| 79 | + |
| 80 | + By("test cluster is ready after scale in 3 -> 2") |
| 81 | + cluster.Spec.Replicas = &two |
| 82 | + Expect(f.Client.Update(context.TODO(), cluster)).To(Succeed()) |
| 83 | + fmt.Println("scale time: ", waitClusterReadiness(f, cluster)) |
| 84 | + }) |
| 85 | + |
| 86 | +}) |
| 87 | + |
| 88 | +// waitClusterReadiness determine whether the cluster is ready. |
| 89 | +func waitClusterReadiness(f *framework.Framework, cluster *apiv1alpha1.MysqlCluster) time.Duration { |
| 90 | + startTime := time.Now() |
| 91 | + timeout := f.Timeout |
| 92 | + if *cluster.Spec.Replicas > 0 { |
| 93 | + timeout = time.Duration(*cluster.Spec.Replicas) * f.Timeout |
| 94 | + } |
| 95 | + // Wait for pods to be ready. |
| 96 | + f.ClusterEventuallyReplicas(cluster, timeout) |
| 97 | + // Wait for xenon to be ready. |
| 98 | + f.ClusterEventuallyRaftStatus(cluster) |
| 99 | + // Wait for condition to be ready. |
| 100 | + f.ClusterEventuallyCondition(cluster, apiv1alpha1.ConditionReady, corev1.ConditionTrue, f.Timeout) |
| 101 | + return time.Since(startTime) |
| 102 | +} |
0 commit comments