...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/webhook/affected_namespaces_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/webhook

     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 webhook
    17  
    18  import (
    19  	"testing"
    20  
    21  	"github.com/onsi/gomega"
    22  
    23  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    24  )
    25  
    26  func TestAffectedNamespaces(t *testing.T) {
    27  	g := gomega.NewGomegaWithT(t)
    28  
    29  	_, namespaces := affectedNamespaces(&v1alpha1.Schedule{
    30  		Spec: v1alpha1.ScheduleSpec{
    31  			ScheduleItem: v1alpha1.ScheduleItem{
    32  				EmbedChaos: v1alpha1.EmbedChaos{
    33  					PodChaos: &v1alpha1.PodChaosSpec{
    34  						ContainerSelector: v1alpha1.ContainerSelector{
    35  							PodSelector: v1alpha1.PodSelector{
    36  								Selector: v1alpha1.PodSelectorSpec{
    37  									GenericSelectorSpec: v1alpha1.GenericSelectorSpec{
    38  										Namespaces: []string{"ns1", "ns2"},
    39  									},
    40  								},
    41  							},
    42  						},
    43  					},
    44  				},
    45  			},
    46  		},
    47  	})
    48  	g.Expect(namespaces).To(gomega.Equal(map[string]struct{}{
    49  		"ns1": {},
    50  		"ns2": {},
    51  	}))
    52  
    53  	_, namespaces = affectedNamespaces(&v1alpha1.Workflow{
    54  		Spec: v1alpha1.WorkflowSpec{
    55  			Templates: []v1alpha1.Template{
    56  				{
    57  					EmbedChaos: &v1alpha1.EmbedChaos{
    58  						NetworkChaos: &v1alpha1.NetworkChaosSpec{
    59  							Target: &v1alpha1.PodSelector{
    60  								Selector: v1alpha1.PodSelectorSpec{
    61  									GenericSelectorSpec: v1alpha1.GenericSelectorSpec{
    62  										Namespaces: []string{"ns1", "ns2"},
    63  									},
    64  								},
    65  							},
    66  						},
    67  					},
    68  				},
    69  			},
    70  		},
    71  	})
    72  	g.Expect(namespaces).To(gomega.Equal(map[string]struct{}{
    73  		"ns1": {},
    74  		"ns2": {},
    75  	}))
    76  
    77  	clusterScoped, _ := affectedNamespaces(&v1alpha1.NetworkChaos{
    78  		Spec: v1alpha1.NetworkChaosSpec{
    79  			Target: &v1alpha1.PodSelector{},
    80  		},
    81  	})
    82  	g.Expect(clusterScoped).To(gomega.BeTrue())
    83  
    84  	clusterScoped, _ = affectedNamespaces(&v1alpha1.NetworkChaos{})
    85  	g.Expect(clusterScoped).To(gomega.BeTrue())
    86  
    87  	_, namespaces = affectedNamespaces(&v1alpha1.Workflow{
    88  		Spec: v1alpha1.WorkflowSpec{
    89  			Templates: []v1alpha1.Template{
    90  				{
    91  					EmbedChaos: &v1alpha1.EmbedChaos{
    92  						NetworkChaos: &v1alpha1.NetworkChaosSpec{
    93  							Target: &v1alpha1.PodSelector{
    94  								Selector: v1alpha1.PodSelectorSpec{
    95  									GenericSelectorSpec: v1alpha1.GenericSelectorSpec{
    96  										Namespaces: []string{"ns1", "ns2"},
    97  									},
    98  								},
    99  							},
   100  						},
   101  					},
   102  				},
   103  				{
   104  					EmbedChaos: &v1alpha1.EmbedChaos{
   105  						NetworkChaos: &v1alpha1.NetworkChaosSpec{
   106  							Target: &v1alpha1.PodSelector{
   107  								Selector: v1alpha1.PodSelectorSpec{
   108  									GenericSelectorSpec: v1alpha1.GenericSelectorSpec{
   109  										Namespaces: []string{"ns3", "ns4"},
   110  									},
   111  								},
   112  							},
   113  						},
   114  					},
   115  				},
   116  			},
   117  		},
   118  	})
   119  	g.Expect(namespaces).To(gomega.Equal(map[string]struct{}{
   120  		"ns1": {},
   121  		"ns2": {},
   122  		"ns3": {},
   123  		"ns4": {},
   124  	}))
   125  
   126  	_, namespaces = affectedNamespaces(&v1alpha1.NetworkChaos{
   127  		Spec: v1alpha1.NetworkChaosSpec{
   128  			Target: &v1alpha1.PodSelector{
   129  				Selector: v1alpha1.PodSelectorSpec{
   130  					Pods: map[string][]string{
   131  						"ns1": {"pod1", "pod2"},
   132  						"ns2": {"pod3", "pod4"},
   133  					},
   134  				},
   135  			},
   136  		},
   137  	})
   138  	g.Expect(namespaces).To(gomega.Equal(map[string]struct{}{
   139  		"ns1": {},
   140  		"ns2": {},
   141  	}))
   142  }
   143