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 2022/10/06 08:33:19 UTC

[shardingsphere-on-cloud] branch main updated: chore(reconcile): add unit test for IsRunning and CountReadyPods (#69)

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 f0a3527  chore(reconcile): add unit test for IsRunning and CountReadyPods (#69)
f0a3527 is described below

commit f0a3527a1aa42c12122788a5cba12917c0678cc8
Author: liyao <ma...@126.com>
AuthorDate: Thu Oct 6 16:33:14 2022 +0800

    chore(reconcile): add unit test for IsRunning and CountReadyPods (#69)
    
    * chore(test): update go module for test
    
    Signed-off-by: mlycore <ma...@126.com>
    
    * chore(test): add unit test for IsRunning
    
    Signed-off-by: mlycore <ma...@126.com>
    
    * chore(test): add unit test for CountingReadyPods
    
    Signed-off-by: mlycore <ma...@126.com>
    
    * fix(reconcile): fix counting ready Pods
    
    Signed-off-by: mlycore <ma...@126.com>
    
    * fix license header
    
    Signed-off-by: mlycore <ma...@126.com>
    
    Signed-off-by: mlycore <ma...@126.com>
---
 shardingsphere-operator/go.mod                     |   2 +
 .../pkg/reconcile/reconcile_test.go                | 269 +++++++++++++++++++++
 shardingsphere-operator/pkg/reconcile/status.go    |   5 +-
 3 files changed, 273 insertions(+), 3 deletions(-)

diff --git a/shardingsphere-operator/go.mod b/shardingsphere-operator/go.mod
index 9ec0443..72c941c 100644
--- a/shardingsphere-operator/go.mod
+++ b/shardingsphere-operator/go.mod
@@ -4,6 +4,7 @@ go 1.18
 
 require (
 	github.com/pkg/errors v0.9.1
+	github.com/stretchr/testify v1.7.0
 	go.uber.org/zap v1.22.0
 	gopkg.in/yaml.v2 v2.4.0
 	k8s.io/api v0.24.4
@@ -52,6 +53,7 @@ require (
 	github.com/nxadm/tail v1.4.8 // indirect
 	github.com/onsi/ginkgo v1.16.5 // indirect
 	github.com/onsi/gomega v1.20.0 // indirect
+	github.com/pmezard/go-difflib v1.0.0 // indirect
 	github.com/prometheus/client_golang v1.12.1 // indirect
 	github.com/prometheus/client_model v0.2.0 // indirect
 	github.com/prometheus/common v0.32.1 // indirect
diff --git a/shardingsphere-operator/pkg/reconcile/reconcile_test.go b/shardingsphere-operator/pkg/reconcile/reconcile_test.go
new file mode 100644
index 0000000..55b5e35
--- /dev/null
+++ b/shardingsphere-operator/pkg/reconcile/reconcile_test.go
@@ -0,0 +1,269 @@
+/*
+ * 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 reconcile
+
+import (
+	"github.com/stretchr/testify/assert"
+	"k8s.io/api/core/v1"
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+	"testing"
+)
+
+func Test_IsRunning(t *testing.T) {
+	ts := metav1.Time{}
+	cases := []struct {
+		podlist *v1.PodList
+		exp     bool
+		message string
+	}{
+		{
+			podlist: &v1.PodList{
+				Items: []v1.Pod{},
+			},
+			exp:     false,
+			message: "Empty PodList should be false",
+		},
+		{
+			podlist: &v1.PodList{
+				Items: []v1.Pod{
+					{
+						Status: v1.PodStatus{
+							Phase: v1.PodRunning,
+						},
+					},
+					{
+						Status: v1.PodStatus{
+							Phase: v1.PodFailed,
+						},
+					},
+				},
+			},
+			// At least one Pod is Running considered the Cluster be availabe
+			exp:     true,
+			message: "First Pod is Running and second Pod is not Running should be false",
+		},
+		{
+			podlist: &v1.PodList{
+				Items: []v1.Pod{
+					{
+						Status: v1.PodStatus{
+							Phase: v1.PodFailed,
+						},
+					},
+					{
+						Status: v1.PodStatus{
+							Phase: v1.PodRunning,
+						},
+					},
+				},
+			},
+			// At least one Pod is Running considered the Cluster be availabe
+			exp:     true,
+			message: "First Pod is not Running and second Pod is Running should be false",
+		},
+		{
+			podlist: &v1.PodList{
+				Items: []v1.Pod{
+					{
+						Status: v1.PodStatus{
+							Phase: v1.PodRunning,
+						},
+					},
+					{
+						Status: v1.PodStatus{
+							Phase: v1.PodRunning,
+						},
+					},
+				},
+			},
+			exp:     true,
+			message: "All Pods are running should be true",
+		},
+		{
+			podlist: &v1.PodList{
+				Items: []v1.Pod{
+					{
+						Status: v1.PodStatus{
+							Phase: v1.PodFailed,
+						},
+					},
+					{
+						Status: v1.PodStatus{
+							Phase: v1.PodFailed,
+						},
+					},
+				},
+			},
+			exp:     false,
+			message: "All Pods are not running should be false",
+		},
+		{
+			podlist: &v1.PodList{
+				Items: []v1.Pod{
+					{
+						ObjectMeta: metav1.ObjectMeta{
+							DeletionTimestamp: &ts,
+						},
+						Status: v1.PodStatus{
+							Phase: v1.PodRunning,
+						},
+					},
+				},
+			},
+			exp:     false,
+			message: "Pod is running with deletion timestamp should be false",
+		},
+	}
+
+	for _, c := range cases {
+		act := IsRunning(c.podlist)
+		if len(c.podlist.Items) != 0 {
+			assert.Equal(t, c.exp, act, c.message)
+		}
+	}
+}
+
+func Test_CountingReadyPods(t *testing.T) {
+	ts := metav1.Time{}
+	cases := []struct {
+		podlist *v1.PodList
+		exp     int32
+		message string
+	}{
+		{
+			podlist: &v1.PodList{
+				Items: []v1.Pod{},
+			},
+			exp:     0,
+			message: "Empty PodList should be 0",
+		},
+		{
+			podlist: &v1.PodList{
+				Items: []v1.Pod{
+					{
+						Status: v1.PodStatus{
+							ContainerStatuses: []v1.ContainerStatus{},
+						},
+					},
+				},
+			},
+			exp:     0,
+			message: "Only one Pod without any container statuses should be 0",
+		},
+		{
+			podlist: &v1.PodList{
+				Items: []v1.Pod{
+					{
+						Status: v1.PodStatus{
+							ContainerStatuses: []v1.ContainerStatus{
+								{
+									Ready: true,
+								},
+							},
+						},
+					},
+				},
+			},
+			exp:     1,
+			message: "Only one Pod is running should be 1",
+		},
+		{
+			podlist: &v1.PodList{
+				Items: []v1.Pod{
+					{
+						ObjectMeta: metav1.ObjectMeta{
+							DeletionTimestamp: &ts,
+						},
+						Status: v1.PodStatus{
+							ContainerStatuses: []v1.ContainerStatus{
+								{
+									Ready: true,
+								},
+							},
+						},
+					},
+				},
+			},
+			exp:     0,
+			message: "Pod has ready container but with deletion timestamp should be 0",
+		},
+		{
+			podlist: &v1.PodList{
+				Items: []v1.Pod{
+					{
+						ObjectMeta: metav1.ObjectMeta{
+							DeletionTimestamp: &ts,
+						},
+						Status: v1.PodStatus{
+							ContainerStatuses: []v1.ContainerStatus{
+								{
+									Ready: true,
+								},
+							},
+						},
+					},
+					{
+						Status: v1.PodStatus{
+							ContainerStatuses: []v1.ContainerStatus{
+								{
+									Ready: true,
+								},
+							},
+						},
+					},
+				},
+			},
+			exp:     1,
+			message: "First Pod has ready container, second Pod has ready container but with deletion timestamp should be 0",
+		},
+		{
+			podlist: &v1.PodList{
+				Items: []v1.Pod{
+					{
+						Status: v1.PodStatus{
+							ContainerStatuses: []v1.ContainerStatus{
+								{
+									Ready: true,
+								},
+							},
+						},
+					},
+					{
+						ObjectMeta: metav1.ObjectMeta{
+							DeletionTimestamp: &ts,
+						},
+						Status: v1.PodStatus{
+							ContainerStatuses: []v1.ContainerStatus{
+								{
+									Ready: true,
+								},
+							},
+						},
+					},
+				},
+			},
+			exp:     1,
+			message: "First Pod has ready container but with deletion timestamp, second Pod has ready container should be 0",
+		},
+	}
+
+	for _, c := range cases {
+		act := CountingReadyPods(c.podlist)
+		assert.Equal(t, c.exp, act, c.message)
+	}
+}
diff --git a/shardingsphere-operator/pkg/reconcile/status.go b/shardingsphere-operator/pkg/reconcile/status.go
index bb528e9..ef05dcc 100644
--- a/shardingsphere-operator/pkg/reconcile/status.go
+++ b/shardingsphere-operator/pkg/reconcile/status.go
@@ -33,11 +33,10 @@ func IsRunning(podList *v1.PodList) bool {
 }
 
 func CountingReadyPods(podList *v1.PodList) int32 {
-	var readyPods int32
-	readyPods = 0
+	var readyPods int32 = 0
 	for _, pod := range podList.Items {
 		if len(pod.Status.ContainerStatuses) == 0 {
-			return readyPods
+			continue
 		}
 		if pod.Status.ContainerStatuses[0].Ready && pod.ObjectMeta.DeletionTimestamp == nil {
 			readyPods++