You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by su...@apache.org on 2023/06/09 08:46:54 UTC

[shardingsphere-on-cloud] branch main updated: refactor(operator): update computenode test to ginkgo (#411)

This is an automated email from the ASF dual-hosted git repository.

sunnianjun pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/shardingsphere-on-cloud.git


The following commit(s) were added to refs/heads/main by this push:
     new af9b0aa  refactor(operator): update computenode test to ginkgo (#411)
af9b0aa is described below

commit af9b0aaae3824da6a5f5ae7ab8f04daeedf78cb5
Author: liyao <ma...@126.com>
AuthorDate: Fri Jun 9 16:46:50 2023 +0800

    refactor(operator): update computenode test to ginkgo (#411)
    
    * fix: update ComputeNodeCondition type
    
    Signed-off-by: mlycore <ma...@126.com>
    
    * refactor: update compute node reconcile test to ginkgo
    
    Signed-off-by: mlycore <ma...@126.com>
    
    * chore: update license
    
    Signed-off-by: mlycore <ma...@126.com>
    
    ---------
    
    Signed-off-by: mlycore <ma...@126.com>
---
 .../api/v1alpha1/compute_node_types.go             |   6 +-
 .../pkg/reconcile/computenode/compute_node.go      |   2 +-
 .../computenode/compute_node_suite_test.go         |  30 ++
 .../pkg/reconcile/computenode/compute_node_test.go | 308 ++++++++++++++++-----
 4 files changed, 269 insertions(+), 77 deletions(-)

diff --git a/shardingsphere-operator/api/v1alpha1/compute_node_types.go b/shardingsphere-operator/api/v1alpha1/compute_node_types.go
index 3b2c126..2f7f886 100644
--- a/shardingsphere-operator/api/v1alpha1/compute_node_types.go
+++ b/shardingsphere-operator/api/v1alpha1/compute_node_types.go
@@ -362,9 +362,9 @@ const (
 type ConditionStatus string
 
 const (
-	ConditionStatusTrue    = "True"
-	ConditionStatusFalse   = "False"
-	ConditionStatusUnknown = "Unknown"
+	ConditionStatusTrue    ConditionStatus = "True"
+	ConditionStatusFalse   ConditionStatus = "False"
+	ConditionStatusUnknown ConditionStatus = "Unknown"
 )
 
 // ComputeNodeCondition defines a condition template
diff --git a/shardingsphere-operator/pkg/reconcile/computenode/compute_node.go b/shardingsphere-operator/pkg/reconcile/computenode/compute_node.go
index 944b01c..719d408 100644
--- a/shardingsphere-operator/pkg/reconcile/computenode/compute_node.go
+++ b/shardingsphere-operator/pkg/reconcile/computenode/compute_node.go
@@ -84,7 +84,7 @@ func getPreferedConditionFromPod(pod *corev1.Pod) []v1alpha1.ComputeNodeConditio
 	}
 
 	podConditions := getPreferedConditionFromPodConditions(pod.Status.Conditions)
-	if pod.Status.Phase == corev1.PodPending {
+	if pod.Status.Phase == corev1.PodPending || pod.Status.Phase == corev1.PodRunning && len(podConditions) == 4 {
 		computenodeConditions = append(computenodeConditions, v1alpha1.ComputeNodeCondition{
 			Type: v1alpha1.ComputeNodeConditionPending,
 		})
diff --git a/shardingsphere-operator/pkg/reconcile/computenode/compute_node_suite_test.go b/shardingsphere-operator/pkg/reconcile/computenode/compute_node_suite_test.go
new file mode 100644
index 0000000..79d6869
--- /dev/null
+++ b/shardingsphere-operator/pkg/reconcile/computenode/compute_node_suite_test.go
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 computenode_test
+
+import (
+	"testing"
+
+	. "github.com/onsi/ginkgo/v2"
+	. "github.com/onsi/gomega"
+)
+
+func TestAws(t *testing.T) {
+	RegisterFailHandler(Fail)
+	RunSpecs(t, "ComputeNode Suite")
+}
diff --git a/shardingsphere-operator/pkg/reconcile/computenode/compute_node_test.go b/shardingsphere-operator/pkg/reconcile/computenode/compute_node_test.go
index d1fbf61..1335dfd 100644
--- a/shardingsphere-operator/pkg/reconcile/computenode/compute_node_test.go
+++ b/shardingsphere-operator/pkg/reconcile/computenode/compute_node_test.go
@@ -15,102 +15,264 @@
  * limitations under the License.
  */
 
-package computenode
+package computenode_test
 
 import (
-	"testing"
-
 	"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/api/v1alpha1"
-	"github.com/stretchr/testify/assert"
+	"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/reconcile/computenode"
+	. "github.com/onsi/ginkgo/v2"
+	. "github.com/onsi/gomega"
 	corev1 "k8s.io/api/core/v1"
 )
 
-func Test_getPreferedConditionFromPodConditions(t *testing.T) {
-	cases := []struct {
-		pcs     []corev1.PodCondition
-		expect  []v1alpha1.ComputeNodeCondition
-		message string
-	}{
-		{
-			pcs: []corev1.PodCondition{
-				corev1.PodCondition{
-					Type:   corev1.PodScheduled,
-					Status: corev1.ConditionTrue,
+var _ = Describe("GetConditionFromPods", func() {
+	Context("Empty Pod list", func() {
+		podlist := &corev1.PodList{Items: []corev1.Pod{}}
+		conditions := computenode.GetConditionFromPods(podlist)
+		It("should be one unknown condition", func() {
+			Expect(len(conditions)).To(Equal(1))
+		})
+
+		It("unknown condition should be true", func() {
+			Expect(conditions[0].Type).To(Equal(v1alpha1.ComputeNodeConditionUnknown))
+			Expect(conditions[0].Status).To(Equal(v1alpha1.ConditionStatusTrue))
+		})
+	})
+
+	Context("One Pod Pending", func() {
+		podlist := &corev1.PodList{Items: []corev1.Pod{
+			{
+				Status: corev1.PodStatus{
+					Phase: corev1.PodPending,
 				},
 			},
-			expect: []v1alpha1.ComputeNodeCondition{
-				{
-					Type: v1alpha1.ComputeNodeConditionDeployed,
+		}}
+		conditions := computenode.GetConditionFromPods(podlist)
+
+		It("should be one pending condition", func() {
+			Expect(len(conditions)).To(Equal(1))
+		})
+
+		It("unknown condition should be true", func() {
+			Expect(conditions[0].Type).To(Equal(v1alpha1.ComputeNodeConditionPending))
+			Expect(conditions[0].Status).To(Equal(v1alpha1.ConditionStatusTrue))
+		})
+	})
+
+	Context("One Pod Scheduled", func() {
+		podlist := &corev1.PodList{Items: []corev1.Pod{
+			{
+				Status: corev1.PodStatus{
+					Phase: corev1.PodPending,
+					Conditions: []corev1.PodCondition{
+						{
+							Type:   corev1.PodReady,
+							Status: corev1.ConditionFalse,
+						},
+						{
+							Type:   corev1.ContainersReady,
+							Status: corev1.ConditionFalse,
+						},
+						{
+							Type:   corev1.PodInitialized,
+							Status: corev1.ConditionFalse,
+						},
+						{
+							Type:   corev1.PodScheduled,
+							Status: corev1.ConditionTrue,
+						},
+					},
 				},
 			},
-			message: "scheduled pod condition should get deployed condition",
-		},
-	}
-	for _, c := range cases {
-		act := getPreferedConditionFromPodConditions(c.pcs)
-		assert.Equal(t, len(c.expect), len(act), c.message)
-		for i := range act {
-			assert.Equal(t, c.expect[i].Type, act[i].Type, c.message)
-		}
-	}
-}
+		}}
+		conditions := computenode.GetConditionFromPods(podlist)
+
+		It("should be two conditions", func() {
+			Expect(len(conditions)).To(Equal(2))
+		})
+
+		It("condition status should be correct", func() {
+			Expect(containConditionType(conditions, v1alpha1.ComputeNodeConditionPending, v1alpha1.ComputeNodeConditionDeployed)).To(BeTrue())
+			for _, cond := range conditions {
+				if cond.Type == v1alpha1.ComputeNodeConditionPending {
+					Expect(cond.Status).To(Equal(v1alpha1.ConditionStatusTrue))
+				}
+				if cond.Type == v1alpha1.ComputeNodeConditionDeployed {
+					Expect(cond.Status).To(Equal(v1alpha1.ConditionStatusTrue))
+				}
+			}
+		})
+	})
 
-func Test_getPreferedConditionFromPod(t *testing.T) {
-	cases := []struct {
-		pod     corev1.Pod
-		expect  []v1alpha1.ComputeNodeCondition
-		message string
-	}{
-		{
-			pod: corev1.Pod{
+	Context("One Pod Initialized", func() {
+		podlist := &corev1.PodList{Items: []corev1.Pod{
+			{
 				Status: corev1.PodStatus{
 					Phase: corev1.PodPending,
+					Conditions: []corev1.PodCondition{
+						{
+							Type:   corev1.PodReady,
+							Status: corev1.ConditionFalse,
+						},
+						{
+							Type:   corev1.ContainersReady,
+							Status: corev1.ConditionFalse,
+						},
+						{
+							Type:   corev1.PodInitialized,
+							Status: corev1.ConditionTrue,
+						},
+						{
+							Type:   corev1.PodScheduled,
+							Status: corev1.ConditionTrue,
+						},
+					},
 				},
 			},
-			expect: []v1alpha1.ComputeNodeCondition{
-				{
-					Type:   v1alpha1.ComputeNodeConditionPending,
-					Status: v1alpha1.ConditionStatusTrue,
+		}}
+		conditions := computenode.GetConditionFromPods(podlist)
+
+		It("should be three conditions", func() {
+			Expect(len(conditions)).To(Equal(3))
+		})
+
+		It("condition status should be correct", func() {
+			Expect(containConditionType(conditions, v1alpha1.ComputeNodeConditionPending, v1alpha1.ComputeNodeConditionDeployed, v1alpha1.ComputeNodeConditionInitialized)).To(BeTrue())
+			for _, cond := range conditions {
+				if cond.Type == v1alpha1.ComputeNodeConditionPending {
+					Expect(cond.Status).To(Equal(v1alpha1.ConditionStatusTrue))
+				}
+				if cond.Type == v1alpha1.ComputeNodeConditionDeployed {
+					Expect(cond.Status).To(Equal(v1alpha1.ConditionStatusTrue))
+				}
+				if cond.Type == v1alpha1.ComputeNodeConditionInitialized {
+					Expect(cond.Status).To(Equal(v1alpha1.ConditionStatusTrue))
+				}
+			}
+		})
+	})
+
+	Context("One Pod Ready", func() {
+		podlist := &corev1.PodList{Items: []corev1.Pod{
+			{
+				Status: corev1.PodStatus{
+					Phase: corev1.PodPending,
+					Conditions: []corev1.PodCondition{
+						{
+							Type:   corev1.PodReady,
+							Status: corev1.ConditionTrue,
+						},
+						{
+							Type:   corev1.ContainersReady,
+							Status: corev1.ConditionTrue,
+						},
+						{
+							Type:   corev1.PodInitialized,
+							Status: corev1.ConditionTrue,
+						},
+						{
+							Type:   corev1.PodScheduled,
+							Status: corev1.ConditionTrue,
+						},
+					},
 				},
 			},
-			message: "pending pod should get pending condition",
-		},
-	}
+		}}
+		conditions := computenode.GetConditionFromPods(podlist)
+		It("should be five conditions", func() {
+			Expect(len(conditions)).To(Equal(5))
+		})
 
-	for _, c := range cases {
-		act := getPreferedConditionFromPod(&c.pod)
-		assert.Equal(t, len(c.expect), len(act), c.message)
-		for i := range act {
-			assert.Equal(t, c.expect[i].Type, act[i].Type, c.message)
-		}
-	}
-}
+		It("condition status should be correct", func() {
+			Expect(containConditionType(conditions, v1alpha1.ComputeNodeConditionPending, v1alpha1.ComputeNodeConditionDeployed, v1alpha1.ComputeNodeConditionInitialized, v1alpha1.ComputeNodeConditionStarted, v1alpha1.ComputeNodeConditionReady)).To(BeTrue())
+			for _, cond := range conditions {
+				if cond.Type == v1alpha1.ComputeNodeConditionPending {
+					Expect(cond.Status).To(Equal(v1alpha1.ConditionStatusTrue))
+				}
+				if cond.Type == v1alpha1.ComputeNodeConditionDeployed {
+					Expect(cond.Status).To(Equal(v1alpha1.ConditionStatusTrue))
+				}
+				if cond.Type == v1alpha1.ComputeNodeConditionInitialized {
+					Expect(cond.Status).To(Equal(v1alpha1.ConditionStatusTrue))
+				}
+				if cond.Type == v1alpha1.ComputeNodeConditionStarted {
+					Expect(cond.Status).To(Equal(v1alpha1.ConditionStatusTrue))
+				}
+				if cond.Type == v1alpha1.ComputeNodeConditionReady {
+					Expect(cond.Status).To(Equal(v1alpha1.ConditionStatusTrue))
+				}
+			}
+		})
+	})
 
-func Test_GetConditionFromPods(t *testing.T) {
-	cases := []struct {
-		podlist *corev1.PodList
-		expect  []v1alpha1.ComputeNodeCondition
-		message string
-	}{
-		{
-			podlist: &corev1.PodList{Items: []corev1.Pod{}},
-			expect: []v1alpha1.ComputeNodeCondition{
-				{
-					Type:   v1alpha1.ComputeNodeConditionUnknown,
-					Status: v1alpha1.ConditionStatusTrue,
+	Context("One Pod Running", func() {
+		podlist := &corev1.PodList{Items: []corev1.Pod{
+			{
+				Status: corev1.PodStatus{
+					Phase: corev1.PodRunning,
+					Conditions: []corev1.PodCondition{
+						{
+							Type:   corev1.PodReady,
+							Status: corev1.ConditionTrue,
+						},
+						{
+							Type:   corev1.ContainersReady,
+							Status: corev1.ConditionTrue,
+						},
+						{
+							Type:   corev1.PodInitialized,
+							Status: corev1.ConditionTrue,
+						},
+						{
+							Type:   corev1.PodScheduled,
+							Status: corev1.ConditionTrue,
+						},
+					},
 				},
 			},
-			message: "empty podlist should get unknown condition",
-		},
+		}}
+		conditions := computenode.GetConditionFromPods(podlist)
+		It("should be five conditions", func() {
+			Expect(len(conditions)).To(Equal(5))
+		})
+
+		It("condition status should be correct", func() {
+			Expect(containConditionType(conditions, v1alpha1.ComputeNodeConditionPending, v1alpha1.ComputeNodeConditionDeployed, v1alpha1.ComputeNodeConditionInitialized, v1alpha1.ComputeNodeConditionStarted, v1alpha1.ComputeNodeConditionReady)).To(BeTrue())
+			for _, cond := range conditions {
+				if cond.Type == v1alpha1.ComputeNodeConditionPending {
+					Expect(cond.Status).To(Equal(v1alpha1.ConditionStatusTrue))
+				}
+				if cond.Type == v1alpha1.ComputeNodeConditionDeployed {
+					Expect(cond.Status).To(Equal(v1alpha1.ConditionStatusTrue))
+				}
+				if cond.Type == v1alpha1.ComputeNodeConditionInitialized {
+					Expect(cond.Status).To(Equal(v1alpha1.ConditionStatusTrue))
+				}
+				if cond.Type == v1alpha1.ComputeNodeConditionStarted {
+					Expect(cond.Status).To(Equal(v1alpha1.ConditionStatusTrue))
+				}
+				if cond.Type == v1alpha1.ComputeNodeConditionReady {
+					Expect(cond.Status).To(Equal(v1alpha1.ConditionStatusTrue))
+				}
+			}
+		})
+	})
+})
+
+func containConditionType(conds []v1alpha1.ComputeNodeCondition, ts ...v1alpha1.ComputeNodeConditionType) bool {
+	if len(conds) != len(ts) {
+		return false
 	}
 
-	for _, c := range cases {
-		act := GetConditionFromPods(c.podlist)
-		assert.Equal(t, len(c.expect), len(act), c.message)
-		for i := range act {
-			assert.Equal(t, c.expect[i].Type, act[i].Type, c.message)
-			assert.Equal(t, c.expect[i].Status, act[i].Status, c.message)
-		}
+	contains := map[v1alpha1.ComputeNodeConditionType]bool{}
+	for _, t := range ts {
+		contains[t] = true
 	}
 
+	for _, c := range conds {
+		if !contains[c.Type] {
+			return false
+		}
+	}
+	return true
 }