...

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

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

     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 field
    17  
    18  import (
    19  	"testing"
    20  
    21  	. "github.com/onsi/gomega"
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"sigs.k8s.io/controller-runtime/pkg/client"
    24  
    25  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    26  	"github.com/chaos-mesh/chaos-mesh/pkg/selector/generic"
    27  	. "github.com/chaos-mesh/chaos-mesh/pkg/testutils"
    28  )
    29  
    30  func TestMatch(t *testing.T) {
    31  	g := NewGomegaWithT(t)
    32  
    33  	nameFieldSelector, err := New(v1alpha1.GenericSelectorSpec{FieldSelectors: map[string]string{"metadata.name": "p2"}}, generic.Option{})
    34  	g.Expect(err).ShouldNot(HaveOccurred())
    35  
    36  	emptySelector, err := New(v1alpha1.GenericSelectorSpec{}, generic.Option{})
    37  	g.Expect(err).ShouldNot(HaveOccurred())
    38  
    39  	addressFieldSelector, err := New(v1alpha1.GenericSelectorSpec{FieldSelectors: map[string]string{"spec.address": "123"}}, generic.Option{})
    40  	g.Expect(err).ShouldNot(HaveOccurred())
    41  
    42  	p1Pod := NewPod(PodArg{Name: "p1"})
    43  	p2Pod := NewPod(PodArg{Name: "p2"})
    44  	p1PhysicalMachine := v1alpha1.PhysicalMachine{ObjectMeta: metav1.ObjectMeta{Name: "p1"}, Spec: v1alpha1.PhysicalMachineSpec{Address: "123"}}
    45  	p2PhysicalMachine := v1alpha1.PhysicalMachine{ObjectMeta: metav1.ObjectMeta{Name: "p2"}}
    46  
    47  	tcs := []struct {
    48  		name     string
    49  		obj      client.Object
    50  		selector generic.Selector
    51  		match    bool
    52  	}{
    53  		{
    54  			name:     "filter by name",
    55  			obj:      &p2Pod,
    56  			selector: nameFieldSelector,
    57  			match:    true,
    58  		}, {
    59  			name:     "filter by name",
    60  			obj:      &p1Pod,
    61  			selector: nameFieldSelector,
    62  			match:    false,
    63  		}, {
    64  			name:     "empty filter",
    65  			obj:      &p1Pod,
    66  			selector: emptySelector,
    67  			match:    true,
    68  		}, {
    69  			name:     "filter by physical machine address",
    70  			obj:      &p1PhysicalMachine,
    71  			selector: addressFieldSelector,
    72  			match:    true,
    73  		}, {
    74  			name:     "filter by physical machine address",
    75  			obj:      &p2PhysicalMachine,
    76  			selector: addressFieldSelector,
    77  			match:    false,
    78  		}, {
    79  			name:     "filter by physical machine address",
    80  			obj:      &p1Pod,
    81  			selector: addressFieldSelector,
    82  			match:    false,
    83  		},
    84  	}
    85  
    86  	for _, tc := range tcs {
    87  		g.Expect(tc.selector.Match(tc.obj)).To(Equal(tc.match), tc.name)
    88  	}
    89  }
    90