istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/label/labels_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 label
    16  
    17  import (
    18  	"strconv"
    19  	"testing"
    20  )
    21  
    22  func TestLabels(t *testing.T) {
    23  	tests := []struct {
    24  		filter   string
    25  		labels   Set
    26  		expected bool
    27  		err      bool
    28  	}{
    29  		{filter: "", labels: nil, expected: true},
    30  		{filter: "", labels: NewSet(Postsubmit), expected: true},
    31  		{filter: "", labels: NewSet(Postsubmit, CustomSetup), expected: true},
    32  		{filter: "$requires.kube", labels: NewSet(Postsubmit, CustomSetup), err: true},
    33  		{filter: "zoo", labels: NewSet(Postsubmit, CustomSetup), expected: true},
    34  		{filter: "postsubmit", labels: NewSet(Postsubmit), expected: true},
    35  		{filter: "postsubmit", labels: NewSet(CustomSetup), expected: false},
    36  		{filter: "postsubmit", labels: NewSet(CustomSetup, Postsubmit), expected: true},
    37  		{filter: "postsubmit,customsetup", labels: NewSet(Postsubmit, CustomSetup), expected: true},
    38  		{filter: "postsubmit,customsetup", labels: NewSet(Postsubmit), expected: false},
    39  		{filter: "+postsubmit,+customsetup", labels: NewSet(Postsubmit), expected: false},
    40  		{filter: "postsubmit,+customsetup", labels: NewSet(Postsubmit, CustomSetup), expected: true},
    41  		{filter: "-postsubmit", labels: NewSet(), expected: true},
    42  		{filter: "-postsubmit", labels: NewSet(Postsubmit), expected: false},
    43  		{filter: "-postsubmit,-customsetup", labels: NewSet(), expected: true},
    44  		{filter: "-postsubmit,customsetup", labels: NewSet(Postsubmit, CustomSetup), expected: false},
    45  		{filter: "-postsubmit,customsetup", labels: NewSet(CustomSetup), expected: true},
    46  		{filter: "-postsubmit,customsetup", labels: NewSet(Postsubmit), expected: false},
    47  		{filter: "-postsubmit,customsetup", labels: NewSet(), expected: false},
    48  		{filter: "-postsubmit,-postsubmit", labels: NewSet(), expected: true},
    49  		{filter: "-postsubmit,-postsubmit", labels: NewSet(Postsubmit), expected: false},
    50  		{filter: "postsubmit,postsubmit", labels: NewSet(), expected: false},
    51  		{filter: "postsubmit,postsubmit", labels: NewSet(Postsubmit), expected: true},
    52  		{filter: "postsubmit,postsubmit", labels: NewSet(), expected: false},
    53  		{filter: "postsubmit,-postsubmit", labels: NewSet(), err: true},
    54  	}
    55  
    56  	for i, te := range tests {
    57  		t.Run(strconv.FormatInt(int64(i), 10), func(t *testing.T) {
    58  			f, err := ParseSelector(te.filter)
    59  			if err != nil {
    60  				if te.err {
    61  					return
    62  				}
    63  				t.Fatalf("Unexpected error: %v, filter:%q, labels:%v", err, te.filter, te.labels)
    64  			} else if te.err {
    65  				t.Fatalf("Expected error not found: filter:%q, labels:%v", te.filter, te.labels)
    66  			}
    67  
    68  			actual := f.Selects(te.labels)
    69  			if actual != te.expected {
    70  				t.Fatalf("Mismatch: got:%v, wanted: %v, filter:%q, labels:%v", actual, te.expected, te.filter, te.labels)
    71  			}
    72  		})
    73  	}
    74  }