...

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

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/selector/physicalmachine

     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 physicalmachine
    17  
    18  import (
    19  	"context"
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/kubectl/pkg/scheme"
    25  	"sigs.k8s.io/controller-runtime/pkg/client"
    26  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    27  
    28  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    29  	"github.com/chaos-mesh/chaos-mesh/pkg/log"
    30  	. "github.com/chaos-mesh/chaos-mesh/pkg/testutils"
    31  )
    32  
    33  func TestSelectPhysicalMachines(t *testing.T) {
    34  	g := NewGomegaWithT(t)
    35  
    36  	objects, physicalMachines := GenerateNPhysicalMachines("p", 5, PhysicalMachineArg{Labels: map[string]string{"l1": "l1"}})
    37  	objects2, physicalMachines2 := GenerateNPhysicalMachines("s", 2, PhysicalMachineArg{Namespace: "test-s", Labels: map[string]string{"l2": "l2"}})
    38  
    39  	objects = append(objects, objects2...)
    40  	physicalMachines = append(physicalMachines, physicalMachines2...)
    41  
    42  	err := v1alpha1.SchemeBuilder.AddToScheme(scheme.Scheme)
    43  	g.Expect(err).NotTo(HaveOccurred())
    44  
    45  	c := fake.NewClientBuilder().
    46  		WithScheme(scheme.Scheme).
    47  		WithRuntimeObjects(objects...).
    48  		WithStatusSubresource(&v1alpha1.PodNetworkChaos{}).
    49  		Build()
    50  	var r client.Reader
    51  
    52  	type TestCase struct {
    53  		name     string
    54  		selector v1alpha1.PhysicalMachineSelectorSpec
    55  		expected []v1alpha1.PhysicalMachine
    56  	}
    57  
    58  	tcs := []TestCase{
    59  		{
    60  			name: "filter specified physical machines",
    61  			selector: v1alpha1.PhysicalMachineSelectorSpec{
    62  				PhysicalMachines: map[string][]string{
    63  					metav1.NamespaceDefault: {"p3", "p4"},
    64  					"test-s":                {"s1"},
    65  				},
    66  			},
    67  			expected: []v1alpha1.PhysicalMachine{physicalMachines[3], physicalMachines[4], physicalMachines[6]},
    68  		},
    69  		{
    70  			name: "filter labels physical machines",
    71  			selector: v1alpha1.PhysicalMachineSelectorSpec{
    72  				GenericSelectorSpec: v1alpha1.GenericSelectorSpec{
    73  					LabelSelectors: map[string]string{"l2": "l2"},
    74  				},
    75  			},
    76  			expected: []v1alpha1.PhysicalMachine{physicalMachines[5], physicalMachines[6]},
    77  		},
    78  		{
    79  			name: "filter physicalMachines by label expressions",
    80  			selector: v1alpha1.PhysicalMachineSelectorSpec{
    81  				GenericSelectorSpec: v1alpha1.GenericSelectorSpec{
    82  					ExpressionSelectors: []metav1.LabelSelectorRequirement{
    83  						{
    84  							Key:      "l2",
    85  							Operator: metav1.LabelSelectorOpIn,
    86  							Values:   []string{"l2"},
    87  						},
    88  					},
    89  				},
    90  			},
    91  			expected: []v1alpha1.PhysicalMachine{physicalMachines[5], physicalMachines[6]},
    92  		},
    93  		{
    94  			name: "filter physicalMachines by label selectors and expression selectors",
    95  			selector: v1alpha1.PhysicalMachineSelectorSpec{
    96  				GenericSelectorSpec: v1alpha1.GenericSelectorSpec{
    97  					LabelSelectors: map[string]string{"l1": "l1"},
    98  					ExpressionSelectors: []metav1.LabelSelectorRequirement{
    99  						{
   100  							Key:      "l2",
   101  							Operator: metav1.LabelSelectorOpIn,
   102  							Values:   []string{"l2"},
   103  						},
   104  					},
   105  				},
   106  			},
   107  			expected: nil,
   108  		},
   109  		{
   110  			name: "filter namespace and labels",
   111  			selector: v1alpha1.PhysicalMachineSelectorSpec{
   112  				GenericSelectorSpec: v1alpha1.GenericSelectorSpec{
   113  					Namespaces:     []string{"test-s"},
   114  					LabelSelectors: map[string]string{"l2": "l2"},
   115  				},
   116  			},
   117  			expected: []v1alpha1.PhysicalMachine{physicalMachines[5], physicalMachines[6]},
   118  		},
   119  		{
   120  			name: "filter namespace and labels",
   121  			selector: v1alpha1.PhysicalMachineSelectorSpec{
   122  				GenericSelectorSpec: v1alpha1.GenericSelectorSpec{
   123  					Namespaces:     []string{metav1.NamespaceDefault},
   124  					LabelSelectors: map[string]string{"l2": "l2"},
   125  				},
   126  			},
   127  			expected: nil,
   128  		},
   129  	}
   130  
   131  	var (
   132  		testCfgClusterScoped   = true
   133  		testCfgTargetNamespace = ""
   134  	)
   135  
   136  	logger, _ := log.NewDefaultZapLogger()
   137  
   138  	for _, tc := range tcs {
   139  		filtered, err := SelectPhysicalMachines(context.Background(), c, r, tc.selector, testCfgClusterScoped, testCfgTargetNamespace, false, logger)
   140  		g.Expect(err).ShouldNot(HaveOccurred(), tc.name)
   141  		g.Expect(len(filtered)).To(Equal(len(tc.expected)), tc.name)
   142  	}
   143  }
   144