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("schedule_webhook", func() {
25 Context("webhook.Validator of schedule", func() {
26 It("Validate", func() {
27
28 type TestCase struct {
29 name string
30 schedule Schedule
31 execute func(schedule *Schedule) error
32 expect string
33 }
34 tcs := []TestCase{
35 {
36 name: "validation for normal chaos",
37 schedule: Schedule{
38 ObjectMeta: metav1.ObjectMeta{
39 Namespace: metav1.NamespaceDefault,
40 Name: "foo1",
41 },
42 Spec: ScheduleSpec{
43 ScheduleItem: ScheduleItem{EmbedChaos: EmbedChaos{PodChaos: &PodChaosSpec{
44 Action: ContainerKillAction,
45 }}},
46 Type: ScheduleTypePodChaos,
47 Schedule: "@every 5s",
48 },
49 },
50 execute: func(schedule *Schedule) error {
51 _, err := schedule.ValidateCreate()
52 return err
53 },
54 expect: "error",
55 },
56 {
57 name: "validation for schedule",
58 schedule: Schedule{
59 ObjectMeta: metav1.ObjectMeta{
60 Namespace: metav1.NamespaceDefault,
61 Name: "foo2",
62 },
63 Spec: ScheduleSpec{
64 ScheduleItem: ScheduleItem{EmbedChaos: EmbedChaos{PodChaos: &PodChaosSpec{}}},
65 Type: ScheduleTypePodChaos,
66 Schedule: "@every -5s",
67 },
68 },
69 execute: func(schedule *Schedule) error {
70 _, err := schedule.ValidateCreate()
71 return err
72 },
73 expect: "",
74 },
75 {
76 name: "validation for workflow",
77 schedule: Schedule{
78 ObjectMeta: metav1.ObjectMeta{
79 Namespace: metav1.NamespaceDefault,
80 Name: "foo3",
81 },
82 Spec: ScheduleSpec{
83 ScheduleItem: ScheduleItem{Workflow: &WorkflowSpec{}},
84 Type: ScheduleTypeWorkflow,
85 Schedule: "@every 5s",
86 },
87 },
88 execute: func(schedule *Schedule) error {
89 _, err := schedule.ValidateCreate()
90 return err
91 },
92 expect: "",
93 },
94 {
95 name: "validation for cron with second",
96 schedule: Schedule{
97 ObjectMeta: metav1.ObjectMeta{
98 Namespace: metav1.NamespaceDefault,
99 Name: "foo3",
100 },
101 Spec: ScheduleSpec{
102 ScheduleItem: ScheduleItem{Workflow: &WorkflowSpec{}},
103 Type: ScheduleTypeWorkflow,
104 Schedule: "*/1 * * * * *",
105 },
106 },
107 execute: func(schedule *Schedule) error {
108 _, err := schedule.ValidateCreate()
109 return err
110 },
111 expect: "",
112 },
113 }
114
115 for _, tc := range tcs {
116 err := tc.execute(&tc.schedule)
117 if tc.expect == "error" {
118 Expect(err).To(HaveOccurred())
119 } else {
120 Expect(err).NotTo(HaveOccurred())
121 }
122 }
123 })
124 })
125
126 Context("webhook.Default of schedule", func() {
127 s := Schedule{
128 ObjectMeta: metav1.ObjectMeta{
129 Namespace: metav1.NamespaceDefault,
130 Name: "foo3",
131 },
132 Spec: ScheduleSpec{
133 ScheduleItem: ScheduleItem{Workflow: &WorkflowSpec{}},
134 Type: ScheduleTypeWorkflow,
135 Schedule: "*/1 * * * * *",
136 },
137 }
138 s.Default()
139 Expect(s.Spec.ConcurrencyPolicy).To(Equal(ForbidConcurrent))
140 })
141 })
142