Skip to content

Commit

Permalink
Address inconsistent vmbus naming, use of io/fs, and style
Browse files Browse the repository at this point in the history
Signed-off-by: Kathryn Baldauf <kabaldau@microsoft.com>
  • Loading branch information
katiewasnothere committed Oct 29, 2021
1 parent 007ec07 commit 1313fe1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
17 changes: 10 additions & 7 deletions internal/guest/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,24 @@ func InstanceIDToName(ctx context.Context, id string, vpciAssigned bool) (_ stri
defer span.End()
defer func() { oc.SetSpanStatus(span, err) }()

vmbusID := strings.ToLower(id)
span.AddAttributes(trace.StringAttribute("adapterInstanceID", vmbusID))
vmBusID := strings.ToLower(id)
span.AddAttributes(trace.StringAttribute("adapterInstanceID", vmBusID))

netDevicePath := ""
if vpciAssigned {
pciDevicePath, err := pciFindDeviceFullPath(ctx, vmbusID)
pciDevicePath, err := pciFindDeviceFullPath(ctx, vmBusID)
if err != nil {
return "", err
}
pciNetDirPattern := filepath.Join(pciDevicePath, "net")
netDevicePath, err = storageWaitForFileMatchingPattern(ctx, pciNetDirPattern)
} else {
vmBusNetSubPath := filepath.Join(vmbusID, "net")
vmBusNetSubPath := filepath.Join(vmBusID, "net")
netDevicePath, err = vmbusWaitForDevicePath(ctx, vmBusNetSubPath)
}
if err != nil {
return "", errors.Wrapf(err, "failed to find adapter %v sysfs path", vmBusID)
}

var deviceDirs []os.FileInfo
for {
Expand All @@ -148,16 +151,16 @@ func InstanceIDToName(ctx context.Context, id string, vpciAssigned bool) (_ stri
continue
}
} else {
return "", errors.Wrapf(err, "failed to read vmbus network device from /sys filesystem for adapter %s", vmbusID)
return "", errors.Wrapf(err, "failed to read vmbus network device from /sys filesystem for adapter %s", vmBusID)
}
}
break
}
if len(deviceDirs) == 0 {
return "", errors.Errorf("no interface name found for adapter %s", vmbusID)
return "", errors.Errorf("no interface name found for adapter %s", vmBusID)
}
if len(deviceDirs) > 1 {
return "", errors.Errorf("multiple interface names found for adapter %s", vmbusID)
return "", errors.Errorf("multiple interface names found for adapter %s", vmBusID)
}
ifname := deviceDirs[0].Name()
log.G(ctx).WithField("ifname", ifname).Debug("resolved ifname")
Expand Down
13 changes: 7 additions & 6 deletions internal/guest/network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package network
import (
"context"
"io/fs"
"os"
"path/filepath"
"testing"
"time"
Expand Down Expand Up @@ -197,24 +198,24 @@ func (t *testFileInfo) Sys() interface{} {
return nil
}

var _ = (fs.FileInfo)(&testFileInfo{})
var _ = (os.FileInfo)(&testFileInfo{})

func Test_InstanceIDToName(t *testing.T) {
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)

vmBusGUID := "1111-2222-3333-4444"
testIfName := "test-eth0"

vmbusWaitForDevicePath = func(_ context.Context, vmbusGUIDPattern string) (string, error) {
vmBusPath := filepath.Join("/sys/bus/vmbus/devices", vmbusGUIDPattern)
vmbusWaitForDevicePath = func(_ context.Context, vmBusGUIDPattern string) (string, error) {
vmBusPath := filepath.Join("/sys/bus/vmbus/devices", vmBusGUIDPattern)
return vmBusPath, nil
}

storageWaitForFileMatchingPattern = func(_ context.Context, pattern string) (string, error) {
return pattern, nil
}

ioutilReadDir = func(dirname string) ([]fs.FileInfo, error) {
ioutilReadDir = func(dirname string) ([]os.FileInfo, error) {
info := &testFileInfo{
FileName: testIfName,
IsDirectory: false,
Expand Down Expand Up @@ -244,12 +245,12 @@ func Test_InstanceIDToName_VPCI(t *testing.T) {
return pattern, nil
}

ioutilReadDir = func(dirname string) ([]fs.FileInfo, error) {
ioutilReadDir = func(dirname string) ([]os.FileInfo, error) {
info := &testFileInfo{
FileName: testIfName,
IsDirectory: false,
}
return []fs.FileInfo{info}, nil
return []os.FileInfo{info}, nil
}
actualIfName, err := InstanceIDToName(ctx, vmBusGUID, true)
if err != nil {
Expand Down
7 changes: 1 addition & 6 deletions internal/guest/storage/pci/pci.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ func FindDeviceFullPath(ctx context.Context, vmBusGUID string) (string, error) {
return "", err
}

pciDevicePath, err := findVMBusPCIDevice(ctx, pciDir)
if err != nil {
return "", err
}

return pciDevicePath, nil
return findVMBusPCIDevice(ctx, pciDir)
}

// findVMBusPCIDir waits for the pci bus directory matching pattern
Expand Down

0 comments on commit 1313fe1

Please sign in to comment.