...

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

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

     1  // Copyright 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  	"strings"
    20  
    21  	. "github.com/onsi/ginkgo/v2"
    22  	. "github.com/onsi/gomega"
    23  )
    24  
    25  var _ = Describe("statuscheck_webhook", func() {
    26  	Context("webhook.Defaultor of statuscheck", func() {
    27  		It("Default", func() {
    28  			statusCheck := &StatusCheck{}
    29  			statusCheck.Default()
    30  			Expect(statusCheck.Spec.Mode).To(Equal(StatusCheckSynchronous))
    31  		})
    32  	})
    33  	Context("webhook.Validator of statuscheck", func() {
    34  		It("Validate", func() {
    35  			type TestCase struct {
    36  				name        string
    37  				statusCheck StatusCheck
    38  				expect      string
    39  			}
    40  			tcs := []TestCase{
    41  				{
    42  					name: "simple Validate",
    43  					statusCheck: StatusCheck{
    44  						Spec: StatusCheckSpec{
    45  							Type: TypeHTTP,
    46  							EmbedStatusCheck: &EmbedStatusCheck{
    47  								HTTPStatusCheck: &HTTPStatusCheck{
    48  									RequestUrl: "http://1.1.1.1",
    49  									Criteria: HTTPCriteria{
    50  										StatusCode: "200",
    51  									},
    52  								},
    53  							},
    54  						},
    55  					},
    56  					expect: "",
    57  				},
    58  				{
    59  					name: "simple Validate with status code range",
    60  					statusCheck: StatusCheck{
    61  						Spec: StatusCheckSpec{
    62  							Type: TypeHTTP,
    63  							EmbedStatusCheck: &EmbedStatusCheck{
    64  								HTTPStatusCheck: &HTTPStatusCheck{
    65  									RequestUrl: "http://1.1.1.1",
    66  									Criteria: HTTPCriteria{
    67  										StatusCode: "200-400",
    68  									},
    69  								},
    70  							},
    71  						},
    72  					},
    73  					expect: "",
    74  				},
    75  				{
    76  					name: "unknown type",
    77  					statusCheck: StatusCheck{
    78  						Spec: StatusCheckSpec{
    79  							Type: "CMD",
    80  						},
    81  					},
    82  					expect: "unrecognized type",
    83  				},
    84  				{
    85  					name: "invalid request url",
    86  					statusCheck: StatusCheck{
    87  						Spec: StatusCheckSpec{
    88  							Type: TypeHTTP,
    89  							EmbedStatusCheck: &EmbedStatusCheck{
    90  								HTTPStatusCheck: &HTTPStatusCheck{
    91  									RequestUrl: "1.1.1.1",
    92  									Criteria: HTTPCriteria{
    93  										StatusCode: "-1",
    94  									},
    95  								},
    96  							},
    97  						},
    98  					},
    99  					expect: "invalid http request url",
   100  				},
   101  				{
   102  					name: "invalid status code",
   103  					statusCheck: StatusCheck{
   104  						Spec: StatusCheckSpec{
   105  							Type: TypeHTTP,
   106  							EmbedStatusCheck: &EmbedStatusCheck{
   107  								HTTPStatusCheck: &HTTPStatusCheck{
   108  									RequestUrl: "http://1.1.1.1",
   109  									Criteria: HTTPCriteria{
   110  										StatusCode: "-1",
   111  									},
   112  								},
   113  							},
   114  						},
   115  					},
   116  					expect: "invalid status code",
   117  				},
   118  				{
   119  					name: "invalid status code range",
   120  					statusCheck: StatusCheck{
   121  						Spec: StatusCheckSpec{
   122  							Type: TypeHTTP,
   123  							EmbedStatusCheck: &EmbedStatusCheck{
   124  								HTTPStatusCheck: &HTTPStatusCheck{
   125  									RequestUrl: "http://1.1.1.1",
   126  									Criteria: HTTPCriteria{
   127  										StatusCode: "200-x",
   128  									},
   129  								},
   130  							},
   131  						},
   132  					},
   133  					expect: "incorrect status code format",
   134  				},
   135  			}
   136  
   137  			for _, tc := range tcs {
   138  				_, err := tc.statusCheck.ValidateCreate()
   139  				if len(tc.expect) != 0 {
   140  					Expect(err).To(HaveOccurred())
   141  					Expect(strings.Contains(err.Error(), tc.expect)).To(BeTrue(), "expected error: %s, got: %s", tc.expect, err.Error())
   142  				} else {
   143  					Expect(err).ToNot(HaveOccurred())
   144  				}
   145  			}
   146  		})
   147  	})
   148  })
   149