istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/resource/matcher_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 resource
    16  
    17  import "testing"
    18  
    19  func TestMatcher(t *testing.T) {
    20  	cases := []struct {
    21  		name      string
    22  		input     []string
    23  		matches   []string
    24  		nomatches []string
    25  	}{
    26  		{
    27  			name:      "empty",
    28  			input:     []string{},
    29  			matches:   []string{},
    30  			nomatches: []string{"", "foo", "foo/bar", ".*"},
    31  		},
    32  		{
    33  			name:      "single",
    34  			input:     []string{"Foo"},
    35  			matches:   []string{"TestFoo", "TestMyFooBar", "TestFoo/TestBar"},
    36  			nomatches: []string{"baz", "baz/foo", "TestBar/TestFoo"},
    37  		},
    38  		{
    39  			name:      "double",
    40  			input:     []string{"Foo/Bar"},
    41  			matches:   []string{"TestFoo/TestBar", "TestFoo/TestBar/TestBaz"},
    42  			nomatches: []string{"TestFoo", "TestBar", "TestMyFooBar"},
    43  		},
    44  		{
    45  			name:    "space",
    46  			input:   []string{"TestFoo/with space"},
    47  			matches: []string{"TestFoo/with_space"},
    48  		},
    49  		{
    50  			name:      "regex",
    51  			input:     []string{"Foo/.*/Baz"},
    52  			matches:   []string{"TestFoo/something/TestBaz"},
    53  			nomatches: []string{"TestFoo", "TestFoo/TestBaz", "TestFoo/something/TestBar"},
    54  		},
    55  		{
    56  			name:      "multiple",
    57  			input:     []string{"TestFoo/subset", "TestBar"},
    58  			matches:   []string{"TestBar", "TestBar/subtest", "TestFoo/subset"},
    59  			nomatches: []string{"TestFoo/something/subset", "TestFoo/something", "TestFoo", "TestFoo/TestBar"},
    60  		},
    61  	}
    62  	for _, tt := range cases {
    63  		t.Run(tt.name, func(t *testing.T) {
    64  			matcher, err := NewMatcher(tt.input)
    65  			if err != nil {
    66  				t.Fatal(err)
    67  			}
    68  			for _, m := range tt.matches {
    69  				got := matcher.MatchTest(m)
    70  				if !got {
    71  					t.Errorf("expected match for %q", m)
    72  				}
    73  			}
    74  			for _, m := range tt.nomatches {
    75  				got := matcher.MatchTest(m)
    76  				if got {
    77  					t.Errorf("expected no match for %q", m)
    78  				}
    79  			}
    80  		})
    81  	}
    82  }