github.com/elfadel/cilium@v1.6.12/pkg/datapath/linux/route/route_test.go (about) 1 // Copyright 2018 Authors of Cilium 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 15 // +build !privileged_tests 16 17 package route 18 19 import ( 20 "fmt" 21 "net" 22 "strings" 23 "testing" 24 25 "github.com/cilium/cilium/pkg/checker" 26 27 . "gopkg.in/check.v1" 28 ) 29 30 // Hook up gocheck into the "go test" runner. 31 func Test(t *testing.T) { TestingT(t) } 32 33 type RouteSuite struct{} 34 35 var _ = Suite(&RouteSuite{}) 36 37 func parseIP(ip string) *net.IP { 38 result := net.ParseIP(ip) 39 return &result 40 } 41 42 func (p *RouteSuite) TestToIPCommand(c *C) { 43 routes := []*Route{ 44 { 45 Prefix: net.IPNet{ 46 IP: net.ParseIP("10.0.0.1"), 47 Mask: net.CIDRMask(8, 32), 48 }, 49 Nexthop: parseIP("192.168.0.1"), 50 }, 51 { 52 Prefix: net.IPNet{ 53 IP: net.ParseIP("::1"), 54 Mask: net.CIDRMask(64, 128), 55 }, 56 Nexthop: parseIP("ff02::2"), 57 }, 58 } 59 for _, r := range routes { 60 dev := "eth0" 61 v6 := "-6 " 62 if r.Prefix.IP.To4() != nil { 63 v6 = "" 64 } 65 masklen, _ := r.Prefix.Mask.Size() 66 expRes := fmt.Sprintf("ip %sroute add %s/%d via %s dev %s", v6, 67 r.Prefix.IP.String(), masklen, r.Nexthop.String(), dev) 68 result := strings.Join(r.ToIPCommand(dev), " ") 69 c.Assert(result, checker.DeepEquals, expRes) 70 71 r.Nexthop = nil 72 expRes = fmt.Sprintf("ip %sroute add %s/%d dev %s", v6, 73 r.Prefix.IP.String(), masklen, dev) 74 result = strings.Join(r.ToIPCommand(dev), " ") 75 c.Assert(result, checker.DeepEquals, expRes) 76 } 77 }