github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/preflight/analyzer/access_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  	"github.com/replicatedhq/troubleshoot/pkg/collect"
    31  	"k8s.io/apimachinery/pkg/version"
    32  
    33  	preflightv1beta2 "github.com/1aal/kubeblocks/externalapis/preflight/v1beta2"
    34  )
    35  
    36  var _ = Describe("access_test", func() {
    37  	var (
    38  		analyzer AnalyzeClusterAccess
    39  		resInfo  collect.ClusterVersion
    40  	)
    41  	Context("AnalyzeHostUtility test", func() {
    42  		BeforeEach(func() {
    43  			analyzer = AnalyzeClusterAccess{
    44  				analyzer: &preflightv1beta2.ClusterAccessAnalyze{
    45  					Outcomes: []*troubleshoot.Outcome{
    46  						{
    47  							Pass: &troubleshoot.SingleOutcome{
    48  								Message: "k8s cluster access success",
    49  							},
    50  							Fail: &troubleshoot.SingleOutcome{
    51  								Message: "k8s cluster access fail",
    52  							},
    53  						},
    54  					}}}
    55  		})
    56  		It("Analyze test, and get file failed", func() {
    57  			Eventually(func(g Gomega) {
    58  				getCollectedFileContents := func(filename string) ([]byte, error) {
    59  					return nil, errors.New("get file failed")
    60  				}
    61  				res, err := analyzer.Analyze(getCollectedFileContents, nil)
    62  				g.Expect(err).NotTo(HaveOccurred())
    63  				g.Expect(res[0].IsFail).Should(BeTrue())
    64  			}).Should(Succeed())
    65  		})
    66  
    67  		It("Analyze test, and return of get file is not version.Info", func() {
    68  			Eventually(func(g Gomega) {
    69  				getCollectedFileContents := func(filename string) ([]byte, error) {
    70  					return []byte("test"), nil
    71  				}
    72  				res, err := analyzer.Analyze(getCollectedFileContents, nil)
    73  				g.Expect(err).NotTo(HaveOccurred())
    74  				g.Expect(res[0].IsFail).Should(BeTrue())
    75  			}).Should(Succeed())
    76  		})
    77  
    78  		It("Analyze test, and analyzer result is expected that pass is true", func() {
    79  			resInfo = collect.ClusterVersion{
    80  				Info: &version.Info{
    81  					Major:        "1",
    82  					Minor:        "23",
    83  					GitVersion:   "v1.23.15",
    84  					GitCommit:    "b84cb8ab29366daa1bba65bc67f54de2f6c34848",
    85  					GitTreeState: "clean",
    86  					BuildDate:    "2022-12-08T10:42:57Z",
    87  					GoVersion:    "go1.17.13",
    88  					Compiler:     "gc",
    89  					Platform:     "linux/arm64",
    90  				},
    91  				String: "v1.23.15",
    92  			}
    93  			Eventually(func(g Gomega) {
    94  				g.Expect(analyzer.IsExcluded()).Should(BeFalse())
    95  				b, err := json.Marshal(resInfo)
    96  				g.Expect(err).NotTo(HaveOccurred())
    97  				getCollectedFileContents := func(filename string) ([]byte, error) {
    98  					return b, nil
    99  				}
   100  				res, err := analyzer.Analyze(getCollectedFileContents, nil)
   101  				g.Expect(err).NotTo(HaveOccurred())
   102  				g.Expect(res[0].IsPass).Should(BeTrue())
   103  			}).Should(Succeed())
   104  		})
   105  	})
   106  })