github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cluster/cluster_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 cluster 21 22 import ( 23 . "github.com/onsi/ginkgo/v2" 24 . "github.com/onsi/gomega" 25 26 "k8s.io/apimachinery/pkg/runtime" 27 "k8s.io/client-go/kubernetes" 28 29 "github.com/1aal/kubeblocks/pkg/cli/testing" 30 dptypes "github.com/1aal/kubeblocks/pkg/dataprotection/types" 31 ) 32 33 var _ = Describe("cluster util", func() { 34 baseObjs := []runtime.Object{ 35 testing.FakePods(3, testing.Namespace, testing.ClusterName), 36 testing.FakeSecrets(testing.Namespace, testing.ClusterName), 37 testing.FakeServices(), 38 testing.FakePVCs(), 39 } 40 41 baseObjsWithBackupPods := func() []runtime.Object { 42 podsWithBackup := testing.FakePods(4, testing.Namespace, testing.ClusterName) 43 labels := podsWithBackup.Items[0].GetLabels() 44 labels[dptypes.BackupNameLabelKey] = testing.BackupName 45 podsWithBackup.Items[0].SetLabels(labels) 46 return []runtime.Object{ 47 podsWithBackup, 48 testing.FakeSecrets(testing.Namespace, testing.ClusterName), 49 testing.FakeServices(), 50 testing.FakePVCs(), 51 } 52 } 53 cluster := testing.FakeCluster(testing.ClusterName, testing.Namespace) 54 dynamic := testing.FakeDynamicClient( 55 cluster, 56 testing.FakeClusterDef(), 57 testing.FakeBackupPolicy("backupPolicy-test", testing.ClusterName), 58 testing.FakeBackupWithCluster(cluster, "backup-test"), 59 testing.FakeClusterVersion()) 60 61 getOptions := GetOptions{ 62 WithClusterDef: true, 63 WithClusterVersion: true, 64 WithConfigMap: true, 65 WithService: true, 66 WithSecret: true, 67 WithPVC: true, 68 WithPod: true, 69 WithDataProtection: true, 70 } 71 72 It("get cluster objects", func() { 73 var ( 74 err error 75 objs *ClusterObjects 76 ) 77 78 testFn := func(client kubernetes.Interface) { 79 clusterName := testing.ClusterName 80 getter := ObjectsGetter{ 81 Client: client, 82 Dynamic: dynamic, 83 Name: clusterName, 84 Namespace: testing.Namespace, 85 GetOptions: getOptions, 86 } 87 88 objs, err = getter.Get() 89 Expect(err).Should(Succeed()) 90 Expect(objs).ShouldNot(BeNil()) 91 Expect(objs.Cluster.Name).Should(Equal(clusterName)) 92 Expect(objs.ClusterDef.Name).Should(Equal(testing.ClusterDefName)) 93 Expect(objs.ClusterVersion.Name).Should(Equal(testing.ClusterVersionName)) 94 Expect(len(objs.Pods.Items)).Should(Equal(3)) 95 Expect(len(objs.Secrets.Items)).Should(Equal(1)) 96 Expect(len(objs.Services.Items)).Should(Equal(4)) 97 Expect(len(objs.PVCs.Items)).Should(Equal(1)) 98 Expect(len(objs.GetComponentInfo())).Should(Equal(1)) 99 } 100 101 By("when node is not found") 102 testFn(testing.FakeClientSet(baseObjs...)) 103 Expect(len(objs.Nodes)).Should(Equal(0)) 104 105 By("when node is available") 106 baseObjs = append(baseObjs, testing.FakeNode()) 107 testFn(testing.FakeClientSet(baseObjs...)) 108 Expect(len(objs.Nodes)).Should(Equal(1)) 109 110 By("when pod is back-up created") 111 testFn(testing.FakeClientSet(baseObjsWithBackupPods()...)) 112 }) 113 })