Skip to content

Commit

Permalink
test: add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Phoeniix Zhao <[email protected]>
  • Loading branch information
Phoenix500526 committed Dec 2, 2023
1 parent e340e72 commit 43886a1
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 12 deletions.
20 changes: 20 additions & 0 deletions api/v1alpha1/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package v1alpha1

import (
"testing"

"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

func TestObjKey(t *testing.T) {
xlineCluster := XlineCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "xline",
Namespace: "default",
},
}
expected_objkey := types.NamespacedName{Name: "xline", Namespace: "default"}
assert.Equal(t, xlineCluster.ObjKey(), expected_objkey)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/go-logr/logr v1.2.4
github.com/onsi/ginkgo/v2 v2.11.0
github.com/onsi/gomega v1.27.10
github.com/stretchr/testify v1.8.4
k8s.io/api v0.28.3
k8s.io/apimachinery v0.28.3
k8s.io/client-go v0.28.3
Expand Down Expand Up @@ -41,6 +42,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
Expand Down
17 changes: 7 additions & 10 deletions internal/reconciler/cluster_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,15 @@ type XlineClusterReconciler struct {
type ClusterStageRecResult struct {
Stage xapi.XlineClusterOprStage
Status xapi.OprStageStatus
Action xapi.OprStageAction
Err error
}

func clusterStageSucc(stage xapi.XlineClusterOprStage, action xapi.OprStageAction) ClusterStageRecResult {
return ClusterStageRecResult{Stage: stage, Status: xapi.StageResultSucceeded, Action: action}
func clusterStageSucc(stage xapi.XlineClusterOprStage) ClusterStageRecResult {
return ClusterStageRecResult{Stage: stage, Status: xapi.StageResultSucceeded}
}

func clusterStageFail(stage xapi.XlineClusterOprStage, action xapi.OprStageAction, err error) ClusterStageRecResult {
return ClusterStageRecResult{Stage: stage, Status: xapi.StageResultFailed, Action: action, Err: err}
func clusterStageFail(stage xapi.XlineClusterOprStage, err error) ClusterStageRecResult {
return ClusterStageRecResult{Stage: stage, Status: xapi.StageResultFailed, Err: err}
}

// Reconcile all sub components
Expand All @@ -69,18 +68,16 @@ func (r *ClusterStageRecResult) AsXlineClusterRecStatus() xapi.XlineClusterRecSt

// reconcile xline cluster resources.
func (r *XlineClusterReconciler) recXlineResources() ClusterStageRecResult {
action := xapi.StageActionApply

// create a xline service
service := tran.MakeService(r.CR, r.Schema)
if err := r.CreateOrUpdate(service, &corev1.Service{}); err != nil {
return clusterStageFail(xapi.StageXlineService, action, err)
return clusterStageFail(xapi.StageXlineService, err)
}
// create a xline statefulset
statefulSet := tran.MakeStatefulSet(r.CR, r.Schema)
if err := r.CreateOrUpdate(statefulSet, &appv1.StatefulSet{}); err != nil {
return clusterStageFail(xapi.StageXlineStatefulSet, action, err)
return clusterStageFail(xapi.StageXlineStatefulSet, err)
}
return clusterStageSucc(xapi.StageComplete, action)
return clusterStageSucc(xapi.StageComplete)

}
1 change: 0 additions & 1 deletion internal/reconciler/cluster_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func (r *XlineClusterReconciler) syncXlineStatus(xlineStatus *xapi.XlineClusterS
}

stsRef := tran.GetStatefulSetKey(r.CR.ObjKey())
// collect members status via ref statefulset
sts := &appv1.StatefulSet{}
exist, err = r.Exist(stsRef, sts)
if err != nil {
Expand Down
29 changes: 29 additions & 0 deletions internal/reconciler/reconcile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package reconciler

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
xapi "github.com/xline-kv/xline-operator/api/v1alpha1"
)

func TestStatusConvert(t *testing.T) {
sucStage := clusterStageSucc(xapi.StageXlineService)
failStage := clusterStageFail(xapi.StageXlineService, errors.New("failed to create service"))

t.Run("Successful Status can covert to XlineClusterRecStatus properly", func(t *testing.T) {
res := sucStage.AsXlineClusterRecStatus()
assert.Equal(t, res.Stage, xapi.StageXlineService)
assert.Equal(t, res.StageStatus, xapi.StageResultSucceeded)
assert.True(t, res.LastMessage == "")
})

t.Run("Failed Status can covert to XlineClusterRecStatus properly", func(t *testing.T) {
res := failStage.AsXlineClusterRecStatus()
assert.Equal(t, res.Stage, xapi.StageXlineService)
assert.Equal(t, res.StageStatus, xapi.StageResultFailed)
assert.Equal(t, res.LastMessage, "failed to create service")
})

}
59 changes: 59 additions & 0 deletions internal/transformer/xlinecluster_resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package transformer

import (
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/assert"
xapi "github.com/xline-kv/xline-operator/api/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestXlineClusterFunc(t *testing.T) {
test_image := "xline-img"
test_image_version := "latest"
xlineCluster := xapi.XlineCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "xline",
Namespace: "default",
},
Spec: xapi.XlineClusterSpec{
Version: test_image_version,
Image: &test_image,
Replicas: 3,
},
}

t.Run("GetServiceKey should work properly", func(t *testing.T) {
xcLookupKey := xlineCluster.ObjKey()
svcObj := GetServiceKey(xcLookupKey)
assert.Equal(t, svcObj.Namespace, "default")
assert.Equal(t, svcObj.Name, "xline-svc")
})

t.Run("GetStatefulSetKey should work properly", func(t *testing.T) {
xcLookupKey := xlineCluster.ObjKey()
stsObj := GetStatefulSetKey(xcLookupKey)
assert.Equal(t, stsObj.Namespace, "default")
assert.Equal(t, stsObj.Name, "xline-sts")
})

t.Run("GetXlineImage should work properly", func(t *testing.T) {
xline_image := GetXlineImage(&xlineCluster)
assert.Equal(t, xline_image, "xline-img:latest")
})

t.Run("GetMemberTopology should work properly", func(t *testing.T) {
xcLookupKey := xlineCluster.ObjKey()
stsRef := GetStatefulSetKey(xcLookupKey)
svcName := GetServiceKey(xcLookupKey).Name
topology := GetMemberTopology(stsRef, svcName, 3)
topologyVec := strings.Split(topology, ",")
assert.Equal(t, len(topologyVec), 3)
for i := 0; i < 3; i++ {
expectRes := fmt.Sprintf("xline-sts-%d=xline-sts-%d.xline-svc.default.svc.cluster.local:2379", i, i)
assert.Equal(t, topologyVec[i], expectRes)
}
})
}
13 changes: 13 additions & 0 deletions internal/util/kubeutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package util

import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/types"
)

func TestK8sObjKeyStr(t *testing.T) {
objkey := types.NamespacedName{Name: "xline", Namespace: "default"}
assert.Equal(t, K8sObjKeyStr(objkey), "xline.default")
}

0 comments on commit 43886a1

Please sign in to comment.