Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolver: unit test filtering from cached capture #106

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions docs/examples/all-ethernet-up/captured.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
ethernets:
metaInfo:
time: "2021-12-15T13:45:40Z"
version: "0"
state:
interfaces:
- accept-all-mac-addresses: false
lldp:
enabled: false
mac-address: 52:55:00:D1:55:02
name: eth0
state: up
type: ethernet
- accept-all-mac-addresses: false
lldp:
enabled: false
mac-address: 52:55:00:D1:56:02
name: eth1
state: down
type: ethernet
ethernets-up:
metaInfo:
time: "2021-12-15T13:45:40Z"
version: "0"
state:
interfaces:
- accept-all-mac-addresses: false
lldp:
enabled: false
mac-address: 52:55:00:D1:55:02
name: eth0
state: up
type: ethernet
ethernets-lldp:
metaInfo:
time: "2021-12-15T13:45:40Z"
version: "0"
state:
interfaces:
- accept-all-mac-addresses: false
lldp:
enabled: false
mac-address: 52:55:00:D1:55:02
name: eth0
state: up
type: ethernet
15 changes: 15 additions & 0 deletions docs/examples/all-ethernet-up/current.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
interfaces:
- accept-all-mac-addresses: false
lldp:
enabled: false
mac-address: 52:55:00:D1:55:02
name: eth0
state: up
type: ethernet
- accept-all-mac-addresses: false
lldp:
enabled: false
mac-address: 52:55:00:D1:56:02
name: eth1
state: down
type: ethernet
8 changes: 8 additions & 0 deletions docs/examples/all-ethernet-up/generated.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
interfaces:
- accept-all-mac-addresses: false
lldp:
enabled: true
mac-address: 52:55:00:D1:55:02
name: eth0
state: up
type: ethernet
9 changes: 9 additions & 0 deletions docs/examples/all-ethernet-up/policy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% raw %}
capture:
ethernets: interfaces.type=="ethernet"
ethernets-up: capture.ethernets.interfaces.state=="up"
ethernets-lldp: capture.ethernets-up | interfaces.lldp.enabled:=true

desiredState:
interfaces: "{{ capture.ethernets-lldp.interfaces }}"
{% endraw %}
96 changes: 96 additions & 0 deletions nmpolicy/internal/resolver/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func TestFilter(t *testing.T) {
testReplaceCapturedState(t)
testReplaceWithCaptureRef(t)
testReplaceOptionalField(t)
testFilterStateCaptureRef(t)
})
}

Expand Down Expand Up @@ -376,6 +377,101 @@ base-iface-routes:
})
}

func testFilterStateCaptureRef(t *testing.T) {
t.Run("Replace boolean in filtered list with capture reference", func(t *testing.T) {
testToRun := test{
capturedStatesCache: `
ethernets:
state:
interfaces:
- name: eth1
description: "1st ethernet interface"
type: ethernet
state: up
ipv4:
address:
- ip: 10.244.0.1
prefix-length: 24
- ip: 169.254.1.0
prefix-length: 16
dhcp: false
enabled: true
- name: eth2
type: ethernet
state: down
ipv4:
address:
- ip: 1.2.3.4
prefix-length: 24
dhcp: false
enabled: false
`,
captureASTPool: `
ethernets-up:
pos: 1
eqfilter:
- pos: 2
path:
- pos: 3
identity: capture
- pos: 4
identity: ethernets
- pos: 5
path:
- pos: 6
identity: interfaces
- pos: 7
identity: state
- pos: 8
string: up
`,

expectedCapturedStates: `
ethernets:
state:
interfaces:
- name: eth1
description: "1st ethernet interface"
type: ethernet
state: up
ipv4:
address:
- ip: 10.244.0.1
prefix-length: 24
- ip: 169.254.1.0
prefix-length: 16
dhcp: false
enabled: true
- name: eth2
type: ethernet
state: down
ipv4:
address:
- ip: 1.2.3.4
prefix-length: 24
dhcp: false
enabled: false
ethernets-up:
state:
interfaces:
- name: eth1
description: "1st ethernet interface"
type: ethernet
state: up
ipv4:
address:
- ip: 10.244.0.1
prefix-length: 24
- ip: 169.254.1.0
prefix-length: 16
dhcp: false
enabled: true
`,
}
runTest(t, &testToRun)
})
}

func testFilterCaptureRefWithoutCapturedState(t *testing.T) {
t.Run("Filter list with capture reference", func(t *testing.T) {
testToRun := test{
Expand Down