github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/preflight/analyzer/host_utility_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  
    29  	troubleshoot "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
    30  
    31  	preflightv1beta2 "github.com/1aal/kubeblocks/externalapis/preflight/v1beta2"
    32  	kbcollector "github.com/1aal/kubeblocks/pkg/cli/preflight/collector"
    33  )
    34  
    35  var _ = Describe("host_utility_test", func() {
    36  	var (
    37  		hostUtilityAnalyzer AnalyzeHostUtility
    38  		resInfo             kbcollector.HostUtilityInfo
    39  	)
    40  
    41  	Context("AnalyzeHostUtility test", func() {
    42  		BeforeEach(func() {
    43  			hostUtilityAnalyzer = AnalyzeHostUtility{
    44  				hostAnalyzer: &preflightv1beta2.HostUtilityAnalyze{
    45  					Outcomes: []*troubleshoot.Outcome{
    46  						{
    47  							Pass: &troubleshoot.SingleOutcome{
    48  								Message: "utility already installed",
    49  							},
    50  							Fail: &troubleshoot.SingleOutcome{
    51  								Message: "utility isn't installed",
    52  							},
    53  							Warn: &troubleshoot.SingleOutcome{
    54  								Message: "utility isn't installed",
    55  							},
    56  						},
    57  					}}}
    58  		})
    59  
    60  		It("Analyze test, and returns fail error", func() {
    61  			resInfo = kbcollector.HostUtilityInfo{
    62  				Path:  "",
    63  				Name:  "helm",
    64  				Error: "helm isn't installed in localhost",
    65  			}
    66  			Eventually(func(g Gomega) {
    67  				getCollectedFileContents := func(filename string) ([]byte, error) {
    68  					return nil, errors.New("get file failed")
    69  				}
    70  				res, err := hostUtilityAnalyzer.Analyze(getCollectedFileContents)
    71  				g.Expect(err).Should(HaveOccurred())
    72  				g.Expect(res).Should(BeNil())
    73  			}).Should(Succeed())
    74  		})
    75  
    76  		It("Analyze test, and analyzer result is expected that fail is true", func() {
    77  			resInfo = kbcollector.HostUtilityInfo{
    78  				Path:  "",
    79  				Name:  "helm",
    80  				Error: "helm isn't installed in localhost",
    81  			}
    82  			Eventually(func(g Gomega) {
    83  				b, err := json.Marshal(resInfo)
    84  				g.Expect(err).NotTo(HaveOccurred())
    85  				getCollectedFileContents := func(filename string) ([]byte, error) {
    86  					return b, nil
    87  				}
    88  				res, err := hostUtilityAnalyzer.Analyze(getCollectedFileContents)
    89  				g.Expect(err).NotTo(HaveOccurred())
    90  				g.Expect(res[0].IsWarn).Should(BeTrue())
    91  			}).Should(Succeed())
    92  		})
    93  
    94  		It("Analyze test, and analyzer result is expected that pass is true", func() {
    95  			resInfo = kbcollector.HostUtilityInfo{
    96  				Path:  "/usr/local/bin/helm",
    97  				Name:  "helm",
    98  				Error: "",
    99  			}
   100  			Eventually(func(g Gomega) {
   101  				g.Expect(hostUtilityAnalyzer.IsExcluded()).Should(BeFalse())
   102  				b, err := json.Marshal(resInfo)
   103  				g.Expect(err).NotTo(HaveOccurred())
   104  				getCollectedFileContents := func(filename string) ([]byte, error) {
   105  					return b, nil
   106  				}
   107  				res, err := hostUtilityAnalyzer.Analyze(getCollectedFileContents)
   108  				g.Expect(err).NotTo(HaveOccurred())
   109  				g.Expect(res[0].IsPass).Should(BeTrue())
   110  			}).Should(Succeed())
   111  		})
   112  	})
   113  })