github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/class/list_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 class 21 22 import ( 23 "bytes" 24 25 . "github.com/onsi/ginkgo/v2" 26 . "github.com/onsi/gomega" 27 "k8s.io/apimachinery/pkg/api/resource" 28 "k8s.io/cli-runtime/pkg/genericiooptions" 29 "k8s.io/client-go/kubernetes/scheme" 30 cmdtesting "k8s.io/kubectl/pkg/cmd/testing" 31 32 appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 33 "github.com/1aal/kubeblocks/pkg/cli/testing" 34 ) 35 36 var _ = Describe("list", func() { 37 var ( 38 out *bytes.Buffer 39 tf *cmdtesting.TestFactory 40 streams genericiooptions.IOStreams 41 ) 42 43 BeforeEach(func() { 44 streams, _, out, _ = genericiooptions.NewTestIOStreams() 45 tf = testing.NewTestFactory(namespace) 46 _ = appsv1alpha1.AddToScheme(scheme.Scheme) 47 tf.FakeDynamicClient = testing.FakeDynamicClient(&classDef) 48 }) 49 50 AfterEach(func() { 51 tf.Cleanup() 52 }) 53 54 It("should succeed", func() { 55 cmd := NewListCommand(tf, streams) 56 Expect(cmd).ShouldNot(BeNil()) 57 _ = cmd.Flags().Set("cluster-definition", "apecloud-mysql") 58 cmd.Run(cmd, []string{}) 59 Expect(out.String()).To(ContainSubstring("general-1c1g")) 60 Expect(out.String()).To(ContainSubstring("mysql")) 61 }) 62 63 It("memory should be normalized", func() { 64 cases := []struct { 65 memory string 66 normalized string 67 }{ 68 { 69 memory: "0.2Gi", 70 normalized: "0.2Gi", 71 }, 72 { 73 memory: "0.2Mi", 74 normalized: "0.2Mi", 75 }, 76 { 77 memory: "0.2Ki", 78 normalized: "0.2Ki", 79 }, 80 { 81 memory: "1024Mi", 82 normalized: "1Gi", 83 }, 84 { 85 memory: "1025Mi", 86 normalized: "1025Mi", 87 }, 88 { 89 memory: "1023Mi", 90 normalized: "1023Mi", 91 }, 92 { 93 memory: "1Gi", 94 normalized: "1Gi", 95 }, 96 { 97 memory: "512Mi", 98 normalized: "512Mi", 99 }, 100 } 101 for _, item := range cases { 102 Expect(normalizeMemory(resource.MustParse(item.memory))).Should(Equal(item.normalized)) 103 } 104 }) 105 })