Skip to content

Commit

Permalink
test: Add pipe to the main scenario
Browse files Browse the repository at this point in the history
Signed-off-by: Quique Llorente <[email protected]>
  • Loading branch information
qinqon committed Dec 7, 2021
1 parent e40c864 commit fca96a0
Showing 1 changed file with 101 additions and 106 deletions.
207 changes: 101 additions & 106 deletions tests/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func TestBasicPolicy(t *testing.T) {
testEmptyPolicy(t)
testPolicyWithOnlyDesiredState(t)
testPolicyWithCachedCaptureAndDesiredStateWithoutRef(t)
testPolicyWithFilterCaptureAndDesiredStateCaptureEntryRef(t)
testPolicyWithDesiredStateRefToCachedCapturedStates(t)
testPolicyWithCaptureEntryExpressionRefToCachedCapturedStates(t)
testPolicyWithoutCache(t)
testPolicyWithFullCache(t)
testPolicyWithPartialCache(t)
testGenerateUniqueTimestamps(t)
})
}
Expand Down Expand Up @@ -166,6 +166,8 @@ interfaces:
enabled: false
port:
- name: "{{ capture.base-iface.interfaces.0.name }}"
routes:
config: "{{ capture.bridge-routes.routes.running }}"
`)

var mainExpectedDesiredState = []byte(`
Expand All @@ -188,42 +190,31 @@ interfaces:
enabled: false
port:
- name: eth1
routes:
config:
- destination: 0.0.0.0/0
next-hop-address: 192.168.100.1
next-hop-interface: br1
table-id: 254
- destination: 1.1.1.0/24
next-hop-address: 192.168.100.1
next-hop-interface: br1
table-id: 254
`)

func testPolicyWithFilterCaptureAndDesiredStateCaptureEntryRef(t *testing.T) {
t.Run("with a eqfilter capture expression and desired state that has no ref", func(t *testing.T) {
policySpec := types.PolicySpec{
Capture: map[string]string{
"default-gw": `routes.running.destination=="0.0.0.0/0"`,
"base-iface": `interfaces.name==capture.default-gw.routes.running.0.next-hop-interface`,
},
DesiredState: mainDesiredState,
}
obtained, err := nmpolicy.GenerateState(
policySpec,
mainCurrentState,
types.CachedState{})
assert.NoError(t, err)

expected := types.GeneratedState{
MetaInfo: types.MetaInfo{
Version: "0",
},
DesiredState: mainExpectedDesiredState,
Cache: types.CachedState{
Capture: map[string]types.CaptureState{
"default-gw": {
State: []byte(`
var defaultGwCapturedState = types.CaptureState{
State: []byte(`
routes:
running:
- destination: 0.0.0.0/0
next-hop-address: 192.168.100.1
next-hop-interface: eth1
table-id: 254
`),
},
"base-iface": {
State: []byte(`
}

var baseIfaceCapturedState = types.CaptureState{
State: []byte(`
interfaces:
- name: eth1
type: ethernet
Expand All @@ -237,7 +228,66 @@ interfaces:
dhcp: false
enabled: true
`),
},
}

var baseIfaceRoutesCapturedState = types.CaptureState{
State: []byte(`
routes:
running:
- destination: 0.0.0.0/0
next-hop-address: 192.168.100.1
next-hop-interface: eth1
table-id: 254
- destination: 1.1.1.0/24
next-hop-address: 192.168.100.1
next-hop-interface: eth1
table-id: 254
`),
}

var bridgeRoutesCapturedState = types.CaptureState{
State: []byte(`
routes:
running:
- destination: 0.0.0.0/0
next-hop-address: 192.168.100.1
next-hop-interface: br1
table-id: 254
- destination: 1.1.1.0/24
next-hop-address: 192.168.100.1
next-hop-interface: br1
table-id: 254
`),
}

func testPolicyWithoutCache(t *testing.T) {
t.Run("without cache", func(t *testing.T) {
policySpec := types.PolicySpec{
Capture: map[string]string{
"default-gw": `routes.running.destination=="0.0.0.0/0"`,
"base-iface": `interfaces.name==capture.default-gw.routes.running.0.next-hop-interface`,
"base-iface-routes": `routes.running.next-hop-interface==capture.default-gw.routes.running.0.next-hop-interface`,
"bridge-routes": `capture.base-iface-routes | routes.running.next-hop-interface:="br1"`,
},
DesiredState: mainDesiredState,
}
obtained, err := nmpolicy.GenerateState(
policySpec,
mainCurrentState,
types.NoCache())
assert.NoError(t, err)

expected := types.GeneratedState{
MetaInfo: types.MetaInfo{
Version: "0",
},
DesiredState: mainExpectedDesiredState,
Cache: types.CachedState{
Capture: map[string]types.CaptureState{
"default-gw": defaultGwCapturedState,
"base-iface": baseIfaceCapturedState,
"base-iface-routes": baseIfaceRoutesCapturedState,
"bridge-routes": bridgeRoutesCapturedState,
},
},
}
Expand All @@ -254,32 +304,19 @@ interfaces:
})
}

func testPolicyWithDesiredStateRefToCachedCapturedStates(t *testing.T) {
t.Run("with desired state refereing a cached captured state", func(t *testing.T) {
func testPolicyWithFullCache(t *testing.T) {
t.Run("with full cache", func(t *testing.T) {
policySpec := types.PolicySpec{
Capture: map[string]string{
"base-iface": "override me with the cache",
"base-iface": "override me with the cache",
"bridge-routes": "override me with the cache",
},
DesiredState: mainDesiredState,
}
cachedState := types.CachedState{
Capture: map[string]types.CaptureState{
"base-iface": {
State: []byte(`
interfaces:
- name: eth1
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
`),
},
"base-iface": baseIfaceCapturedState,
"bridge-routes": bridgeRoutesCapturedState,
},
}

Expand All @@ -293,22 +330,8 @@ interfaces:
DesiredState: mainExpectedDesiredState,
Cache: types.CachedState{
Capture: map[string]types.CaptureState{
"base-iface": {
State: []byte(`
interfaces:
- name: eth1
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
`),
},
"base-iface": baseIfaceCapturedState,
"bridge-routes": bridgeRoutesCapturedState,
},
},
}
Expand All @@ -325,27 +348,21 @@ interfaces:
})
}

func testPolicyWithCaptureEntryExpressionRefToCachedCapturedStates(t *testing.T) {
t.Run("with a eqfilter capture expression and desired state that has no ref", func(t *testing.T) {
func testPolicyWithPartialCache(t *testing.T) {
t.Run("with partial cache", func(t *testing.T) {
policySpec := types.PolicySpec{
Capture: map[string]string{
"default-gw": "override me with the cache",
"base-iface": `interfaces.name==capture.default-gw.routes.running.0.next-hop-interface`,
"default-gw": "override me with the cache",
"base-iface": `interfaces.name==capture.default-gw.routes.running.0.next-hop-interface`,
"base-iface-routes": `routes.running.next-hop-interface==capture.default-gw.routes.running.0.next-hop-interface`,
"bridge-routes": `capture.base-iface-routes | routes.running.next-hop-interface:="br1"`,
},
DesiredState: mainDesiredState,
}
cachedState := types.CachedState{
Capture: map[string]types.CaptureState{
"default-gw": {
State: []byte(`
routes:
running:
- destination: 0.0.0.0/0
next-hop-address: 192.168.100.1
next-hop-interface: eth1
table-id: 254
`),
},
"default-gw": defaultGwCapturedState,
"bridge-routes": bridgeRoutesCapturedState,
},
}

Expand All @@ -359,32 +376,10 @@ routes:
DesiredState: mainExpectedDesiredState,
Cache: types.CachedState{
Capture: map[string]types.CaptureState{
"default-gw": {
State: []byte(`
routes:
running:
- destination: 0.0.0.0/0
next-hop-address: 192.168.100.1
next-hop-interface: eth1
table-id: 254
`),
},
"base-iface": {
State: []byte(`
interfaces:
- name: eth1
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
`),
},
"default-gw": defaultGwCapturedState,
"base-iface": baseIfaceCapturedState,
"base-iface-routes": baseIfaceRoutesCapturedState,
"bridge-routes": bridgeRoutesCapturedState,
},
},
}
Expand Down

0 comments on commit fca96a0

Please sign in to comment.