Skip to content

Commit 4540187

Browse files
authored
feat(recipe): Add support for string in pathcheck (#142)
Signed-off-by: Amit Bhatt <[email protected]>
1 parent 828c1c7 commit 4540187

File tree

3 files changed

+181
-3
lines changed

3 files changed

+181
-3
lines changed

pkg/recipe/path_check.go

+49-3
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,58 @@ func (pc *PathChecking) assertValueFloat64(obj *unstructured.Unstructured) (bool
203203
return true, nil
204204
}
205205

206+
func (pc *PathChecking) assertValueString(obj *unstructured.Unstructured) (bool, error) {
207+
got, found, err := unstructured.NestedString(
208+
obj.UnstructuredContent(),
209+
strings.Split(pc.PathCheck.Path, ".")...,
210+
)
211+
if err != nil {
212+
return false, err
213+
}
214+
if !found {
215+
return false, errors.Errorf(
216+
"PathCheck failed: Path %q not found: TaskName %s",
217+
pc.PathCheck.Path,
218+
pc.TaskName,
219+
)
220+
}
221+
val := pc.PathCheck.Value
222+
expected, ok := val.(string)
223+
if !ok {
224+
return false, errors.Errorf(
225+
"PathCheck failed: Value %v is of type %T, expected string: TaskName %s",
226+
val,
227+
val,
228+
pc.TaskName,
229+
)
230+
}
231+
pc.result.Verbose = fmt.Sprintf(
232+
"Expected value %s got %s",
233+
expected,
234+
got,
235+
)
236+
if pc.retryIfValueEquals && got == expected {
237+
return false, nil
238+
}
239+
if pc.retryIfValueNotEquals && got != expected {
240+
return false, nil
241+
}
242+
// returning true will no longer retry
243+
return true, nil
244+
}
245+
206246
func (pc *PathChecking) assertValue(obj *unstructured.Unstructured) (bool, error) {
207-
if pc.PathCheck.DataType == types.PathValueDataTypeInt64 {
247+
248+
switch pc.PathCheck.DataType {
249+
case types.PathValueDataTypeInt64:
208250
return pc.assertValueInt64(obj)
251+
252+
case types.PathValueDataTypeFloat64:
253+
return pc.assertValueFloat64(obj)
254+
255+
default:
256+
return pc.assertValueString(obj)
209257
}
210-
// currently float & int64 are supported data types
211-
return pc.assertValueFloat64(obj)
212258
}
213259

214260
func (pc *PathChecking) assertPath(obj *unstructured.Unstructured) (bool, error) {

pkg/recipe/path_check_test.go

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package recipe
2+
3+
import (
4+
"testing"
5+
6+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
7+
types "mayadata.io/d-operators/types/recipe"
8+
)
9+
10+
func TestPathCheckingAssertValueString(t *testing.T) {
11+
var tests = map[string]struct {
12+
State *unstructured.Unstructured
13+
TaskName string
14+
PathCheck string
15+
Path string
16+
Value string
17+
retryIfValueEquals bool
18+
retryIfValueNotEquals bool
19+
IsError bool
20+
ExpectedAssert bool
21+
}{
22+
"assert ipaddress None == None": {
23+
State: &unstructured.Unstructured{
24+
Object: map[string]interface{}{
25+
"apiVersion": "v1",
26+
"kind": "Service",
27+
"metadata": map[string]interface{}{
28+
"name": "test",
29+
"namespace": "default",
30+
},
31+
"spec": map[string]interface{}{
32+
"ipAddress": "None",
33+
},
34+
},
35+
},
36+
Path: "spec.ipAddress",
37+
Value: "None",
38+
retryIfValueNotEquals: true,
39+
ExpectedAssert: true,
40+
},
41+
"assert ipaddress ip != 12.123.12.11": {
42+
State: &unstructured.Unstructured{
43+
Object: map[string]interface{}{
44+
"apiVersion": "v1",
45+
"kind": "Service",
46+
"metadata": map[string]interface{}{
47+
"name": "test",
48+
"namespace": "default",
49+
},
50+
"spec": map[string]interface{}{
51+
"ipAddress": "12.123.12.11",
52+
},
53+
},
54+
},
55+
Path: "spec.ipAddress",
56+
Value: "None",
57+
retryIfValueEquals: true,
58+
ExpectedAssert: true,
59+
},
60+
"assert ipaddress ip != ": {
61+
State: &unstructured.Unstructured{
62+
Object: map[string]interface{}{
63+
"apiVersion": "v1",
64+
"kind": "Service",
65+
"metadata": map[string]interface{}{
66+
"name": "test",
67+
"namespace": "default",
68+
},
69+
"spec": map[string]interface{}{
70+
"ipAddress": "12.123.12.11",
71+
},
72+
},
73+
},
74+
Path: "spec.ipAddress",
75+
Value: "",
76+
retryIfValueEquals: true,
77+
ExpectedAssert: true,
78+
},
79+
"assert ipaddress Nil != None": {
80+
State: &unstructured.Unstructured{
81+
Object: map[string]interface{}{
82+
"apiVersion": "v1",
83+
"kind": "Service",
84+
"metadata": map[string]interface{}{
85+
"name": "test",
86+
"namespace": "default",
87+
},
88+
"spec": map[string]interface{}{
89+
"ipAddress": "None",
90+
},
91+
},
92+
},
93+
Path: "spec.ipAddress",
94+
Value: "Nil",
95+
retryIfValueNotEquals: true,
96+
ExpectedAssert: false,
97+
},
98+
}
99+
for scenario, tObj := range tests {
100+
scenario := scenario
101+
tObj := tObj
102+
t.Run(scenario, func(t *testing.T) {
103+
pc := &PathChecking{
104+
PathCheck: types.PathCheck{
105+
Path: tObj.Path,
106+
Value: tObj.Value,
107+
},
108+
retryIfValueEquals: tObj.retryIfValueEquals,
109+
retryIfValueNotEquals: tObj.retryIfValueNotEquals,
110+
result: &types.PathCheckResult{},
111+
}
112+
got, err := pc.assertValueString(tObj.State)
113+
if tObj.IsError && err == nil {
114+
t.Fatalf("Expected error got none")
115+
}
116+
if !tObj.IsError && err != nil {
117+
t.Fatalf("Expected no error got %s", err.Error())
118+
}
119+
if tObj.IsError {
120+
return
121+
}
122+
if got != tObj.ExpectedAssert {
123+
t.Fatalf("Expected assert = %t got %t", tObj.ExpectedAssert, got)
124+
}
125+
126+
})
127+
}
128+
}

types/recipe/patch_check.go

+4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ const (
8484
// PathValueDataTypeFloat64 expects path's value with float64
8585
// as its data type
8686
PathValueDataTypeFloat64 PathValueDataType = "float64"
87+
88+
// PathValueDataTypeString expects path's value with string
89+
// as its data type
90+
PathValueDataTypeString PathValueDataType = "string"
8791
)
8892

8993
// PathCheck verifies expected field value against

0 commit comments

Comments
 (0)