-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatch_hook_test.go
More file actions
120 lines (103 loc) · 3.7 KB
/
Copy pathpatch_hook_test.go
File metadata and controls
120 lines (103 loc) · 3.7 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package hookinfolder_test
import (
"bytes"
"context"
"strings"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"github.com/deckhouse/deckhouse/pkg/log"
"github.com/deckhouse/module-sdk/pkg"
"github.com/deckhouse/module-sdk/testing/mock"
subfolder "example-module/subfolder"
)
var _ = Describe("patch hook", func() {
Context("HandlerHookPatch function", func() {
var (
patchCollector *mock.PatchCollectorMock
buf *bytes.Buffer
input *pkg.HookInput
)
BeforeEach(func() {
patchCollector = mock.NewPatchCollectorMock(GinkgoT())
buf = bytes.NewBuffer([]byte{})
input = &pkg.HookInput{
PatchCollector: patchCollector,
Logger: log.NewLogger(log.Options{
Level: log.LevelDebug.Level(),
Output: buf,
TimeFunc: func(_ time.Time) time.Time {
parsedTime, err := time.Parse(time.DateTime, "2006-01-02 15:04:05")
Expect(err).ShouldNot(HaveOccurred())
return parsedTime
},
}),
}
})
It("logs hello message and executes patch collector operations", func() {
// Set expectations for Create
patchCollector.CreateMock.Set(func(obj any) {
pod, ok := obj.(*corev1.Pod)
Expect(ok).To(BeTrue())
Expect(pod.Name).To(Equal("my-first-pod"))
Expect(pod.Namespace).To(Equal("default"))
Expect(pod.Status.Phase).To(Equal(corev1.PodRunning))
})
// Set expectations for CreateOrUpdate
patchCollector.CreateOrUpdateMock.Set(func(obj any) {
pod, ok := obj.(*corev1.Pod)
Expect(ok).To(BeTrue())
Expect(pod.Name).To(Equal("my-second-pod"))
Expect(pod.Namespace).To(Equal("default"))
Expect(pod.Status.Phase).To(Equal(corev1.PodRunning))
})
// Set expectations for CreateIfNotExists
patchCollector.CreateIfNotExistsMock.Set(func(obj any) {
pod, ok := obj.(*corev1.Pod)
Expect(ok).To(BeTrue())
Expect(pod.Name).To(Equal("my-third-pod"))
Expect(pod.Namespace).To(Equal("default"))
Expect(pod.Status.Phase).To(Equal(corev1.PodRunning))
})
// Set expectations for Delete
patchCollector.DeleteMock.Set(func(apiVersion, kind, namespace, name string) {
Expect(apiVersion).To(Equal("v1"))
Expect(kind).To(Equal("Pod"))
Expect(namespace).To(Equal("default"))
Expect(name).To(Equal("my-first-pod"))
})
// Set expectations for DeleteInBackground
patchCollector.DeleteInBackgroundMock.Set(func(apiVersion, kind, namespace, name string) {
Expect(apiVersion).To(Equal("v1"))
Expect(kind).To(Equal("Pod"))
Expect(namespace).To(Equal("default"))
Expect(name).To(Equal("my-second-pod"))
})
// Set expectations for DeleteNonCascading
patchCollector.DeleteNonCascadingMock.Set(func(apiVersion, kind, namespace, name string) {
Expect(apiVersion).To(Equal("v1"))
Expect(kind).To(Equal("Pod"))
Expect(namespace).To(Equal("default"))
Expect(name).To(Equal("my-third-pod"))
})
// Set expectations for PatchWithMerge
patchCollector.PatchWithMergeMock.Set(func(mergePatch any, apiVersion, kind, namespace, name string, opts ...pkg.PatchCollectorOption) {
patchMap, ok := mergePatch.(map[string]any)
Expect(ok).To(BeTrue())
Expect(patchMap).To(HaveKeyWithValue("/status", "newStatus"))
Expect(apiVersion).To(Equal("v1"))
Expect(kind).To(Equal("Pod"))
Expect(namespace).To(Equal("default"))
Expect(name).To(Equal("my-third-pod"))
Expect(len(opts)).To(Equal(2))
})
// Execute the handler function
err := subfolder.HandlerHookPatch(context.Background(), input)
Expect(err).ShouldNot(HaveOccurred())
// Verify log messages
logs := strings.Split(buf.String(), "\n")
Expect(logs[0]).To(ContainSubstring(`"level":"info","msg":"hello from patch hook"`))
})
})
})