github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/cluster/describe_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 "bytes" 24 "net/http" 25 "strings" 26 27 . "github.com/onsi/ginkgo/v2" 28 . "github.com/onsi/gomega" 29 corev1 "k8s.io/api/core/v1" 30 "k8s.io/apimachinery/pkg/runtime" 31 "k8s.io/apimachinery/pkg/runtime/schema" 32 "k8s.io/cli-runtime/pkg/genericiooptions" 33 "k8s.io/cli-runtime/pkg/resource" 34 "k8s.io/client-go/kubernetes/scheme" 35 clientfake "k8s.io/client-go/rest/fake" 36 cmdtesting "k8s.io/kubectl/pkg/cmd/testing" 37 38 dpv1alpha1 "github.com/1aal/kubeblocks/apis/dataprotection/v1alpha1" 39 "github.com/1aal/kubeblocks/pkg/cli/testing" 40 "github.com/1aal/kubeblocks/pkg/cli/types" 41 ) 42 43 var _ = Describe("Expose", func() { 44 const ( 45 namespace = "test" 46 clusterName = "test" 47 ) 48 49 var ( 50 streams genericiooptions.IOStreams 51 tf *cmdtesting.TestFactory 52 cluster = testing.FakeCluster(clusterName, namespace) 53 pods = testing.FakePods(3, namespace, clusterName) 54 ) 55 BeforeEach(func() { 56 streams, _, _, _ = genericiooptions.NewTestIOStreams() 57 tf = testing.NewTestFactory(namespace) 58 codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...) 59 httpResp := func(obj runtime.Object) *http.Response { 60 return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, obj)} 61 } 62 63 tf.UnstructuredClient = &clientfake.RESTClient{ 64 GroupVersion: schema.GroupVersion{Group: types.AppsAPIGroup, Version: types.AppsAPIVersion}, 65 NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer, 66 Client: clientfake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { 67 urlPrefix := "/api/v1/namespaces/" + namespace 68 mapping := map[string]*http.Response{ 69 "/api/v1/nodes/" + testing.NodeName: httpResp(testing.FakeNode()), 70 urlPrefix + "/services": httpResp(&corev1.ServiceList{}), 71 urlPrefix + "/events": httpResp(&corev1.EventList{}), 72 urlPrefix + "/persistentvolumeclaims": httpResp(&corev1.PersistentVolumeClaimList{}), 73 urlPrefix + "/pods": httpResp(pods), 74 } 75 return mapping[req.URL.Path], nil 76 }), 77 } 78 79 tf.Client = tf.UnstructuredClient 80 tf.FakeDynamicClient = testing.FakeDynamicClient(cluster, testing.FakeClusterDef(), testing.FakeClusterVersion()) 81 }) 82 83 AfterEach(func() { 84 tf.Cleanup() 85 }) 86 87 It("describe", func() { 88 cmd := NewDescribeCmd(tf, streams) 89 Expect(cmd).ShouldNot(BeNil()) 90 }) 91 92 It("complete", func() { 93 o := newOptions(tf, streams) 94 Expect(o.complete(nil)).Should(HaveOccurred()) 95 Expect(o.complete([]string{clusterName})).Should(Succeed()) 96 Expect(o.names).Should(Equal([]string{clusterName})) 97 Expect(o.client).ShouldNot(BeNil()) 98 Expect(o.dynamic).ShouldNot(BeNil()) 99 Expect(o.namespace).Should(Equal(namespace)) 100 }) 101 102 It("run", func() { 103 o := newOptions(tf, streams) 104 Expect(o.complete([]string{clusterName})).Should(Succeed()) 105 Expect(o.run()).Should(Succeed()) 106 }) 107 108 It("showEvents", func() { 109 out := &bytes.Buffer{} 110 showEvents("test-cluster", namespace, out) 111 Expect(out.String()).ShouldNot(BeEmpty()) 112 }) 113 114 It("showDataProtections", func() { 115 out := &bytes.Buffer{} 116 fakeBackupPolicies := []dpv1alpha1.BackupPolicy{ 117 *testing.FakeBackupPolicy("backup-policy-test", "test-cluster"), 118 } 119 fakeBackupSchedules := []dpv1alpha1.BackupSchedule{ 120 *testing.FakeBackupSchedule("backup-schedule-test", "backup-policy-test"), 121 } 122 123 showDataProtection(fakeBackupPolicies, fakeBackupSchedules, "test-repository", out) 124 strs := strings.Split(out.String(), "\n") 125 Expect(strs).ShouldNot(BeEmpty()) 126 }) 127 })