-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0315ff9
commit 2ae54e3
Showing
23 changed files
with
3,436 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
package anycast | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"github.com/telekom/das-schiff-network-operator/pkg/nl" | ||
mock_nl "github.com/telekom/das-schiff-network-operator/pkg/nl/mock" | ||
"github.com/vishvananda/netlink" | ||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
var ( | ||
mockctrl *gomock.Controller | ||
) | ||
|
||
var _ = BeforeSuite(func() { | ||
|
||
}) | ||
|
||
func TestAnycast(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
mockctrl = gomock.NewController(t) | ||
defer mockctrl.Finish() | ||
RunSpecs(t, | ||
"Anycast Suite") | ||
} | ||
|
||
var _ = Describe("anycast", func() { | ||
Context("buildRoute() should", func() { | ||
It("build route", func() { | ||
route := buildRoute(0, &netlink.Bridge{}, &net.IPNet{}, 0) | ||
Expect(route).ToNot(BeNil()) | ||
}) | ||
}) | ||
Context("containsIPAddress() should", func() { | ||
It("return false if list does not contain IP address", func() { | ||
result := containsIPAddress([]netlink.Neigh{{IP: net.IPv4(0, 0, 0, 0)}}, &net.IPNet{IP: net.IPv4(1, 1, 1, 1)}) | ||
Expect(result).To(BeFalse()) | ||
}) | ||
It("return true if list does contain IP address", func() { | ||
result := containsIPAddress([]netlink.Neigh{{IP: net.IPv4(0, 0, 0, 0)}}, &net.IPNet{IP: net.IPv4(0, 0, 0, 0)}) | ||
Expect(result).To(BeTrue()) | ||
}) | ||
}) | ||
Context("containsIPNetwork() should", func() { | ||
It("return false if list does not contain IP network", func() { | ||
result := containsIPNetwork([]*net.IPNet{{IP: net.IPv4(0, 0, 0, 0), Mask: net.IPv4Mask(255, 255, 255, 0)}}, | ||
&net.IPNet{IP: net.IPv4(0, 0, 0, 0), Mask: net.IPv4Mask(255, 255, 255, 254)}) | ||
Expect(result).To(BeFalse()) | ||
}) | ||
It("return true if list does contain IP network", func() { | ||
result := containsIPNetwork([]*net.IPNet{{IP: net.IPv4(0, 0, 0, 0), Mask: net.IPv4Mask(255, 255, 255, 0)}}, | ||
&net.IPNet{IP: net.IPv4(0, 0, 0, 0), Mask: net.IPv4Mask(255, 255, 255, 0)}) | ||
Expect(result).To(BeTrue()) | ||
}) | ||
}) | ||
Context("filterNeighbors() should", func() { | ||
It("return empty list if flags not as expected", func() { | ||
neighIn := []netlink.Neigh{{Flags: netlink.NTF_EXT_LEARNED}} | ||
result := filterNeighbors(neighIn) | ||
Expect(result).To(BeEmpty()) | ||
}) | ||
It("return empty list if state not as expected", func() { | ||
neighIn := []netlink.Neigh{{Flags: netlink.NTF_EXT_MANAGED, State: netlink.NUD_INCOMPLETE}} | ||
result := filterNeighbors(neighIn) | ||
Expect(result).To(BeEmpty()) | ||
}) | ||
It("return non empty list", func() { | ||
neighIn := []netlink.Neigh{{Flags: netlink.NTF_EXT_MANAGED}} | ||
result := filterNeighbors(neighIn) | ||
Expect(result).ToNot(BeEmpty()) | ||
}) | ||
}) | ||
Context("syncInterfaceByFamily() should", func() { | ||
netlinkMock := mock_nl.NewMockNetlinkToolkitInterface(mockctrl) | ||
It("return error if cannot get neighbors", func() { | ||
netlinkMock.EXPECT().NeighList(0, 0).Return(nil, errors.New("fake error")) | ||
err := syncInterfaceByFamily(&netlink.Bridge{}, 0, 0, netlinkMock) | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
It("return error if cannot filter routes", func() { | ||
family := 0 | ||
linkIndex := 0 | ||
netlinkMock.EXPECT().NeighList(linkIndex, family).Return([]netlink.Neigh{{Flags: netlink.NTF_EXT_MANAGED}}, nil) | ||
netlinkMock.EXPECT().RouteListFiltered(0, gomock.Any(), netlink.RT_FILTER_OIF|netlink.RT_FILTER_TABLE|netlink.RT_FILTER_PROTOCOL). | ||
Return(nil, errors.New("fake error")) | ||
err := syncInterfaceByFamily(&netlink.Bridge{}, family, 0, netlinkMock) | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
It("return no error if cannot delete route", func() { | ||
family := 0 | ||
linkIndex := 0 | ||
route := netlink.Route{Flags: netlink.NTF_EXT_MANAGED, Dst: netlink.NewIPNet(net.IPv4(1, 1, 1, 1))} | ||
netlinkMock.EXPECT().NeighList(linkIndex, family).Return([]netlink.Neigh{{Flags: netlink.NTF_EXT_MANAGED, IP: net.IPv4(0, 0, 0, 0)}}, nil) | ||
netlinkMock.EXPECT().RouteListFiltered(0, gomock.Any(), netlink.RT_FILTER_OIF|netlink.RT_FILTER_TABLE|netlink.RT_FILTER_PROTOCOL). | ||
Return([]netlink.Route{route}, nil) | ||
netlinkMock.EXPECT().RouteDel(gomock.Any()).Return(errors.New("fake error")) // error is only logged, not returned | ||
netlinkMock.EXPECT().NewIPNet(gomock.Any()).Return(netlink.NewIPNet(net.IPv4(1, 1, 1, 1))) | ||
netlinkMock.EXPECT().RouteAdd(gomock.Any()).Return(errors.New("fake error")) // error is only logged, not returned | ||
err := syncInterfaceByFamily(&netlink.Bridge{}, family, 0, netlinkMock) | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
It("return no error if appended route", func() { | ||
family := 0 | ||
linkIndex := 0 | ||
route := netlink.Route{Flags: netlink.NTF_EXT_MANAGED, Dst: netlink.NewIPNet(net.IPv4(1, 1, 1, 1))} | ||
netlinkMock.EXPECT().NeighList(linkIndex, family).Return([]netlink.Neigh{{Flags: netlink.NTF_EXT_MANAGED, IP: net.IPv4(1, 1, 1, 1)}}, nil) | ||
netlinkMock.EXPECT().RouteListFiltered(0, gomock.Any(), netlink.RT_FILTER_OIF|netlink.RT_FILTER_TABLE|netlink.RT_FILTER_PROTOCOL). | ||
Return([]netlink.Route{route}, nil) | ||
netlinkMock.EXPECT().NewIPNet(gomock.Any()).Return(netlink.NewIPNet(net.IPv4(1, 1, 1, 1))) | ||
err := syncInterfaceByFamily(&netlink.Bridge{}, family, 0, netlinkMock) | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
}) | ||
Context("syncInterface() should", func() { | ||
netlinkMock := mock_nl.NewMockNetlinkToolkitInterface(mockctrl) | ||
It("return no error if interface's Master Index <= 0", func() { | ||
intf := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{MasterIndex: 0}} | ||
// returning error just to quit syncInterfaceByFamily call | ||
netlinkMock.EXPECT().NeighList(gomock.Any(), gomock.Any()).Return(nil, errors.New("fake error")).Times(2) | ||
err := syncInterface(intf, netlinkMock) | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
It("return error if cannot get interface by index", func() { | ||
masterIndex := 1 | ||
intf := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{MasterIndex: masterIndex}} | ||
netlinkMock.EXPECT().LinkByIndex(masterIndex).Return(nil, errors.New("fake error")) | ||
err := syncInterface(intf, netlinkMock) | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
It("return error if could get interface but it's type is not vrf", func() { | ||
masterIndex := 1 | ||
intf := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{MasterIndex: masterIndex}} | ||
netlinkMock.EXPECT().LinkByIndex(masterIndex).Return(&netlink.Gretun{}, nil) | ||
err := syncInterface(intf, netlinkMock) | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
It("return no error", func() { | ||
masterIndex := 1 | ||
intf := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{MasterIndex: masterIndex}} | ||
netlinkMock.EXPECT().LinkByIndex(masterIndex).Return(&netlink.Vrf{}, nil) | ||
// returning error just to quit syncInterfaceByFamily call | ||
netlinkMock.EXPECT().NeighList(gomock.Any(), gomock.Any()).Return(nil, errors.New("fake error")).Times(2) | ||
err := syncInterface(intf, netlinkMock) | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
}) | ||
Context("NewTracker() should", func() { | ||
It("return new anycast tracker", func() { | ||
toolkit := &nl.NetlinkToolkit{} | ||
tracker := NewTracker(toolkit) | ||
Expect(tracker).ToNot(BeNil()) | ||
Expect(tracker.netlinkToolkit).To(Equal(toolkit)) | ||
}) | ||
}) | ||
Context("checkTrackedInterfaces() should", func() { | ||
netlinkMock := mock_nl.NewMockNetlinkToolkitInterface(mockctrl) | ||
It("return no error if cannot get link by index", func() { | ||
netlinkMock.EXPECT().LinkByIndex(gomock.Any()).Return(nil, errors.New("fake error")) | ||
tracker := NewTracker(netlinkMock) | ||
Expect(tracker).ToNot(BeNil()) | ||
Expect(tracker.netlinkToolkit).To(Equal(netlinkMock)) | ||
tracker.TrackedBridges = []int{0} | ||
tracker.checkTrackedInterfaces() | ||
}) | ||
It("return no error", func() { | ||
netlinkMock.EXPECT().LinkByIndex(gomock.Any()).Return(&netlink.Bridge{}, nil) | ||
// returning error just to quit syncInterface call | ||
netlinkMock.EXPECT().NeighList(gomock.Any(), gomock.Any()).Return(nil, errors.New("fake error")).Times(2) | ||
tracker := NewTracker(netlinkMock) | ||
Expect(tracker).ToNot(BeNil()) | ||
Expect(tracker.netlinkToolkit).To(Equal(netlinkMock)) | ||
tracker.TrackedBridges = []int{0} | ||
tracker.checkTrackedInterfaces() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
const operatorConfigEnv = "OPERATOR_CONFIG" | ||
|
||
var _ = BeforeSuite(func() { | ||
|
||
}) | ||
|
||
func TestDebounce(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, | ||
"Config Suite") | ||
} | ||
|
||
var _ = Describe("config", func() { | ||
Context("LoadConfig() should", func() { | ||
It("return error if cannot read config", func() { | ||
oldEnv, isSet := os.LookupEnv(operatorConfigEnv) | ||
os.Setenv(operatorConfigEnv, "some-invalid-path") | ||
_, err := LoadConfig() | ||
Expect(err).To(HaveOccurred()) | ||
if isSet { | ||
err = os.Setenv(operatorConfigEnv, oldEnv) | ||
Expect(err).ToNot(HaveOccurred()) | ||
} else { | ||
err = os.Unsetenv(operatorConfigEnv) | ||
Expect(err).ToNot(HaveOccurred()) | ||
} | ||
}) | ||
It("return error if cannot unmarshal config", func() { | ||
oldEnv, isSet := os.LookupEnv(operatorConfigEnv) | ||
os.Setenv(operatorConfigEnv, "./testdata/invalidConfig.yaml") | ||
_, err := LoadConfig() | ||
Expect(err).To(HaveOccurred()) | ||
if isSet { | ||
err = os.Setenv(operatorConfigEnv, oldEnv) | ||
Expect(err).ToNot(HaveOccurred()) | ||
} else { | ||
err = os.Unsetenv(operatorConfigEnv) | ||
Expect(err).ToNot(HaveOccurred()) | ||
} | ||
}) | ||
It("return no error", func() { | ||
oldEnv, isSet := os.LookupEnv(operatorConfigEnv) | ||
os.Setenv(operatorConfigEnv, "./testdata/config.yaml") | ||
_, err := LoadConfig() | ||
Expect(err).ToNot(HaveOccurred()) | ||
if isSet { | ||
err = os.Setenv(operatorConfigEnv, oldEnv) | ||
Expect(err).ToNot(HaveOccurred()) | ||
} else { | ||
err = os.Unsetenv(operatorConfigEnv) | ||
Expect(err).ToNot(HaveOccurred()) | ||
} | ||
}) | ||
}) | ||
Context("ShouldSkipVRFConfig() should", func() { | ||
It("return false", func() { | ||
cfg := &Config{SkipVRFConfig: []string{"", "", ""}} | ||
result := cfg.ShouldSkipVRFConfig("skip") | ||
Expect(result).To(BeFalse()) | ||
}) | ||
It("return true", func() { | ||
cfg := &Config{SkipVRFConfig: []string{"", "skip", ""}} | ||
result := cfg.ShouldSkipVRFConfig("skip") | ||
Expect(result).To(BeTrue()) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.