fpga tests cleanup

- used t.Run api for better visibility
- used ioutil.TempDir to create temporary directories

Signed-off-by: Ed Bartosh <eduard.bartosh@intel.com>
This commit is contained in:
Ed Bartosh 2020-03-31 12:25:38 +03:00
parent 08fde67a88
commit 2ec6677ab0
2 changed files with 83 additions and 65 deletions

View File

@ -15,13 +15,12 @@
package main package main
import ( import (
"fmt" "io/ioutil"
"os" "os"
"path" "path"
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
"time"
dpapi "github.com/intel/intel-device-plugins-for-kubernetes/pkg/deviceplugin" dpapi "github.com/intel/intel-device-plugins-for-kubernetes/pkg/deviceplugin"
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1" pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
@ -48,10 +47,15 @@ func TestNewDevicePluginDFL(t *testing.T) {
} }
for _, tcase := range tcases { for _, tcase := range tcases {
t.Run(tcase.mode, func(t *testing.T) {
_, err := newDevicePluginDFL("", "", tcase.mode) _, err := newDevicePluginDFL("", "", tcase.mode)
if tcase.expectedErr && err == nil { if tcase.expectedErr && err == nil {
t.Error("No error generated when creating Cache with invalid parameters") t.Error("Unexpected success")
} }
if !tcase.expectedErr && err != nil {
t.Errorf("Unexpected error: %+v", err)
}
})
} }
} }
@ -282,7 +286,10 @@ func TestGetAfuTreeDFL(t *testing.T) {
} }
func TestScanFPGAsDFL(t *testing.T) { func TestScanFPGAsDFL(t *testing.T) {
tmpdir := fmt.Sprintf("/tmp/fpgaplugin-TestDiscoverFPGAs-%d", time.Now().Unix()) tmpdir, err := ioutil.TempDir("", "TestScanFPGAsDFL")
if err != nil {
t.Fatalf("can't create temporary directory: %+v", err)
}
sysfs := path.Join(tmpdir, "sys", "class", "fpga_region") sysfs := path.Join(tmpdir, "sys", "class", "fpga_region")
devfs := path.Join(tmpdir, "dev") devfs := path.Join(tmpdir, "dev")
tcases := []struct { tcases := []struct {
@ -490,6 +497,7 @@ func TestScanFPGAsDFL(t *testing.T) {
} }
for _, tcase := range tcases { for _, tcase := range tcases {
t.Run(tcase.name, func(t *testing.T) {
err := createTestDirs(devfs, sysfs, tcase.devfsdirs, tcase.sysfsdirs, tcase.sysfsfiles) err := createTestDirs(devfs, sysfs, tcase.devfsdirs, tcase.sysfsdirs, tcase.sysfsfiles)
if err != nil { if err != nil {
t.Fatalf("%+v", err) t.Fatalf("%+v", err)
@ -497,8 +505,8 @@ func TestScanFPGAsDFL(t *testing.T) {
plugin, err := newDevicePluginDFL(sysfs, devfs, tcase.mode) plugin, err := newDevicePluginDFL(sysfs, devfs, tcase.mode)
if err != nil { if err != nil {
t.Errorf("Test case '%s': %+v", tcase.name, err) t.Errorf("error creating DFL plugin: %+v", err)
continue return
} }
plugin.getDevTree = func(devices []device) dpapi.DeviceTree { plugin.getDevTree = func(devices []device) dpapi.DeviceTree {
return dpapi.NewDeviceTree() return dpapi.NewDeviceTree()
@ -507,15 +515,16 @@ func TestScanFPGAsDFL(t *testing.T) {
_, err = plugin.scanFPGAs() _, err = plugin.scanFPGAs()
if tcase.errorContains != "" { if tcase.errorContains != "" {
if err == nil || !strings.Contains(err.Error(), tcase.errorContains) { if err == nil || !strings.Contains(err.Error(), tcase.errorContains) {
t.Errorf("Test case '%s': expected error '%s', but got '%v'", tcase.name, tcase.errorContains, err) t.Errorf("expected error '%s', but got '%v'", tcase.errorContains, err)
} }
} else if err != nil { } else if err != nil {
t.Errorf("Test case '%s': expected no error, but got '%+v'", tcase.name, err) t.Errorf("expected no error, but got '%+v'", err)
} }
err = os.RemoveAll(tmpdir) err = os.RemoveAll(tmpdir)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
})
} }
} }

View File

@ -15,13 +15,12 @@
package main package main
import ( import (
"fmt" "io/ioutil"
"os" "os"
"path" "path"
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
"time"
dpapi "github.com/intel/intel-device-plugins-for-kubernetes/pkg/deviceplugin" dpapi "github.com/intel/intel-device-plugins-for-kubernetes/pkg/deviceplugin"
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1" pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
@ -48,10 +47,15 @@ func TestNewDevicePluginOPAE(t *testing.T) {
} }
for _, tcase := range tcases { for _, tcase := range tcases {
t.Run(tcase.mode, func(t *testing.T) {
_, err := newDevicePluginOPAE("", "", tcase.mode) _, err := newDevicePluginOPAE("", "", tcase.mode)
if tcase.expectedErr && err == nil { if tcase.expectedErr && err == nil {
t.Error("No error generated when creating Cache with invalid parameters") t.Error("Unexpected success")
} }
if !tcase.expectedErr && err != nil {
t.Errorf("Unexpected error: %+v", err)
}
})
} }
} }
@ -233,7 +237,10 @@ func TestGetAfuTreeOPAE(t *testing.T) {
} }
func TestScanFPGAsOPAE(t *testing.T) { func TestScanFPGAsOPAE(t *testing.T) {
tmpdir := fmt.Sprintf("/tmp/fpgaplugin-TestDiscoverFPGAs-%d", time.Now().Unix()) tmpdir, err := ioutil.TempDir("", "TestScanFPGAsOPAE")
if err != nil {
t.Fatalf("can't create temporary directory: %+v", err)
}
sysfs := path.Join(tmpdir, "sys", "class", "fpga") sysfs := path.Join(tmpdir, "sys", "class", "fpga")
devfs := path.Join(tmpdir, "dev") devfs := path.Join(tmpdir, "dev")
tcases := []struct { tcases := []struct {
@ -353,6 +360,7 @@ func TestScanFPGAsOPAE(t *testing.T) {
} }
for _, tcase := range tcases { for _, tcase := range tcases {
t.Run(tcase.name, func(t *testing.T) {
err := createTestDirs(devfs, sysfs, tcase.devfsdirs, tcase.sysfsdirs, tcase.sysfsfiles) err := createTestDirs(devfs, sysfs, tcase.devfsdirs, tcase.sysfsdirs, tcase.sysfsfiles)
if err != nil { if err != nil {
t.Fatalf("%+v", err) t.Fatalf("%+v", err)
@ -371,15 +379,16 @@ func TestScanFPGAsOPAE(t *testing.T) {
_, err = plugin.scanFPGAs() _, err = plugin.scanFPGAs()
if tcase.errorContains != "" { if tcase.errorContains != "" {
if err == nil || !strings.Contains(err.Error(), tcase.errorContains) { if err == nil || !strings.Contains(err.Error(), tcase.errorContains) {
t.Errorf("Test case '%s': expected error '%s', but got '%v'", tcase.name, tcase.errorContains, err) t.Errorf("expected error '%s', but got '%v'", tcase.errorContains, err)
} }
} else if err != nil { } else if err != nil {
t.Errorf("Test case '%s': expected no error, but got '%+v'", tcase.name, err) t.Errorf("expected no error, but got '%+v'", err)
} }
err = os.RemoveAll(tmpdir) err = os.RemoveAll(tmpdir)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
})
} }
} }