Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions test/e2e/cluster/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
Copyright 2021 RadonDB.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cluster

import (
"context"
"fmt"
"math/rand"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"

apiv1alpha1 "github.com/radondb/radondb-mysql-kubernetes/api/v1alpha1"
"github.com/radondb/radondb-mysql-kubernetes/test/e2e/framework"
)

const (
POLLING = 2 * time.Second
)

var _ = Describe("MySQL Cluster E2E Tests", func() {
f := framework.NewFramework("mc-1")
two := int32(2)
three := int32(3)
five := int32(5)

var (
cluster *apiv1alpha1.MysqlCluster
clusterKey types.NamespacedName
name string
)

BeforeEach(func() {
// Be careful, mysql allowed hostname lenght is <63.
name = fmt.Sprintf("cl-%d", rand.Int31()/1000)

By("creating a new cluster")
cluster = framework.NewCluster(name, f.Namespace.Name)
clusterKey = types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}
Expect(f.Client.Create(context.TODO(), cluster)).To(Succeed(), "failed to create cluster '%s'", cluster.Name)

By("testing the cluster readiness")
waitClusterReadiness(f, cluster)
Expect(f.Client.Get(context.TODO(), clusterKey, cluster)).To(Succeed(), "failed to get cluster %s", cluster.Name)
})

It("scale out/in a cluster, 2 -> 3 -> 5 -> 3 -> 2", func() {
By("test cluster is ready after scale out 2 -> 3")
cluster.Spec.Replicas = &three
Expect(f.Client.Update(context.TODO(), cluster)).To(Succeed())
fmt.Println("scale time: ", waitClusterReadiness(f, cluster))

By("test cluster is ready after scale out 3 -> 5")
cluster.Spec.Replicas = &five
Expect(f.Client.Update(context.TODO(), cluster)).To(Succeed())
fmt.Println("scale time: ", waitClusterReadiness(f, cluster))

By("test cluster is ready after scale in 5 -> 3")
cluster.Spec.Replicas = &three
Expect(f.Client.Update(context.TODO(), cluster)).To(Succeed())
fmt.Println("scale time: ", waitClusterReadiness(f, cluster))

By("test cluster is ready after scale in 3 -> 2")
cluster.Spec.Replicas = &two
Expect(f.Client.Update(context.TODO(), cluster)).To(Succeed())
fmt.Println("scale time: ", waitClusterReadiness(f, cluster))
})

})

// waitClusterReadiness determine whether the cluster is ready.
func waitClusterReadiness(f *framework.Framework, cluster *apiv1alpha1.MysqlCluster) time.Duration {
startTime := time.Now()
timeout := f.Timeout
if *cluster.Spec.Replicas > 0 {
timeout = time.Duration(*cluster.Spec.Replicas) * f.Timeout
}
// Wait for pods to be ready.
f.ClusterEventuallyReplicas(cluster, timeout)
// Wait for xenon to be ready.
f.ClusterEventuallyRaftStatus(cluster)
// Wait for condition to be ready.
f.ClusterEventuallyCondition(cluster, apiv1alpha1.ConditionReady, corev1.ConditionTrue, f.Timeout)
return time.Since(startTime)
}
59 changes: 54 additions & 5 deletions test/e2e/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,61 @@ limitations under the License.
package e2e

import (
"context"
"fmt"
"os"
"path"
"strings"
"testing"

"github.com/golang/glog"
. "github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/config"
"github.com/onsi/ginkgo/reporters"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
runtimeutils "k8s.io/apimachinery/pkg/util/runtime"
clientset "k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/radondb/radondb-mysql-kubernetes/test/e2e/framework"
"github.com/radondb/radondb-mysql-kubernetes/test/e2e/framework/ginkgowrapper"
)

const (
operatorNamespace = "mysql-operator"
RadondbMysqlE2eNamespace = "radondb-mysql-e2e"
)

var releaseName = framework.RandStr(6)

var _ = SynchronizedBeforeSuite(func() []byte {
// BeforeSuite logic.
kubeCfg, err := framework.LoadConfig()
Expect(err).To(Succeed())
// restClient := core.NewForConfigOrDie(kubeCfg).RESTClient()

c, err := client.New(kubeCfg, client.Options{})
if err != nil {
Fail(fmt.Sprintf("can't instantiate k8s client: %s", err))
}

// ginkgo node 1
By("Install operator")
operatorNsObj := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: RadondbMysqlE2eNamespace,
},
}
if err := c.Create(context.TODO(), operatorNsObj); err != nil {
if !strings.Contains(err.Error(), "already exists") {
Fail(fmt.Sprintf("can't create mysql-operator namespace: %s", err))
}
}
framework.HelmInstallChart(releaseName, RadondbMysqlE2eNamespace)

return nil

}, func(data []byte) {
// all other nodes
framework.Logf("Running BeforeSuite actions on all node")
Expand All @@ -50,7 +81,25 @@ var _ = SynchronizedBeforeSuite(func() []byte {
// Here, the order of functions is reversed; first, the function which runs everywhere,
// and then the function that only runs on the first Ginkgo node.
var _ = SynchronizedAfterSuite(func() {
// AfterSuite logic.
// Run on all Ginkgo nodes
framework.Logf("Running AfterSuite actions on all node")
framework.RunCleanupActions()

// get the kubernetes client
kubeCfg, err := framework.LoadConfig()
Expect(err).To(Succeed())

client, err := clientset.NewForConfig(kubeCfg)
Expect(err).NotTo(HaveOccurred())

By("Remove operator release")
framework.HelmPurgeRelease(releaseName, RadondbMysqlE2eNamespace)

By("Delete operator namespace")

if err := framework.DeleteNS(client, RadondbMysqlE2eNamespace, framework.DefaultNamespaceDeletionTimeout); err != nil {
framework.Failf(fmt.Sprintf("Can't delete namespace: %s", err))
}
}, func() {
// Run only Ginkgo on node 1
framework.Logf("Running AfterSuite actions on node 1")
Expand Down Expand Up @@ -84,13 +133,13 @@ func RunE2ETests(t *testing.T) {

// add logs dumper
if framework.TestContext.DumpLogsOnFailure {
rps = append(rps, NewLogsPodReporter(operatorNamespace, path.Join(framework.TestContext.ReportDir,
rps = append(rps, NewLogsPodReporter(RadondbMysqlE2eNamespace, path.Join(framework.TestContext.ReportDir,
fmt.Sprintf("pods_logs_%d_%d.txt", config.GinkgoConfig.RandomSeed, config.GinkgoConfig.ParallelNode))))
}
} else {
// if reportDir is not specified then print logs to stdout
if framework.TestContext.DumpLogsOnFailure {
rps = append(rps, NewLogsPodReporter(operatorNamespace, ""))
rps = append(rps, NewLogsPodReporter(RadondbMysqlE2eNamespace, ""))
}
}
return
Expand Down
1 change: 1 addition & 0 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package e2e
import (
"testing"

// _ "github.com/radondb/radondb-mysql-kubernetes/test/e2e/cluster"
"github.com/radondb/radondb-mysql-kubernetes/test/e2e/framework"
_ "github.com/radondb/radondb-mysql-kubernetes/test/e2e/simplecase"
)
Expand Down
Loading