| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- // 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 netutils2
- import (
- "testing"
- )
- func TestFindDefaultNic(t *testing.T) {
- cases := []struct {
- nics [][]string
- mac string
- index int
- }{
- {
- nics: [][]string{
- {"192.168.202.147", "00:22:00:00:00:01", "192.168.202.1", "", "", "false"},
- },
- mac: "00:22:00:00:00:01",
- index: 0,
- },
- {
- nics: [][]string{
- {"192.168.202.147", "00:22:00:00:00:01", "192.168.202.1", "", "", "false"},
- {"192.168.203.147", "00:22:00:00:00:02", "192.168.203.1", "", "", "false"},
- },
- mac: "00:22:00:00:00:01",
- index: 0,
- },
- {
- nics: [][]string{
- {"192.168.202.147", "00:22:00:00:00:01", "192.168.202.1", "", "", "false"},
- {"192.168.203.147", "00:22:00:00:00:02", "192.168.203.1", "", "", "true"},
- },
- mac: "00:22:00:00:00:02",
- index: 1,
- },
- {
- nics: [][]string{
- {"", "00:22:00:00:00:01", "", "3ffe:3200:fe::fb", "3ffe:3200:fe::1", "false"},
- {"", "00:22:00:00:00:02", "", "3ffe:3200:ff::fb", "3ffe:3200:ff::1", "true"},
- },
- mac: "00:22:00:00:00:02",
- index: 1,
- },
- {
- nics: [][]string{
- {"", "00:22:00:00:00:01", "", "3ffe:3200:fe::fb", "3ffe:3200:fe::1", "false"},
- {"", "00:22:00:00:00:02", "", "3ffe:3200:ff::fb", "", "true"},
- },
- mac: "00:22:00:00:00:01",
- index: 0,
- },
- {
- nics: [][]string{
- {"192.168.202.147", "00:22:00:00:00:01", "192.168.202.1", "", "", "false"},
- {"202.168.202.147", "00:22:00:00:00:02", "202.168.202.1", "", "", "false"},
- },
- mac: "00:22:00:00:00:02",
- index: 1,
- },
- {
- nics: [][]string{
- {"202.168.202.147", "00:22:00:00:00:01", "202.168.202.1", "", "", "false"},
- {"192.168.202.147", "00:22:00:00:00:02", "192.168.202.1", "", "", "false"},
- },
- mac: "00:22:00:00:00:01",
- index: 0,
- },
- {
- nics: [][]string{
- {"192.168.202.147", "00:22:00:00:00:01", "192.168.202.1", "", "", "false"},
- {"", "00:22:00:00:00:02", "", "", "", "false"},
- {"202.168.202.147", "00:22:00:00:00:03", "202.168.202.1", "", "", "false"},
- },
- mac: "00:22:00:00:00:03",
- index: 2,
- },
- }
- for _, c := range cases {
- nics := SNicInfoList{}
- for _, n := range c.nics {
- nics = nics.Add(n[1], n[0], n[2], n[3], n[4], n[5] == "true")
- }
- gotMac, gotIdx := nics.FindDefaultNicMac()
- if gotMac != c.mac || gotIdx != c.index {
- t.Errorf("want %s (%d) got %s (%d)", c.mac, c.index, gotMac, gotIdx)
- }
- }
- }
|