defgw_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package netutils2
  15. import (
  16. "testing"
  17. )
  18. func TestFindDefaultNic(t *testing.T) {
  19. cases := []struct {
  20. nics [][]string
  21. mac string
  22. index int
  23. }{
  24. {
  25. nics: [][]string{
  26. {"192.168.202.147", "00:22:00:00:00:01", "192.168.202.1", "", "", "false"},
  27. },
  28. mac: "00:22:00:00:00:01",
  29. index: 0,
  30. },
  31. {
  32. nics: [][]string{
  33. {"192.168.202.147", "00:22:00:00:00:01", "192.168.202.1", "", "", "false"},
  34. {"192.168.203.147", "00:22:00:00:00:02", "192.168.203.1", "", "", "false"},
  35. },
  36. mac: "00:22:00:00:00:01",
  37. index: 0,
  38. },
  39. {
  40. nics: [][]string{
  41. {"192.168.202.147", "00:22:00:00:00:01", "192.168.202.1", "", "", "false"},
  42. {"192.168.203.147", "00:22:00:00:00:02", "192.168.203.1", "", "", "true"},
  43. },
  44. mac: "00:22:00:00:00:02",
  45. index: 1,
  46. },
  47. {
  48. nics: [][]string{
  49. {"", "00:22:00:00:00:01", "", "3ffe:3200:fe::fb", "3ffe:3200:fe::1", "false"},
  50. {"", "00:22:00:00:00:02", "", "3ffe:3200:ff::fb", "3ffe:3200:ff::1", "true"},
  51. },
  52. mac: "00:22:00:00:00:02",
  53. index: 1,
  54. },
  55. {
  56. nics: [][]string{
  57. {"", "00:22:00:00:00:01", "", "3ffe:3200:fe::fb", "3ffe:3200:fe::1", "false"},
  58. {"", "00:22:00:00:00:02", "", "3ffe:3200:ff::fb", "", "true"},
  59. },
  60. mac: "00:22:00:00:00:01",
  61. index: 0,
  62. },
  63. {
  64. nics: [][]string{
  65. {"192.168.202.147", "00:22:00:00:00:01", "192.168.202.1", "", "", "false"},
  66. {"202.168.202.147", "00:22:00:00:00:02", "202.168.202.1", "", "", "false"},
  67. },
  68. mac: "00:22:00:00:00:02",
  69. index: 1,
  70. },
  71. {
  72. nics: [][]string{
  73. {"202.168.202.147", "00:22:00:00:00:01", "202.168.202.1", "", "", "false"},
  74. {"192.168.202.147", "00:22:00:00:00:02", "192.168.202.1", "", "", "false"},
  75. },
  76. mac: "00:22:00:00:00:01",
  77. index: 0,
  78. },
  79. {
  80. nics: [][]string{
  81. {"192.168.202.147", "00:22:00:00:00:01", "192.168.202.1", "", "", "false"},
  82. {"", "00:22:00:00:00:02", "", "", "", "false"},
  83. {"202.168.202.147", "00:22:00:00:00:03", "202.168.202.1", "", "", "false"},
  84. },
  85. mac: "00:22:00:00:00:03",
  86. index: 2,
  87. },
  88. }
  89. for _, c := range cases {
  90. nics := SNicInfoList{}
  91. for _, n := range c.nics {
  92. nics = nics.Add(n[1], n[0], n[2], n[3], n[4], n[5] == "true")
  93. }
  94. gotMac, gotIdx := nics.FindDefaultNicMac()
  95. if gotMac != c.mac || gotIdx != c.index {
  96. t.Errorf("want %s (%d) got %s (%d)", c.mac, c.index, gotMac, gotIdx)
  97. }
  98. }
  99. }