sandbox_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package sandbox
  2. import (
  3. "net"
  4. "os"
  5. "testing"
  6. "github.com/docker/docker/pkg/reexec"
  7. )
  8. func TestMain(m *testing.M) {
  9. if reexec.Init() {
  10. return
  11. }
  12. os.Exit(m.Run())
  13. }
  14. func TestSandboxCreate(t *testing.T) {
  15. key, err := newKey(t)
  16. if err != nil {
  17. t.Fatalf("Failed to obtain a key: %v", err)
  18. }
  19. s, err := NewSandbox(key, true)
  20. if err != nil {
  21. t.Fatalf("Failed to create a new sandbox: %v", err)
  22. }
  23. if s.Key() != key {
  24. t.Fatalf("s.Key() returned %s. Expected %s", s.Key(), key)
  25. }
  26. info, err := newInfo(t)
  27. if err != nil {
  28. t.Fatalf("Failed to generate new sandbox info: %v", err)
  29. }
  30. for _, i := range info.Interfaces {
  31. err = s.AddInterface(i)
  32. if err != nil {
  33. t.Fatalf("Failed to add interfaces to sandbox: %v", err)
  34. }
  35. }
  36. err = s.SetGateway(info.Gateway)
  37. if err != nil {
  38. t.Fatalf("Failed to set gateway to sandbox: %v", err)
  39. }
  40. err = s.SetGatewayIPv6(info.GatewayIPv6)
  41. if err != nil {
  42. t.Fatalf("Failed to set ipv6 gateway to sandbox: %v", err)
  43. }
  44. verifySandbox(t, s)
  45. s.Destroy()
  46. verifyCleanup(t, s, true)
  47. }
  48. func TestSandboxCreateTwice(t *testing.T) {
  49. key, err := newKey(t)
  50. if err != nil {
  51. t.Fatalf("Failed to obtain a key: %v", err)
  52. }
  53. _, err = NewSandbox(key, true)
  54. if err != nil {
  55. t.Fatalf("Failed to create a new sandbox: %v", err)
  56. }
  57. // Create another sandbox with the same key to see if we handle it
  58. // gracefully.
  59. s, err := NewSandbox(key, true)
  60. if err != nil {
  61. t.Fatalf("Failed to create a new sandbox: %v", err)
  62. }
  63. s.Destroy()
  64. }
  65. func TestSandboxGC(t *testing.T) {
  66. key, err := newKey(t)
  67. if err != nil {
  68. t.Fatalf("Failed to obtain a key: %v", err)
  69. }
  70. s, err := NewSandbox(key, true)
  71. if err != nil {
  72. t.Fatalf("Failed to create a new sandbox: %v", err)
  73. }
  74. s.Destroy()
  75. GC()
  76. verifyCleanup(t, s, false)
  77. }
  78. func TestAddRemoveInterface(t *testing.T) {
  79. key, err := newKey(t)
  80. if err != nil {
  81. t.Fatalf("Failed to obtain a key: %v", err)
  82. }
  83. s, err := NewSandbox(key, true)
  84. if err != nil {
  85. t.Fatalf("Failed to create a new sandbox: %v", err)
  86. }
  87. if s.Key() != key {
  88. t.Fatalf("s.Key() returned %s. Expected %s", s.Key(), key)
  89. }
  90. info, err := newInfo(t)
  91. if err != nil {
  92. t.Fatalf("Failed to generate new sandbox info: %v", err)
  93. }
  94. for _, i := range info.Interfaces {
  95. err = s.AddInterface(i)
  96. if err != nil {
  97. t.Fatalf("Failed to add interfaces to sandbox: %v", err)
  98. }
  99. }
  100. interfaces := s.Interfaces()
  101. if !(interfaces[0].Equal(info.Interfaces[0]) && interfaces[1].Equal(info.Interfaces[1])) {
  102. t.Fatalf("Failed to update Sandbox.sinfo.Interfaces in AddInterfaces")
  103. }
  104. if err := s.RemoveInterface(info.Interfaces[0]); err != nil {
  105. t.Fatalf("Failed to remove interfaces from sandbox: %v", err)
  106. }
  107. if !s.Interfaces()[0].Equal(info.Interfaces[1]) {
  108. t.Fatalf("Failed to update the sanbox.sinfo.Interfaces in RemoveInterferce")
  109. }
  110. if err := s.AddInterface(info.Interfaces[0]); err != nil {
  111. t.Fatalf("Failed to add interfaces to sandbox: %v", err)
  112. }
  113. interfaces = s.Interfaces()
  114. if !(interfaces[0].Equal(info.Interfaces[1]) && interfaces[1].Equal(info.Interfaces[0])) {
  115. t.Fatalf("Failed to update Sandbox.sinfo.Interfaces in AddInterfaces")
  116. }
  117. s.Destroy()
  118. }
  119. func TestInterfaceEqual(t *testing.T) {
  120. list := getInterfaceList()
  121. if !list[0].Equal(list[0]) {
  122. t.Fatalf("Interface.Equal() returned false negative")
  123. }
  124. if list[0].Equal(list[1]) {
  125. t.Fatalf("Interface.Equal() returned false positive")
  126. }
  127. if list[0].Equal(list[1]) != list[1].Equal(list[0]) {
  128. t.Fatalf("Interface.Equal() failed commutative check")
  129. }
  130. }
  131. func TestSandboxInfoEqual(t *testing.T) {
  132. si1 := &Info{Interfaces: getInterfaceList(), Gateway: net.ParseIP("192.168.1.254"), GatewayIPv6: net.ParseIP("2001:2345::abcd:8889")}
  133. si2 := &Info{Interfaces: getInterfaceList(), Gateway: net.ParseIP("172.18.255.254"), GatewayIPv6: net.ParseIP("2001:2345::abcd:8888")}
  134. if !si1.Equal(si1) {
  135. t.Fatalf("Info.Equal() returned false negative")
  136. }
  137. if si1.Equal(si2) {
  138. t.Fatalf("Info.Equal() returned false positive")
  139. }
  140. if si1.Equal(si2) != si2.Equal(si1) {
  141. t.Fatalf("Info.Equal() failed commutative check")
  142. }
  143. }
  144. func TestInterfaceCopy(t *testing.T) {
  145. for _, iface := range getInterfaceList() {
  146. cp := iface.GetCopy()
  147. if !iface.Equal(cp) {
  148. t.Fatalf("Failed to return a copy of Interface")
  149. }
  150. if iface == cp {
  151. t.Fatalf("Failed to return a true copy of Interface")
  152. }
  153. }
  154. }
  155. func TestSandboxInfoCopy(t *testing.T) {
  156. si := Info{Interfaces: getInterfaceList(), Gateway: net.ParseIP("192.168.1.254"), GatewayIPv6: net.ParseIP("2001:2345::abcd:8889")}
  157. cp := si.GetCopy()
  158. if !si.Equal(cp) {
  159. t.Fatalf("Failed to return a copy of Info")
  160. }
  161. if &si == cp {
  162. t.Fatalf("Failed to return a true copy of Info")
  163. }
  164. }
  165. func getInterfaceList() []*Interface {
  166. _, netv4a, _ := net.ParseCIDR("192.168.30.1/24")
  167. _, netv4b, _ := net.ParseCIDR("172.18.255.2/23")
  168. _, netv6a, _ := net.ParseCIDR("2001:2345::abcd:8888/80")
  169. _, netv6b, _ := net.ParseCIDR("2001:2345::abcd:8889/80")
  170. return []*Interface{
  171. &Interface{
  172. SrcName: "veth1234567",
  173. DstName: "eth0",
  174. Address: netv4a,
  175. AddressIPv6: netv6a,
  176. Routes: []*net.IPNet{netv4a, netv6a},
  177. },
  178. &Interface{
  179. SrcName: "veth7654321",
  180. DstName: "eth1",
  181. Address: netv4b,
  182. AddressIPv6: netv6b,
  183. Routes: []*net.IPNet{netv4b, netv6b},
  184. },
  185. }
  186. }