...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/networkchaos_webhook_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/api/v1alpha1

     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 v1alpha1
    17  
    18  import (
    19  	. "github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  )
    23  
    24  var _ = Describe("networkchaos_webhook", func() {
    25  	Context("Defaulter", func() {
    26  		It("set default namespace selector", func() {
    27  			networkchaos := &NetworkChaos{
    28  				ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault},
    29  			}
    30  			networkchaos.Default()
    31  			Expect(networkchaos.Spec.Selector.Namespaces[0]).To(Equal(metav1.NamespaceDefault))
    32  		})
    33  
    34  		It("set default DelaySpec", func() {
    35  			networkchaos := &NetworkChaos{
    36  				ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault},
    37  				Spec: NetworkChaosSpec{
    38  					TcParameter: TcParameter{
    39  						Delay: &DelaySpec{
    40  							Latency: "90ms",
    41  						},
    42  					},
    43  				},
    44  			}
    45  			networkchaos.Default()
    46  			Expect(string(networkchaos.Spec.Delay.Correlation)).To(Equal(DefaultCorrelation))
    47  			Expect(string(networkchaos.Spec.Delay.Jitter)).To(Equal(DefaultJitter))
    48  		})
    49  	})
    50  	Context("webhook.Validator of networkchaos", func() {
    51  		It("Validate", func() {
    52  
    53  			type TestCase struct {
    54  				name    string
    55  				chaos   NetworkChaos
    56  				execute func(chaos *NetworkChaos) error
    57  				expect  string
    58  			}
    59  			tcs := []TestCase{
    60  				{
    61  					name: "simple ValidateCreate",
    62  					chaos: NetworkChaos{
    63  						ObjectMeta: metav1.ObjectMeta{
    64  							Namespace: metav1.NamespaceDefault,
    65  							Name:      "foo1",
    66  						},
    67  					},
    68  					execute: func(chaos *NetworkChaos) error {
    69  						return chaos.ValidateCreate()
    70  					},
    71  					expect: "",
    72  				},
    73  				{
    74  					name: "simple ValidateUpdate",
    75  					chaos: NetworkChaos{
    76  						ObjectMeta: metav1.ObjectMeta{
    77  							Namespace: metav1.NamespaceDefault,
    78  							Name:      "foo2",
    79  						},
    80  					},
    81  					execute: func(chaos *NetworkChaos) error {
    82  						return chaos.ValidateUpdate(chaos)
    83  					},
    84  					expect: "",
    85  				},
    86  				{
    87  					name: "simple ValidateDelete",
    88  					chaos: NetworkChaos{
    89  						ObjectMeta: metav1.ObjectMeta{
    90  							Namespace: metav1.NamespaceDefault,
    91  							Name:      "foo3",
    92  						},
    93  					},
    94  					execute: func(chaos *NetworkChaos) error {
    95  						return chaos.ValidateDelete()
    96  					},
    97  					expect: "",
    98  				},
    99  				{
   100  					name: "validate the delay",
   101  					chaos: NetworkChaos{
   102  						ObjectMeta: metav1.ObjectMeta{
   103  							Namespace: metav1.NamespaceDefault,
   104  							Name:      "foo6",
   105  						},
   106  						Spec: NetworkChaosSpec{
   107  							TcParameter: TcParameter{
   108  								Delay: &DelaySpec{
   109  									Latency:     "1S",
   110  									Jitter:      "1S",
   111  									Correlation: "num",
   112  								},
   113  							},
   114  						},
   115  					},
   116  					execute: func(chaos *NetworkChaos) error {
   117  						return chaos.ValidateCreate()
   118  					},
   119  					expect: "error",
   120  				},
   121  				{
   122  					name: "validate the reorder",
   123  					chaos: NetworkChaos{
   124  						ObjectMeta: metav1.ObjectMeta{
   125  							Namespace: metav1.NamespaceDefault,
   126  							Name:      "foo7",
   127  						},
   128  						Spec: NetworkChaosSpec{
   129  							TcParameter: TcParameter{
   130  								Delay: &DelaySpec{
   131  									Reorder: &ReorderSpec{
   132  										Reorder:     "num",
   133  										Correlation: "num",
   134  									},
   135  								},
   136  							},
   137  						},
   138  					},
   139  					execute: func(chaos *NetworkChaos) error {
   140  						return chaos.ValidateCreate()
   141  					},
   142  					expect: "error",
   143  				},
   144  				{
   145  					name: "validate the loss",
   146  					chaos: NetworkChaos{
   147  						ObjectMeta: metav1.ObjectMeta{
   148  							Namespace: metav1.NamespaceDefault,
   149  							Name:      "foo8",
   150  						},
   151  						Spec: NetworkChaosSpec{
   152  							TcParameter: TcParameter{
   153  								Loss: &LossSpec{
   154  									Loss:        "num",
   155  									Correlation: "num",
   156  								},
   157  							},
   158  						},
   159  					},
   160  					execute: func(chaos *NetworkChaos) error {
   161  						return chaos.ValidateCreate()
   162  					},
   163  					expect: "error",
   164  				},
   165  				{
   166  					name: "validate the duplicate",
   167  					chaos: NetworkChaos{
   168  						ObjectMeta: metav1.ObjectMeta{
   169  							Namespace: metav1.NamespaceDefault,
   170  							Name:      "foo9",
   171  						},
   172  						Spec: NetworkChaosSpec{
   173  							TcParameter: TcParameter{
   174  								Duplicate: &DuplicateSpec{
   175  									Duplicate:   "num",
   176  									Correlation: "num",
   177  								},
   178  							},
   179  						},
   180  					},
   181  					execute: func(chaos *NetworkChaos) error {
   182  						return chaos.ValidateCreate()
   183  					},
   184  					expect: "error",
   185  				},
   186  				{
   187  					name: "validate the corrupt",
   188  					chaos: NetworkChaos{
   189  						ObjectMeta: metav1.ObjectMeta{
   190  							Namespace: metav1.NamespaceDefault,
   191  							Name:      "foo10",
   192  						},
   193  						Spec: NetworkChaosSpec{
   194  							TcParameter: TcParameter{
   195  								Corrupt: &CorruptSpec{
   196  									Corrupt:     "num",
   197  									Correlation: "num",
   198  								},
   199  							},
   200  						},
   201  					},
   202  					execute: func(chaos *NetworkChaos) error {
   203  						return chaos.ValidateCreate()
   204  					},
   205  					expect: "error",
   206  				},
   207  				{
   208  					name: "validate the bandwidth",
   209  					chaos: NetworkChaos{
   210  						ObjectMeta: metav1.ObjectMeta{
   211  							Namespace: metav1.NamespaceDefault,
   212  							Name:      "foo11",
   213  						},
   214  						Spec: NetworkChaosSpec{
   215  							TcParameter: TcParameter{
   216  								Bandwidth: &BandwidthSpec{
   217  									Rate: "10",
   218  								},
   219  							},
   220  						},
   221  					},
   222  					execute: func(chaos *NetworkChaos) error {
   223  						return chaos.ValidateCreate()
   224  					},
   225  					expect: "error",
   226  				},
   227  				{
   228  					name: "validate the target",
   229  					chaos: NetworkChaos{
   230  						ObjectMeta: metav1.ObjectMeta{
   231  							Namespace: metav1.NamespaceDefault,
   232  							Name:      "foo12",
   233  						},
   234  						Spec: NetworkChaosSpec{
   235  							Target: &PodSelector{
   236  								Mode:  FixedMode,
   237  								Value: "0",
   238  							},
   239  						},
   240  					},
   241  					execute: func(chaos *NetworkChaos) error {
   242  						return chaos.ValidateCreate()
   243  					},
   244  					expect: "error",
   245  				},
   246  				{
   247  					name: "validate direction and externalTargets",
   248  					chaos: NetworkChaos{
   249  						ObjectMeta: metav1.ObjectMeta{
   250  							Namespace: metav1.NamespaceDefault,
   251  							Name:      "foo12",
   252  						},
   253  						Spec: NetworkChaosSpec{
   254  							Direction:       From,
   255  							ExternalTargets: []string{"8.8.8.8"},
   256  						},
   257  					},
   258  					execute: func(chaos *NetworkChaos) error {
   259  						return chaos.ValidateCreate()
   260  					},
   261  					expect: "error",
   262  				},
   263  			}
   264  
   265  			for _, tc := range tcs {
   266  				err := tc.execute(&tc.chaos)
   267  				if tc.expect == "error" {
   268  					Expect(err).To(HaveOccurred())
   269  				} else {
   270  					Expect(err).NotTo(HaveOccurred())
   271  				}
   272  			}
   273  		})
   274  	})
   275  	Context("convertUnitToBytes", func() {
   276  		It("should convert number with unit successfully", func() {
   277  			n, err := ConvertUnitToBytes("  10   mbPs  ")
   278  			Expect(err).Should(Succeed())
   279  			Expect(n).To(Equal(uint64(10 * 1024 * 1024)))
   280  		})
   281  
   282  		It("should return error with invalid unit", func() {
   283  			n, err := ConvertUnitToBytes(" 10 cpbs")
   284  			Expect(err).Should(HaveOccurred())
   285  			Expect(n).To(Equal(uint64(0)))
   286  		})
   287  	})
   288  })
   289