Skip to content

Commit

Permalink
test: add standalone wal e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
zyy17 committed Sep 20, 2024
1 parent 7cd8c90 commit 84728ec
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 12 deletions.
6 changes: 5 additions & 1 deletion controllers/greptimedbstandalone/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,12 @@ func (d *StandaloneDeployer) deleteStorage(ctx context.Context, namespace, name
constant.GreptimeDBComponentName: common.ResourceName(name, v1alpha1.StandaloneKind),
}

if additionalLabels != nil {
matachedLabels = util.MergeStringMap(matachedLabels, additionalLabels)
}

selector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{
MatchLabels: util.MergeStringMap(matachedLabels, additionalLabels),
MatchLabels: matachedLabels,
})
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/greptimedbcluster/test_basic_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestBasicCluster(ctx context.Context, h *helper.Helper) {
}, helper.DefaultTimeout, time.Second).Should(HaveOccurred())

By("The PVC of the datanode should be retained")
datanodePVCs, err := h.GetPVCs(ctx, testCluster.Namespace, testCluster.Name, greptimev1alpha1.DatanodeComponentKind)
datanodePVCs, err := h.GetPVCs(ctx, testCluster.Namespace, testCluster.Name, greptimev1alpha1.DatanodeComponentKind, nil)
Expect(err).NotTo(HaveOccurred(), "failed to get datanode PVCs")
Expect(int32(len(datanodePVCs))).To(Equal(*testCluster.Spec.Datanode.Replicas), "the number of datanode PVCs should be equal to the number of datanode replicas")

Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/greptimedbcluster/test_cluster_enable_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestClusterEnableFlow(ctx context.Context, h *helper.Helper) {
}, helper.DefaultTimeout, time.Second).Should(HaveOccurred())

By("The PVC of the datanode should be retained")
datanodePVCs, err := h.GetPVCs(ctx, testCluster.Namespace, testCluster.Name, greptimev1alpha1.DatanodeComponentKind)
datanodePVCs, err := h.GetPVCs(ctx, testCluster.Namespace, testCluster.Name, greptimev1alpha1.DatanodeComponentKind, nil)
Expect(err).NotTo(HaveOccurred(), "failed to get datanode PVCs")
Expect(int32(len(datanodePVCs))).To(Equal(*testCluster.Spec.Datanode.Replicas), "the number of datanode PVCs should be equal to the number of datanode replicas")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestClusterEnableRemoteWal(ctx context.Context, h *helper.Helper) {
}, helper.DefaultTimeout, time.Second).Should(HaveOccurred())

By("The PVC of the datanode should be retained")
datanodePVCs, err := h.GetPVCs(ctx, testCluster.Namespace, testCluster.Name, greptimev1alpha1.DatanodeComponentKind)
datanodePVCs, err := h.GetPVCs(ctx, testCluster.Namespace, testCluster.Name, greptimev1alpha1.DatanodeComponentKind, nil)
Expect(err).NotTo(HaveOccurred(), "failed to get datanode PVCs")
Expect(int32(len(datanodePVCs))).To(Equal(*testCluster.Spec.Datanode.Replicas), "the number of datanode PVCs should be equal to the number of datanode replicas")

Expand Down
111 changes: 111 additions & 0 deletions tests/e2e/greptimedbcluster/test_cluster_standalone_wal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright 2024 Greptime Team
//
// 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 greptimedbcluster

import (
"context"
"fmt"
"net"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"sigs.k8s.io/controller-runtime/pkg/client"

greptimev1alpha1 "github.com/GreptimeTeam/greptimedb-operator/apis/v1alpha1"
"github.com/GreptimeTeam/greptimedb-operator/controllers/common"
"github.com/GreptimeTeam/greptimedb-operator/tests/e2e/helper"
)

// TestClusterStandaloneWAL tests a basic cluster.
func TestClusterStandaloneWAL(ctx context.Context, h *helper.Helper) {
const (
testCRFile = "./testdata/resources/cluster/standalone-wal/cluster.yaml"
testSQLFile = "./testdata/sql/cluster/partition.sql"
)

By(fmt.Sprintf("greptimecluster test with CR file %s and SQL file %s", testCRFile, testSQLFile))

testCluster := new(greptimev1alpha1.GreptimeDBCluster)
err := h.LoadCR(testCRFile, testCluster)
Expect(err).NotTo(HaveOccurred(), "failed to load greptimedbcluster yaml file")

err = h.Create(ctx, testCluster)
Expect(err).NotTo(HaveOccurred(), "failed to create greptimedbcluster")

By("Check the status of testCluster")
Eventually(func() error {
clusterPhase, err := h.GetPhase(ctx, testCluster.Namespace, testCluster.Name, new(greptimev1alpha1.GreptimeDBCluster))
if err != nil {
return err
}

if clusterPhase != greptimev1alpha1.PhaseRunning {
return fmt.Errorf("cluster is not running")
}

return nil
}, helper.DefaultTimeout, time.Second).ShouldNot(HaveOccurred())

By("Execute distributed SQL test")
frontendAddr, err := h.PortForward(ctx, testCluster.Namespace, common.ResourceName(testCluster.Name, greptimev1alpha1.FrontendComponentKind), int(testCluster.Spec.PostgreSQLPort))
Expect(err).NotTo(HaveOccurred(), "failed to port forward frontend service")
Eventually(func() error {
conn, err := net.Dial("tcp", frontendAddr)
if err != nil {
return err
}
conn.Close()
return nil
}, helper.DefaultTimeout, time.Second).ShouldNot(HaveOccurred())

err = h.RunSQLTest(ctx, frontendAddr, testSQLFile)
Expect(err).NotTo(HaveOccurred(), "failed to run sql test")

By("Kill the port forwarding process")
h.KillPortForwardProcess()

By("Delete cluster")
err = h.Delete(ctx, testCluster)
Expect(err).NotTo(HaveOccurred(), "failed to delete cluster")
Eventually(func() error {
// The cluster will be deleted eventually.
return h.Get(ctx, client.ObjectKey{Name: testCluster.Namespace, Namespace: testCluster.Namespace}, testCluster)
}, helper.DefaultTimeout, time.Second).Should(HaveOccurred())

By("The PVC of the datanode should be retained")
datanodePVCs, err := h.GetPVCs(ctx, testCluster.Namespace, testCluster.Name, greptimev1alpha1.DatanodeComponentKind, common.DatanodeFileStorageLabels)
Expect(err).NotTo(HaveOccurred(), "failed to get datanode PVCs")
Expect(int32(len(datanodePVCs))).To(Equal(*testCluster.Spec.Datanode.Replicas), "the number of datanode PVCs should be equal to the number of datanode replicas")

By("The PVC of the WAL should be deleted")
Eventually(func() error {
walPVCs, err := h.GetPVCs(ctx, testCluster.Namespace, testCluster.Name, greptimev1alpha1.DatanodeComponentKind, common.WALFileStorageLabels)
if err != nil {
return err
}
if len(walPVCs) != 0 {
return fmt.Errorf("the number of WAL PVCs should be 0")
}
return nil
}, helper.DefaultTimeout, time.Second).ShouldNot(HaveOccurred())

By("Remove the PVC of the datanode")
for _, pvc := range datanodePVCs {
err = h.Delete(ctx, &pvc)
Expect(err).NotTo(HaveOccurred(), "failed to delete datanode PVC")
}
}
2 changes: 1 addition & 1 deletion tests/e2e/greptimedbstandalone/test_basic_standalone.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestBasicStandalone(ctx context.Context, h *helper.Helper) {
}, helper.DefaultTimeout, time.Second).Should(HaveOccurred())

By("The PVC of the database should be retained")
dataPVCs, err := h.GetPVCs(ctx, testStandalone.Namespace, testStandalone.Name, greptimev1alpha1.StandaloneKind)
dataPVCs, err := h.GetPVCs(ctx, testStandalone.Namespace, testStandalone.Name, greptimev1alpha1.StandaloneKind, nil)
Expect(err).NotTo(HaveOccurred(), "failed to get data PVCs")
Expect(len(dataPVCs)).To(Equal(1), "the number of datanode PVCs should be equal to 1")

Expand Down
21 changes: 14 additions & 7 deletions tests/e2e/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os/exec"
"time"

"github.com/GreptimeTeam/greptimedb-operator/pkg/util"
"github.com/jackc/pgx/v5"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -124,24 +125,30 @@ func (h *Helper) GetPhase(ctx context.Context, namespace, name string, object cl
}

// GetPVCs returns the PVC list of the given component.
func (h *Helper) GetPVCs(ctx context.Context, namespace, name string, kind greptimev1alpha1.ComponentKind) ([]corev1.PersistentVolumeClaim, error) {
func (h *Helper) GetPVCs(ctx context.Context, namespace, name string, kind greptimev1alpha1.ComponentKind, additionalLabels map[string]string) ([]corev1.PersistentVolumeClaim, error) {
matachedLabels := map[string]string{
constant.GreptimeDBComponentName: common.ResourceName(name, kind),
}

if additionalLabels != nil {
matachedLabels = util.MergeStringMap(matachedLabels, additionalLabels)
}

selector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{
MatchLabels: map[string]string{
constant.GreptimeDBComponentName: common.ResourceName(name, kind),
},
MatchLabels: matachedLabels,
})
if err != nil {
return nil, err
}

pvcList := new(corev1.PersistentVolumeClaimList)
claims := new(corev1.PersistentVolumeClaimList)

if err = h.List(ctx, pvcList, client.InNamespace(namespace),
if err = h.List(ctx, claims, client.InNamespace(namespace),
client.MatchingLabelsSelector{Selector: selector}); err != nil {
return nil, err
}

return pvcList.Items, nil
return claims.Items, nil
}

// CleanEtcdData cleans up all data in etcd by executing the etcdctl command in the given pod.
Expand Down
25 changes: 25 additions & 0 deletions tests/e2e/testdata/resources/cluster/standalone-wal/cluster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: greptime.io/v1alpha1
kind: GreptimeDBCluster
metadata:
name: cluster-with-standalone-wal
namespace: default
spec:
base:
main:
image: greptime/greptimedb:latest
frontend:
replicas: 1
meta:
replicas: 1
etcdEndpoints:
- "etcd.etcd-cluster:2379"
datanode:
replicas: 1
wal:
raftEngine:
fs:
name: wal
storageClassName: standard
storageSize: 5Gi
mountPath: /wal
storageRetainPolicy: Delete # The wal will be deleted after cluster is destroyed.

0 comments on commit 84728ec

Please sign in to comment.