Skip to content

Commit 77704a3

Browse files
committed
e2e: MysqlCluster's test case and related api. radondb#344
1 parent b31899b commit 77704a3

File tree

8 files changed

+536
-18
lines changed

8 files changed

+536
-18
lines changed

test/e2e/cluster/cluster.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
}

test/e2e/e2e.go

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,61 @@ limitations under the License.
1717
package e2e
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"os"
2223
"path"
24+
"strings"
2325
"testing"
2426

2527
"github.com/golang/glog"
2628
. "github.com/onsi/ginkgo"
2729
"github.com/onsi/ginkgo/config"
2830
"github.com/onsi/ginkgo/reporters"
2931
. "github.com/onsi/gomega"
32+
corev1 "k8s.io/api/core/v1"
33+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3034
runtimeutils "k8s.io/apimachinery/pkg/util/runtime"
35+
clientset "k8s.io/client-go/kubernetes"
3136
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
37+
"sigs.k8s.io/controller-runtime/pkg/client"
3238

3339
"github.com/radondb/radondb-mysql-kubernetes/test/e2e/framework"
3440
"github.com/radondb/radondb-mysql-kubernetes/test/e2e/framework/ginkgowrapper"
3541
)
3642

3743
const (
38-
operatorNamespace = "mysql-operator"
44+
RadondbMysqlE2eNamespace = "radondb-mysql-e2e"
3945
)
4046

47+
var releaseName = framework.RandStr(6)
48+
4149
var _ = SynchronizedBeforeSuite(func() []byte {
42-
// BeforeSuite logic.
50+
kubeCfg, err := framework.LoadConfig()
51+
Expect(err).To(Succeed())
52+
// restClient := core.NewForConfigOrDie(kubeCfg).RESTClient()
53+
54+
c, err := client.New(kubeCfg, client.Options{})
55+
if err != nil {
56+
Fail(fmt.Sprintf("can't instantiate k8s client: %s", err))
57+
}
58+
59+
// ginkgo node 1
60+
By("Install operator")
61+
operatorNsObj := &corev1.Namespace{
62+
ObjectMeta: metav1.ObjectMeta{
63+
Name: RadondbMysqlE2eNamespace,
64+
},
65+
}
66+
if err := c.Create(context.TODO(), operatorNsObj); err != nil {
67+
if !strings.Contains(err.Error(), "already exists") {
68+
Fail(fmt.Sprintf("can't create mysql-operator namespace: %s", err))
69+
}
70+
}
71+
framework.HelmInstallChart(releaseName, RadondbMysqlE2eNamespace)
72+
4373
return nil
74+
4475
}, func(data []byte) {
4576
// all other nodes
4677
framework.Logf("Running BeforeSuite actions on all node")
@@ -50,7 +81,25 @@ var _ = SynchronizedBeforeSuite(func() []byte {
5081
// Here, the order of functions is reversed; first, the function which runs everywhere,
5182
// and then the function that only runs on the first Ginkgo node.
5283
var _ = SynchronizedAfterSuite(func() {
53-
// AfterSuite logic.
84+
// Run on all Ginkgo nodes
85+
framework.Logf("Running AfterSuite actions on all node")
86+
framework.RunCleanupActions()
87+
88+
// get the kubernetes client
89+
kubeCfg, err := framework.LoadConfig()
90+
Expect(err).To(Succeed())
91+
92+
client, err := clientset.NewForConfig(kubeCfg)
93+
Expect(err).NotTo(HaveOccurred())
94+
95+
By("Remove operator release")
96+
framework.HelmPurgeRelease(releaseName, RadondbMysqlE2eNamespace)
97+
98+
By("Delete operator namespace")
99+
100+
if err := framework.DeleteNS(client, RadondbMysqlE2eNamespace, framework.DefaultNamespaceDeletionTimeout); err != nil {
101+
framework.Failf(fmt.Sprintf("Can't delete namespace: %s", err))
102+
}
54103
}, func() {
55104
// Run only Ginkgo on node 1
56105
framework.Logf("Running AfterSuite actions on node 1")
@@ -84,13 +133,13 @@ func RunE2ETests(t *testing.T) {
84133

85134
// add logs dumper
86135
if framework.TestContext.DumpLogsOnFailure {
87-
rps = append(rps, NewLogsPodReporter(operatorNamespace, path.Join(framework.TestContext.ReportDir,
136+
rps = append(rps, NewLogsPodReporter(RadondbMysqlE2eNamespace, path.Join(framework.TestContext.ReportDir,
88137
fmt.Sprintf("pods_logs_%d_%d.txt", config.GinkgoConfig.RandomSeed, config.GinkgoConfig.ParallelNode))))
89138
}
90139
} else {
91140
// if reportDir is not specified then print logs to stdout
92141
if framework.TestContext.DumpLogsOnFailure {
93-
rps = append(rps, NewLogsPodReporter(operatorNamespace, ""))
142+
rps = append(rps, NewLogsPodReporter(RadondbMysqlE2eNamespace, ""))
94143
}
95144
}
96145
return

test/e2e/e2e_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package e2e
1919
import (
2020
"testing"
2121

22+
// _ "github.com/radondb/radondb-mysql-kubernetes/test/e2e/cluster"
2223
"github.com/radondb/radondb-mysql-kubernetes/test/e2e/framework"
2324
_ "github.com/radondb/radondb-mysql-kubernetes/test/e2e/simplecase"
2425
)

0 commit comments

Comments
 (0)