intel-device-plugins-for-ku.../cmd/gpu_plugin/gpu_plugin_test.go
Dmitry Rozhkov bbee3fde77 refactor device plugins to increase code reuse
Every device plugin is supposed to implement PluginInterfaceServer
interface to be exposed as a gRPC service. But this functionality is
common for all our device plugins and can be hidden in a Manager
which manages all gRPC servers dynamically.

The only mandatory functionality that needs to be provided by a device
plugin and which differentiate one plugin from another is the code
scanning the host for devices present on it.

Refactor the internal deviceplugin package to accept only
one mandatory method implementation from device plugins - Scan().

In addition to that  a device plugin can optionally implement a
PostAllocate() method which mutates responses returned by
PluginInterfaceServer.Allocate() method.

Also to narrow the gap between these device plugins and the
kubevirt's collection the naming scheme for resources has been changed.
Now device plugins provide a namespace for the device types they
operate with. E.g. for resources in format "color.example.com/<color>"
the namespace would be "color.example.com". So, the resource name
"intel.com/fpga-region-fffffff" becomes "fpga.intel.com/region-fffffff".
2018-07-30 15:29:33 +03:00

118 lines
2.9 KiB
Go

// Copyright 2017 Intel Corporation. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"testing"
"time"
)
func TestScan(t *testing.T) {
tmpdir := fmt.Sprintf("/tmp/gpuplugin-test-%d", time.Now().Unix())
sysfs := path.Join(tmpdir, "sysfs")
devfs := path.Join(tmpdir, "devfs")
tcases := []struct {
devfsdirs []string
sysfsdirs []string
sysfsfiles map[string][]byte
expectedDevs int
expectedErr bool
}{
{
expectedErr: true,
expectedDevs: 0,
},
{
sysfsdirs: []string{"card0"},
expectedDevs: 0,
expectedErr: false,
},
{
sysfsdirs: []string{"card0/device"},
sysfsfiles: map[string][]byte{
"card0/device/vendor": []byte("0x8086"),
},
expectedDevs: 0,
expectedErr: true,
},
{
sysfsdirs: []string{"card0/device/drm/card0"},
sysfsfiles: map[string][]byte{
"card0/device/vendor": []byte("0x8086"),
},
devfsdirs: []string{"card0"},
expectedDevs: 1,
expectedErr: false,
},
{
sysfsdirs: []string{
"card0/device/drm/card0",
"card1/device/drm/card1",
},
sysfsfiles: map[string][]byte{
"card0/device/vendor": []byte("0x8086"),
"card1/device/vendor": []byte("0x8086"),
},
devfsdirs: []string{"card0"},
expectedDevs: 1,
expectedErr: false,
},
}
testPlugin := newDevicePlugin(sysfs, devfs)
if testPlugin == nil {
t.Fatal("Failed to create a deviceManager")
}
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)
}
}
for _, sysfsdir := range tcase.sysfsdirs {
err := os.MkdirAll(path.Join(sysfs, sysfsdir), 0755)
if err != nil {
t.Fatalf("Failed to create fake device directory: %+v", err)
}
}
for filename, body := range tcase.sysfsfiles {
err := ioutil.WriteFile(path.Join(sysfs, filename), body, 0644)
if err != nil {
t.Fatalf("Failed to create fake vendor file: %+v", err)
}
}
tree, err := testPlugin.scan()
if tcase.expectedErr && err == nil {
t.Error("Expected error hasn't been triggered")
}
if tcase.expectedDevs != len(tree[deviceType]) {
t.Errorf("Wrong number of discovered devices")
}
err = os.RemoveAll(tmpdir)
if err != nil {
t.Fatalf("Failed to remove fake device directory: %+v", err)
}
}
}