Skip to content
This repository was archived by the owner on Dec 12, 2025. It is now read-only.

Commit 640b726

Browse files
committed
resolver, eqfilter: Support optional fields
A field like "description" can be missing at some interfaces, this make eqfilter to fail since it expected the the field is present. This change does not fail if the path does not exist, instead it just filters out the entry. Signed-off-by: Quique Llorente <[email protected]>
1 parent fca96a0 commit 640b726

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

nmpolicy/internal/resolver/filter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func isEqual(obtainedValue interface{}, desiredValue ast.Node) (bool, error) {
5656
func mapContainsValue(mapToFilter map[string]interface{}, filterKey string, expectedNode ast.Node) (interface{}, error) {
5757
obtainedValue, ok := mapToFilter[filterKey]
5858
if !ok {
59-
return nil, fmt.Errorf("cannot find key %s in %v", filterKey, mapToFilter)
59+
return nil, nil
6060
}
6161
valueIsEqual, err := isEqual(obtainedValue, expectedNode)
6262
if err != nil {

nmpolicy/internal/resolver/resolver_test.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ routes:
5151
table-id: 254
5252
interfaces:
5353
- name: eth1
54+
description: "1st ethernet interface"
5455
type: ethernet
5556
state: up
5657
ipv4:
@@ -107,7 +108,7 @@ func TestFilter(t *testing.T) {
107108
testFilterCaptureRefInvalidStateForPathMap(t)
108109
testFilterCaptureRefInvalidStateForPathSlice(t)
109110
testFilterInvalidTypeOnPath(t)
110-
testFilterInvalidPath(t)
111+
testFilterOptionalField(t)
111112
testFilterNonCaptureRefPathAtThirdArg(t)
112113
testReplaceCurrentState(t)
113114
testReplaceCapturedState(t)
@@ -214,6 +215,7 @@ specific-ipv4:
214215
state:
215216
interfaces:
216217
- name: eth1
218+
description: "1st ethernet interface"
217219
type: ethernet
218220
state: up
219221
ipv4:
@@ -403,11 +405,11 @@ invalid-path-type:
403405
})
404406
}
405407

406-
func testFilterInvalidPath(t *testing.T) {
407-
t.Run("Filter invalid path", func(t *testing.T) {
408+
func testFilterOptionalField(t *testing.T) {
409+
t.Run("Filter optional field", func(t *testing.T) {
408410
testToRun := test{
409411
captureASTPool: `
410-
invalid-path-identity:
412+
description-eth1:
411413
pos: 1
412414
eqfilter:
413415
- pos: 2
@@ -417,14 +419,27 @@ invalid-path-identity:
417419
- pos: 4
418420
identity: interfaces
419421
- pos: 5
420-
identity: name-invalid-path
422+
identity: description
421423
- pos: 6
422-
string: eth0
424+
string: 1st ethernet interface
423425
`,
424-
err: "resolve error: eqfilter error: failed applying operation on the path: cannot find key name-invalid-path in " +
425-
"map[ipv4:map[address:[map[ip:10.244.0.1 prefix-length:24] " +
426-
"map[ip:169.254.1.0 prefix-length:16]] dhcp:false enabled:true] name:eth1 state:up type:ethernet]",
427-
}
426+
expectedCapturedStates: `
427+
description-eth1:
428+
state:
429+
interfaces:
430+
- name: eth1
431+
description: "1st ethernet interface"
432+
type: ethernet
433+
state: up
434+
ipv4:
435+
address:
436+
- ip: 10.244.0.1
437+
prefix-length: 24
438+
- ip: 169.254.1.0
439+
prefix-length: 16
440+
dhcp: false
441+
enabled: true
442+
`}
428443
runTest(t, testToRun)
429444
})
430445
}
@@ -761,6 +776,7 @@ bridge-routes:
761776
table-id: 254
762777
interfaces:
763778
- name: eth1
779+
description: "1st ethernet interface"
764780
type: ethernet
765781
state: up
766782
ipv4:

0 commit comments

Comments
 (0)