...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/selector/pod/phase_selector_test.go

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

     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 pod
    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 TestPhaseSelectorMatch(t *testing.T) {
    30  	g := NewGomegaWithT(t)
    31  
    32  	runningSelector, err := newPhaseSelector(v1alpha1.PodSelectorSpec{PodPhaseSelectors: []string{string(v1.PodRunning)}})
    33  	g.Expect(err).ShouldNot(HaveOccurred())
    34  
    35  	emptySelector, err := newPhaseSelector(v1alpha1.PodSelectorSpec{})
    36  	g.Expect(err).ShouldNot(HaveOccurred())
    37  
    38  	runningAndPendingSelector, err := newPhaseSelector(v1alpha1.PodSelectorSpec{PodPhaseSelectors: []string{string(v1.PodRunning), string(v1.PodPending)}})
    39  	g.Expect(err).ShouldNot(HaveOccurred())
    40  
    41  	failedSelector, err := newPhaseSelector(v1alpha1.PodSelectorSpec{PodPhaseSelectors: []string{string(v1.PodFailed)}})
    42  	g.Expect(err).ShouldNot(HaveOccurred())
    43  
    44  	unknownSelector, err := newPhaseSelector(v1alpha1.PodSelectorSpec{PodPhaseSelectors: []string{string(v1.PodUnknown)}})
    45  	g.Expect(err).ShouldNot(HaveOccurred())
    46  
    47  	pods := []v1.Pod{
    48  		NewPod(PodArg{Name: "p1", Status: v1.PodRunning}),
    49  		NewPod(PodArg{Name: "p2", Status: v1.PodRunning}),
    50  		NewPod(PodArg{Name: "p3", Status: v1.PodPending}),
    51  		NewPod(PodArg{Name: "p4", Status: v1.PodFailed}),
    52  	}
    53  
    54  	tcs := []struct {
    55  		name     string
    56  		pod      v1.Pod
    57  		selector generic.Selector
    58  		match    bool
    59  	}{
    60  		{
    61  			name:     "filter running pod, exist running pod",
    62  			pod:      pods[0],
    63  			selector: runningSelector,
    64  			match:    true,
    65  		}, {
    66  			name:     "filter running pod, not exist running pod",
    67  			pod:      pods[2],
    68  			selector: runningSelector,
    69  			match:    false,
    70  		}, {
    71  			name:     "empty filter",
    72  			pod:      pods[0],
    73  			selector: emptySelector,
    74  			match:    true,
    75  		}, {
    76  			name:     "filter running and pending",
    77  			pod:      pods[0],
    78  			selector: runningAndPendingSelector,
    79  			match:    true,
    80  		}, {
    81  			name:     "filter running and pending",
    82  			pod:      pods[2],
    83  			selector: runningAndPendingSelector,
    84  			match:    true,
    85  		}, {
    86  			name:     "filter running and pending",
    87  			pod:      pods[3],
    88  			selector: runningAndPendingSelector,
    89  			match:    false,
    90  		}, {
    91  			name:     "filter failed",
    92  			pod:      pods[3],
    93  			selector: failedSelector,
    94  			match:    true,
    95  		}, {
    96  			name:     "filter failed",
    97  			pod:      pods[0],
    98  			selector: failedSelector,
    99  			match:    false,
   100  		}, {
   101  			name:     "filter unknown",
   102  			pod:      pods[0],
   103  			selector: unknownSelector,
   104  			match:    false,
   105  		},
   106  	}
   107  
   108  	for _, tc := range tcs {
   109  		g.Expect(tc.selector.Match(&tc.pod)).To(Equal(tc.match), tc.name)
   110  	}
   111  }
   112