| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- // Copyright 2019 Yunion
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package fsdriver
- import (
- "testing"
- "yunion.io/x/jsonutils"
- "yunion.io/x/onecloud/pkg/cloudcommon/types"
- "yunion.io/x/onecloud/pkg/util/netplan"
- )
- func TestNewNetplanConfig(t *testing.T) {
- cases := []struct {
- mainIp string
- nics []*types.SServerNic
- want *netplan.Configuration
- mainIp6 string
- }{
- {
- mainIp: "10.168.222.175",
- nics: []*types.SServerNic{
- {
- Name: "eth0",
- Index: 0,
- Bridge: "br0",
- Domain: "cloud.local",
- Ip: "10.168.222.175",
- Vlan: 1,
- Driver: "virtio",
- Masklen: 24,
- Virtual: false,
- Manual: true,
- WireId: "399a06f3-7925-46c1-8b9f-d5a8580a74df",
- NetId: "22c93412-5882-4de0-8357-45ce647ceada",
- Mac: "00:24:c7:16:80:f2",
- BandWidth: 1000,
- Mtu: 1500,
- Dns: "8.8.8.8",
- Ntp: "",
- Net: "vnet222",
- Interface: "ens5",
- Gateway: "10.168.222.1",
- Ifname: "vnet222-175",
- Routes: nil,
- LinkUp: true,
- },
- {
- Name: "eth1",
- Index: 1,
- Bridge: "br0",
- Domain: "cloud.local",
- Ip: "",
- Vlan: 1,
- Driver: "virtio",
- Masklen: 0,
- Virtual: true,
- Manual: true,
- WireId: "399a06f3-7925-46c1-8b9f-d5a8580a74df",
- NetId: "22c93412-5882-4de0-8357-45ce647ceada",
- Mac: "00:24:c7:16:80:f3",
- BandWidth: 1000,
- Mtu: 1500,
- Dns: "8.8.8.8",
- Ntp: "",
- Net: "vnet222",
- Interface: "ens5",
- Gateway: "",
- Ifname: "vnet222-bpg",
- Routes: nil,
- LinkUp: true,
- TeamWith: "00:24:c7:16:80:f2",
- },
- },
- want: &netplan.Configuration{
- Network: &netplan.Network{
- Version: 2,
- Renderer: netplan.NetworkRendererNetworkd,
- Ethernets: map[string]*netplan.EthernetConfig{
- "eth0": {
- MacAddress: "00:24:c7:16:80:f2",
- Match: &netplan.EthernetConfigMatch{
- MacAddress: "00:24:c7:16:80:f2",
- },
- Mtu: 1500,
- },
- "eth1": {
- MacAddress: "00:24:c7:16:80:f3",
- Match: &netplan.EthernetConfigMatch{
- MacAddress: "00:24:c7:16:80:f3",
- },
- Mtu: 1500,
- },
- },
- Bonds: map[string]*netplan.Bond{
- "bond0": {
- EthernetConfig: netplan.EthernetConfig{
- Addresses: []string{
- "10.168.222.175/24",
- },
- MacAddress: "00:24:c7:16:80:f2",
- Gateway4: "10.168.222.1",
- Nameservers: &netplan.Nameservers{
- Search: []string{
- "cloud.local",
- },
- Addresses: []string{
- "8.8.8.8",
- },
- },
- Mtu: 1500,
- Routes: []*netplan.Route{
- &netplan.Route{
- To: "169.254.169.254/32",
- Via: "0.0.0.0",
- },
- },
- },
- Interfaces: []string{
- "eth0",
- "eth1",
- },
- Parameters: &netplan.BondMode4Params{
- BondModeBaseParams: &netplan.BondModeBaseParams{
- Mode: "802.3ad",
- MiiMonitorInterval: 100,
- },
- },
- },
- },
- },
- },
- },
- {
- mainIp: "10.168.22.175",
- nics: []*types.SServerNic{
- {
- Name: "eth0",
- Index: 0,
- Bridge: "br0",
- Domain: "cloud.local",
- Ip: "10.168.222.175",
- Vlan: 1,
- Driver: "virtio",
- Masklen: 24,
- Virtual: false,
- Manual: true,
- WireId: "399a06f3-7925-46c1-8b9f-d5a8580a74df",
- NetId: "22c93412-5882-4de0-8357-45ce647ceada",
- Mac: "00:24:c7:16:80:f2",
- BandWidth: 1000,
- Mtu: 1500,
- Dns: "8.8.8.8",
- Ntp: "",
- Net: "vnet222",
- Interface: "ens5",
- Gateway: "10.168.222.1",
- Ifname: "vnet222-175",
- Routes: nil,
- LinkUp: true,
- },
- {
- Name: "eth1",
- Index: 1,
- Bridge: "br0",
- Domain: "cloud.local",
- Ip: "",
- Vlan: 1,
- Driver: "virtio",
- Masklen: 0,
- Virtual: true,
- Manual: true,
- WireId: "399a06f3-7925-46c1-8b9f-d5a8580a74df",
- NetId: "22c93412-5882-4de0-8357-45ce647ceada",
- Mac: "00:24:c7:16:80:f3",
- BandWidth: 1000,
- Mtu: 1500,
- Dns: "8.8.8.8",
- Ntp: "",
- Net: "vnet222",
- Interface: "ens5",
- Gateway: "",
- Ifname: "vnet222-bpg",
- Routes: nil,
- LinkUp: true,
- TeamWith: "00:24:c7:16:80:f2",
- },
- },
- want: &netplan.Configuration{
- Network: &netplan.Network{
- Version: 2,
- Renderer: netplan.NetworkRendererNetworkd,
- Ethernets: map[string]*netplan.EthernetConfig{
- "eth0": {
- MacAddress: "00:24:c7:16:80:f2",
- Match: &netplan.EthernetConfigMatch{
- MacAddress: "00:24:c7:16:80:f2",
- },
- Mtu: 1500,
- },
- "eth1": {
- MacAddress: "00:24:c7:16:80:f3",
- Match: &netplan.EthernetConfigMatch{
- MacAddress: "00:24:c7:16:80:f3",
- },
- Mtu: 1500,
- },
- },
- Bonds: map[string]*netplan.Bond{
- "bond0": {
- EthernetConfig: netplan.EthernetConfig{
- Addresses: []string{
- "10.168.222.175/24",
- },
- MacAddress: "00:24:c7:16:80:f2",
- Nameservers: &netplan.Nameservers{
- Search: []string{
- "cloud.local",
- },
- Addresses: []string{
- "8.8.8.8",
- },
- },
- Mtu: 1500,
- },
- Interfaces: []string{
- "eth0",
- "eth1",
- },
- Parameters: &netplan.BondMode4Params{
- BondModeBaseParams: &netplan.BondModeBaseParams{
- Mode: "802.3ad",
- MiiMonitorInterval: 100,
- },
- },
- },
- },
- },
- },
- },
- {
- mainIp: "10.168.22.175",
- mainIp6: "2001:db8::23",
- nics: []*types.SServerNic{
- {
- Name: "eth0",
- Index: 0,
- Bridge: "br0",
- Domain: "cloud.local",
- Ip: "10.168.222.175",
- Vlan: 1,
- Driver: "virtio",
- Masklen: 24,
- Virtual: false,
- Manual: true,
- WireId: "399a06f3-7925-46c1-8b9f-d5a8580a74df",
- NetId: "22c93412-5882-4de0-8357-45ce647ceada",
- Mac: "00:24:c7:16:80:f2",
- BandWidth: 1000,
- Mtu: 1500,
- Dns: "8.8.8.8",
- Ntp: "",
- Net: "vnet222",
- Interface: "ens5",
- Gateway: "10.168.222.1",
- Ifname: "vnet222-175",
- Routes: nil,
- LinkUp: true,
- Ip6: "2001:db8::23",
- Masklen6: 64,
- Gateway6: "2001:db8::1",
- },
- {
- Name: "eth1",
- Index: 1,
- Bridge: "br0",
- Domain: "cloud.local",
- Ip: "",
- Vlan: 1,
- Driver: "virtio",
- Masklen: 0,
- Virtual: true,
- Manual: true,
- WireId: "399a06f3-7925-46c1-8b9f-d5a8580a74df",
- NetId: "22c93412-5882-4de0-8357-45ce647ceada",
- Mac: "00:24:c7:16:80:f3",
- BandWidth: 1000,
- Mtu: 1500,
- Dns: "8.8.8.8",
- Ntp: "",
- Net: "vnet222",
- Interface: "ens5",
- Gateway: "",
- Ifname: "vnet222-bpg",
- Routes: nil,
- LinkUp: true,
- TeamWith: "00:24:c7:16:80:f2",
- },
- },
- want: &netplan.Configuration{
- Network: &netplan.Network{
- Version: 2,
- Renderer: netplan.NetworkRendererNetworkd,
- Ethernets: map[string]*netplan.EthernetConfig{
- "eth0": {
- MacAddress: "00:24:c7:16:80:f2",
- Match: &netplan.EthernetConfigMatch{
- MacAddress: "00:24:c7:16:80:f2",
- },
- Mtu: 1500,
- },
- "eth1": {
- MacAddress: "00:24:c7:16:80:f3",
- Match: &netplan.EthernetConfigMatch{
- MacAddress: "00:24:c7:16:80:f3",
- },
- Mtu: 1500,
- },
- },
- Bonds: map[string]*netplan.Bond{
- "bond0": {
- EthernetConfig: netplan.EthernetConfig{
- Addresses: []string{
- "10.168.222.175/24",
- "2001:db8::23/64",
- },
- MacAddress: "00:24:c7:16:80:f2",
- Gateway6: "2001:db8::1",
- Nameservers: &netplan.Nameservers{
- Search: []string{
- "cloud.local",
- },
- Addresses: []string{
- "8.8.8.8",
- },
- },
- Mtu: 1500,
- Routes: []*netplan.Route{
- {
- Metric: 0,
- To: "fd00:ec2::254/128",
- Via: "::",
- },
- },
- },
- Interfaces: []string{
- "eth0",
- "eth1",
- },
- Parameters: &netplan.BondMode4Params{
- BondModeBaseParams: &netplan.BondModeBaseParams{
- Mode: "802.3ad",
- MiiMonitorInterval: 100,
- },
- },
- },
- },
- },
- },
- },
- }
- for i, c := range cases {
- allNics, bondNics := convertNicConfigs(c.nics)
- netplanConfig := NewNetplanConfig(allNics, bondNics, c.mainIp, c.mainIp6)
- if jsonutils.Marshal(netplanConfig).String() != jsonutils.Marshal(c.want).String() {
- t.Errorf("nics %d: %s want: %s got: %s", i, jsonutils.Marshal(c.nics), jsonutils.Marshal(c.want).PrettyString(), jsonutils.Marshal(netplanConfig).PrettyString())
- }
- }
- }
|