From cfbb69ddd636e8c5d794da9296f5e0ed76443ed3 Mon Sep 17 00:00:00 2001 From: Alek Du Date: Mon, 30 Mar 2020 17:30:59 +0800 Subject: [PATCH] vpu: improve test coverage Changed code a little bit to improve test coverage: * call Scan in test code * call Scan without hddl socket * call Scan with 0 SharedDevNum * move SharedDevNum in newDevicePlugin * use Ticker instead of Sleep Signed-off-by: Alek Du --- cmd/vpu_plugin/vpu_plugin.go | 28 ++++++++++------ cmd/vpu_plugin/vpu_plugin_test.go | 53 +++++++++++++++++++++++++++---- 2 files changed, 66 insertions(+), 15 deletions(-) diff --git a/cmd/vpu_plugin/vpu_plugin.go b/cmd/vpu_plugin/vpu_plugin.go index 14496372..336a2641 100644 --- a/cmd/vpu_plugin/vpu_plugin.go +++ b/cmd/vpu_plugin/vpu_plugin.go @@ -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() } diff --git a/cmd/vpu_plugin/vpu_plugin_test.go b/cmd/vpu_plugin/vpu_plugin_test.go index 02321426..cd7301f4 100644 --- a/cmd/vpu_plugin/vpu_plugin_test.go +++ b/cmd/vpu_plugin/vpu_plugin_test.go @@ -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") } }