macvlan_setup_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package macvlan
  2. import (
  3. "testing"
  4. "github.com/vishvananda/netlink"
  5. )
  6. // TestValidateLink tests the parentExists function
  7. func TestValidateLink(t *testing.T) {
  8. validIface := "lo"
  9. invalidIface := "foo12345"
  10. // test a valid parent interface validation
  11. if ok := parentExists(validIface); !ok {
  12. t.Fatalf("failed validating loopback %s", validIface)
  13. }
  14. // test an invalid parent interface validation
  15. if ok := parentExists(invalidIface); ok {
  16. t.Fatalf("failed to invalidate interface %s", invalidIface)
  17. }
  18. }
  19. // TestValidateSubLink tests valid 802.1q naming convention
  20. func TestValidateSubLink(t *testing.T) {
  21. validSubIface := "lo.10"
  22. invalidSubIface1 := "lo"
  23. invalidSubIface2 := "lo:10"
  24. invalidSubIface3 := "foo123.456"
  25. // test a valid parent_iface.vlan_id
  26. _, _, err := parseVlan(validSubIface)
  27. if err != nil {
  28. t.Fatalf("failed subinterface validation: %v", err)
  29. }
  30. // test an invalid vid with a valid parent link
  31. _, _, err = parseVlan(invalidSubIface1)
  32. if err == nil {
  33. t.Fatalf("failed subinterface validation test: %s", invalidSubIface1)
  34. }
  35. // test a valid vid with a valid parent link with an invalid delimiter
  36. _, _, err = parseVlan(invalidSubIface2)
  37. if err == nil {
  38. t.Fatalf("failed subinterface validation test: %v", invalidSubIface2)
  39. }
  40. // test an invalid parent link with a valid vid
  41. _, _, err = parseVlan(invalidSubIface3)
  42. if err == nil {
  43. t.Fatalf("failed subinterface validation test: %v", invalidSubIface3)
  44. }
  45. }
  46. // TestSetMacVlanMode tests the macvlan mode setter
  47. func TestSetMacVlanMode(t *testing.T) {
  48. // test macvlan bridge mode
  49. mode, err := setMacVlanMode(modeBridge)
  50. if err != nil {
  51. t.Fatalf("error parsing %v vlan mode: %v", mode, err)
  52. }
  53. if mode != netlink.MACVLAN_MODE_BRIDGE {
  54. t.Fatalf("expected %d got %d", netlink.MACVLAN_MODE_BRIDGE, mode)
  55. }
  56. // test macvlan passthrough mode
  57. mode, err = setMacVlanMode(modePassthru)
  58. if err != nil {
  59. t.Fatalf("error parsing %v vlan mode: %v", mode, err)
  60. }
  61. if mode != netlink.MACVLAN_MODE_PASSTHRU {
  62. t.Fatalf("expected %d got %d", netlink.MACVLAN_MODE_PASSTHRU, mode)
  63. }
  64. // test macvlan private mode
  65. mode, err = setMacVlanMode(modePrivate)
  66. if err != nil {
  67. t.Fatalf("error parsing %v vlan mode: %v", mode, err)
  68. }
  69. if mode != netlink.MACVLAN_MODE_PRIVATE {
  70. t.Fatalf("expected %d got %d", netlink.MACVLAN_MODE_PRIVATE, mode)
  71. }
  72. // test macvlan vepa mode
  73. mode, err = setMacVlanMode(modeVepa)
  74. if err != nil {
  75. t.Fatalf("error parsing %v vlan mode: %v", mode, err)
  76. }
  77. if mode != netlink.MACVLAN_MODE_VEPA {
  78. t.Fatalf("expected %d got %d", netlink.MACVLAN_MODE_VEPA, mode)
  79. }
  80. // test invalid mode
  81. mode, err = setMacVlanMode("foo")
  82. if err == nil {
  83. t.Fatal("invalid macvlan mode should have returned an error")
  84. }
  85. if mode != 0 {
  86. t.Fatalf("expected 0 got %d", mode)
  87. }
  88. // test null mode
  89. mode, err = setMacVlanMode("")
  90. if err == nil {
  91. t.Fatal("invalid macvlan mode should have returned an error")
  92. }
  93. if mode != 0 {
  94. t.Fatalf("expected 0 got %d", mode)
  95. }
  96. }