|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "strings" |
5 | 6 | "testing" |
6 | 7 |
|
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" |
8 | 10 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
9 | 11 | ) |
10 | 12 |
|
@@ -183,3 +185,68 @@ func TestVaultJobMode(t *testing.T) { |
183 | 185 | }) |
184 | 186 | } |
185 | 187 | } |
| 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