Merge pull request #352 from alekdu/improve_vpu_coverage

vpu: improve test coverage
This commit is contained in:
Dmitry Rozhkov 2020-03-31 10:09:59 +03:00 committed by GitHub
commit 70ac83bdc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 15 deletions

View File

@ -39,6 +39,8 @@ const (
hddlServicePath1 = "/var/tmp/hddl_service_ready.mutex"
hddlServicePath2 = "/var/tmp/hddl_service_alive.mutex"
ionDevNode = "/dev/ion"
// Frequency of device scans
scanFrequency = 5 * time.Second
)
var (
@ -55,18 +57,27 @@ type devicePlugin struct {
vendorID int
productIDs []int
sharedDevNum int
scanTicker *time.Ticker
scanDone chan bool
}
func newDevicePlugin(usbContext gousbContext, vendorID int, productIDs []int, sharedDevNum int) *devicePlugin {
if sharedDevNum < 1 {
klog.V(1).Info("The number of containers sharing the same VPU must greater than zero")
return nil
}
return &devicePlugin{
usbContext: usbContext,
vendorID: vendorID,
productIDs: productIDs,
sharedDevNum: sharedDevNum,
scanTicker: time.NewTicker(scanFrequency),
scanDone: make(chan bool, 1),
}
}
func (dp *devicePlugin) Scan(notifier dpapi.Notifier) error {
defer dp.scanTicker.Stop()
for {
devTree, err := dp.scan()
if err != nil {
@ -75,7 +86,11 @@ func (dp *devicePlugin) Scan(notifier dpapi.Notifier) error {
notifier.Notify(devTree)
time.Sleep(5 * time.Second)
select {
case <-dp.scanDone:
return nil
case <-dp.scanTicker.C:
}
}
}
@ -156,16 +171,8 @@ func main() {
var sharedDevNum int
flag.IntVar(&sharedDevNum, "shared-dev-num", 1, "number of containers sharing the same VPU device")
flag.Parse()
klog.V(4).Info("debug is on")
if sharedDevNum < 1 {
klog.Fatal("The number of containers sharing the same VPU must greater than zero")
os.Exit(1)
}
klog.V(1).Info("VPU device plugin started")
// add lsusb here
@ -179,6 +186,9 @@ func main() {
}
plugin := newDevicePlugin(ctx, vendorID, productIDs, sharedDevNum)
if plugin == nil {
klog.Fatal("Cannot create device plugin, please check above error messages.")
}
manager := dpapi.NewManager(namespace, plugin)
manager.Run()
}

View File

@ -20,6 +20,7 @@ import (
"testing"
"github.com/google/gousb"
dpapi "github.com/intel/intel-device-plugins-for-kubernetes/pkg/deviceplugin"
"k8s.io/klog"
)
@ -49,9 +50,22 @@ func (t *testCase) OpenDevices(opener func(desc *gousb.DeviceDesc) bool) ([]*gou
return ret, nil
}
// fakeNotifier implements Notifier interface.
type fakeNotifier struct {
scanDone chan bool
tree dpapi.DeviceTree
}
// Notify stops plugin Scan
func (n *fakeNotifier) Notify(newDeviceTree dpapi.DeviceTree) {
n.tree = newDeviceTree
n.scanDone <- true
}
func TestScan(t *testing.T) {
var fN fakeNotifier
f, err := os.Create("/var/tmp/hddl_service.sock")
defer f.Close()
if err != nil {
t.Error("create fake hddl file failed")
}
@ -64,13 +78,40 @@ func TestScan(t *testing.T) {
testPlugin := newDevicePlugin(tc, vendorID, productIDs, 10)
if testPlugin == nil {
t.Error("vpu plugin test failed")
t.Error("vpu plugin test failed with newDevicePlugin().")
}
tree, err := testPlugin.scan()
fN.scanDone = testPlugin.scanDone
err = testPlugin.Scan(&fN)
if err != nil {
t.Error("vpu plugin test failed")
} else {
klog.V(4).Infof("tree len is %d", len(tree[deviceType]))
t.Error("vpu plugin test failed with testPlugin.Scan()")
}
if len(fN.tree[deviceType]) == 0 {
t.Error("vpu plugin test failed with testPlugin.Scan(): tree len is 0")
}
klog.V(4).Infof("tree len is %d", len(fN.tree[deviceType]))
//remove the hddl_service.sock and test with no hddl socket case
f.Close()
os.Remove("/var/tmp/hddl_service.sock")
testPlugin = newDevicePlugin(tc, vendorID, productIDs, 10)
if testPlugin == nil {
t.Error("vpu plugin test failed with newDevicePlugin() in no hddl_service.sock case.")
}
fN.scanDone = testPlugin.scanDone
err = testPlugin.Scan(&fN)
if err != nil {
t.Error("vpu plugin test failed with testPlugin.Scan() in no hddl_service.sock case.")
}
if len(fN.tree[deviceType]) != 0 {
t.Error("vpu plugin test failed with testPlugin.Scan(): tree len should be 0 in no hddl_service.sock case.")
}
//test with sharedNum equals 0 case
testPlugin = newDevicePlugin(tc, vendorID, productIDs, 0)
if testPlugin != nil {
t.Error("vpu plugin test fail: newDevicePlugin should fail with 0 sharedDevNum")
}
}