route_network_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2017 flannel authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // +build !windows
  15. package backend
  16. import (
  17. "net"
  18. "testing"
  19. "github.com/coreos/flannel/pkg/ip"
  20. "github.com/coreos/flannel/pkg/ns"
  21. "github.com/coreos/flannel/subnet"
  22. "github.com/vishvananda/netlink"
  23. )
  24. func TestRouteCache(t *testing.T) {
  25. teardown := ns.SetUpNetlinkTest(t)
  26. defer teardown()
  27. lo, err := netlink.LinkByName("lo")
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. if err := netlink.AddrAdd(lo, &netlink.Addr{IPNet: &net.IPNet{IP: net.ParseIP("127.0.0.1"), Mask: net.CIDRMask(32, 32)}}); err != nil {
  32. t.Fatal(err)
  33. }
  34. if err := netlink.LinkSetUp(lo); err != nil {
  35. t.Fatal(err)
  36. }
  37. nw := RouteNetwork{
  38. SimpleNetwork: SimpleNetwork{
  39. ExtIface: &ExternalInterface{Iface: &net.Interface{Index: lo.Attrs().Index}},
  40. },
  41. BackendType: "host-gw",
  42. LinkIndex: lo.Attrs().Index,
  43. }
  44. nw.GetRoute = func(lease *subnet.Lease) *netlink.Route {
  45. return &netlink.Route{
  46. Dst: lease.Subnet.ToIPNet(),
  47. Gw: lease.Attrs.PublicIP.ToIP(),
  48. LinkIndex: nw.LinkIndex,
  49. }
  50. }
  51. gw1, gw2 := ip.FromIP(net.ParseIP("127.0.0.1")), ip.FromIP(net.ParseIP("127.0.0.2"))
  52. subnet1 := ip.IP4Net{IP: ip.FromIP(net.ParseIP("192.168.0.0")), PrefixLen: 24}
  53. nw.handleSubnetEvents([]subnet.Event{
  54. {Type: subnet.EventAdded, Lease: subnet.Lease{
  55. Subnet: subnet1, Attrs: subnet.LeaseAttrs{PublicIP: gw1, BackendType: "host-gw"}}},
  56. })
  57. if len(nw.routes) != 1 {
  58. t.Fatal(nw.routes)
  59. }
  60. if !routeEqual(nw.routes[0], netlink.Route{Dst: subnet1.ToIPNet(), Gw: gw1.ToIP(), LinkIndex: lo.Attrs().Index}) {
  61. t.Fatal(nw.routes[0])
  62. }
  63. // change gateway of previous route
  64. nw.handleSubnetEvents([]subnet.Event{
  65. {Type: subnet.EventAdded, Lease: subnet.Lease{
  66. Subnet: subnet1, Attrs: subnet.LeaseAttrs{PublicIP: gw2, BackendType: "host-gw"}}}})
  67. if len(nw.routes) != 1 {
  68. t.Fatal(nw.routes)
  69. }
  70. if !routeEqual(nw.routes[0], netlink.Route{Dst: subnet1.ToIPNet(), Gw: gw2.ToIP(), LinkIndex: lo.Attrs().Index}) {
  71. t.Fatal(nw.routes[0])
  72. }
  73. }