mirror of
https://github.com/intel/intel-device-plugins-for-kubernetes.git
synced 2025-06-03 03:59:37 +00:00
Merge pull request #378 from rojkov/gpu-tests
gpu: increase code coverage for unit tests
This commit is contained in:
commit
fdaecd1d98
@ -50,8 +50,8 @@ const (
|
|||||||
unhealthyAfuID = "ffffffffffffffffffffffffffffffff"
|
unhealthyAfuID = "ffffffffffffffffffffffffffffffff"
|
||||||
unhealthyInterfaceID = "ffffffffffffffffffffffffffffffff"
|
unhealthyInterfaceID = "ffffffffffffffffffffffffffffffff"
|
||||||
|
|
||||||
// Frequency of device scans
|
// Period of device scans
|
||||||
scanFrequency = 5 * time.Second
|
scanPeriod = 5 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
type getDevTreeFunc func(devices []device) dpapi.DeviceTree
|
type getDevTreeFunc func(devices []device) dpapi.DeviceTree
|
||||||
@ -206,7 +206,7 @@ func newDevicePlugin(mode string, rootPath string) (*devicePlugin, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
dp.scanTicker = time.NewTicker(scanFrequency)
|
dp.scanTicker = time.NewTicker(scanPeriod)
|
||||||
dp.scanDone = make(chan bool, 1) // buffered as we may send to it before Scan starts receiving from it
|
dp.scanDone = make(chan bool, 1) // buffered as we may send to it before Scan starts receiving from it
|
||||||
|
|
||||||
return dp, nil
|
return dp, nil
|
||||||
|
@ -42,6 +42,9 @@ const (
|
|||||||
// Device plugin settings.
|
// Device plugin settings.
|
||||||
namespace = "gpu.intel.com"
|
namespace = "gpu.intel.com"
|
||||||
deviceType = "i915"
|
deviceType = "i915"
|
||||||
|
|
||||||
|
// Period of device scans
|
||||||
|
scanPeriod = 5 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
type devicePlugin struct {
|
type devicePlugin struct {
|
||||||
@ -52,6 +55,9 @@ type devicePlugin struct {
|
|||||||
|
|
||||||
gpuDeviceReg *regexp.Regexp
|
gpuDeviceReg *regexp.Regexp
|
||||||
controlDeviceReg *regexp.Regexp
|
controlDeviceReg *regexp.Regexp
|
||||||
|
|
||||||
|
scanTicker *time.Ticker
|
||||||
|
scanDone chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDevicePlugin(sysfsDir, devfsDir string, sharedDevNum int) *devicePlugin {
|
func newDevicePlugin(sysfsDir, devfsDir string, sharedDevNum int) *devicePlugin {
|
||||||
@ -61,10 +67,13 @@ func newDevicePlugin(sysfsDir, devfsDir string, sharedDevNum int) *devicePlugin
|
|||||||
sharedDevNum: sharedDevNum,
|
sharedDevNum: sharedDevNum,
|
||||||
gpuDeviceReg: regexp.MustCompile(gpuDeviceRE),
|
gpuDeviceReg: regexp.MustCompile(gpuDeviceRE),
|
||||||
controlDeviceReg: regexp.MustCompile(controlDeviceRE),
|
controlDeviceReg: regexp.MustCompile(controlDeviceRE),
|
||||||
|
scanTicker: time.NewTicker(scanPeriod),
|
||||||
|
scanDone: make(chan bool, 1), // buffered as we may send to it before Scan starts receiving from it
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dp *devicePlugin) Scan(notifier dpapi.Notifier) error {
|
func (dp *devicePlugin) Scan(notifier dpapi.Notifier) error {
|
||||||
|
defer dp.scanTicker.Stop()
|
||||||
var previouslyFound int = -1
|
var previouslyFound int = -1
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@ -81,7 +90,11 @@ func (dp *devicePlugin) Scan(notifier dpapi.Notifier) error {
|
|||||||
|
|
||||||
notifier.Notify(devTree)
|
notifier.Notify(devTree)
|
||||||
|
|
||||||
time.Sleep(5 * time.Second)
|
select {
|
||||||
|
case <-dp.scanDone:
|
||||||
|
return nil
|
||||||
|
case <-dp.scanTicker.C:
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,56 +16,73 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
dpapi "github.com/intel/intel-device-plugins-for-kubernetes/pkg/deviceplugin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flag.Set("v", "4") //Enable debug output
|
flag.Set("v", "4") //Enable debug output
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// mockNotifier implements Notifier interface.
|
||||||
|
type mockNotifier struct {
|
||||||
|
scanDone chan bool
|
||||||
|
devCount int
|
||||||
|
}
|
||||||
|
|
||||||
|
// Notify stops plugin Scan
|
||||||
|
func (n *mockNotifier) Notify(newDeviceTree dpapi.DeviceTree) {
|
||||||
|
n.devCount = len(newDeviceTree[deviceType])
|
||||||
|
n.scanDone <- true
|
||||||
|
}
|
||||||
|
|
||||||
func TestScan(t *testing.T) {
|
func TestScan(t *testing.T) {
|
||||||
tmpdir := fmt.Sprintf("/tmp/gpuplugin-test-%d", time.Now().Unix())
|
root, err := ioutil.TempDir("", "test_new_device_plugin")
|
||||||
sysfs := path.Join(tmpdir, "sysfs")
|
if err != nil {
|
||||||
devfs := path.Join(tmpdir, "devfs")
|
t.Fatalf("can't create temporary directory: %+v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer os.RemoveAll(root)
|
||||||
|
|
||||||
|
sysfs := path.Join(root, "sys")
|
||||||
|
devfs := path.Join(root, "dev")
|
||||||
|
|
||||||
tcases := []struct {
|
tcases := []struct {
|
||||||
|
name string
|
||||||
devfsdirs []string
|
devfsdirs []string
|
||||||
sysfsdirs []string
|
sysfsdirs []string
|
||||||
sysfsfiles map[string][]byte
|
sysfsfiles map[string][]byte
|
||||||
expectedDevs int
|
expectedDevs int
|
||||||
expectedErr bool
|
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
expectedErr: true,
|
name: "no sysfs mounted",
|
||||||
expectedDevs: 0,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "no device installed",
|
||||||
sysfsdirs: []string{"card0"},
|
sysfsdirs: []string{"card0"},
|
||||||
expectedDevs: 0,
|
|
||||||
expectedErr: false,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "missing dev node",
|
||||||
sysfsdirs: []string{"card0/device"},
|
sysfsdirs: []string{"card0/device"},
|
||||||
sysfsfiles: map[string][]byte{
|
sysfsfiles: map[string][]byte{
|
||||||
"card0/device/vendor": []byte("0x8086"),
|
"card0/device/vendor": []byte("0x8086"),
|
||||||
},
|
},
|
||||||
expectedDevs: 0,
|
|
||||||
expectedErr: true,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sysfsdirs: []string{"card0/device/drm/card0"},
|
name: "all is correct",
|
||||||
|
sysfsdirs: []string{"card0/device/drm/card0", "card0/device/drm/controlD64"},
|
||||||
sysfsfiles: map[string][]byte{
|
sysfsfiles: map[string][]byte{
|
||||||
"card0/device/vendor": []byte("0x8086"),
|
"card0/device/vendor": []byte("0x8086"),
|
||||||
},
|
},
|
||||||
devfsdirs: []string{"card0"},
|
devfsdirs: []string{"card0"},
|
||||||
expectedDevs: 1,
|
expectedDevs: 1,
|
||||||
expectedErr: false,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "two sysfs records but one dev node",
|
||||||
sysfsdirs: []string{
|
sysfsdirs: []string{
|
||||||
"card0/device/drm/card0",
|
"card0/device/drm/card0",
|
||||||
"card1/device/drm/card1",
|
"card1/device/drm/card1",
|
||||||
@ -76,64 +93,53 @@ func TestScan(t *testing.T) {
|
|||||||
},
|
},
|
||||||
devfsdirs: []string{"card0"},
|
devfsdirs: []string{"card0"},
|
||||||
expectedDevs: 1,
|
expectedDevs: 1,
|
||||||
expectedErr: false,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "wrong vendor",
|
||||||
sysfsdirs: []string{"card0/device/drm/card0"},
|
sysfsdirs: []string{"card0/device/drm/card0"},
|
||||||
sysfsfiles: map[string][]byte{
|
sysfsfiles: map[string][]byte{
|
||||||
"card0/device/vendor": []byte("0xbeef"),
|
"card0/device/vendor": []byte("0xbeef"),
|
||||||
},
|
},
|
||||||
devfsdirs: []string{"card0"},
|
devfsdirs: []string{"card0"},
|
||||||
expectedDevs: 0,
|
|
||||||
expectedErr: false,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "no sysfs records",
|
||||||
sysfsdirs: []string{"non_gpu_card"},
|
sysfsdirs: []string{"non_gpu_card"},
|
||||||
expectedDevs: 0,
|
|
||||||
expectedErr: false,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
testPlugin := newDevicePlugin(sysfs, devfs, 1)
|
for _, tc := range tcases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
if testPlugin == nil {
|
for _, devfsdir := range tc.devfsdirs {
|
||||||
t.Fatal("Failed to create a deviceManager")
|
if err := os.MkdirAll(path.Join(devfs, devfsdir), 0755); err != nil {
|
||||||
}
|
|
||||||
|
|
||||||
for _, tcase := range tcases {
|
|
||||||
for _, devfsdir := range tcase.devfsdirs {
|
|
||||||
err := os.MkdirAll(path.Join(devfs, devfsdir), 0755)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to create fake device directory: %+v", err)
|
t.Fatalf("Failed to create fake device directory: %+v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, sysfsdir := range tcase.sysfsdirs {
|
for _, sysfsdir := range tc.sysfsdirs {
|
||||||
err := os.MkdirAll(path.Join(sysfs, sysfsdir), 0755)
|
if err := os.MkdirAll(path.Join(sysfs, sysfsdir), 0755); err != nil {
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to create fake device directory: %+v", err)
|
t.Fatalf("Failed to create fake device directory: %+v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for filename, body := range tcase.sysfsfiles {
|
for filename, body := range tc.sysfsfiles {
|
||||||
err := ioutil.WriteFile(path.Join(sysfs, filename), body, 0644)
|
if err := ioutil.WriteFile(path.Join(sysfs, filename), body, 0644); err != nil {
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to create fake vendor file: %+v", err)
|
t.Fatalf("Failed to create fake vendor file: %+v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tree, err := testPlugin.scan()
|
plugin := newDevicePlugin(sysfs, devfs, 1)
|
||||||
if tcase.expectedErr && err == nil {
|
|
||||||
t.Error("Expected error hasn't been triggered")
|
notifier := &mockNotifier{
|
||||||
}
|
scanDone: plugin.scanDone,
|
||||||
if !tcase.expectedErr && err != nil {
|
|
||||||
t.Errorf("Unexpcted error: %+v", err)
|
|
||||||
}
|
|
||||||
if tcase.expectedDevs != len(tree[deviceType]) {
|
|
||||||
t.Errorf("Wrong number of discovered devices")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.RemoveAll(tmpdir)
|
err := plugin.Scan(notifier)
|
||||||
|
// Scans in GPU plugin never fail
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to remove fake device directory: %+v", err)
|
t.Errorf("unexpected error: %+v", err)
|
||||||
}
|
}
|
||||||
|
if tc.expectedDevs != notifier.devCount {
|
||||||
|
t.Errorf("Wrong number of discovered devices")
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user