@@ -18,29 +18,146 @@ package v1
18
18
19
19
import (
20
20
. "github.com/onsi/ginkgo/v2"
21
+ . "github.com/onsi/gomega"
22
+ // TODO (user): Add any additional imports if needed
21
23
)
22
24
23
25
var _ = Describe ("CronJob Webhook" , func () {
26
+ var (
27
+ obj * CronJob
28
+ oldObj * CronJob
29
+ validator CronJobCustomValidator
30
+ )
31
+
32
+ BeforeEach (func () {
33
+ obj = & CronJob {
34
+ Spec : CronJobSpec {
35
+ Schedule : "*/5 * * * *" ,
36
+ ConcurrencyPolicy : AllowConcurrent ,
37
+ SuccessfulJobsHistoryLimit : new (int32 ),
38
+ FailedJobsHistoryLimit : new (int32 ),
39
+ },
40
+ }
41
+ * obj .Spec .SuccessfulJobsHistoryLimit = 3
42
+ * obj .Spec .FailedJobsHistoryLimit = 1
43
+
44
+ oldObj = & CronJob {
45
+ Spec : CronJobSpec {
46
+ Schedule : "*/5 * * * *" ,
47
+ ConcurrencyPolicy : AllowConcurrent ,
48
+ SuccessfulJobsHistoryLimit : new (int32 ),
49
+ FailedJobsHistoryLimit : new (int32 ),
50
+ },
51
+ }
52
+ * oldObj .Spec .SuccessfulJobsHistoryLimit = 3
53
+ * oldObj .Spec .FailedJobsHistoryLimit = 1
54
+
55
+ validator = CronJobCustomValidator {}
56
+
57
+ Expect (obj ).NotTo (BeNil (), "Expected obj to be initialized" )
58
+ Expect (oldObj ).NotTo (BeNil (), "Expected oldObj to be initialized" )
59
+ })
24
60
25
- Context ("When creating CronJob under Defaulting Webhook" , func () {
26
- It ("Should fill in the default value if a required field is empty" , func () {
61
+ AfterEach (func () {
62
+ // TODO (user): Add any teardown logic common to all tests
63
+ })
27
64
28
- // TODO(user): Add your logic here
65
+ Context ("When creating CronJob under Defaulting Webhook" , func () {
66
+ It ("Should apply defaults when a required field is empty" , func () {
67
+ By ("simulating a scenario where defaults should be applied" )
68
+ obj .Spec .ConcurrencyPolicy = "" // This should default to AllowConcurrent
69
+ obj .Spec .Suspend = nil // This should default to false
70
+ obj .Spec .SuccessfulJobsHistoryLimit = nil // This should default to 3
71
+ obj .Spec .FailedJobsHistoryLimit = nil // This should default to 1
72
+
73
+ By ("calling the Default method to apply defaults" )
74
+ obj .Default ()
75
+
76
+ By ("checking that the default values are set" )
77
+ Expect (obj .Spec .ConcurrencyPolicy ).To (Equal (AllowConcurrent ), "Expected ConcurrencyPolicy to default to AllowConcurrent" )
78
+ Expect (* obj .Spec .Suspend ).To (BeFalse (), "Expected Suspend to default to false" )
79
+ Expect (* obj .Spec .SuccessfulJobsHistoryLimit ).To (Equal (int32 (3 )), "Expected SuccessfulJobsHistoryLimit to default to 3" )
80
+ Expect (* obj .Spec .FailedJobsHistoryLimit ).To (Equal (int32 (1 )), "Expected FailedJobsHistoryLimit to default to 1" )
81
+ })
29
82
83
+ It ("Should not overwrite fields that are already set" , func () {
84
+ By ("setting fields that would normally get a default" )
85
+ obj .Spec .ConcurrencyPolicy = ForbidConcurrent
86
+ obj .Spec .Suspend = new (bool )
87
+ * obj .Spec .Suspend = true
88
+ obj .Spec .SuccessfulJobsHistoryLimit = new (int32 )
89
+ * obj .Spec .SuccessfulJobsHistoryLimit = 5
90
+ obj .Spec .FailedJobsHistoryLimit = new (int32 )
91
+ * obj .Spec .FailedJobsHistoryLimit = 2
92
+
93
+ By ("calling the Default method to apply defaults" )
94
+ obj .Default ()
95
+
96
+ By ("checking that the fields were not overwritten" )
97
+ Expect (obj .Spec .ConcurrencyPolicy ).To (Equal (ForbidConcurrent ), "Expected ConcurrencyPolicy to retain its set value" )
98
+ Expect (* obj .Spec .Suspend ).To (BeTrue (), "Expected Suspend to retain its set value" )
99
+ Expect (* obj .Spec .SuccessfulJobsHistoryLimit ).To (Equal (int32 (5 )), "Expected SuccessfulJobsHistoryLimit to retain its set value" )
100
+ Expect (* obj .Spec .FailedJobsHistoryLimit ).To (Equal (int32 (2 )), "Expected FailedJobsHistoryLimit to retain its set value" )
30
101
})
31
102
})
32
103
33
- Context ("When creating CronJob under Validating Webhook" , func () {
34
- It ("Should deny if a required field is empty" , func () {
104
+ Context ("When creating or updating CronJob under Validating Webhook" , func () {
105
+ It ("Should deny creation if the name is too long" , func () {
106
+ obj .ObjectMeta .Name = "this-name-is-way-too-long-and-should-fail-validation-because-it-is-way-too-long"
107
+ warnings , err := validator .ValidateCreate (ctx , obj )
108
+ Expect (err ).To (HaveOccurred (), "Expected name validation to fail for a too-long name" )
109
+ Expect (warnings ).To (BeNil ())
110
+ Expect (err .Error ()).To (ContainSubstring ("must be no more than 52 characters" ))
111
+ })
112
+
113
+ It ("Should admit creation if the name is valid" , func () {
114
+ obj .ObjectMeta .Name = "valid-cronjob-name"
115
+ warnings , err := validator .ValidateCreate (ctx , obj )
116
+ Expect (err ).NotTo (HaveOccurred (), "Expected name validation to pass for a valid name" )
117
+ Expect (warnings ).To (BeNil ())
118
+ })
119
+
120
+ It ("Should deny creation if the schedule is invalid" , func () {
121
+ obj .Spec .Schedule = "invalid-cron-schedule"
122
+ warnings , err := validator .ValidateCreate (ctx , obj )
123
+ Expect (err ).To (HaveOccurred (), "Expected spec validation to fail for an invalid schedule" )
124
+ Expect (warnings ).To (BeNil ())
125
+ Expect (err .Error ()).To (ContainSubstring ("Expected exactly 5 fields, found 1: invalid-cron-schedule" ))
126
+ })
127
+
128
+ It ("Should admit creation if the schedule is valid" , func () {
129
+ obj .Spec .Schedule = "*/5 * * * *"
130
+ warnings , err := validator .ValidateCreate (ctx , obj )
131
+ Expect (err ).NotTo (HaveOccurred (), "Expected spec validation to pass for a valid schedule" )
132
+ Expect (warnings ).To (BeNil ())
133
+ })
134
+
135
+ It ("Should deny update if both name and spec are invalid" , func () {
136
+ oldObj .ObjectMeta .Name = "valid-cronjob-name"
137
+ oldObj .Spec .Schedule = "*/5 * * * *"
35
138
36
- // TODO(user): Add your logic here
139
+ By ("simulating an update" )
140
+ obj .ObjectMeta .Name = "this-name-is-way-too-long-and-should-fail-validation-because-it-is-way-too-long"
141
+ obj .Spec .Schedule = "invalid-cron-schedule"
37
142
143
+ By ("validating an update" )
144
+ warnings , err := validator .ValidateUpdate (ctx , oldObj , obj )
145
+ Expect (err ).To (HaveOccurred (), "Expected validation to fail for both name and spec" )
146
+ Expect (warnings ).To (BeNil ())
38
147
})
39
148
40
- It ("Should admit if all required fields are provided" , func () {
149
+ It ("Should admit update if both name and spec are valid" , func () {
150
+ oldObj .ObjectMeta .Name = "valid-cronjob-name"
151
+ oldObj .Spec .Schedule = "*/5 * * * *"
41
152
42
- // TODO(user): Add your logic here
153
+ By ("simulating an update" )
154
+ obj .ObjectMeta .Name = "valid-cronjob-name-updated"
155
+ obj .Spec .Schedule = "0 0 * * *"
43
156
157
+ By ("validating an update" )
158
+ warnings , err := validator .ValidateUpdate (ctx , oldObj , obj )
159
+ Expect (err ).NotTo (HaveOccurred (), "Expected validation to pass for a valid update" )
160
+ Expect (warnings ).To (BeNil ())
44
161
})
45
162
})
46
163
0 commit comments