From 0ee4520dd2f122ac39072cae1dbe4802606961d5 Mon Sep 17 00:00:00 2001 From: Enrique Llorente Date: Tue, 6 Sep 2022 09:52:47 +0200 Subject: [PATCH] replace: Return map if missing path Signed-off-by: Enrique Llorente --- docs/examples/all-ethernet-up/captured.yaml | 30 +++++++++++++++++--- docs/examples/all-ethernet-up/current.yaml | 13 ++++++++- docs/examples/all-ethernet-up/generated.yaml | 9 +++++- nmpolicy/internal/resolver/replace.go | 2 +- tests/examples_test.go | 9 +++++- 5 files changed, 55 insertions(+), 8 deletions(-) diff --git a/docs/examples/all-ethernet-up/captured.yaml b/docs/examples/all-ethernet-up/captured.yaml index dd1fd3fb..f4269f61 100644 --- a/docs/examples/all-ethernet-up/captured.yaml +++ b/docs/examples/all-ethernet-up/captured.yaml @@ -7,7 +7,7 @@ ethernets: - accept-all-mac-addresses: false lldp: enabled: false - mac-address: 52:55:00:D1:55:02 + mac-address: 52:55:00:D1:55:01 name: eth0 state: up type: ethernet @@ -18,6 +18,16 @@ ethernets: name: eth1 state: down type: ethernet + - accept-all-mac-addresses: false + mac-address: 52:55:00:D1:57:03 + name: eth4 + state: up + type: ethernet + - accept-all-mac-addresses: false + mac-address: 52:55:00:D1:56:04 + name: eth2 + state: down + type: ethernet ethernets-up: metaInfo: time: "2021-12-15T13:45:40Z" @@ -27,10 +37,15 @@ ethernets-up: - accept-all-mac-addresses: false lldp: enabled: false - mac-address: 52:55:00:D1:55:02 + mac-address: 52:55:00:D1:55:01 name: eth0 state: up type: ethernet + - accept-all-mac-addresses: false + mac-address: 52:55:00:D1:57:03 + name: eth4 + state: up + type: ethernet ethernets-lldp: metaInfo: time: "2021-12-15T13:45:40Z" @@ -39,8 +54,15 @@ ethernets-lldp: interfaces: - accept-all-mac-addresses: false lldp: - enabled: false - mac-address: 52:55:00:D1:55:02 + enabled: true + mac-address: 52:55:00:D1:55:01 name: eth0 state: up type: ethernet + - accept-all-mac-addresses: false + lldp: + enabled: true + mac-address: 52:55:00:D1:57:03 + name: eth4 + state: up + type: ethernet diff --git a/docs/examples/all-ethernet-up/current.yaml b/docs/examples/all-ethernet-up/current.yaml index 1b4844f1..979f9c93 100644 --- a/docs/examples/all-ethernet-up/current.yaml +++ b/docs/examples/all-ethernet-up/current.yaml @@ -2,7 +2,7 @@ interfaces: - accept-all-mac-addresses: false lldp: enabled: false - mac-address: 52:55:00:D1:55:02 + mac-address: 52:55:00:D1:55:01 name: eth0 state: up type: ethernet @@ -13,3 +13,14 @@ interfaces: name: eth1 state: down type: ethernet +- accept-all-mac-addresses: false + mac-address: 52:55:00:D1:57:03 + name: eth4 + state: up + type: ethernet +- accept-all-mac-addresses: false + mac-address: 52:55:00:D1:56:04 + name: eth2 + state: down + type: ethernet + diff --git a/docs/examples/all-ethernet-up/generated.yaml b/docs/examples/all-ethernet-up/generated.yaml index 3c151ade..5cee659e 100644 --- a/docs/examples/all-ethernet-up/generated.yaml +++ b/docs/examples/all-ethernet-up/generated.yaml @@ -2,7 +2,14 @@ interfaces: - accept-all-mac-addresses: false lldp: enabled: true - mac-address: 52:55:00:D1:55:02 + mac-address: 52:55:00:D1:55:01 name: eth0 state: up type: ethernet +- accept-all-mac-addresses: false + lldp: + enabled: true + mac-address: 52:55:00:D1:57:03 + name: eth4 + state: up + type: ethernet diff --git a/nmpolicy/internal/resolver/replace.go b/nmpolicy/internal/resolver/replace.go index ef01d83f..59e48bd4 100644 --- a/nmpolicy/internal/resolver/replace.go +++ b/nmpolicy/internal/resolver/replace.go @@ -61,7 +61,7 @@ func (r replaceOpVisitor) visitMap(p path, mapToVisit map[string]interface{}) (i } interfaceToVisit, ok := mapToVisit[*p.currentStep.Identity] if !ok { - return nil, nil + interfaceToVisit = map[string]interface{}{} } visitResult, err := visitState(p.nextStep(), interfaceToVisit, &r) diff --git a/tests/examples_test.go b/tests/examples_test.go index 73e82f73..02ba8c00 100644 --- a/tests/examples_test.go +++ b/tests/examples_test.go @@ -105,7 +105,8 @@ func TestExamples(t *testing.T) { assert.NoError(t, err) obtainedCaptuerdStates, err := formatCapturedStates(obtained.Cache.Capture) assert.NoError(t, err) - assert.Equal(t, resetCapturedStatesTimeStamp(expectedCapturedStates), resetCapturedStatesTimeStamp(obtainedCaptuerdStates)) + assert.YAMLEq(t, marshalCapturedStates(t, resetCapturedStatesTimeStamp(expectedCapturedStates)), + marshalCapturedStates(t, resetCapturedStatesTimeStamp(obtainedCaptuerdStates))) }) } } @@ -127,3 +128,9 @@ func formatCapturedStates(capturedStates map[string]types.CaptureState) (map[str } return capturedStates, nil } + +func marshalCapturedStates(t *testing.T, capturedStates map[string]types.CaptureState) string { + marshaledBytes, err := yaml.Marshal(capturedStates) + assert.NoError(t, err) + return string(marshaledBytes) +}