sandbox_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. verifySandbox(t, s)
  19. }
  20. func TestInterfaceEqual(t *testing.T) {
  21. list := getInterfaceList()
  22. if !list[0].Equal(list[0]) {
  23. t.Fatalf("Interface.Equal() returned false negative")
  24. }
  25. if list[0].Equal(list[1]) {
  26. t.Fatalf("Interface.Equal() returned false positive")
  27. }
  28. if list[0].Equal(list[1]) != list[1].Equal(list[0]) {
  29. t.Fatalf("Interface.Equal() failed commutative check")
  30. }
  31. }
  32. func TestInterfaceCopy(t *testing.T) {
  33. for _, iface := range getInterfaceList() {
  34. cp := iface.GetCopy()
  35. if !iface.Equal(cp) {
  36. t.Fatalf("Failed to return a copy of Interface")
  37. }
  38. if iface == cp {
  39. t.Fatalf("Failed to return a true copy of Interface")
  40. }
  41. }
  42. }
  43. func TestSandboxInfoCopy(t *testing.T) {
  44. si := Info{Interfaces: getInterfaceList(), Gateway: net.ParseIP("192.168.1.254"), GatewayIPv6: net.ParseIP("2001:2345::abcd:8889")}
  45. cp := si.GetCopy()
  46. if !si.Equal(cp) {
  47. t.Fatalf("Failed to return a copy of Info")
  48. }
  49. if &si == cp {
  50. t.Fatalf("Failed to return a true copy of Info")
  51. }
  52. }
  53. func getInterfaceList() []*Interface {
  54. _, netv4a, _ := net.ParseCIDR("192.168.30.1/24")
  55. _, netv4b, _ := net.ParseCIDR("172.18.255.2/23")
  56. _, netv6a, _ := net.ParseCIDR("2001:2345::abcd:8888/80")
  57. _, netv6b, _ := net.ParseCIDR("2001:2345::abcd:8889/80")
  58. return []*Interface{
  59. &Interface{
  60. SrcName: "veth1234567",
  61. DstName: "eth0",
  62. Address: netv4a,
  63. AddressIPv6: netv6a,
  64. },
  65. &Interface{
  66. SrcName: "veth7654321",
  67. DstName: "eth1",
  68. Address: netv4b,
  69. AddressIPv6: netv6b,
  70. },
  71. }
  72. }