-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalues_getting_hook_test.go
More file actions
111 lines (90 loc) · 3.94 KB
/
Copy pathvalues_getting_hook_test.go
File metadata and controls
111 lines (90 loc) · 3.94 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
package hookinfolder_test
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
"github.com/deckhouse/deckhouse/pkg/log"
"github.com/deckhouse/module-sdk/pkg"
"github.com/deckhouse/module-sdk/testing/helpers"
"github.com/deckhouse/module-sdk/testing/mock"
subfolder "example-module/subfolder"
)
// The values hook reads a few paths and writes one. Where possible we
// drive it with a real PatchableValues store (via helpers.NewValuesFromJSON);
// only the deliberately error-injecting cases keep the mock.
func TestHandlerHookValues_HappyPath(t *testing.T) {
values := helpers.NewValuesFromJSON(`{
"some": {
"path": {
"to": {
"field": {
"someInt": 1,
"array": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
}
}
}
}
}`)
in := helpers.NewInputBuilder(t).WithValues(values).Build()
require.NoError(t, subfolder.HandlerHookValues(context.Background(), in))
patches := in.Values.GetPatches()
require.NotEmpty(t, patches)
var setStr, removeOp bool
for _, p := range patches {
if p.Op == "add" && p.Path == "/some/path/to/field/str" {
setStr = true
}
if p.Op == "remove" && p.Path == "/some/path/to/field" {
removeOp = true
}
}
assert.True(t, setStr, "expected Set on .str path")
assert.True(t, removeOp, "expected Remove on .field path")
}
// The remaining cases use the typed mock since they need to inject
// behaviour the real PatchableValues cannot reproduce easily (failing
// ArrayCount, non-float GetRaw value, GetOk returning false on an
// existing path).
func TestHandlerHookValues_GetOkReturnsFalse(t *testing.T) {
values := mock.NewOutputPatchableValuesCollectorMock(t)
values.GetMock.When("some.path.to.field").Then(gjson.Result{Str: "str-value"})
values.GetOkMock.When("some.path.to.field").Then(gjson.Result{Str: "str-value"}, false)
values.ExistsMock.When("some.path.to.field.str").Then(false)
values.SetMock.When("some.path.to.field.str", "some_string")
values.RemoveMock.When("some.path.to.field")
values.ArrayCountMock.When("some.path.to.field.array").Then(10, nil)
in := &pkg.HookInput{Values: values, Logger: log.NewNop()}
require.NoError(t, subfolder.HandlerHookValues(context.Background(), in))
}
func TestHandlerHookValues_GetRawNotFloat(t *testing.T) {
values := mock.NewOutputPatchableValuesCollectorMock(t)
values.GetMock.When("some.path.to.field").Then(gjson.Result{Str: "str-value"})
values.GetOkMock.When("some.path.to.field").Then(gjson.Result{Str: "str-value"}, true)
values.GetPatchesMock.Return(nil)
values.GetRawMock.When("some.path.to.field.someInt").Then("not-number")
values.ExistsMock.When("some.path.to.field.str").Then(false)
values.SetMock.When("some.path.to.field.str", "some_string")
values.RemoveMock.When("some.path.to.field")
values.ArrayCountMock.When("some.path.to.field.array").Then(10, nil)
in := &pkg.HookInput{Values: values, Logger: log.NewNop()}
require.NoError(t, subfolder.HandlerHookValues(context.Background(), in))
}
func TestHandlerHookValues_ArrayCountReturnsError(t *testing.T) {
wantErr := errors.New("boom")
values := mock.NewOutputPatchableValuesCollectorMock(t)
values.GetMock.When("some.path.to.field").Then(gjson.Result{Str: "str-value"})
values.GetOkMock.When("some.path.to.field").Then(gjson.Result{Str: "str-value"}, true)
values.GetPatchesMock.Return(nil)
values.GetRawMock.When("some.path.to.field.someInt").Then(float64(1))
values.ExistsMock.When("some.path.to.field.str").Then(false)
values.SetMock.When("some.path.to.field.str", "some_string")
values.RemoveMock.When("some.path.to.field")
values.ArrayCountMock.When("some.path.to.field.array").Then(0, wantErr)
in := &pkg.HookInput{Values: values, Logger: log.NewNop()}
err := subfolder.HandlerHookValues(context.Background(), in)
require.Error(t, err)
assert.ErrorIs(t, err, wantErr)
}