...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/physical_machine_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  	"strings"
    20  
    21  	. "github.com/onsi/ginkgo/v2"
    22  	. "github.com/onsi/gomega"
    23  )
    24  
    25  var _ = Describe("physicalmachine_webhook", func() {
    26  	Context("webhook.Defaultor of physicalmachine", func() {
    27  		It("Default", func() {
    28  			physicalMachine := &PhysicalMachine{
    29  				Spec: PhysicalMachineSpec{
    30  					Address: "123.123.123.123:123",
    31  				},
    32  			}
    33  			physicalMachine.Default()
    34  			Expect(physicalMachine.Spec.Address).To(BeEquivalentTo("http://123.123.123.123:123"))
    35  		})
    36  	})
    37  	Context("webhook.Validator of physicalmachine", func() {
    38  		It("Validate", func() {
    39  			testCases := []struct {
    40  				physicalMachine PhysicalMachine
    41  				err             string
    42  			}{
    43  				{
    44  					PhysicalMachine{
    45  						Spec: PhysicalMachineSpec{
    46  							Address: "",
    47  						},
    48  					},
    49  					"the address is required",
    50  				}, {
    51  					PhysicalMachine{
    52  						Spec: PhysicalMachineSpec{
    53  							Address: "123",
    54  						},
    55  					},
    56  					"the address is invalid",
    57  				}, {
    58  					PhysicalMachine{
    59  						Spec: PhysicalMachineSpec{
    60  							Address: "http://123.123.123.123:123",
    61  						},
    62  					},
    63  					"",
    64  				},
    65  			}
    66  
    67  			for _, testCase := range testCases {
    68  				_, err := testCase.physicalMachine.ValidateCreate()
    69  				if len(testCase.err) != 0 {
    70  					Expect(err).To(HaveOccurred())
    71  					Expect(strings.Contains(err.Error(), testCase.err)).To(BeTrue())
    72  				} else {
    73  					Expect(err).ToNot(HaveOccurred())
    74  				}
    75  			}
    76  		})
    77  	})
    78  })
    79