istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/analyze/analyze_test.go (about)

     1  // Copyright Istio Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package analyze
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  
    21  	. "github.com/onsi/gomega"
    22  
    23  	"istio.io/istio/istioctl/pkg/cli"
    24  	"istio.io/istio/istioctl/pkg/util/testutil"
    25  	"istio.io/istio/pkg/config/analysis/diag"
    26  )
    27  
    28  func TestErrorOnIssuesFound(t *testing.T) {
    29  	g := NewWithT(t)
    30  
    31  	msgs := []diag.Message{
    32  		diag.NewMessage(
    33  			diag.NewMessageType(diag.Error, "B1", "Template: %q"),
    34  			nil,
    35  			"",
    36  		),
    37  		diag.NewMessage(
    38  			diag.NewMessageType(diag.Warning, "A1", "Template: %q"),
    39  			nil,
    40  			"",
    41  		),
    42  	}
    43  
    44  	err := errorIfMessagesExceedThreshold(msgs)
    45  
    46  	g.Expect(err).To(BeIdenticalTo(AnalyzerFoundIssuesError{}))
    47  }
    48  
    49  func TestNoErrorIfMessageLevelsBelowThreshold(t *testing.T) {
    50  	g := NewWithT(t)
    51  
    52  	msgs := []diag.Message{
    53  		diag.NewMessage(
    54  			diag.NewMessageType(diag.Info, "B1", "Template: %q"),
    55  			nil,
    56  			"",
    57  		),
    58  		diag.NewMessage(
    59  			diag.NewMessageType(diag.Warning, "A1", "Template: %q"),
    60  			nil,
    61  			"",
    62  		),
    63  	}
    64  
    65  	err := errorIfMessagesExceedThreshold(msgs)
    66  
    67  	g.Expect(err).To(BeNil())
    68  }
    69  
    70  func TestSkipPodsInFiles(t *testing.T) {
    71  	c := testutil.TestCase{
    72  		Args: strings.Split(
    73  			"-A --use-kube=false --failure-threshold ERROR testdata/analyze-file/public-gateway.yaml",
    74  			" "),
    75  		WantException: false,
    76  	}
    77  	analyze := Analyze(cli.NewFakeContext(nil))
    78  	testutil.VerifyOutput(t, analyze, c)
    79  }