sandbox_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package sandbox
  2. import (
  3. "net"
  4. "testing"
  5. )
  6. func TestSandboxCreate(t *testing.T) {
  7. key, err := newKey(t)
  8. if err != nil {
  9. t.Fatalf("Failed to obtain a key: %v", err)
  10. }
  11. s, err := NewSandbox(key)
  12. if err != nil {
  13. t.Fatalf("Failed to create a new sandbox: %v", err)
  14. }
  15. if s.Key() != key {
  16. t.Fatalf("s.Key() returned %s. Expected %s", s.Key(), key)
  17. }
  18. info, err := newInfo(t)
  19. if err != nil {
  20. t.Fatalf("Failed to generate new sandbox info: %v", err)
  21. }
  22. for _, i := range info.Interfaces {
  23. err = s.AddInterface(i)
  24. if err != nil {
  25. t.Fatalf("Failed to add interfaces to sandbox: %v", err)
  26. }
  27. }
  28. err = s.SetGateway(info.Gateway)
  29. if err != nil {
  30. t.Fatalf("Failed to set gateway to sandbox: %v", err)
  31. }
  32. err = s.SetGatewayIPv6(info.GatewayIPv6)
  33. if err != nil {
  34. t.Fatalf("Failed to set ipv6 gateway to sandbox: %v", err)
  35. }
  36. verifySandbox(t, s)
  37. s.Destroy()
  38. }
  39. func TestInterfaceEqual(t *testing.T) {
  40. list := getInterfaceList()
  41. if !list[0].Equal(list[0]) {
  42. t.Fatalf("Interface.Equal() returned false negative")
  43. }
  44. if list[0].Equal(list[1]) {
  45. t.Fatalf("Interface.Equal() returned false positive")
  46. }
  47. if list[0].Equal(list[1]) != list[1].Equal(list[0]) {
  48. t.Fatalf("Interface.Equal() failed commutative check")
  49. }
  50. }
  51. func TestSandboxInfoEqual(t *testing.T) {
  52. si1 := &Info{Interfaces: getInterfaceList(), Gateway: net.ParseIP("192.168.1.254"), GatewayIPv6: net.ParseIP("2001:2345::abcd:8889")}
  53. si2 := &Info{Interfaces: getInterfaceList(), Gateway: net.ParseIP("172.18.255.254"), GatewayIPv6: net.ParseIP("2001:2345::abcd:8888")}
  54. if !si1.Equal(si1) {
  55. t.Fatalf("Info.Equal() returned false negative")
  56. }
  57. if si1.Equal(si2) {
  58. t.Fatalf("Info.Equal() returned false positive")
  59. }
  60. if si1.Equal(si2) != si2.Equal(si1) {
  61. t.Fatalf("Info.Equal() failed commutative check")
  62. }
  63. }
  64. func TestInterfaceCopy(t *testing.T) {
  65. for _, iface := range getInterfaceList() {
  66. cp := iface.GetCopy()
  67. if !iface.Equal(cp) {
  68. t.Fatalf("Failed to return a copy of Interface")
  69. }
  70. if iface == cp {
  71. t.Fatalf("Failed to return a true copy of Interface")
  72. }
  73. }
  74. }
  75. func TestSandboxInfoCopy(t *testing.T) {
  76. si := Info{Interfaces: getInterfaceList(), Gateway: net.ParseIP("192.168.1.254"), GatewayIPv6: net.ParseIP("2001:2345::abcd:8889")}
  77. cp := si.GetCopy()
  78. if !si.Equal(cp) {
  79. t.Fatalf("Failed to return a copy of Info")
  80. }
  81. if &si == cp {
  82. t.Fatalf("Failed to return a true copy of Info")
  83. }
  84. }
  85. func getInterfaceList() []*Interface {
  86. _, netv4a, _ := net.ParseCIDR("192.168.30.1/24")
  87. _, netv4b, _ := net.ParseCIDR("172.18.255.2/23")
  88. _, netv6a, _ := net.ParseCIDR("2001:2345::abcd:8888/80")
  89. _, netv6b, _ := net.ParseCIDR("2001:2345::abcd:8889/80")
  90. return []*Interface{
  91. &Interface{
  92. SrcName: "veth1234567",
  93. DstName: "eth0",
  94. Address: netv4a,
  95. AddressIPv6: netv6a,
  96. },
  97. &Interface{
  98. SrcName: "veth7654321",
  99. DstName: "eth1",
  100. Address: netv4b,
  101. AddressIPv6: netv6b,
  102. },
  103. }
  104. }