github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/preflight/analyzer/kb_taint_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 25 . "github.com/onsi/ginkgo/v2" 26 . "github.com/onsi/gomega" 27 28 "github.com/pkg/errors" 29 troubleshoot "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" 30 "helm.sh/helm/v3/pkg/cli/values" 31 v1 "k8s.io/api/core/v1" 32 33 preflightv1beta2 "github.com/1aal/kubeblocks/externalapis/preflight/v1beta2" 34 ) 35 36 var ( 37 nodeList1 = v1.NodeList{Items: []v1.Node{ 38 {Spec: v1.NodeSpec{Taints: []v1.Taint{ 39 {Key: "dev", Value: "true", Effect: v1.TaintEffectNoSchedule}, 40 {Key: "large", Value: "true", Effect: v1.TaintEffectNoSchedule}, 41 }}}, 42 {Spec: v1.NodeSpec{Taints: []v1.Taint{ 43 {Key: "dev", Value: "false", Effect: v1.TaintEffectNoSchedule}, 44 }}}, 45 }} 46 nodeList2 = v1.NodeList{Items: []v1.Node{ 47 {Spec: v1.NodeSpec{Taints: []v1.Taint{ 48 {Key: "dev", Value: "false", Effect: v1.TaintEffectNoSchedule}, 49 {Key: "large", Value: "true", Effect: v1.TaintEffectNoSchedule}, 50 }}}, 51 }} 52 nodeList3 = v1.NodeList{Items: []v1.Node{ 53 {Spec: v1.NodeSpec{}}, 54 }} 55 ) 56 57 var _ = Describe("taint_class_test", func() { 58 var ( 59 analyzer AnalyzeTaintClassByKb 60 ) 61 Context("analyze taint test", func() { 62 BeforeEach(func() { 63 JSONStr := "tolerations=[ { \"key\": \"dev\", \"operator\": \"Equal\", \"effect\": \"NoSchedule\", \"value\": \"true\" }, " + 64 "{ \"key\": \"large\", \"operator\": \"Equal\", \"effect\": \"NoSchedule\", \"value\": \"true\" } ]," + 65 "prometheus.server.tolerations=[ { \"key\": \"dev\", \"operator\": \"Equal\", \"effect\": \"NoSchedule\", \"value\": \"true\" }, " + 66 "{ \"key\": \"large\", \"operator\": \"Equal\", \"effect\": \"NoSchedule\", \"value\": \"true\" } ]," + 67 "grafana.tolerations=[ { \"key\": \"dev\", \"operator\": \"Equal\", \"effect\": \"NoSchedule\", \"value\": \"true\" }, " + 68 "{ \"key\": \"large\", \"operator\": \"Equal\", \"effect\": \"NoSchedule\", \"value\": \"true\" } ]," 69 analyzer = AnalyzeTaintClassByKb{ 70 analyzer: &preflightv1beta2.KBTaintAnalyze{ 71 Outcomes: []*troubleshoot.Outcome{ 72 { 73 Pass: &troubleshoot.SingleOutcome{ 74 Message: "analyze taint success", 75 }, 76 Fail: &troubleshoot.SingleOutcome{ 77 Message: "analyze taint fail", 78 }, 79 }, 80 }, 81 }, 82 HelmOpts: &values.Options{JSONValues: []string{JSONStr}}, 83 } 84 85 }) 86 It("Analyze test, and get file failed", func() { 87 Eventually(func(g Gomega) { 88 getCollectedFileContents := func(filename string) ([]byte, error) { 89 return nil, errors.New("get file failed") 90 } 91 res, err := analyzer.Analyze(getCollectedFileContents, nil) 92 g.Expect(err).To(HaveOccurred()) 93 g.Expect(res[0].IsFail).Should(BeTrue()) 94 g.Expect(res[0].IsPass).Should(BeFalse()) 95 }).Should(Succeed()) 96 }) 97 98 It("Analyze test, and return of get file is not clusterResource", func() { 99 Eventually(func(g Gomega) { 100 getCollectedFileContents := func(filename string) ([]byte, error) { 101 return []byte("test"), nil 102 } 103 res, err := analyzer.Analyze(getCollectedFileContents, nil) 104 g.Expect(err).To(HaveOccurred()) 105 g.Expect(res[0].IsFail).Should(BeTrue()) 106 g.Expect(res[0].IsPass).Should(BeFalse()) 107 }).Should(Succeed()) 108 }) 109 110 It("Analyze test, and analyzer result is expected that pass is true", func() { 111 Eventually(func(g Gomega) { 112 g.Expect(analyzer.IsExcluded()).Should(BeFalse()) 113 b, err := json.Marshal(nodeList1) 114 g.Expect(err).NotTo(HaveOccurred()) 115 getCollectedFileContents := func(filename string) ([]byte, error) { 116 return b, nil 117 } 118 res, err := analyzer.Analyze(getCollectedFileContents, nil) 119 g.Expect(err).NotTo(HaveOccurred()) 120 g.Expect(res[0].IsPass).Should(BeTrue()) 121 g.Expect(res[0].IsFail).Should(BeFalse()) 122 }).Should(Succeed()) 123 }) 124 It("Analyze test, and analyzer result is expected that fail is true", func() { 125 Eventually(func(g Gomega) { 126 g.Expect(analyzer.IsExcluded()).Should(BeFalse()) 127 b, err := json.Marshal(nodeList2) 128 g.Expect(err).NotTo(HaveOccurred()) 129 getCollectedFileContents := func(filename string) ([]byte, error) { 130 return b, nil 131 } 132 res, err := analyzer.Analyze(getCollectedFileContents, nil) 133 g.Expect(err).NotTo(HaveOccurred()) 134 g.Expect(res[0].IsPass).Should(BeFalse()) 135 g.Expect(res[0].IsFail).Should(BeTrue()) 136 }).Should(Succeed()) 137 }) 138 It("Analyze test, the taints are nil, and analyzer result is expected that pass is true", func() { 139 Eventually(func(g Gomega) { 140 g.Expect(analyzer.IsExcluded()).Should(BeFalse()) 141 b, err := json.Marshal(nodeList3) 142 g.Expect(err).NotTo(HaveOccurred()) 143 getCollectedFileContents := func(filename string) ([]byte, error) { 144 return b, nil 145 } 146 res, err := analyzer.Analyze(getCollectedFileContents, nil) 147 g.Expect(err).NotTo(HaveOccurred()) 148 g.Expect(res[0].IsPass).Should(BeTrue()) 149 g.Expect(res[0].IsFail).Should(BeFalse()) 150 }).Should(Succeed()) 151 }) 152 It("Analyze test, the tolerations are nil, and analyzer result is expected that fail is true", func() { 153 Eventually(func(g Gomega) { 154 g.Expect(analyzer.IsExcluded()).Should(BeFalse()) 155 b, err := json.Marshal(nodeList2) 156 g.Expect(err).NotTo(HaveOccurred()) 157 getCollectedFileContents := func(filename string) ([]byte, error) { 158 return b, nil 159 } 160 analyzer.HelmOpts = nil 161 res, err := analyzer.Analyze(getCollectedFileContents, nil) 162 g.Expect(err).NotTo(HaveOccurred()) 163 g.Expect(res[0].IsPass).Should(BeFalse()) 164 g.Expect(res[0].IsFail).Should(BeTrue()) 165 }).Should(Succeed()) 166 }) 167 }) 168 })