1515package main
1616
1717import (
18- "fmt "
18+ "io/ioutil "
1919"os"
2020"path"
2121"reflect"
2222"strings"
2323"testing"
24- "time"
2524
2625dpapi "github.com/intel/intel-device-plugins-for-kubernetes/pkg/deviceplugin"
2726pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
@@ -48,10 +47,15 @@ func TestNewDevicePluginDFL(t *testing.T) {
4847}
4948
5049for _ , tcase := range tcases {
51- _ , err := newDevicePluginDFL ("" , "" , tcase .mode )
52- if tcase .expectedErr && err == nil {
53- t .Error ("No error generated when creating Cache with invalid parameters" )
54- }
50+ t .Run (tcase .mode , func (t * testing.T ) {
51+ _ , err := newDevicePluginDFL ("" , "" , tcase .mode )
52+ if tcase .expectedErr && err == nil {
53+ t .Error ("Unexpected success" )
54+ }
55+ if ! tcase .expectedErr && err != nil {
56+ t .Errorf ("Unexpected error: %+v" , err )
57+ }
58+ })
5559}
5660}
5761
@@ -282,7 +286,10 @@ func TestGetAfuTreeDFL(t *testing.T) {
282286}
283287
284288func TestScanFPGAsDFL (t * testing.T ) {
285- tmpdir := fmt .Sprintf ("/tmp/fpgaplugin-TestDiscoverFPGAs-%d" , time .Now ().Unix ())
289+ tmpdir , err := ioutil .TempDir ("" , "TestScanFPGAsDFL" )
290+ if err != nil {
291+ t .Fatalf ("can't create temporary directory: %+v" , err )
292+ }
286293sysfs := path .Join (tmpdir , "sys" , "class" , "fpga_region" )
287294devfs := path .Join (tmpdir , "dev" )
288295tcases := []struct {
@@ -490,32 +497,34 @@ func TestScanFPGAsDFL(t *testing.T) {
490497}
491498
492499for _ , tcase := range tcases {
493- err := createTestDirs (devfs , sysfs , tcase .devfsdirs , tcase .sysfsdirs , tcase .sysfsfiles )
494- if err != nil {
495- t .Fatalf ("%+v" , err )
496- }
500+ t .Run (tcase .name , func (t * testing.T ) {
501+ err := createTestDirs (devfs , sysfs , tcase .devfsdirs , tcase .sysfsdirs , tcase .sysfsfiles )
502+ if err != nil {
503+ t .Fatalf ("%+v" , err )
504+ }
497505
498- plugin , err := newDevicePluginDFL (sysfs , devfs , tcase .mode )
499- if err != nil {
500- t .Errorf ("Test case '%s' : %+v" , tcase . name , err )
501- continue
502- }
503- plugin .getDevTree = func (devices []device ) dpapi.DeviceTree {
504- return dpapi .NewDeviceTree ()
505- }
506+ plugin , err := newDevicePluginDFL (sysfs , devfs , tcase .mode )
507+ if err != nil {
508+ t .Errorf ("error creating DFL plugin : %+v" , err )
509+ return
510+ }
511+ plugin .getDevTree = func (devices []device ) dpapi.DeviceTree {
512+ return dpapi .NewDeviceTree ()
513+ }
506514
507- _ , err = plugin .scanFPGAs ()
508- if tcase .errorContains != "" {
509- if err == nil || ! strings .Contains (err .Error (), tcase .errorContains ) {
510- t .Errorf ("Test case '%s': expected error '%s', but got '%v'" , tcase .name , tcase .errorContains , err )
515+ _ , err = plugin .scanFPGAs ()
516+ if tcase .errorContains != "" {
517+ if err == nil || ! strings .Contains (err .Error (), tcase .errorContains ) {
518+ t .Errorf ("expected error '%s', but got '%v'" , tcase .errorContains , err )
519+ }
520+ } else if err != nil {
521+ t .Errorf ("expected no error, but got '%+v'" , err )
511522}
512- } else if err != nil {
513- t .Errorf ("Test case '%s': expected no error, but got '%+v'" , tcase .name , err )
514- }
515523
516- err = os .RemoveAll (tmpdir )
517- if err != nil {
518- t .Fatal (err )
519- }
524+ err = os .RemoveAll (tmpdir )
525+ if err != nil {
526+ t .Fatal (err )
527+ }
528+ })
520529}
521530}
0 commit comments