github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/preflight/text_results_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 preflight
    21  
    22  import (
    23  	. "github.com/onsi/ginkgo/v2"
    24  	. "github.com/onsi/gomega"
    25  
    26  	"k8s.io/cli-runtime/pkg/genericiooptions"
    27  
    28  	analyzerunner "github.com/replicatedhq/troubleshoot/pkg/analyze"
    29  )
    30  
    31  var _ = Describe("text_results_test", func() {
    32  	var (
    33  		preflightName    = "stdoutPreflightName"
    34  		jsonFormat       = "json"
    35  		yamlFormat       = "yaml"
    36  		kbcliFormat      = "kbcli"
    37  		unknownFormat    = "unknown"
    38  		streams, _, _, _ = genericiooptions.NewTestIOStreams()
    39  		out              = streams.Out
    40  	)
    41  	It("ShowStdoutResults Test", func() {
    42  		analyzeResults := []*analyzerunner.AnalyzeResult{
    43  			{
    44  				IsPass:  true,
    45  				Title:   "pass item",
    46  				Message: "message for pass test",
    47  				URI:     "https://kubernetes.io",
    48  			},
    49  			{
    50  				IsWarn:  true,
    51  				Title:   "warn item",
    52  				Message: "message for warn test",
    53  				URI:     "https://kubernetes.io",
    54  				Strict:  true,
    55  			},
    56  		}
    57  		Eventually(func(g Gomega) {
    58  			err := ShowTextResults(preflightName, analyzeResults, jsonFormat, true, out)
    59  			g.Expect(err).NotTo(HaveOccurred())
    60  			err = ShowTextResults(preflightName, analyzeResults, yamlFormat, false, out)
    61  			g.Expect(err).NotTo(HaveOccurred())
    62  			err = ShowTextResults(preflightName, analyzeResults, kbcliFormat, false, out)
    63  			g.Expect(err).NotTo(HaveOccurred())
    64  			err = ShowTextResults(preflightName, analyzeResults, unknownFormat, false, out)
    65  			g.Expect(err).To(HaveOccurred())
    66  		}).ShouldNot(HaveOccurred())
    67  	})
    68  	It("ShowStdoutResults Test", func() {
    69  		analyzeResults := []*analyzerunner.AnalyzeResult{
    70  			{
    71  				IsFail:  true,
    72  				Title:   "fail item",
    73  				Message: "message for fail test",
    74  				URI:     "https://kubernetes.io",
    75  			},
    76  		}
    77  		Eventually(func(g Gomega) {
    78  			err := ShowTextResults(preflightName, analyzeResults, jsonFormat, true, out)
    79  			g.Expect(err).NotTo(HaveOccurred())
    80  			err = ShowTextResults(preflightName, analyzeResults, yamlFormat, false, out)
    81  			g.Expect(err).NotTo(HaveOccurred())
    82  			err = ShowTextResults(preflightName, analyzeResults, kbcliFormat, false, out)
    83  			g.Expect(err).NotTo(HaveOccurred())
    84  			err = ShowTextResults(preflightName, analyzeResults, unknownFormat, false, out)
    85  			g.Expect(err).To(HaveOccurred())
    86  		}).Should(HaveOccurred())
    87  	})
    88  })