Skip to content

Commit 33e6f9f

Browse files
committed
feat(expand CRDs): Added unit tests
1 parent 86edf36 commit 33e6f9f

File tree

2 files changed

+74
-15
lines changed

2 files changed

+74
-15
lines changed

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,14 @@ type Container struct {
3939
}
4040

4141
/*
42-
Check if Container.Lifecycle.PreStop is valid.
43-
This is to avoid mishandling incomplete inputs like the below:
44-
4542
https://pkg.go.dev/k8s.io/api/core/v1#LifecycleHandler
46-
{
47-
48-
"Lifecycle": {
49-
"PostStart": null,
50-
"PreStop": {
51-
"Exec": null, # <----- Missing Command!!
52-
"HTTPGet": null,
53-
"TCPSocket": null
54-
}
55-
}
43+
Check if Container.Lifecycle.PreStop is valid. This is to avoid mishandling incomplete inputs like the below:
5644
57-
}
45+
{ "Lifecycle": {
46+
"PostStart": null,
47+
"PreStop": {
48+
"Exec": null, # <----- Missing Command!!
49+
"HTTPGet": null,"TCPSocket": null}}}
5850
*/
5951
func (c Container) HasValidPreStop() bool {
6052
return c.Lifecycle.PreStop != nil &&

vault_test.go

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package main
22

33
import (
4+
"fmt"
45
"strings"
56
"testing"
67

7-
"k8s.io/api/core/v1"
8+
"github.com/uswitch/vault-webhook/pkg/apis/vaultwebhook.uswitch.com/v1alpha1"
9+
v1 "k8s.io/api/core/v1"
810
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
911
)
1012

@@ -183,3 +185,68 @@ func TestVaultJobMode(t *testing.T) {
183185
})
184186
}
185187
}
188+
189+
// Can we add a preStop hook to the vault container?
190+
func TestAddLifecyclePreStopHook(t *testing.T) {
191+
192+
// Define test cases
193+
var tests = []struct {
194+
scenario string
195+
lifecycleObj v1alpha1.Container
196+
answer bool
197+
}{
198+
{
199+
scenario: "Test passing a complete lifecyle config",
200+
lifecycleObj: v1alpha1.Container{
201+
Lifecycle: v1.Lifecycle{
202+
PreStop: &v1.LifecycleHandler{
203+
Exec: &v1.ExecAction{
204+
Command: []string{"echo", "hello"},
205+
},
206+
},
207+
},
208+
},
209+
answer: true,
210+
},
211+
{
212+
scenario: "Test passing an incomplete lifecycle config",
213+
lifecycleObj: v1alpha1.Container{
214+
Lifecycle: v1.Lifecycle{
215+
PreStop: &v1.LifecycleHandler{
216+
Exec: nil,
217+
},
218+
},
219+
},
220+
answer: false,
221+
},
222+
{
223+
// v1alpha1.Container{}, comes from corev1.Container{} and this ALWAYS have a c.Lifecycle object. The latter, always has pointers to PostStart and PreStop handlers ( but no further down the struct since they are pointers )
224+
// if our dcb input does not specify a container object, the received input will look like this: {Lifecycle:{PostStart:nil PreStop:nil}}
225+
scenario: "Test passing no lifecycle config",
226+
lifecycleObj: v1alpha1.Container{
227+
Lifecycle: v1.Lifecycle{
228+
PreStop: nil,
229+
},
230+
},
231+
answer: false,
232+
},
233+
}
234+
235+
// Run tests
236+
for _, tt := range tests {
237+
// t.Run enables running "subtests", one for each table entry. These are shown separately when executing `go test -v`.
238+
vaultContainer := v1.Container{} // Define a Vault sidecar Container
239+
testname := fmt.Sprintf("%v", tt.scenario)
240+
t.Run(testname, func(t *testing.T) {
241+
ans := addLifecycleHook(vaultContainer, tt.lifecycleObj)
242+
243+
//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
245+
246+
if isValid != tt.answer {
247+
t.Errorf("got %v, want %v", isValid, tt.answer)
248+
}
249+
})
250+
}
251+
252+
}

0 commit comments

Comments
 (0)