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