istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/config/analysis/analyzer_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 analysis
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/onsi/gomega"
    21  
    22  	"istio.io/istio/pkg/config"
    23  	"istio.io/istio/pkg/config/analysis/diag"
    24  	"istio.io/istio/pkg/config/resource"
    25  	"istio.io/istio/pkg/config/schema/collection"
    26  	resource2 "istio.io/istio/pkg/config/schema/resource"
    27  )
    28  
    29  type analyzer struct {
    30  	name   string
    31  	inputs []config.GroupVersionKind
    32  	ran    bool
    33  }
    34  
    35  // Metadata implements Analyzer
    36  func (a *analyzer) Metadata() Metadata {
    37  	return Metadata{
    38  		Name:   a.name,
    39  		Inputs: a.inputs,
    40  	}
    41  }
    42  
    43  // Analyze implements Analyzer
    44  func (a *analyzer) Analyze(Context) {
    45  	a.ran = true
    46  }
    47  
    48  type context struct{}
    49  
    50  func (ctx *context) Report(config.GroupVersionKind, diag.Message)                       {}
    51  func (ctx *context) Find(config.GroupVersionKind, resource.FullName) *resource.Instance { return nil }
    52  func (ctx *context) Exists(config.GroupVersionKind, resource.FullName) bool             { return false }
    53  func (ctx *context) ForEach(config.GroupVersionKind, IteratorFn)                        {}
    54  func (ctx *context) Canceled() bool                                                     { return false }
    55  func (ctx *context) SetAnalyzer(_ string)                                               {}
    56  
    57  func TestCombinedAnalyzer(t *testing.T) {
    58  	g := NewWithT(t)
    59  
    60  	col1 := newSchema("col1")
    61  	col2 := newSchema("col2")
    62  	col3 := newSchema("col3")
    63  	col4 := newSchema("col4")
    64  
    65  	a1 := &analyzer{name: "a1", inputs: []config.GroupVersionKind{col1.GroupVersionKind()}}
    66  	a2 := &analyzer{name: "a2", inputs: []config.GroupVersionKind{col2.GroupVersionKind()}}
    67  	a3 := &analyzer{name: "a3", inputs: []config.GroupVersionKind{col3.GroupVersionKind()}}
    68  	a4 := &analyzer{name: "a4", inputs: []config.GroupVersionKind{col4.GroupVersionKind()}}
    69  
    70  	a := Combine("combined", a1, a2, a3, a4)
    71  	g.Expect(a.Metadata().Inputs).To(ConsistOf(col1.GroupVersionKind(), col2.GroupVersionKind(), col3.GroupVersionKind(), col4.GroupVersionKind()))
    72  
    73  	removed := a.RemoveSkipped(collection.NewSchemasBuilder().MustAdd(col1).MustAdd(col2).Build())
    74  
    75  	g.Expect(removed).To(ConsistOf(a3.Metadata().Name, a4.Metadata().Name))
    76  	g.Expect(a.Metadata().Inputs).To(ConsistOf(col1.GroupVersionKind(), col2.GroupVersionKind()))
    77  
    78  	a.Analyze(&context{})
    79  
    80  	g.Expect(a1.ran).To(BeTrue())
    81  	g.Expect(a2.ran).To(BeTrue())
    82  	g.Expect(a3.ran).To(BeFalse())
    83  	g.Expect(a4.ran).To(BeFalse())
    84  }
    85  
    86  func newSchema(name string) resource2.Schema {
    87  	return resource2.Builder{
    88  		Kind:         name,
    89  		Plural:       name + "s",
    90  		ProtoPackage: "github.com/gogo/protobuf/types",
    91  		Proto:        "google.protobuf.Empty",
    92  	}.MustBuild()
    93  }