networkmanager_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 fsdriver
  15. import (
  16. "testing"
  17. "yunion.io/x/onecloud/pkg/cloudcommon/types"
  18. )
  19. func TestNicDescToNetworkManager(t *testing.T) {
  20. cases := []struct {
  21. nicDesc *types.SServerNic
  22. mainIp string
  23. mainIp6 string
  24. want string
  25. }{
  26. {
  27. nicDesc: &types.SServerNic{
  28. Name: "eth0",
  29. Ip: "192.168.1.100",
  30. Masklen: 24,
  31. Gateway: "192.168.1.1",
  32. Ip6: "2001:db8::200",
  33. Masklen6: 64,
  34. Gateway6: "2001:db8::1",
  35. Mac: "00:22:0a:0b:0c:0d",
  36. Manual: false,
  37. Mtu: 1440,
  38. },
  39. mainIp: "192.168.1.100",
  40. mainIp6: "2001:db8::1",
  41. want: `[connection]
  42. id=eth0
  43. uuid=de05e375-af25-9477-66ca-d4f41fe5e750
  44. interface-name=eth0
  45. type=ethernet
  46. autoconnect=true
  47. [ethernet]
  48. mac-address=00:22:0a:0b:0c:0d
  49. mtu=1440
  50. [ipv4]
  51. method=auto
  52. [ipv6]
  53. method=auto
  54. `,
  55. },
  56. {
  57. nicDesc: &types.SServerNic{
  58. Name: "eth0",
  59. Ip: "192.168.1.100",
  60. Masklen: 24,
  61. Gateway: "192.168.1.1",
  62. Ip6: "2001:db8::200",
  63. Masklen6: 64,
  64. Gateway6: "2001:db8::1",
  65. Mac: "00:22:0a:0b:0c:0d",
  66. Manual: true,
  67. },
  68. mainIp: "192.168.1.100",
  69. mainIp6: "2001:db8::200",
  70. want: `[connection]
  71. id=eth0
  72. uuid=de05e375-af25-9477-66ca-d4f41fe5e750
  73. interface-name=eth0
  74. type=ethernet
  75. autoconnect=true
  76. [ethernet]
  77. mac-address=00:22:0a:0b:0c:0d
  78. [ipv4]
  79. method=manual
  80. address1=192.168.1.100/24
  81. gateway=192.168.1.1
  82. route1=169.254.169.254/32
  83. [ipv6]
  84. method=manual
  85. address1=2001:db8::200/64
  86. gateway=2001:db8::1
  87. route1=fd00:ec2::254/128
  88. `,
  89. },
  90. {
  91. nicDesc: &types.SServerNic{
  92. Name: "eth0",
  93. Ip: "192.168.1.100",
  94. Masklen: 24,
  95. Gateway: "192.168.1.1",
  96. Ip6: "2001:db8::200",
  97. Masklen6: 64,
  98. Gateway6: "2001:db8::1",
  99. Mac: "00:22:0a:0b:0c:0d",
  100. Domain: "onecloud.io",
  101. Dns: "192.168.1.1,192.168.1.2,fc00::3fe:1",
  102. Manual: true,
  103. },
  104. mainIp: "192.168.2.100",
  105. mainIp6: "2001:db7::100",
  106. want: `[connection]
  107. id=eth0
  108. uuid=de05e375-af25-9477-66ca-d4f41fe5e750
  109. interface-name=eth0
  110. type=ethernet
  111. autoconnect=true
  112. [ethernet]
  113. mac-address=00:22:0a:0b:0c:0d
  114. [ipv4]
  115. method=manual
  116. address1=192.168.1.100/24
  117. dns=192.168.1.1,192.168.1.2
  118. [ipv6]
  119. method=manual
  120. address1=2001:db8::200/64
  121. dns=fc00::3fe:1
  122. `,
  123. },
  124. {
  125. nicDesc: &types.SServerNic{
  126. Name: "bond0",
  127. Ip: "192.168.1.100",
  128. Masklen: 24,
  129. Gateway: "192.168.1.1",
  130. Ip6: "2001:db8::200",
  131. Masklen6: 64,
  132. Gateway6: "2001:db8::1",
  133. Mac: "00:22:0a:0b:0c:0d",
  134. Domain: "onecloud.io",
  135. Dns: "192.168.1.1,192.168.1.2,fc00::3fe:1",
  136. TeamingSlaves: []*types.SServerNic{
  137. {
  138. Name: "eth0",
  139. },
  140. {
  141. Name: "eth1",
  142. },
  143. },
  144. },
  145. mainIp: "192.168.2.100",
  146. mainIp6: "2001:db7::100",
  147. want: `[connection]
  148. id=bond0
  149. uuid=01bf7157-f43f-58f4-15b0-cfb6d6dfdb5b
  150. interface-name=bond0
  151. type=bond
  152. autoconnect=true
  153. [bond]
  154. mode=802.3ad
  155. miimon=100
  156. [ethernet]
  157. mac-address=00:22:0a:0b:0c:0d
  158. [ipv4]
  159. method=auto
  160. [ipv6]
  161. method=auto
  162. `,
  163. },
  164. {
  165. nicDesc: &types.SServerNic{
  166. Name: "eth0",
  167. Ip: "192.168.1.100",
  168. Masklen: 24,
  169. Gateway: "192.168.1.1",
  170. Ip6: "2001:db8::200",
  171. Masklen6: 64,
  172. Gateway6: "2001:db8::1",
  173. Mac: "00:22:0a:0b:0c:0d",
  174. Domain: "onecloud.io",
  175. Dns: "192.168.1.1,192.168.1.2,fc00::3fe:1",
  176. TeamingMaster: &types.SServerNic{
  177. Name: "bond0",
  178. },
  179. },
  180. mainIp: "192.168.2.100",
  181. mainIp6: "2001:db7::100",
  182. want: `[connection]
  183. id=eth0
  184. uuid=de05e375-af25-9477-66ca-d4f41fe5e750
  185. interface-name=eth0
  186. type=ethernet
  187. master=bond0
  188. slave-type=bond
  189. [ethernet]
  190. mac-address=00:22:0a:0b:0c:0d
  191. [ipv4]
  192. method=disabled
  193. [ipv6]
  194. method=disabled
  195. `,
  196. },
  197. }
  198. for _, c := range cases {
  199. got := nicDescToNetworkManager(c.nicDesc, c.mainIp, c.mainIp6, 1)
  200. if got != c.want {
  201. t.Errorf("[[got]]\n%s\n[[want]]\n%s\n[[end]]", got, c.want)
  202. }
  203. }
  204. }