sandbox_linux_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package osl
  2. import (
  3. "crypto/rand"
  4. "encoding/hex"
  5. "io"
  6. "net"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "syscall"
  11. "testing"
  12. "time"
  13. "github.com/docker/libnetwork/testutils"
  14. "github.com/docker/libnetwork/types"
  15. "github.com/vishvananda/netlink"
  16. "github.com/vishvananda/netlink/nl"
  17. "github.com/vishvananda/netns"
  18. )
  19. const (
  20. vethName1 = "wierdlongname1"
  21. vethName2 = "wierdlongname2"
  22. vethName3 = "wierdlongname3"
  23. vethName4 = "wierdlongname4"
  24. sboxIfaceName = "containername"
  25. )
  26. func generateRandomName(prefix string, size int) (string, error) {
  27. id := make([]byte, 32)
  28. if _, err := io.ReadFull(rand.Reader, id); err != nil {
  29. return "", err
  30. }
  31. return prefix + hex.EncodeToString(id)[:size], nil
  32. }
  33. func newKey(t *testing.T) (string, error) {
  34. name, err := generateRandomName("netns", 12)
  35. if err != nil {
  36. return "", err
  37. }
  38. name = filepath.Join("/tmp", name)
  39. if _, err := os.Create(name); err != nil {
  40. return "", err
  41. }
  42. // Set the rpmCleanupPeriod to be low to make the test run quicker
  43. gpmLock.Lock()
  44. gpmCleanupPeriod = 2 * time.Second
  45. gpmLock.Unlock()
  46. return name, nil
  47. }
  48. func newInfo(hnd *netlink.Handle, t *testing.T) (Sandbox, error) {
  49. veth := &netlink.Veth{
  50. LinkAttrs: netlink.LinkAttrs{Name: vethName1, TxQLen: 0},
  51. PeerName: vethName2}
  52. if err := hnd.LinkAdd(veth); err != nil {
  53. return nil, err
  54. }
  55. // Store the sandbox side pipe interface
  56. // This is needed for cleanup on DeleteEndpoint()
  57. intf1 := &nwIface{}
  58. intf1.srcName = vethName2
  59. intf1.dstName = sboxIfaceName
  60. ip4, addr, err := net.ParseCIDR("192.168.1.100/24")
  61. if err != nil {
  62. return nil, err
  63. }
  64. intf1.address = addr
  65. intf1.address.IP = ip4
  66. ip6, addrv6, err := net.ParseCIDR("2001:DB8::ABCD/48")
  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. }
  189. func TestSetInterfaceIP(t *testing.T) {
  190. defer testutils.SetupTestOSContext(t)()
  191. ipv4, _ := types.ParseCIDR("172.30.0.33/24")
  192. ipv6, _ := types.ParseCIDR("2001:db8::44/64")
  193. iface := &nwIface{address: ipv4, addressIPv6: ipv6}
  194. nlh, err := netlink.NewHandle(syscall.NETLINK_ROUTE)
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. if err := nlh.LinkAdd(&netlink.Veth{
  199. LinkAttrs: netlink.LinkAttrs{Name: "sideA"},
  200. PeerName: "sideB",
  201. }); err != nil {
  202. t.Fatal(err)
  203. }
  204. linkA, err := nlh.LinkByName("sideA")
  205. if err != nil {
  206. t.Fatal(err)
  207. }
  208. linkB, err := nlh.LinkByName("sideB")
  209. if err != nil {
  210. t.Fatal(err)
  211. }
  212. if err := nlh.LinkSetUp(linkA); err != nil {
  213. t.Fatal(err)
  214. }
  215. if err := nlh.LinkSetUp(linkB); err != nil {
  216. t.Fatal(err)
  217. }
  218. if err := setInterfaceIP(nlh, linkA, iface); err != nil {
  219. t.Fatal(err)
  220. }
  221. if err := setInterfaceIPv6(nlh, linkA, iface); err != nil {
  222. t.Fatal(err)
  223. }
  224. err = setInterfaceIP(nlh, linkB, iface)
  225. if err == nil {
  226. t.Fatalf("Expected route conflict error, but succeeded")
  227. }
  228. if !strings.Contains(err.Error(), "conflicts with existing route") {
  229. t.Fatalf("Unexpected error: %v", err)
  230. }
  231. err = setInterfaceIPv6(nlh, linkB, iface)
  232. if err == nil {
  233. t.Fatalf("Expected route conflict error, but succeeded")
  234. }
  235. if !strings.Contains(err.Error(), "conflicts with existing route") {
  236. t.Fatalf("Unexpected error: %v", err)
  237. }
  238. }