...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package v1alpha1
17
18 import (
19 . "github.com/onsi/ginkgo/v2"
20 . "github.com/onsi/gomega"
21 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22 )
23
24 var _ = Describe("podchaos_webhook", func() {
25 Context("Defaulter", func() {
26 It("set default namespace selector", func() {
27 podchaos := &PodChaos{
28 ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault},
29 }
30 podchaos.Default()
31 Expect(podchaos.Spec.Selector.Namespaces[0]).To(Equal(metav1.NamespaceDefault))
32 })
33 })
34 Context("webhook.Validator of podchaos", func() {
35 It("Validate", func() {
36
37 type TestCase struct {
38 name string
39 chaos PodChaos
40 execute func(chaos *PodChaos) error
41 expect string
42 }
43 tcs := []TestCase{
44 {
45 name: "simple ValidateCreate for ContainerKillAction",
46 chaos: PodChaos{
47 ObjectMeta: metav1.ObjectMeta{
48 Namespace: metav1.NamespaceDefault,
49 Name: "foo1",
50 },
51 Spec: PodChaosSpec{
52 Action: ContainerKillAction,
53 },
54 },
55 execute: func(chaos *PodChaos) error {
56 _, err := chaos.ValidateCreate()
57 return err
58 },
59 expect: "error",
60 },
61 {
62 name: "simple ValidateDelete",
63 chaos: PodChaos{
64 ObjectMeta: metav1.ObjectMeta{
65 Namespace: metav1.NamespaceDefault,
66 Name: "foo3",
67 },
68 },
69 execute: func(chaos *PodChaos) error {
70 _, err := chaos.ValidateDelete()
71 return err
72 },
73 expect: "",
74 },
75 {
76 name: "validate the ContainerNames",
77 chaos: PodChaos{
78 ObjectMeta: metav1.ObjectMeta{
79 Namespace: metav1.NamespaceDefault,
80 Name: "foo7",
81 },
82 Spec: PodChaosSpec{
83 Action: ContainerKillAction,
84 },
85 },
86 execute: func(chaos *PodChaos) error {
87 _, err := chaos.ValidateCreate()
88 return err
89 },
90 expect: "error",
91 },
92 }
93
94 for _, tc := range tcs {
95 err := tc.execute(&tc.chaos)
96 if tc.expect == "error" {
97 Expect(err).To(HaveOccurred())
98 } else {
99 Expect(err).NotTo(HaveOccurred())
100 }
101 }
102 })
103 })
104 })
105