sandbox_linux_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package osl
  2. import (
  3. "crypto/rand"
  4. "encoding/hex"
  5. "io"
  6. "net"
  7. "os"
  8. "path/filepath"
  9. "syscall"
  10. "testing"
  11. "time"
  12. "github.com/docker/libnetwork/testutils"
  13. "github.com/docker/libnetwork/types"
  14. "github.com/vishvananda/netlink"
  15. "github.com/vishvananda/netlink/nl"
  16. "github.com/vishvananda/netns"
  17. )
  18. const (
  19. vethName1 = "wierdlongname1"
  20. vethName2 = "wierdlongname2"
  21. vethName3 = "wierdlongname3"
  22. vethName4 = "wierdlongname4"
  23. sboxIfaceName = "containername"
  24. )
  25. func generateRandomName(prefix string, size int) (string, error) {
  26. id := make([]byte, 32)
  27. if _, err := io.ReadFull(rand.Reader, id); err != nil {
  28. return "", err
  29. }
  30. return prefix + hex.EncodeToString(id)[:size], nil
  31. }
  32. func newKey(t *testing.T) (string, error) {
  33. name, err := generateRandomName("netns", 12)
  34. if err != nil {
  35. return "", err
  36. }
  37. name = filepath.Join("/tmp", name)
  38. if _, err := os.Create(name); err != nil {
  39. return "", err
  40. }
  41. // Set the rpmCleanupPeriod to be low to make the test run quicker
  42. gpmLock.Lock()
  43. gpmCleanupPeriod = 2 * time.Second
  44. gpmLock.Unlock()
  45. return name, nil
  46. }
  47. func newInfo(hnd *netlink.Handle, t *testing.T) (Sandbox, error) {
  48. veth := &netlink.Veth{
  49. LinkAttrs: netlink.LinkAttrs{Name: vethName1, TxQLen: 0},
  50. PeerName: vethName2}
  51. if err := hnd.LinkAdd(veth); err != nil {
  52. return nil, err
  53. }
  54. // Store the sandbox side pipe interface
  55. // This is needed for cleanup on DeleteEndpoint()
  56. intf1 := &nwIface{}
  57. intf1.srcName = vethName2
  58. intf1.dstName = sboxIfaceName
  59. ip4, addr, err := net.ParseCIDR("192.168.1.100/24")
  60. if err != nil {
  61. return nil, err
  62. }
  63. intf1.address = addr
  64. intf1.address.IP = ip4
  65. // ip6, addrv6, err := net.ParseCIDR("2001:DB8::ABCD/48")
  66. ip6, addrv6, err := net.ParseCIDR("fe80::2/64")
  67. if err != nil {
  68. return nil, err
  69. }
  70. intf1.addressIPv6 = addrv6
  71. intf1.addressIPv6.IP = ip6
  72. _, route, err := net.ParseCIDR("192.168.2.1/32")
  73. if err != nil {
  74. return nil, err
  75. }
  76. intf1.routes = []*net.IPNet{route}
  77. intf2 := &nwIface{}
  78. intf2.srcName = "testbridge"
  79. intf2.dstName = sboxIfaceName
  80. intf2.bridge = true
  81. veth = &netlink.Veth{
  82. LinkAttrs: netlink.LinkAttrs{Name: vethName3, TxQLen: 0},
  83. PeerName: vethName4}
  84. if err := hnd.LinkAdd(veth); err != nil {
  85. return nil, err
  86. }
  87. intf3 := &nwIface{}
  88. intf3.srcName = vethName4
  89. intf3.dstName = sboxIfaceName
  90. intf3.master = "testbridge"
  91. info := &networkNamespace{iFaces: []*nwIface{intf1, intf2, intf3}}
  92. info.gw = net.ParseIP("192.168.1.1")
  93. // sinfo.GatewayIPv6 = net.ParseIP("2001:DB8::1")
  94. info.gwv6 = net.ParseIP("fe80::1")
  95. return info, nil
  96. }
  97. func verifySandbox(t *testing.T, s Sandbox, ifaceSuffixes []string) {
  98. _, ok := s.(*networkNamespace)
  99. if !ok {
  100. t.Fatalf("The sandox interface returned is not of type networkNamespace")
  101. }
  102. sbNs, err := netns.GetFromPath(s.Key())
  103. if err != nil {
  104. t.Fatalf("Failed top open network namespace path %q: %v", s.Key(), err)
  105. }
  106. defer sbNs.Close()
  107. nh, err := netlink.NewHandleAt(sbNs)
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. defer nh.Delete()
  112. for _, suffix := range ifaceSuffixes {
  113. _, err = nh.LinkByName(sboxIfaceName + suffix)
  114. if err != nil {
  115. t.Fatalf("Could not find the interface %s inside the sandbox: %v",
  116. sboxIfaceName+suffix, err)
  117. }
  118. }
  119. }
  120. func verifyCleanup(t *testing.T, s Sandbox, wait bool) {
  121. if wait {
  122. time.Sleep(time.Duration(gpmCleanupPeriod * 2))
  123. }
  124. if _, err := os.Stat(s.Key()); err == nil {
  125. if wait {
  126. t.Fatalf("The sandbox path %s is not getting cleaned up even after twice the cleanup period", s.Key())
  127. } else {
  128. t.Fatalf("The sandbox path %s is not cleaned up after running gc", s.Key())
  129. }
  130. }
  131. }
  132. func TestScanStatistics(t *testing.T) {
  133. data :=
  134. "Inter-| Receive | Transmit\n" +
  135. " face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed\n" +
  136. " eth0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
  137. " wlan0: 7787685 11141 0 0 0 0 0 0 1681390 7220 0 0 0 0 0 0\n" +
  138. " lo: 783782 1853 0 0 0 0 0 0 783782 1853 0 0 0 0 0 0\n" +
  139. "lxcbr0: 0 0 0 0 0 0 0 0 9006 61 0 0 0 0 0 0\n"
  140. i := &types.InterfaceStatistics{}
  141. if err := scanInterfaceStats(data, "wlan0", i); err != nil {
  142. t.Fatal(err)
  143. }
  144. if i.TxBytes != 1681390 || i.TxPackets != 7220 || i.RxBytes != 7787685 || i.RxPackets != 11141 {
  145. t.Fatalf("Error scanning the statistics")
  146. }
  147. if err := scanInterfaceStats(data, "lxcbr0", i); err != nil {
  148. t.Fatal(err)
  149. }
  150. if i.TxBytes != 9006 || i.TxPackets != 61 || i.RxBytes != 0 || i.RxPackets != 0 {
  151. t.Fatalf("Error scanning the statistics")
  152. }
  153. }
  154. func TestDisableIPv6DAD(t *testing.T) {
  155. if testutils.RunningOnCircleCI() {
  156. t.Skipf("Skipping as not supported on CIRCLE CI kernel")
  157. }
  158. defer testutils.SetupTestOSContext(t)()
  159. ipv6, _ := types.ParseCIDR("2001:db8::44/64")
  160. iface := &nwIface{addressIPv6: ipv6}
  161. veth := &netlink.Veth{
  162. LinkAttrs: netlink.LinkAttrs{Name: "sideA"},
  163. PeerName: "sideB",
  164. }
  165. nlh, err := netlink.NewHandle(syscall.NETLINK_ROUTE)
  166. if err != nil {
  167. t.Fatal(err)
  168. }
  169. err = nlh.LinkAdd(veth)
  170. if err != nil {
  171. t.Fatal(err)
  172. }
  173. link, err := nlh.LinkByName("sideA")
  174. if err != nil {
  175. t.Fatal(err)
  176. }
  177. err = setInterfaceIPv6(nlh, link, iface)
  178. if err != nil {
  179. t.Fatal(err)
  180. }
  181. addrList, err := nlh.AddrList(link, nl.FAMILY_V6)
  182. if err != nil {
  183. t.Fatal(err)
  184. }
  185. if addrList[0].Flags&syscall.IFA_F_NODAD == 0 {
  186. t.Fatalf("Unexpected interface flags: 0x%x. Expected to contain 0x%x", addrList[0].Flags, syscall.IFA_F_NODAD)
  187. }
  188. }