github.com/vmware/govmomi@v0.37.1/toolbox/guest_info_test.go (about)

     1  /*
     2  Copyright (c) 2017 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package toolbox
    18  
    19  import (
    20  	"net"
    21  	"reflect"
    22  	"testing"
    23  )
    24  
    25  func TestDefaultGuestNicProto(t *testing.T) {
    26  	p := DefaultGuestNicInfo()
    27  
    28  	info := p.V3
    29  
    30  	for _, nic := range info.Nics {
    31  		if len(nic.MacAddress) == 0 {
    32  			continue
    33  		}
    34  		_, err := net.ParseMAC(nic.MacAddress)
    35  		if err != nil {
    36  			t.Errorf("invalid MAC %s: %s", nic.MacAddress, err)
    37  		}
    38  	}
    39  
    40  	b, err := EncodeXDR(p)
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  
    45  	var dp GuestNicInfo
    46  	err = DecodeXDR(b, &dp)
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	if !reflect.DeepEqual(p, &dp) {
    52  		t.Error("decode mismatch")
    53  	}
    54  }
    55  
    56  func TestMaxGuestNic(t *testing.T) {
    57  	p := DefaultGuestNicInfo()
    58  
    59  	maxNics = len(p.V3.Nics)
    60  
    61  	a, _ := net.Interfaces()
    62  	a = append(a, a...) // double the number of interfaces returned
    63  	netInterfaces = func() ([]net.Interface, error) {
    64  		return a, nil
    65  	}
    66  
    67  	p = DefaultGuestNicInfo()
    68  
    69  	l := len(p.V3.Nics)
    70  	if l != maxNics {
    71  		t.Errorf("Nics=%d", l)
    72  	}
    73  }