netconf_linux_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package netconf
  2. import (
  3. "net"
  4. "testing"
  5. "github.com/vishvananda/netlink"
  6. )
  7. type mockLink struct {
  8. attrs netlink.LinkAttrs
  9. t string
  10. }
  11. func (l mockLink) Attrs() *netlink.LinkAttrs {
  12. return &l.attrs
  13. }
  14. func (l mockLink) Type() string {
  15. return l.t
  16. }
  17. func TestFindMatch(t *testing.T) {
  18. testCases := []struct {
  19. match string
  20. mac string
  21. t string
  22. name string
  23. bond string
  24. expected bool
  25. }{
  26. {
  27. "mac:aa:bb:cc:dd:ee:ff",
  28. "aa:bb:cc:dd:ee:ff",
  29. "fake",
  30. "eth0",
  31. "bond0",
  32. true,
  33. },
  34. {
  35. "mac:aa:bb:cc:*",
  36. "aa:bb:cc:12:34:56",
  37. "fake",
  38. "eth0",
  39. "bond0",
  40. true,
  41. },
  42. {
  43. "mac:aa:bb:cc:*",
  44. "11:bb:cc:dd:ee:ff",
  45. "fake",
  46. "eth0",
  47. "bond0",
  48. false,
  49. },
  50. {
  51. "mac:aa:bb:cc:dd:ee:ff",
  52. "aa:bb:cc:dd:ee:11",
  53. "fake",
  54. "eth0",
  55. "bond0",
  56. false,
  57. },
  58. // This is a bond eg. bond0
  59. {
  60. "mac:aa:bb:*",
  61. "aa:bb:cc:dd:ee:11",
  62. "bond",
  63. "bond0",
  64. "bond0",
  65. false,
  66. },
  67. }
  68. for i, tt := range testCases {
  69. netCfg := NetworkConfig{
  70. Interfaces: map[string]InterfaceConfig{
  71. tt.name: InterfaceConfig{
  72. Match: tt.match,
  73. Bond: tt.bond,
  74. },
  75. },
  76. }
  77. linkAttrs := netlink.NewLinkAttrs()
  78. linkAttrs.Name = tt.name
  79. linkAttrs.HardwareAddr, _ = net.ParseMAC(tt.mac)
  80. link := mockLink{attrs: linkAttrs}
  81. _, match := findMatch(link, &netCfg)
  82. if match != tt.expected {
  83. t.Errorf("Test case %d failed: mac: '%s' match '%s' expected: '%v' got: '%v'", i, tt.mac, tt.match, tt.expected, match)
  84. }
  85. }
  86. }