github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/preflight/analyzer/anzlyzer_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 "context" 24 "encoding/json" 25 "errors" 26 27 . "github.com/onsi/ginkgo/v2" 28 . "github.com/onsi/gomega" 29 30 troubleshoot "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" 31 "github.com/replicatedhq/troubleshoot/pkg/collect" 32 "k8s.io/apimachinery/pkg/version" 33 34 preflightv1beta2 "github.com/1aal/kubeblocks/externalapis/preflight/v1beta2" 35 kbcollector "github.com/1aal/kubeblocks/pkg/cli/preflight/collector" 36 ) 37 38 var _ = Describe("analyzer_test", func() { 39 Context("KBAnalyze test", func() { 40 It("KBAnalyze test, and ExtendAnalyze is nil", func() { 41 Eventually(func(g Gomega) { 42 res := KBAnalyze(context.TODO(), &preflightv1beta2.ExtendAnalyze{}, nil, nil, nil) 43 g.Expect(res[0].IsFail).Should(BeTrue()) 44 }).Should(Succeed()) 45 }) 46 47 It("KBAnalyze test, and expect success", func() { 48 kbAnalyzer := &preflightv1beta2.ExtendAnalyze{ 49 ClusterAccess: &preflightv1beta2.ClusterAccessAnalyze{ 50 Outcomes: []*troubleshoot.Outcome{ 51 { 52 Pass: &troubleshoot.SingleOutcome{ 53 Message: "k8s cluster access success", 54 }, 55 Fail: &troubleshoot.SingleOutcome{ 56 Message: "k8s cluster access fail", 57 }, 58 }, 59 }}} 60 resInfo := collect.ClusterVersion{ 61 Info: &version.Info{ 62 Major: "1", 63 Minor: "23", 64 GitVersion: "v1.23.15", 65 GitCommit: "b84cb8ab29366daa1bba65bc67f54de2f6c34848", 66 GitTreeState: "clean", 67 BuildDate: "2022-12-08T10:42:57Z", 68 GoVersion: "go1.17.13", 69 Compiler: "gc", 70 Platform: "linux/arm64", 71 }, 72 String: "v1.23.15", 73 } 74 Eventually(func(g Gomega) { 75 b, err := json.Marshal(resInfo) 76 g.Expect(err).NotTo(HaveOccurred()) 77 getCollectedFileContents := func(string) ([]byte, error) { 78 return b, nil 79 } 80 res := KBAnalyze(context.TODO(), kbAnalyzer, getCollectedFileContents, nil, nil) 81 Expect(len(res)).Should(Equal(1)) 82 g.Expect(res[0].IsPass).Should(BeTrue()) 83 }).Should(Succeed()) 84 }) 85 }) 86 87 Context("HostKBAnalyze test", func() { 88 It("HostKBAnalyze test, and ExtendHostAnalyze is nil", func() { 89 Eventually(func(g Gomega) { 90 res := HostKBAnalyze(context.TODO(), &preflightv1beta2.ExtendHostAnalyze{}, nil, nil) 91 g.Expect(res[0].IsFail).Should(BeTrue()) 92 }).Should(Succeed()) 93 }) 94 95 It("HostKBAnalyze test, and expect success", func() { 96 kbHostAnalyzer := &preflightv1beta2.ExtendHostAnalyze{ 97 HostUtility: &preflightv1beta2.HostUtilityAnalyze{ 98 Outcomes: []*troubleshoot.Outcome{ 99 { 100 Pass: &troubleshoot.SingleOutcome{ 101 Message: "utility already installed", 102 }, 103 Fail: &troubleshoot.SingleOutcome{ 104 Message: "utility isn't installed", 105 }, 106 }, 107 }}} 108 resInfo := kbcollector.HostUtilityInfo{ 109 Path: "/usr/local/bin/helm", 110 Name: "helm", 111 Error: "", 112 } 113 Eventually(func(g Gomega) { 114 b, err := json.Marshal(resInfo) 115 g.Expect(err).NotTo(HaveOccurred()) 116 getCollectedFileContents := func(string) ([]byte, error) { 117 return b, nil 118 } 119 res := HostKBAnalyze(context.TODO(), kbHostAnalyzer, getCollectedFileContents, nil) 120 Expect(len(res)).Should(Equal(1)) 121 g.Expect(res[0].IsPass).Should(BeTrue()) 122 }).Should(Succeed()) 123 }) 124 }) 125 126 It("GetAnalyzer test, and expect success", func() { 127 Eventually(func(g Gomega) { 128 collector, ok := GetAnalyzer(&preflightv1beta2.ExtendAnalyze{ClusterAccess: &preflightv1beta2.ClusterAccessAnalyze{}}, nil) 129 g.Expect(collector).ShouldNot(BeNil()) 130 g.Expect(ok).Should(BeTrue()) 131 collector, ok = GetAnalyzer(&preflightv1beta2.ExtendAnalyze{}, nil) 132 g.Expect(collector).Should(BeNil()) 133 g.Expect(ok).Should(BeFalse()) 134 }).Should(Succeed()) 135 }) 136 137 It("NewAnalyzeResultError test, argument isn't nil", func() { 138 res := NewAnalyzeResultError(&AnalyzeClusterAccess{analyzer: &preflightv1beta2.ClusterAccessAnalyze{}}, errors.New("mock error")) 139 Expect(len(res)).Should(Equal(1)) 140 Expect(res[0].IsFail).Should(BeTrue()) 141 }) 142 })