netplan_test.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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/jsonutils"
  18. "yunion.io/x/onecloud/pkg/cloudcommon/types"
  19. "yunion.io/x/onecloud/pkg/util/netplan"
  20. )
  21. func TestNewNetplanConfig(t *testing.T) {
  22. cases := []struct {
  23. mainIp string
  24. nics []*types.SServerNic
  25. want *netplan.Configuration
  26. mainIp6 string
  27. }{
  28. {
  29. mainIp: "10.168.222.175",
  30. nics: []*types.SServerNic{
  31. {
  32. Name: "eth0",
  33. Index: 0,
  34. Bridge: "br0",
  35. Domain: "cloud.local",
  36. Ip: "10.168.222.175",
  37. Vlan: 1,
  38. Driver: "virtio",
  39. Masklen: 24,
  40. Virtual: false,
  41. Manual: true,
  42. WireId: "399a06f3-7925-46c1-8b9f-d5a8580a74df",
  43. NetId: "22c93412-5882-4de0-8357-45ce647ceada",
  44. Mac: "00:24:c7:16:80:f2",
  45. BandWidth: 1000,
  46. Mtu: 1500,
  47. Dns: "8.8.8.8",
  48. Ntp: "",
  49. Net: "vnet222",
  50. Interface: "ens5",
  51. Gateway: "10.168.222.1",
  52. Ifname: "vnet222-175",
  53. Routes: nil,
  54. LinkUp: true,
  55. },
  56. {
  57. Name: "eth1",
  58. Index: 1,
  59. Bridge: "br0",
  60. Domain: "cloud.local",
  61. Ip: "",
  62. Vlan: 1,
  63. Driver: "virtio",
  64. Masklen: 0,
  65. Virtual: true,
  66. Manual: true,
  67. WireId: "399a06f3-7925-46c1-8b9f-d5a8580a74df",
  68. NetId: "22c93412-5882-4de0-8357-45ce647ceada",
  69. Mac: "00:24:c7:16:80:f3",
  70. BandWidth: 1000,
  71. Mtu: 1500,
  72. Dns: "8.8.8.8",
  73. Ntp: "",
  74. Net: "vnet222",
  75. Interface: "ens5",
  76. Gateway: "",
  77. Ifname: "vnet222-bpg",
  78. Routes: nil,
  79. LinkUp: true,
  80. TeamWith: "00:24:c7:16:80:f2",
  81. },
  82. },
  83. want: &netplan.Configuration{
  84. Network: &netplan.Network{
  85. Version: 2,
  86. Renderer: netplan.NetworkRendererNetworkd,
  87. Ethernets: map[string]*netplan.EthernetConfig{
  88. "eth0": {
  89. MacAddress: "00:24:c7:16:80:f2",
  90. Match: &netplan.EthernetConfigMatch{
  91. MacAddress: "00:24:c7:16:80:f2",
  92. },
  93. Mtu: 1500,
  94. },
  95. "eth1": {
  96. MacAddress: "00:24:c7:16:80:f3",
  97. Match: &netplan.EthernetConfigMatch{
  98. MacAddress: "00:24:c7:16:80:f3",
  99. },
  100. Mtu: 1500,
  101. },
  102. },
  103. Bonds: map[string]*netplan.Bond{
  104. "bond0": {
  105. EthernetConfig: netplan.EthernetConfig{
  106. Addresses: []string{
  107. "10.168.222.175/24",
  108. },
  109. MacAddress: "00:24:c7:16:80:f2",
  110. Gateway4: "10.168.222.1",
  111. Nameservers: &netplan.Nameservers{
  112. Search: []string{
  113. "cloud.local",
  114. },
  115. Addresses: []string{
  116. "8.8.8.8",
  117. },
  118. },
  119. Mtu: 1500,
  120. Routes: []*netplan.Route{
  121. &netplan.Route{
  122. To: "169.254.169.254/32",
  123. Via: "0.0.0.0",
  124. },
  125. },
  126. },
  127. Interfaces: []string{
  128. "eth0",
  129. "eth1",
  130. },
  131. Parameters: &netplan.BondMode4Params{
  132. BondModeBaseParams: &netplan.BondModeBaseParams{
  133. Mode: "802.3ad",
  134. MiiMonitorInterval: 100,
  135. },
  136. },
  137. },
  138. },
  139. },
  140. },
  141. },
  142. {
  143. mainIp: "10.168.22.175",
  144. nics: []*types.SServerNic{
  145. {
  146. Name: "eth0",
  147. Index: 0,
  148. Bridge: "br0",
  149. Domain: "cloud.local",
  150. Ip: "10.168.222.175",
  151. Vlan: 1,
  152. Driver: "virtio",
  153. Masklen: 24,
  154. Virtual: false,
  155. Manual: true,
  156. WireId: "399a06f3-7925-46c1-8b9f-d5a8580a74df",
  157. NetId: "22c93412-5882-4de0-8357-45ce647ceada",
  158. Mac: "00:24:c7:16:80:f2",
  159. BandWidth: 1000,
  160. Mtu: 1500,
  161. Dns: "8.8.8.8",
  162. Ntp: "",
  163. Net: "vnet222",
  164. Interface: "ens5",
  165. Gateway: "10.168.222.1",
  166. Ifname: "vnet222-175",
  167. Routes: nil,
  168. LinkUp: true,
  169. },
  170. {
  171. Name: "eth1",
  172. Index: 1,
  173. Bridge: "br0",
  174. Domain: "cloud.local",
  175. Ip: "",
  176. Vlan: 1,
  177. Driver: "virtio",
  178. Masklen: 0,
  179. Virtual: true,
  180. Manual: true,
  181. WireId: "399a06f3-7925-46c1-8b9f-d5a8580a74df",
  182. NetId: "22c93412-5882-4de0-8357-45ce647ceada",
  183. Mac: "00:24:c7:16:80:f3",
  184. BandWidth: 1000,
  185. Mtu: 1500,
  186. Dns: "8.8.8.8",
  187. Ntp: "",
  188. Net: "vnet222",
  189. Interface: "ens5",
  190. Gateway: "",
  191. Ifname: "vnet222-bpg",
  192. Routes: nil,
  193. LinkUp: true,
  194. TeamWith: "00:24:c7:16:80:f2",
  195. },
  196. },
  197. want: &netplan.Configuration{
  198. Network: &netplan.Network{
  199. Version: 2,
  200. Renderer: netplan.NetworkRendererNetworkd,
  201. Ethernets: map[string]*netplan.EthernetConfig{
  202. "eth0": {
  203. MacAddress: "00:24:c7:16:80:f2",
  204. Match: &netplan.EthernetConfigMatch{
  205. MacAddress: "00:24:c7:16:80:f2",
  206. },
  207. Mtu: 1500,
  208. },
  209. "eth1": {
  210. MacAddress: "00:24:c7:16:80:f3",
  211. Match: &netplan.EthernetConfigMatch{
  212. MacAddress: "00:24:c7:16:80:f3",
  213. },
  214. Mtu: 1500,
  215. },
  216. },
  217. Bonds: map[string]*netplan.Bond{
  218. "bond0": {
  219. EthernetConfig: netplan.EthernetConfig{
  220. Addresses: []string{
  221. "10.168.222.175/24",
  222. },
  223. MacAddress: "00:24:c7:16:80:f2",
  224. Nameservers: &netplan.Nameservers{
  225. Search: []string{
  226. "cloud.local",
  227. },
  228. Addresses: []string{
  229. "8.8.8.8",
  230. },
  231. },
  232. Mtu: 1500,
  233. },
  234. Interfaces: []string{
  235. "eth0",
  236. "eth1",
  237. },
  238. Parameters: &netplan.BondMode4Params{
  239. BondModeBaseParams: &netplan.BondModeBaseParams{
  240. Mode: "802.3ad",
  241. MiiMonitorInterval: 100,
  242. },
  243. },
  244. },
  245. },
  246. },
  247. },
  248. },
  249. {
  250. mainIp: "10.168.22.175",
  251. mainIp6: "2001:db8::23",
  252. nics: []*types.SServerNic{
  253. {
  254. Name: "eth0",
  255. Index: 0,
  256. Bridge: "br0",
  257. Domain: "cloud.local",
  258. Ip: "10.168.222.175",
  259. Vlan: 1,
  260. Driver: "virtio",
  261. Masklen: 24,
  262. Virtual: false,
  263. Manual: true,
  264. WireId: "399a06f3-7925-46c1-8b9f-d5a8580a74df",
  265. NetId: "22c93412-5882-4de0-8357-45ce647ceada",
  266. Mac: "00:24:c7:16:80:f2",
  267. BandWidth: 1000,
  268. Mtu: 1500,
  269. Dns: "8.8.8.8",
  270. Ntp: "",
  271. Net: "vnet222",
  272. Interface: "ens5",
  273. Gateway: "10.168.222.1",
  274. Ifname: "vnet222-175",
  275. Routes: nil,
  276. LinkUp: true,
  277. Ip6: "2001:db8::23",
  278. Masklen6: 64,
  279. Gateway6: "2001:db8::1",
  280. },
  281. {
  282. Name: "eth1",
  283. Index: 1,
  284. Bridge: "br0",
  285. Domain: "cloud.local",
  286. Ip: "",
  287. Vlan: 1,
  288. Driver: "virtio",
  289. Masklen: 0,
  290. Virtual: true,
  291. Manual: true,
  292. WireId: "399a06f3-7925-46c1-8b9f-d5a8580a74df",
  293. NetId: "22c93412-5882-4de0-8357-45ce647ceada",
  294. Mac: "00:24:c7:16:80:f3",
  295. BandWidth: 1000,
  296. Mtu: 1500,
  297. Dns: "8.8.8.8",
  298. Ntp: "",
  299. Net: "vnet222",
  300. Interface: "ens5",
  301. Gateway: "",
  302. Ifname: "vnet222-bpg",
  303. Routes: nil,
  304. LinkUp: true,
  305. TeamWith: "00:24:c7:16:80:f2",
  306. },
  307. },
  308. want: &netplan.Configuration{
  309. Network: &netplan.Network{
  310. Version: 2,
  311. Renderer: netplan.NetworkRendererNetworkd,
  312. Ethernets: map[string]*netplan.EthernetConfig{
  313. "eth0": {
  314. MacAddress: "00:24:c7:16:80:f2",
  315. Match: &netplan.EthernetConfigMatch{
  316. MacAddress: "00:24:c7:16:80:f2",
  317. },
  318. Mtu: 1500,
  319. },
  320. "eth1": {
  321. MacAddress: "00:24:c7:16:80:f3",
  322. Match: &netplan.EthernetConfigMatch{
  323. MacAddress: "00:24:c7:16:80:f3",
  324. },
  325. Mtu: 1500,
  326. },
  327. },
  328. Bonds: map[string]*netplan.Bond{
  329. "bond0": {
  330. EthernetConfig: netplan.EthernetConfig{
  331. Addresses: []string{
  332. "10.168.222.175/24",
  333. "2001:db8::23/64",
  334. },
  335. MacAddress: "00:24:c7:16:80:f2",
  336. Gateway6: "2001:db8::1",
  337. Nameservers: &netplan.Nameservers{
  338. Search: []string{
  339. "cloud.local",
  340. },
  341. Addresses: []string{
  342. "8.8.8.8",
  343. },
  344. },
  345. Mtu: 1500,
  346. Routes: []*netplan.Route{
  347. {
  348. Metric: 0,
  349. To: "fd00:ec2::254/128",
  350. Via: "::",
  351. },
  352. },
  353. },
  354. Interfaces: []string{
  355. "eth0",
  356. "eth1",
  357. },
  358. Parameters: &netplan.BondMode4Params{
  359. BondModeBaseParams: &netplan.BondModeBaseParams{
  360. Mode: "802.3ad",
  361. MiiMonitorInterval: 100,
  362. },
  363. },
  364. },
  365. },
  366. },
  367. },
  368. },
  369. }
  370. for i, c := range cases {
  371. allNics, bondNics := convertNicConfigs(c.nics)
  372. netplanConfig := NewNetplanConfig(allNics, bondNics, c.mainIp, c.mainIp6)
  373. if jsonutils.Marshal(netplanConfig).String() != jsonutils.Marshal(c.want).String() {
  374. t.Errorf("nics %d: %s want: %s got: %s", i, jsonutils.Marshal(c.nics), jsonutils.Marshal(c.want).PrettyString(), jsonutils.Marshal(netplanConfig).PrettyString())
  375. }
  376. }
  377. }