...
1
2
3
4
5
6
7
8
9
10
11
12
13
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