github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/preflight/analyzer/host_region_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("region_test", func() {
    36  	var (
    37  		analyzer AnalyzeClusterRegion
    38  		resInfo  kbcollector.ClusterRegionInfo
    39  	)
    40  	Context("AnalyzeHostUtility test", func() {
    41  		BeforeEach(func() {
    42  			analyzer = AnalyzeClusterRegion{
    43  				analyzer: &preflightv1beta2.ClusterRegionAnalyze{
    44  					Outcomes: []*troubleshoot.Outcome{
    45  						{
    46  							Pass: &troubleshoot.SingleOutcome{
    47  								Message: "k8s cluster region is matched",
    48  							},
    49  							Fail: &troubleshoot.SingleOutcome{
    50  								Message: "k8s cluster access isn't matched",
    51  							},
    52  							Warn: &troubleshoot.SingleOutcome{
    53  								Message: "k8s cluster access isn't matched",
    54  							},
    55  						},
    56  					},
    57  					RegionNames: []string{"beijing", "shanghai"}}}
    58  		})
    59  		It("Analyze test, and get file failed", func() {
    60  			Eventually(func(g Gomega) {
    61  				getCollectedFileContents := func(string) ([]byte, error) {
    62  					return nil, errors.New("get file failed")
    63  				}
    64  				_, err := analyzer.Analyze(getCollectedFileContents)
    65  				g.Expect(err).Should(HaveOccurred())
    66  			}).Should(Succeed())
    67  		})
    68  
    69  		It("Analyze test, and return of get file is not ClusterRegionInfo", func() {
    70  			Eventually(func(g Gomega) {
    71  				getCollectedFileContents := func(string) ([]byte, error) {
    72  					return []byte("test"), nil
    73  				}
    74  				_, err := analyzer.Analyze(getCollectedFileContents)
    75  				g.Expect(err).Should(HaveOccurred())
    76  			}).Should(Succeed())
    77  		})
    78  
    79  		It("Analyze test, and analyzer warn is expected", func() {
    80  			resInfo = kbcollector.ClusterRegionInfo{
    81  				RegionName: "",
    82  			}
    83  			Eventually(func(g Gomega) {
    84  				g.Expect(analyzer.IsExcluded()).Should(BeFalse())
    85  				b, err := json.Marshal(resInfo)
    86  				g.Expect(err).NotTo(HaveOccurred())
    87  				getCollectedFileContents := func(filename string) ([]byte, error) {
    88  					return b, nil
    89  				}
    90  				res, err := analyzer.Analyze(getCollectedFileContents)
    91  				g.Expect(err).NotTo(HaveOccurred())
    92  				g.Expect(res[0].IsWarn).Should(BeTrue())
    93  			}).Should(Succeed())
    94  		})
    95  
    96  		It("Analyze test, and analyzer warn is expected ", func() {
    97  			resInfo = kbcollector.ClusterRegionInfo{
    98  				RegionName: "hangzhou",
    99  			}
   100  			Eventually(func(g Gomega) {
   101  				g.Expect(analyzer.IsExcluded()).Should(BeFalse())
   102  				b, err := json.Marshal(resInfo)
   103  				g.Expect(err).NotTo(HaveOccurred())
   104  				getCollectedFileContents := func(string) ([]byte, error) {
   105  					return b, nil
   106  				}
   107  				res, err := analyzer.Analyze(getCollectedFileContents)
   108  				g.Expect(err).NotTo(HaveOccurred())
   109  				g.Expect(res[0].IsWarn).Should(BeTrue())
   110  			}).Should(Succeed())
   111  		})
   112  
   113  		It("Analyze test, and analyzer pass is expected ", func() {
   114  			resInfo = kbcollector.ClusterRegionInfo{
   115  				RegionName: "beijing",
   116  			}
   117  			Eventually(func(g Gomega) {
   118  				g.Expect(analyzer.IsExcluded()).Should(BeFalse())
   119  				b, err := json.Marshal(resInfo)
   120  				g.Expect(err).NotTo(HaveOccurred())
   121  				getCollectedFileContents := func(string) ([]byte, error) {
   122  					return b, nil
   123  				}
   124  				res, err := analyzer.Analyze(getCollectedFileContents)
   125  				g.Expect(err).NotTo(HaveOccurred())
   126  				g.Expect(res[0].IsPass).Should(BeTrue())
   127  			}).Should(Succeed())
   128  		})
   129  	})
   130  })