github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/accounts/delete_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 accounts 21 22 import ( 23 "bytes" 24 "net/http" 25 26 . "github.com/onsi/ginkgo/v2" 27 . "github.com/onsi/gomega" 28 "k8s.io/apimachinery/pkg/runtime" 29 "k8s.io/apimachinery/pkg/runtime/schema" 30 "k8s.io/cli-runtime/pkg/genericiooptions" 31 "k8s.io/cli-runtime/pkg/resource" 32 "k8s.io/client-go/kubernetes/scheme" 33 clientfake "k8s.io/client-go/rest/fake" 34 cmdtesting "k8s.io/kubectl/pkg/cmd/testing" 35 36 "github.com/1aal/kubeblocks/pkg/cli/testing" 37 "github.com/1aal/kubeblocks/pkg/cli/types" 38 ) 39 40 var _ = Describe("Delete Account Options", func() { 41 const ( 42 namespace = "test" 43 clusterName = "apple" 44 ) 45 46 var ( 47 streams genericiooptions.IOStreams 48 in *bytes.Buffer 49 tf *cmdtesting.TestFactory 50 cluster = testing.FakeCluster(clusterName, namespace) 51 pods = testing.FakePods(3, namespace, clusterName) 52 ) 53 54 BeforeEach(func() { 55 streams, in, _, _ = genericiooptions.NewTestIOStreams() 56 tf = testing.NewTestFactory(namespace) 57 codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...) 58 httpResp := func(obj runtime.Object) *http.Response { 59 return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, obj)} 60 } 61 62 tf.UnstructuredClient = &clientfake.RESTClient{ 63 GroupVersion: schema.GroupVersion{Group: types.AppsAPIGroup, Version: types.AppsAPIVersion}, 64 NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer, 65 Client: clientfake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { 66 urlPrefix := "/api/v1/namespaces/" + namespace 67 mapping := map[string]*http.Response{ 68 urlPrefix + "/pods": httpResp(pods), 69 urlPrefix + "/pods/" + pods.Items[0].Name: httpResp(&pods.Items[0]), 70 } 71 return mapping[req.URL.Path], nil 72 }), 73 } 74 75 tf.Client = tf.UnstructuredClient 76 tf.FakeDynamicClient = testing.FakeDynamicClient(cluster, testing.FakeClusterDef(), testing.FakeClusterVersion()) 77 }) 78 79 AfterEach(func() { 80 tf.Cleanup() 81 }) 82 83 Context("new options", func() { 84 It("new option", func() { 85 o := NewDeleteUserOptions(tf, streams) 86 Expect(o).ShouldNot(BeNil()) 87 Expect(o.AccountBaseOptions).ShouldNot(BeNil()) 88 }) 89 90 It("validate user name and password", func() { 91 o := NewDeleteUserOptions(tf, streams) 92 Expect(o).ShouldNot(BeNil()) 93 args := []string{} 94 Expect(o.Validate(args)).Should(MatchError(errClusterNameorInstName)) 95 96 // add one element 97 By("add one more args, should fail") 98 args = []string{clusterName} 99 Expect(o.Validate(args)).Should(MatchError(errMissingUserName)) 100 101 // set user name 102 o.userName = "lilei" 103 _, _ = in.Write([]byte("hanmeimei\n")) 104 Expect(o.Validate(args)).Should(HaveOccurred()) 105 in.Reset() 106 _, _ = in.Write([]byte(o.userName + "\n")) 107 Expect(o.Validate(args)).Should(Succeed()) 108 }) 109 110 It("complete options", func() { 111 o := NewDeleteUserOptions(tf, streams) 112 Expect(o).ShouldNot(BeNil()) 113 114 o.PodName = pods.Items[0].Name 115 o.ClusterName = clusterName 116 o.userName = "lily" 117 118 Expect(o.Complete(tf)).Should(Succeed()) 119 }) 120 }) 121 })