mirror of
https://github.com/juanfont/headscale.git
synced 2025-04-21 01:16:54 +02:00
Test also matchers in tests for Policy.Filter
This commit is contained in:
parent
d1812eeec9
commit
2073a84e31
@ -1,6 +1,7 @@
|
|||||||
package v1
|
package v1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/juanfont/headscale/hscontrol/policy/matcher"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
@ -27,6 +28,7 @@ func TestPolicySetChange(t *testing.T) {
|
|||||||
wantNodesChange bool
|
wantNodesChange bool
|
||||||
wantPolicyChange bool
|
wantPolicyChange bool
|
||||||
wantFilter []tailcfg.FilterRule
|
wantFilter []tailcfg.FilterRule
|
||||||
|
wantMatchers []matcher.Match
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "set-nodes",
|
name: "set-nodes",
|
||||||
@ -42,6 +44,9 @@ func TestPolicySetChange(t *testing.T) {
|
|||||||
DstPorts: []tailcfg.NetPortRange{{IP: "100.64.0.1/32", Ports: tailcfg.PortRangeAny}},
|
DstPorts: []tailcfg.NetPortRange{{IP: "100.64.0.1/32", Ports: tailcfg.PortRangeAny}},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
wantMatchers: []matcher.Match{
|
||||||
|
matcher.MatchFromStrings([]string{}, []string{"100.64.0.1/32"}),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "set-users",
|
name: "set-users",
|
||||||
@ -52,6 +57,9 @@ func TestPolicySetChange(t *testing.T) {
|
|||||||
DstPorts: []tailcfg.NetPortRange{{IP: "100.64.0.1/32", Ports: tailcfg.PortRangeAny}},
|
DstPorts: []tailcfg.NetPortRange{{IP: "100.64.0.1/32", Ports: tailcfg.PortRangeAny}},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
wantMatchers: []matcher.Match{
|
||||||
|
matcher.MatchFromStrings([]string{}, []string{"100.64.0.1/32"}),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "set-users-and-node",
|
name: "set-users-and-node",
|
||||||
@ -70,6 +78,9 @@ func TestPolicySetChange(t *testing.T) {
|
|||||||
DstPorts: []tailcfg.NetPortRange{{IP: "100.64.0.1/32", Ports: tailcfg.PortRangeAny}},
|
DstPorts: []tailcfg.NetPortRange{{IP: "100.64.0.1/32", Ports: tailcfg.PortRangeAny}},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
wantMatchers: []matcher.Match{
|
||||||
|
matcher.MatchFromStrings([]string{"100.64.0.2/32"}, []string{"100.64.0.1/32"}),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "set-policy",
|
name: "set-policy",
|
||||||
@ -95,6 +106,9 @@ func TestPolicySetChange(t *testing.T) {
|
|||||||
DstPorts: []tailcfg.NetPortRange{{IP: "100.64.0.62/32", Ports: tailcfg.PortRangeAny}},
|
DstPorts: []tailcfg.NetPortRange{{IP: "100.64.0.62/32", Ports: tailcfg.PortRangeAny}},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
wantMatchers: []matcher.Match{
|
||||||
|
matcher.MatchFromStrings([]string{"100.64.0.61/32"}, []string{"100.64.0.62/32"}),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,9 +164,16 @@ func TestPolicySetChange(t *testing.T) {
|
|||||||
assert.Equal(t, tt.wantNodesChange, change)
|
assert.Equal(t, tt.wantNodesChange, change)
|
||||||
}
|
}
|
||||||
|
|
||||||
filter, _ := pm.Filter()
|
filter, matchers := pm.Filter()
|
||||||
if diff := cmp.Diff(tt.wantFilter, filter); diff != "" {
|
if diff := cmp.Diff(tt.wantFilter, filter); diff != "" {
|
||||||
t.Errorf("TestPolicySetChange() unexpected result (-want +got):\n%s", diff)
|
t.Errorf("TestPolicySetChange() unexpected filter (-want +got):\n%s", diff)
|
||||||
|
}
|
||||||
|
if diff := cmp.Diff(
|
||||||
|
tt.wantMatchers,
|
||||||
|
matchers,
|
||||||
|
cmp.AllowUnexported(matcher.Match{}),
|
||||||
|
); diff != "" {
|
||||||
|
t.Errorf("TestPolicySetChange() unexpected matchers (-want +got):\n%s", diff)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package v2
|
package v2
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/juanfont/headscale/hscontrol/policy/matcher"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
@ -29,16 +30,18 @@ func TestPolicyManager(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
pol string
|
pol string
|
||||||
nodes types.Nodes
|
nodes types.Nodes
|
||||||
wantFilter []tailcfg.FilterRule
|
wantFilter []tailcfg.FilterRule
|
||||||
|
wantMatchers []matcher.Match
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "empty-policy",
|
name: "empty-policy",
|
||||||
pol: "{}",
|
pol: "{}",
|
||||||
nodes: types.Nodes{},
|
nodes: types.Nodes{},
|
||||||
wantFilter: nil,
|
wantFilter: nil,
|
||||||
|
wantMatchers: nil,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,9 +50,16 @@ func TestPolicyManager(t *testing.T) {
|
|||||||
pm, err := NewPolicyManager([]byte(tt.pol), users, tt.nodes)
|
pm, err := NewPolicyManager([]byte(tt.pol), users, tt.nodes)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
filter, _ := pm.Filter()
|
filter, matchers := pm.Filter()
|
||||||
if diff := cmp.Diff(filter, tt.wantFilter); diff != "" {
|
if diff := cmp.Diff(tt.wantFilter, filter); diff != "" {
|
||||||
t.Errorf("Filter() mismatch (-want +got):\n%s", diff)
|
t.Errorf("Filter() filter mismatch (-want +got):\n%s", diff)
|
||||||
|
}
|
||||||
|
if diff := cmp.Diff(
|
||||||
|
tt.wantMatchers,
|
||||||
|
matchers,
|
||||||
|
cmp.AllowUnexported(matcher.Match{}),
|
||||||
|
); diff != "" {
|
||||||
|
t.Errorf("Filter() matchers mismatch (-want +got):\n%s", diff)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(kradalby): Test SSH Policy
|
// TODO(kradalby): Test SSH Policy
|
||||||
|
Loading…
Reference in New Issue
Block a user