driverapi_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package driverapi
  2. import (
  3. "net"
  4. "testing"
  5. )
  6. func TestInterfaceEqual(t *testing.T) {
  7. list := getInterfaceList()
  8. if !list[0].Equal(list[0]) {
  9. t.Fatalf("Interface.Equal() returned false negative")
  10. }
  11. if list[0].Equal(list[1]) {
  12. t.Fatalf("Interface.Equal() returned false positive")
  13. }
  14. if list[0].Equal(list[1]) != list[1].Equal(list[0]) {
  15. t.Fatalf("Interface.Equal() failed commutative check")
  16. }
  17. }
  18. func TestInterfaceCopy(t *testing.T) {
  19. for _, iface := range getInterfaceList() {
  20. cp := iface.GetCopy()
  21. if !iface.Equal(cp) {
  22. t.Fatalf("Failed to return a copy of Interface")
  23. }
  24. if iface == cp {
  25. t.Fatalf("Failed to return a true copy of Interface")
  26. }
  27. }
  28. }
  29. func TestSandboxInfoCopy(t *testing.T) {
  30. si := SandboxInfo{Interfaces: getInterfaceList(), Gateway: net.ParseIP("192.168.1.254"), GatewayIPv6: net.ParseIP("2001:2345::abcd:8889")}
  31. cp := si.GetCopy()
  32. if !si.Equal(cp) {
  33. t.Fatalf("Failed to return a copy of SandboxInfo")
  34. }
  35. if &si == cp {
  36. t.Fatalf("Failed to return a true copy of SanboxInfo")
  37. }
  38. }
  39. func getInterfaceList() []*Interface {
  40. _, netv4a, _ := net.ParseCIDR("192.168.30.1/24")
  41. _, netv4b, _ := net.ParseCIDR("172.18.255.2/23")
  42. _, netv6a, _ := net.ParseCIDR("2001:2345::abcd:8888/80")
  43. _, netv6b, _ := net.ParseCIDR("2001:2345::abcd:8889/80")
  44. return []*Interface{
  45. &Interface{
  46. SrcName: "veth1234567",
  47. DstName: "eth0",
  48. Address: netv4a,
  49. AddressIPv6: netv6a,
  50. },
  51. &Interface{
  52. SrcName: "veth7654321",
  53. DstName: "eth1",
  54. Address: netv4b,
  55. AddressIPv6: netv6b,
  56. },
  57. }
  58. }