...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/selector/generic/label/selector_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/selector/generic/label

     1  // Copyright 2021 Chaos Mesh 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  
    16  package label
    17  
    18  import (
    19  	"testing"
    20  
    21  	. "github.com/onsi/gomega"
    22  	v1 "k8s.io/api/core/v1"
    23  
    24  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    25  	"github.com/chaos-mesh/chaos-mesh/pkg/selector/generic"
    26  	. "github.com/chaos-mesh/chaos-mesh/pkg/testutils"
    27  )
    28  
    29  func TestMatch(t *testing.T) {
    30  	g := NewGomegaWithT(t)
    31  
    32  	p2Selector, err := New(v1alpha1.GenericSelectorSpec{LabelSelectors: map[string]string{"p2": "p2"}}, generic.Option{})
    33  	g.Expect(err).ShouldNot(HaveOccurred())
    34  
    35  	emptySelector, err := New(v1alpha1.GenericSelectorSpec{}, generic.Option{})
    36  	g.Expect(err).ShouldNot(HaveOccurred())
    37  
    38  	tcs := []struct {
    39  		name     string
    40  		pod      v1.Pod
    41  		selector generic.Selector
    42  		match    bool
    43  	}{
    44  		{
    45  			name:     "filter p2, exist p2 labels",
    46  			pod:      NewPod(PodArg{Name: "p2", Labels: map[string]string{"p2": "p2"}}),
    47  			selector: p2Selector,
    48  			match:    true,
    49  		}, {
    50  			name:     "filter p2, not exist p2 labels",
    51  			pod:      NewPod(PodArg{Name: "p1", Labels: map[string]string{"p1": "p1"}}),
    52  			selector: p2Selector,
    53  			match:    false,
    54  		}, {
    55  			name:     "empty filter",
    56  			pod:      NewPod(PodArg{Name: "p1", Labels: map[string]string{"p1": "p1"}}),
    57  			selector: emptySelector,
    58  			match:    true,
    59  		},
    60  	}
    61  
    62  	for _, tc := range tcs {
    63  		g.Expect(tc.selector.Match(&tc.pod)).To(Equal(tc.match), tc.name)
    64  	}
    65  }
    66