Skip to content

Commit

Permalink
pcidev: bootstrap unit tests
Browse files Browse the repository at this point in the history
add bare bones unit tests

Signed-off-by: Francesco Romani <[email protected]>
  • Loading branch information
ffromani committed Jul 24, 2020
1 parent 4bb9f06 commit c6baae3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/fakesysfs/fakesysfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fakesysfs
import (
"io/ioutil"
"os"
"strings"
)

type Tree interface {
Expand Down Expand Up @@ -63,6 +64,18 @@ func (t *tree) SetAttrs() error {
return err
}

func MakeAttrs(attrs map[string]string) map[string]string {
resAttrs := make(map[string]string)
for key, value := range attrs {
if strings.HasSuffix(value, "\n") {
resAttrs[key] = value
} else {
resAttrs[key] = value + "\n"
}
}
return resAttrs
}

type creator struct{}

func newCreator() Creator {
Expand Down
63 changes: 63 additions & 0 deletions pkg/topologyinfo/pcidev/pcidev_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package pcidev

import (
"io/ioutil"
"os"
"path/filepath"

"testing"

"github.com/fromanirh/numalign/pkg/fakesysfs"
)

func TestPCIDevsTrivialTree(t *testing.T) {
base, err := ioutil.TempDir("/tmp", "fakesysfs")
if err != nil {
t.Errorf("error creating temp base dir: %v", err)
}
fs, err := fakesysfs.NewFakeSysfs(base)
if err != nil {
t.Errorf("error creating fakesysfs: %v", err)
}
t.Logf("sysfs at %q", fs.Base())

sysDevs := fs.AddTree("sys", "bus", "pci", "devices")

attrs := map[string]string{
"numa_node": "0",
"class": "0x020000", // MUST be a network device
"vendor": "0x10ec",
"device": "0x8168",
}
sysDevs.Add("0000:07:00.0", fakesysfs.MakeAttrs(attrs))

err = fs.Setup()
if err != nil {
t.Errorf("error setting up fakesysfs: %v", err)
}

pciDevs, err := NewPCIDevices(filepath.Join(fs.Base(), "sys"))
if err != nil {
t.Errorf("error in NewPCIDevices: %v", err)
}

if len(pciDevs.Items) != 1 {
t.Errorf("found unexpected amount of PCI devices: %d", len(pciDevs.Items))
}
pciDev := pciDevs.Items[0]
if pciDev.DevClass() != DevClassNetwork {
t.Errorf("device class mismatch found %x expected %x", pciDev.DevClass(), DevClassNetwork)
}
if pciDev.String() != "pci@0000:07:00.0 10ec:8168 numa_node=0 physfn=false vfn=false" {
t.Errorf("device misdetected: %v", pciDev.String())
}

if _, ok := os.LookupEnv("TOPOLOGYINFO_TEST_KEEP_TREE"); ok {
t.Logf("found environment variable, keeping fake tree")
} else {
err = fs.Teardown()
if err != nil {
t.Errorf("error tearing down fakesysfs: %v", err)
}
}
}

0 comments on commit c6baae3

Please sign in to comment.