netplan_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 netplan
  15. import (
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func TestNewEthernetConfig(t *testing.T) {
  20. c := NewDHCPEthernetConfig().EnableDHCP4()
  21. assert := assert.New(t)
  22. assert.YAMLEq("dhcp4: true", c.YAMLString())
  23. c.EnableDHCP6()
  24. assert.YAMLEq("dhcp4: true\ndhcp6: true\naccept-ra: true", c.YAMLString())
  25. }
  26. func TestNewBondMode4(t *testing.T) {
  27. c := NewBondMode4(
  28. &EthernetConfig{
  29. DHCP4: false,
  30. DHCP6: false,
  31. Gateway4: "192.168.1.1",
  32. Gateway6: "fd:3ffe:3200::1",
  33. Addresses: []string{"192.168.1.252/24", "fd:3ffe:3200::2/64"},
  34. Nameservers: &Nameservers{
  35. Search: []string{"local"},
  36. Addresses: []string{"8.8.8.8", "8.8.4.4"},
  37. },
  38. },
  39. []string{"enp2s0", "enp3s0"},
  40. )
  41. yamlStr := `
  42. addresses:
  43. - 192.168.1.252/24
  44. - fd:3ffe:3200::2/64
  45. gateway4: 192.168.1.1
  46. gateway6: fd:3ffe:3200::1
  47. interfaces:
  48. - enp2s0
  49. - enp3s0
  50. nameservers:
  51. addresses:
  52. - 8.8.8.8
  53. - 8.8.4.4
  54. search:
  55. - local
  56. parameters:
  57. mii-monitor-interval: 100
  58. mode: "802.3ad"
  59. `
  60. assert := assert.New(t)
  61. assert.YAMLEq(yamlStr, c.YAMLString())
  62. }
  63. func TestNewNetwork(t *testing.T) {
  64. n := NewNetwork()
  65. n.AddEthernet("eth0", NewDHCPEthernetConfig().EnableDHCP4())
  66. n.AddEthernet("eth1", NewStaticEthernetConfig(
  67. "10.10.10.2/24",
  68. "",
  69. "10.10.10.1",
  70. "",
  71. []string{"mydomain", "otherdomain"},
  72. []string{"114.114.114.114"},
  73. nil,
  74. ))
  75. c := NewConfiguration(n)
  76. yamlStr := `
  77. network:
  78. ethernets:
  79. eth0:
  80. dhcp4: true
  81. eth1:
  82. addresses:
  83. - 10.10.10.2/24
  84. gateway4: 10.10.10.1
  85. nameservers:
  86. addresses:
  87. - 114.114.114.114
  88. search: ["mydomain", "otherdomain"]
  89. renderer: networkd
  90. version: 2
  91. `
  92. assert := assert.New(t)
  93. assert.YAMLEq(yamlStr, c.YAMLString())
  94. }