...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/httpchaos_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  	"net/http"
    20  
    21  	. "github.com/onsi/ginkgo/v2"
    22  	. "github.com/onsi/gomega"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  )
    25  
    26  var _ = Describe("HTTPChaos Webhook", func() {
    27  	Context("webhook.Validator of httpchaos", func() {
    28  		It("Validate", func() {
    29  			type TestCase struct {
    30  				name    string
    31  				chaos   HTTPChaos
    32  				execute func(chaos *HTTPChaos) error
    33  				expect  string
    34  			}
    35  			errorDuration := "400S"
    36  			errorMethod := "gET"
    37  			validMethod := http.MethodGet
    38  			errorDelay := "1"
    39  			valideDelay := "1s"
    40  
    41  			tcs := []TestCase{
    42  				{
    43  					name: "simple ValidateCreate",
    44  					chaos: HTTPChaos{
    45  						ObjectMeta: metav1.ObjectMeta{
    46  							Namespace: metav1.NamespaceDefault,
    47  							Name:      "foo1",
    48  						},
    49  						Spec: HTTPChaosSpec{
    50  							Target: PodHttpRequest,
    51  							Port:   80,
    52  						},
    53  					},
    54  					execute: func(chaos *HTTPChaos) error {
    55  						_, err := chaos.ValidateCreate()
    56  						return err
    57  					},
    58  					expect: "",
    59  				},
    60  				{
    61  					name: "simple ValidateUpdate",
    62  					chaos: HTTPChaos{
    63  						ObjectMeta: metav1.ObjectMeta{
    64  							Namespace: metav1.NamespaceDefault,
    65  							Name:      "foo2",
    66  						},
    67  						Spec: HTTPChaosSpec{
    68  							Target: PodHttpRequest,
    69  							Port:   80,
    70  						},
    71  					},
    72  					execute: func(chaos *HTTPChaos) error {
    73  						_, err := chaos.ValidateUpdate(chaos)
    74  						return err
    75  					},
    76  					expect: "",
    77  				},
    78  				{
    79  					name: "simple ValidateDelete",
    80  					chaos: HTTPChaos{
    81  						ObjectMeta: metav1.ObjectMeta{
    82  							Namespace: metav1.NamespaceDefault,
    83  							Name:      "foo3",
    84  						},
    85  						Spec: HTTPChaosSpec{
    86  							Target: PodHttpRequest,
    87  							Port:   80,
    88  						},
    89  					},
    90  					execute: func(chaos *HTTPChaos) error {
    91  						_, err := chaos.ValidateDelete()
    92  						return err
    93  					},
    94  					expect: "",
    95  				},
    96  				{
    97  					name: "parse the duration error",
    98  					chaos: HTTPChaos{
    99  						ObjectMeta: metav1.ObjectMeta{
   100  							Namespace: metav1.NamespaceDefault,
   101  							Name:      "foo6",
   102  						},
   103  						Spec: HTTPChaosSpec{
   104  							Duration: &errorDuration,
   105  							Target:   PodHttpRequest,
   106  							Port:     80,
   107  						},
   108  					},
   109  					execute: func(chaos *HTTPChaos) error {
   110  						_, err := chaos.ValidateCreate()
   111  						return err
   112  					},
   113  					expect: "error",
   114  				},
   115  				{
   116  					name: "validate value with FixedPercentMode",
   117  					chaos: HTTPChaos{
   118  						ObjectMeta: metav1.ObjectMeta{
   119  							Namespace: metav1.NamespaceDefault,
   120  							Name:      "foo7",
   121  						},
   122  						Spec: HTTPChaosSpec{
   123  							PodSelector: PodSelector{
   124  								Value: "0",
   125  								Mode:  FixedMode,
   126  							},
   127  							Port:   80,
   128  							Target: PodHttpRequest,
   129  						},
   130  					},
   131  					execute: func(chaos *HTTPChaos) error {
   132  						_, err := chaos.ValidateCreate()
   133  						return err
   134  					},
   135  					expect: "error",
   136  				},
   137  				{
   138  					name: "validate value with FixedPercentMode, parse value error",
   139  					chaos: HTTPChaos{
   140  						ObjectMeta: metav1.ObjectMeta{
   141  							Namespace: metav1.NamespaceDefault,
   142  							Name:      "foo8",
   143  						},
   144  						Spec: HTTPChaosSpec{
   145  							PodSelector: PodSelector{
   146  								Value: "num",
   147  								Mode:  FixedMode,
   148  							},
   149  							Port:   80,
   150  							Target: PodHttpRequest,
   151  						},
   152  					},
   153  					execute: func(chaos *HTTPChaos) error {
   154  						_, err := chaos.ValidateCreate()
   155  						return err
   156  					},
   157  					expect: "error",
   158  				},
   159  				{
   160  					name: "validate value with RandomMaxPercentMode",
   161  					chaos: HTTPChaos{
   162  						ObjectMeta: metav1.ObjectMeta{
   163  							Namespace: metav1.NamespaceDefault,
   164  							Name:      "foo9",
   165  						},
   166  						Spec: HTTPChaosSpec{
   167  							PodSelector: PodSelector{
   168  								Value: "0",
   169  								Mode:  RandomMaxPercentMode,
   170  							},
   171  							Port:   80,
   172  							Target: PodHttpRequest,
   173  						},
   174  					},
   175  					execute: func(chaos *HTTPChaos) error {
   176  						_, err := chaos.ValidateCreate()
   177  						return err
   178  					},
   179  					expect: "error",
   180  				},
   181  				{
   182  					name: "validate value with RandomMaxPercentMode ,parse value error",
   183  					chaos: HTTPChaos{
   184  						ObjectMeta: metav1.ObjectMeta{
   185  							Namespace: metav1.NamespaceDefault,
   186  							Name:      "foo10",
   187  						},
   188  						Spec: HTTPChaosSpec{
   189  							PodSelector: PodSelector{
   190  								Value: "num",
   191  								Mode:  RandomMaxPercentMode,
   192  							},
   193  							Port:   80,
   194  							Target: PodHttpRequest,
   195  						},
   196  					},
   197  					execute: func(chaos *HTTPChaos) error {
   198  						_, err := chaos.ValidateCreate()
   199  						return err
   200  					},
   201  					expect: "error",
   202  				},
   203  				{
   204  					name: "validate value with FixedPercentMode",
   205  					chaos: HTTPChaos{
   206  						ObjectMeta: metav1.ObjectMeta{
   207  							Namespace: metav1.NamespaceDefault,
   208  							Name:      "foo11",
   209  						},
   210  						Spec: HTTPChaosSpec{
   211  							PodSelector: PodSelector{
   212  								Value: "101",
   213  								Mode:  FixedPercentMode,
   214  							},
   215  							Port:   80,
   216  							Target: PodHttpRequest,
   217  						},
   218  					},
   219  					execute: func(chaos *HTTPChaos) error {
   220  						_, err := chaos.ValidateCreate()
   221  						return err
   222  					},
   223  					expect: "error",
   224  				},
   225  				{
   226  					name: "validate port 1",
   227  					chaos: HTTPChaos{
   228  						ObjectMeta: metav1.ObjectMeta{
   229  							Namespace: metav1.NamespaceDefault,
   230  							Name:      "foo12",
   231  						},
   232  						Spec: HTTPChaosSpec{
   233  							Target: PodHttpRequest,
   234  						},
   235  					},
   236  					execute: func(chaos *HTTPChaos) error {
   237  						_, err := chaos.ValidateCreate()
   238  						return err
   239  					},
   240  					expect: "error",
   241  				},
   242  				{
   243  					name: "validate port 2",
   244  					chaos: HTTPChaos{
   245  						ObjectMeta: metav1.ObjectMeta{
   246  							Namespace: metav1.NamespaceDefault,
   247  							Name:      "foo13",
   248  						},
   249  						Spec: HTTPChaosSpec{
   250  							Port:   -1,
   251  							Target: PodHttpRequest,
   252  						},
   253  					},
   254  					execute: func(chaos *HTTPChaos) error {
   255  						_, err := chaos.ValidateCreate()
   256  						return err
   257  					},
   258  					expect: "error",
   259  				},
   260  				{
   261  					name: "validate target 1",
   262  					chaos: HTTPChaos{
   263  						ObjectMeta: metav1.ObjectMeta{
   264  							Namespace: metav1.NamespaceDefault,
   265  							Name:      "foo14",
   266  						},
   267  						Spec: HTTPChaosSpec{
   268  							Port: 80,
   269  						},
   270  					},
   271  					execute: func(chaos *HTTPChaos) error {
   272  						_, err := chaos.ValidateCreate()
   273  						return err
   274  					},
   275  					expect: "error",
   276  				},
   277  				{
   278  					name: "validate target 2",
   279  					chaos: HTTPChaos{
   280  						ObjectMeta: metav1.ObjectMeta{
   281  							Namespace: metav1.NamespaceDefault,
   282  							Name:      "foo15",
   283  						},
   284  						Spec: HTTPChaosSpec{
   285  							Port:   80,
   286  							Target: "request",
   287  						},
   288  					},
   289  					execute: func(chaos *HTTPChaos) error {
   290  						_, err := chaos.ValidateCreate()
   291  						return err
   292  					},
   293  					expect: "error",
   294  				},
   295  				{
   296  					name: "valid method 1",
   297  					chaos: HTTPChaos{
   298  						ObjectMeta: metav1.ObjectMeta{
   299  							Namespace: metav1.NamespaceDefault,
   300  							Name:      "foo16",
   301  						},
   302  						Spec: HTTPChaosSpec{
   303  							Port:   80,
   304  							Target: PodHttpRequest,
   305  							Method: &validMethod,
   306  						},
   307  					},
   308  					execute: func(chaos *HTTPChaos) error {
   309  						_, err := chaos.ValidateCreate()
   310  						return err
   311  					},
   312  					expect: "ok",
   313  				},
   314  				{
   315  					name: "valid method 2",
   316  					chaos: HTTPChaos{
   317  						ObjectMeta: metav1.ObjectMeta{
   318  							Namespace: metav1.NamespaceDefault,
   319  							Name:      "foo17",
   320  						},
   321  						Spec: HTTPChaosSpec{
   322  							Port:   80,
   323  							Target: PodHttpResponse,
   324  							Method: &errorMethod,
   325  						},
   326  					},
   327  					execute: func(chaos *HTTPChaos) error {
   328  						_, err := chaos.ValidateCreate()
   329  						return err
   330  					},
   331  					expect: "ok",
   332  				},
   333  				{
   334  					name: "invalid method",
   335  					chaos: HTTPChaos{
   336  						ObjectMeta: metav1.ObjectMeta{
   337  							Namespace: metav1.NamespaceDefault,
   338  							Name:      "foo18",
   339  						},
   340  						Spec: HTTPChaosSpec{
   341  							Port:   80,
   342  							Target: PodHttpRequest,
   343  							Method: &errorMethod,
   344  						},
   345  					},
   346  					execute: func(chaos *HTTPChaos) error {
   347  						_, err := chaos.ValidateCreate()
   348  						return err
   349  					},
   350  					expect: "error",
   351  				},
   352  				{
   353  					name: "valid delay",
   354  					chaos: HTTPChaos{
   355  						ObjectMeta: metav1.ObjectMeta{
   356  							Namespace: metav1.NamespaceDefault,
   357  							Name:      "foo19",
   358  						},
   359  						Spec: HTTPChaosSpec{
   360  							Port:   80,
   361  							Target: PodHttpRequest,
   362  							PodHttpChaosActions: PodHttpChaosActions{
   363  								Delay: &valideDelay,
   364  							},
   365  						},
   366  					},
   367  					execute: func(chaos *HTTPChaos) error {
   368  						_, err := chaos.ValidateCreate()
   369  						return err
   370  					},
   371  					expect: "ok",
   372  				},
   373  				{
   374  					name: "invalid delay",
   375  					chaos: HTTPChaos{
   376  						ObjectMeta: metav1.ObjectMeta{
   377  							Namespace: metav1.NamespaceDefault,
   378  							Name:      "foo20",
   379  						},
   380  						Spec: HTTPChaosSpec{
   381  							Port:   80,
   382  							Target: PodHttpRequest,
   383  							PodHttpChaosActions: PodHttpChaosActions{
   384  								Delay: &errorDelay,
   385  							},
   386  						},
   387  					},
   388  					execute: func(chaos *HTTPChaos) error {
   389  						_, err := chaos.ValidateCreate()
   390  						return err
   391  					},
   392  					expect: "error",
   393  				},
   394  			}
   395  
   396  			for _, tc := range tcs {
   397  				err := tc.execute(&tc.chaos)
   398  				if tc.expect == "error" {
   399  					Expect(err).To(HaveOccurred())
   400  				} else {
   401  					Expect(err).NotTo(HaveOccurred())
   402  				}
   403  			}
   404  		})
   405  	})
   406  })
   407