Skip to content

Commit 82899ce

Browse files
author
Ravi Sankar Penta
committed
Added test cases for router FilterNamespaces()
1 parent 1e95a72 commit 82899ce

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

pkg/router/template/router_test.go

+96
Original file line numberDiff line numberDiff line change
@@ -672,3 +672,99 @@ func TestAddRouteEdgeTerminationInsecurePolicy(t *testing.T) {
672672
}
673673
}
674674
}
675+
676+
func TestFilterNamespaces(t *testing.T) {
677+
router := NewFakeTemplateRouter()
678+
679+
testCases := []struct {
680+
name string
681+
serviceUnits map[string]ServiceUnit
682+
state map[string]ServiceAliasConfig
683+
684+
filterNamespaces sets.String
685+
686+
expectedServiceUnits map[string]ServiceUnit
687+
expectedState map[string]ServiceAliasConfig
688+
expectedStateChanged bool
689+
}{
690+
{
691+
name: "empty",
692+
serviceUnits: map[string]ServiceUnit{},
693+
state: map[string]ServiceAliasConfig{},
694+
filterNamespaces: sets.NewString("ns1"),
695+
expectedServiceUnits: map[string]ServiceUnit{},
696+
expectedState: map[string]ServiceAliasConfig{},
697+
expectedStateChanged: false,
698+
},
699+
{
700+
name: "valid, filter none",
701+
serviceUnits: map[string]ServiceUnit{
702+
endpointsKeyFromParts("ns1", "svc"): ServiceUnit{},
703+
endpointsKeyFromParts("ns2", "svc"): ServiceUnit{},
704+
},
705+
state: map[string]ServiceAliasConfig{
706+
routeKeyFromParts("ns1", "svc"): ServiceAliasConfig{},
707+
routeKeyFromParts("ns2", "svc"): ServiceAliasConfig{},
708+
},
709+
filterNamespaces: sets.NewString("ns1", "ns2"),
710+
expectedServiceUnits: map[string]ServiceUnit{
711+
endpointsKeyFromParts("ns1", "svc"): ServiceUnit{},
712+
endpointsKeyFromParts("ns2", "svc"): ServiceUnit{},
713+
},
714+
expectedState: map[string]ServiceAliasConfig{
715+
routeKeyFromParts("ns1", "svc"): ServiceAliasConfig{},
716+
routeKeyFromParts("ns2", "svc"): ServiceAliasConfig{},
717+
},
718+
expectedStateChanged: false,
719+
},
720+
{
721+
name: "valid, filter some",
722+
serviceUnits: map[string]ServiceUnit{
723+
endpointsKeyFromParts("ns1", "svc"): ServiceUnit{},
724+
endpointsKeyFromParts("ns2", "svc"): ServiceUnit{},
725+
},
726+
state: map[string]ServiceAliasConfig{
727+
routeKeyFromParts("ns1", "svc"): ServiceAliasConfig{},
728+
routeKeyFromParts("ns2", "svc"): ServiceAliasConfig{},
729+
},
730+
filterNamespaces: sets.NewString("ns2"),
731+
expectedServiceUnits: map[string]ServiceUnit{
732+
endpointsKeyFromParts("ns2", "svc"): ServiceUnit{},
733+
},
734+
expectedState: map[string]ServiceAliasConfig{
735+
routeKeyFromParts("ns2", "svc"): ServiceAliasConfig{},
736+
},
737+
expectedStateChanged: true,
738+
},
739+
{
740+
name: "valid, filter all",
741+
serviceUnits: map[string]ServiceUnit{
742+
endpointsKeyFromParts("ns1", "svc"): ServiceUnit{},
743+
endpointsKeyFromParts("ns2", "svc"): ServiceUnit{},
744+
},
745+
state: map[string]ServiceAliasConfig{
746+
routeKeyFromParts("ns1", "svc"): ServiceAliasConfig{},
747+
routeKeyFromParts("ns2", "svc"): ServiceAliasConfig{},
748+
},
749+
filterNamespaces: sets.NewString("ns3"),
750+
expectedServiceUnits: map[string]ServiceUnit{},
751+
expectedState: map[string]ServiceAliasConfig{},
752+
expectedStateChanged: true,
753+
},
754+
}
755+
756+
for _, tc := range testCases {
757+
router.serviceUnits = tc.serviceUnits
758+
router.state = tc.state
759+
router.FilterNamespaces(tc.filterNamespaces)
760+
if !reflect.DeepEqual(router.serviceUnits, tc.expectedServiceUnits) {
761+
t.Errorf("test %s: expected router serviceUnits:%v but got %v", tc.name, tc.expectedServiceUnits, router.serviceUnits)
762+
}
763+
if !reflect.DeepEqual(router.state, tc.expectedState) {
764+
t.Errorf("test %s: expected router state:%v but got %v", tc.name, tc.expectedState, router.state)
765+
}
766+
if router.stateChanged != tc.expectedStateChanged {
767+
t.Errorf("test %s: expected router stateChanged:%v but got %v", tc.name, tc.expectedStateChanged, router.stateChanged)
768+
}
769+
}
770+
}

0 commit comments

Comments
 (0)