github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/preflight/analyzer/kb_storage_class_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 analyzer 21 22 import ( 23 "encoding/json" 24 "errors" 25 26 . "github.com/onsi/ginkgo/v2" 27 . "github.com/onsi/gomega" 28 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 29 "k8s.io/kubectl/pkg/util/storage" 30 31 troubleshoot "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" 32 storagev1beta1 "k8s.io/api/storage/v1beta1" 33 34 preflightv1beta2 "github.com/1aal/kubeblocks/externalapis/preflight/v1beta2" 35 ) 36 37 var ( 38 clusterResources = storagev1beta1.StorageClassList{ 39 Items: []storagev1beta1.StorageClass{ 40 { 41 Provisioner: "ebs.csi.aws.com", 42 Parameters: map[string]string{"type": "gp3"}, 43 }, 44 }, 45 } 46 clusterResources2 = storagev1beta1.StorageClassList{ 47 Items: []storagev1beta1.StorageClass{ 48 { 49 Provisioner: "ebs.csi.aws.com", 50 Parameters: map[string]string{"type": "gp3"}, 51 ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{storage.IsDefaultStorageClassAnnotation: "true"}}, 52 }, 53 }, 54 } 55 ) 56 57 var _ = Describe("kb_storage_class_test", func() { 58 var ( 59 analyzer AnalyzeStorageClassByKb 60 ) 61 Context("analyze storage class test", func() { 62 BeforeEach(func() { 63 analyzer = AnalyzeStorageClassByKb{ 64 analyzer: &preflightv1beta2.KBStorageClassAnalyze{ 65 Provisioner: "ebs.csi.aws.com", 66 StorageClassType: "gp3", 67 Outcomes: []*troubleshoot.Outcome{ 68 { 69 Pass: &troubleshoot.SingleOutcome{ 70 71 Message: "analyze storage class success", 72 }, 73 Fail: &troubleshoot.SingleOutcome{ 74 Message: "analyze storage class fail", 75 }, 76 }, 77 }}} 78 }) 79 It("Analyze test, and get file failed", func() { 80 Eventually(func(g Gomega) { 81 getCollectedFileContents := func(filename string) ([]byte, error) { 82 return nil, errors.New("get file failed") 83 } 84 res, err := analyzer.Analyze(getCollectedFileContents, nil) 85 g.Expect(err).To(HaveOccurred()) 86 g.Expect(res[0].IsWarn).Should(BeTrue()) 87 g.Expect(res[0].IsPass).Should(BeFalse()) 88 }).Should(Succeed()) 89 }) 90 91 It("Analyze test, and return of get file is not clusterResource", func() { 92 Eventually(func(g Gomega) { 93 getCollectedFileContents := func(filename string) ([]byte, error) { 94 return []byte("test"), nil 95 } 96 res, err := analyzer.Analyze(getCollectedFileContents, nil) 97 g.Expect(err).To(HaveOccurred()) 98 g.Expect(res[0].IsWarn).Should(BeTrue()) 99 g.Expect(res[0].IsPass).Should(BeFalse()) 100 }).Should(Succeed()) 101 }) 102 103 It("Analyze test, and analyzer result is expected that pass is true", func() { 104 Eventually(func(g Gomega) { 105 g.Expect(analyzer.IsExcluded()).Should(BeFalse()) 106 b, err := json.Marshal(clusterResources) 107 g.Expect(err).NotTo(HaveOccurred()) 108 getCollectedFileContents := func(filename string) ([]byte, error) { 109 return b, nil 110 } 111 res, err := analyzer.Analyze(getCollectedFileContents, nil) 112 g.Expect(err).NotTo(HaveOccurred()) 113 g.Expect(res[0].IsPass).Should(BeTrue()) 114 g.Expect(res[0].IsFail).Should(BeFalse()) 115 }).Should(Succeed()) 116 }) 117 It("Analyze test, and analyzer result is expected that fail is true", func() { 118 Eventually(func(g Gomega) { 119 g.Expect(analyzer.IsExcluded()).Should(BeFalse()) 120 clusterResources.Items[0].Provisioner = "apecloud" 121 b, err := json.Marshal(clusterResources) 122 g.Expect(err).NotTo(HaveOccurred()) 123 getCollectedFileContents := func(filename string) ([]byte, error) { 124 return b, nil 125 } 126 res, err := analyzer.Analyze(getCollectedFileContents, nil) 127 g.Expect(err).NotTo(HaveOccurred()) 128 g.Expect(res[0].IsPass).Should(BeFalse()) 129 g.Expect(res[0].IsFail).Should(BeTrue()) 130 }).Should(Succeed()) 131 }) 132 It("Analyze test, and analyzer result is expected that fail is true", func() { 133 Eventually(func(g Gomega) { 134 g.Expect(analyzer.IsExcluded()).Should(BeFalse()) 135 b, err := json.Marshal(clusterResources2) 136 g.Expect(err).NotTo(HaveOccurred()) 137 getCollectedFileContents := func(filename string) ([]byte, error) { 138 return b, nil 139 } 140 analyzer.analyzer.StorageClassType = "" 141 res, err := analyzer.Analyze(getCollectedFileContents, nil) 142 g.Expect(err).NotTo(HaveOccurred()) 143 g.Expect(res[0].IsPass).Should(BeTrue()) 144 g.Expect(res[0].IsWarn).Should(BeFalse()) 145 }).Should(Succeed()) 146 }) 147 }) 148 })