Skip to content

Commit 89c380a

Browse files
committed
feat(lifecycle Sleep): Added unit tests
1 parent b93845d commit 89c380a

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

pkg/apis/vaultwebhook.uswitch.com/v1alpha1/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (c Container) HasValidPreStop() bool {
5555
return true
5656

5757
// is Sleep set correctly?
58-
} else if c.Lifecycle.PreStop.Sleep != nil && (c.Lifecycle.PreStop.Sleep.Seconds > 0) { // We do not like negative values here
58+
} else if c.Lifecycle.PreStop.Sleep != nil && c.Lifecycle.PreStop.Sleep.Seconds > 0 { // We do not like negative values here
5959
return true
6060

6161
// TODO: Handle HTTPGet and TCPSocket usecases

vault_test.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func TestAddLifecyclePreStopHook(t *testing.T) {
196196
answer bool
197197
}{
198198
{
199-
scenario: "Test passing a complete lifecyle config",
199+
scenario: "Test passing a complete lifecyle config - Exec Command",
200200
lifecycleObj: v1alpha1.Container{
201201
Lifecycle: v1.Lifecycle{
202202
PreStop: &v1.LifecycleHandler{
@@ -230,6 +230,32 @@ func TestAddLifecyclePreStopHook(t *testing.T) {
230230
},
231231
answer: false,
232232
},
233+
{
234+
scenario: "Test passing a complete lifecyle config - Sleep",
235+
lifecycleObj: v1alpha1.Container{
236+
Lifecycle: v1.Lifecycle{
237+
PreStop: &v1.LifecycleHandler{
238+
Sleep: &v1.SleepAction{
239+
Seconds: 10,
240+
},
241+
},
242+
},
243+
},
244+
answer: true,
245+
},
246+
{
247+
scenario: "Test passing an incorrect lifecyle config - Sleep",
248+
lifecycleObj: v1alpha1.Container{
249+
Lifecycle: v1.Lifecycle{
250+
PreStop: &v1.LifecycleHandler{
251+
Sleep: &v1.SleepAction{
252+
Seconds: -10,
253+
},
254+
},
255+
},
256+
},
257+
answer: false,
258+
},
233259
}
234260

235261
// Run tests
@@ -241,7 +267,16 @@ func TestAddLifecyclePreStopHook(t *testing.T) {
241267
ans := addLifecycleHook(vaultContainer, tt.lifecycleObj)
242268

243269
//log.Printf("%+v", ans)
244-
isValid := ans.Lifecycle != nil && ans.Lifecycle.PreStop != nil && ans.Lifecycle.PreStop.Exec != nil && len(ans.Lifecycle.PreStop.Exec.Command) > 0
270+
271+
// Is our function returning a container object with valid Lifecycle config?
272+
isValid := bool(false)
273+
if ans.Lifecycle != nil && ans.Lifecycle.PreStop != nil {
274+
if ans.Lifecycle.PreStop.Exec != nil && len(ans.Lifecycle.PreStop.Exec.Command) > 0 { // Check preStop Exec format
275+
isValid = true
276+
} else if ans.Lifecycle.PreStop.Sleep != nil && ans.Lifecycle.PreStop.Sleep.Seconds > 0 { // Check preStop Sleep format
277+
isValid = true
278+
}
279+
}
245280

246281
if isValid != tt.answer {
247282
t.Errorf("got %v, want %v", isValid, tt.answer)

0 commit comments

Comments
 (0)