-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatch_hook_test.go
More file actions
70 lines (57 loc) · 2.07 KB
/
Copy pathpatch_hook_test.go
File metadata and controls
70 lines (57 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package hookinfolder_test
import (
"context"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
"github.com/deckhouse/module-sdk/testing/helpers"
subfolder "example-module/subfolder"
)
// TestHandlerHookPatch_RecordsExpectedOperations verifies that the patch
// hook issues the full sequence of Create/Delete/Patch operations against
// its PatchCollector.
//
// The RecordingPatchCollector lets us assert on each call without the
// minimock boilerplate the original Ginkgo test required.
func TestHandlerHookPatch_RecordsExpectedOperations(t *testing.T) {
b := helpers.NewInputBuilder(t).
WithRecordingPatchCollector().
WithCapturedLogger()
require.NoError(t, subfolder.HandlerHookPatch(context.Background(), b.Build()))
ops := b.RecordingPatchCollector().Recorded()
require.Len(t, ops, 7, "expected 3 creates, 3 deletes and 1 merge patch")
// Creates
assert.Equal(t, "Create", ops[0].Op)
assert.Equal(t, "my-first-pod", ops[0].Object.(*corev1.Pod).Name)
assert.Equal(t, "CreateOrUpdate", ops[1].Op)
assert.Equal(t, "my-second-pod", ops[1].Object.(*corev1.Pod).Name)
assert.Equal(t, "CreateIfNotExists", ops[2].Op)
assert.Equal(t, "my-third-pod", ops[2].Object.(*corev1.Pod).Name)
// Deletes
for i, expected := range []struct {
op string
name string
}{
{"Delete", "my-first-pod"},
{"DeleteInBackground", "my-second-pod"},
{"DeleteNonCascading", "my-third-pod"},
} {
op := ops[3+i]
assert.Equal(t, expected.op, op.Op)
assert.Equal(t, "v1", op.APIVersion)
assert.Equal(t, "Pod", op.Kind)
assert.Equal(t, "default", op.Namespace)
assert.Equal(t, expected.name, op.Name)
}
// Merge patch with options
mp := ops[6]
assert.Equal(t, "MergePatch", mp.Op)
assert.Equal(t, "my-third-pod", mp.Name)
assert.Equal(t, map[string]any{"/status": "newStatus"}, mp.Patch)
assert.Len(t, mp.Options, 2, "WithSubresource + WithIgnoreMissingObject expected")
// Logger asserts
logs := strings.Split(b.LogBuffer().String(), "\n")
assert.Contains(t, logs[0], `"msg":"hello from patch hook"`)
}