github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/common/stateful_set_utils_test.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package common 21 22 import ( 23 "testing" 24 25 apps "k8s.io/api/apps/v1" 26 27 intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil" 28 testk8s "github.com/1aal/kubeblocks/pkg/testutil/k8s" 29 ) 30 31 func TestGetParentNameAndOrdinal(t *testing.T) { 32 set := testk8s.NewFakeStatefulSet("foo", 3) 33 pod := testk8s.NewFakeStatefulSetPod(set, 1) 34 if parent, ordinal := intctrlutil.GetParentNameAndOrdinal(pod); parent != set.Name { 35 t.Errorf("Extracted the wrong parent name expected %s found %s", set.Name, parent) 36 } else if ordinal != 1 { 37 t.Errorf("Extracted the wrong ordinal expected %d found %d", 1, ordinal) 38 } 39 pod.Name = "1-bar" 40 if parent, ordinal := intctrlutil.GetParentNameAndOrdinal(pod); parent != "" { 41 t.Error("Expected empty string for non-member Pod parent") 42 } else if ordinal != -1 { 43 t.Error("Expected -1 for non member Pod ordinal") 44 } 45 } 46 47 func TestIsMemberOf(t *testing.T) { 48 set := testk8s.NewFakeStatefulSet("foo", 3) 49 set2 := testk8s.NewFakeStatefulSet("bar", 3) 50 set2.Name = "foo2" 51 pod := testk8s.NewFakeStatefulSetPod(set, 1) 52 if !IsMemberOf(set, pod) { 53 t.Error("isMemberOf returned false negative") 54 } 55 if IsMemberOf(set2, pod) { 56 t.Error("isMemberOf returned false positive") 57 } 58 } 59 60 func TestStatefulSetPodsAreReady(t *testing.T) { 61 sts := testk8s.NewFakeStatefulSet("test", 3) 62 testk8s.MockStatefulSetReady(sts) 63 ready := statefulSetPodsAreReady(sts, *sts.Spec.Replicas) 64 if !ready { 65 t.Errorf("StatefulSet pods should be ready") 66 } 67 convertSts := convertToStatefulSet(sts) 68 if convertSts == nil { 69 t.Errorf("Convert to statefulSet should be succeed") 70 } 71 convertSts = convertToStatefulSet(&apps.Deployment{}) 72 if convertSts != nil { 73 t.Errorf("Convert to statefulSet should be failed") 74 } 75 convertSts = convertToStatefulSet(nil) 76 if convertSts != nil { 77 t.Errorf("Convert to statefulSet should be failed") 78 } 79 } 80 81 func TestSStatefulSetOfComponentIsReady(t *testing.T) { 82 sts := testk8s.NewFakeStatefulSet("test", 3) 83 testk8s.MockStatefulSetReady(sts) 84 ready := statefulSetOfComponentIsReady(sts, true, nil) 85 if !ready { 86 t.Errorf("StatefulSet should be ready") 87 } 88 ready = statefulSetOfComponentIsReady(sts, false, nil) 89 if ready { 90 t.Errorf("StatefulSet should not be ready") 91 } 92 }