Skip to content

Commit 2ec6677

Browse files
committed
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>
1 parent 08fde67 commit 2ec6677

File tree

2 files changed

+77
-59
lines changed

2 files changed

+77
-59
lines changed

cmd/fpga_plugin/dfl_test.go

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515
package main
1616

1717
import (
18-
"fmt"
18+
"io/ioutil"
1919
"os"
2020
"path"
2121
"reflect"
2222
"strings"
2323
"testing"
24-
"time"
2524

2625
dpapi "github.com/intel/intel-device-plugins-for-kubernetes/pkg/deviceplugin"
2726
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
@@ -48,10 +47,15 @@ func TestNewDevicePluginDFL(t *testing.T) {
4847
}
4948

5049
for _, 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

284288
func 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+
}
286293
sysfs := path.Join(tmpdir, "sys", "class", "fpga_region")
287294
devfs := path.Join(tmpdir, "dev")
288295
tcases := []struct {
@@ -490,32 +497,34 @@ func TestScanFPGAsDFL(t *testing.T) {
490497
}
491498

492499
for _, 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
}

cmd/fpga_plugin/opae_test.go

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515
package main
1616

1717
import (
18-
"fmt"
18+
"io/ioutil"
1919
"os"
2020
"path"
2121
"reflect"
2222
"strings"
2323
"testing"
24-
"time"
2524

2625
dpapi "github.com/intel/intel-device-plugins-for-kubernetes/pkg/deviceplugin"
2726
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
@@ -48,10 +47,15 @@ func TestNewDevicePluginOPAE(t *testing.T) {
4847
}
4948

5049
for _, tcase := range tcases {
51-
_, err := newDevicePluginOPAE("", "", 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 := newDevicePluginOPAE("", "", 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

@@ -233,7 +237,10 @@ func TestGetAfuTreeOPAE(t *testing.T) {
233237
}
234238

235239
func TestScanFPGAsOPAE(t *testing.T) {
236-
tmpdir := fmt.Sprintf("/tmp/fpgaplugin-TestDiscoverFPGAs-%d", time.Now().Unix())
240+
tmpdir, err := ioutil.TempDir("", "TestScanFPGAsOPAE")
241+
if err != nil {
242+
t.Fatalf("can't create temporary directory: %+v", err)
243+
}
237244
sysfs := path.Join(tmpdir, "sys", "class", "fpga")
238245
devfs := path.Join(tmpdir, "dev")
239246
tcases := []struct {
@@ -353,33 +360,35 @@ func TestScanFPGAsOPAE(t *testing.T) {
353360
}
354361

355362
for _, tcase := range tcases {
356-
err := createTestDirs(devfs, sysfs, tcase.devfsdirs, tcase.sysfsdirs, tcase.sysfsfiles)
357-
if err != nil {
358-
t.Fatalf("%+v", err)
359-
}
363+
t.Run(tcase.name, func(t *testing.T) {
364+
err := createTestDirs(devfs, sysfs, tcase.devfsdirs, tcase.sysfsdirs, tcase.sysfsfiles)
365+
if err != nil {
366+
t.Fatalf("%+v", err)
367+
}
360368

361-
plugin, err := newDevicePluginOPAE(sysfs, devfs, tcase.mode)
369+
plugin, err := newDevicePluginOPAE(sysfs, devfs, tcase.mode)
362370

363-
plugin.getDevTree = func(devices []device) dpapi.DeviceTree {
364-
return dpapi.NewDeviceTree()
365-
}
371+
plugin.getDevTree = func(devices []device) dpapi.DeviceTree {
372+
return dpapi.NewDeviceTree()
373+
}
366374

367-
if err != nil {
368-
t.Fatalf("%+v", err)
369-
}
375+
if err != nil {
376+
t.Fatalf("%+v", err)
377+
}
370378

371-
_, err = plugin.scanFPGAs()
372-
if tcase.errorContains != "" {
373-
if err == nil || !strings.Contains(err.Error(), tcase.errorContains) {
374-
t.Errorf("Test case '%s': expected error '%s', but got '%v'", tcase.name, tcase.errorContains, err)
379+
_, err = plugin.scanFPGAs()
380+
if tcase.errorContains != "" {
381+
if err == nil || !strings.Contains(err.Error(), tcase.errorContains) {
382+
t.Errorf("expected error '%s', but got '%v'", tcase.errorContains, err)
383+
}
384+
} else if err != nil {
385+
t.Errorf("expected no error, but got '%+v'", err)
375386
}
376-
} else if err != nil {
377-
t.Errorf("Test case '%s': expected no error, but got '%+v'", tcase.name, err)
378-
}
379387

380-
err = os.RemoveAll(tmpdir)
381-
if err != nil {
382-
t.Fatal(err)
383-
}
388+
err = os.RemoveAll(tmpdir)
389+
if err != nil {
390+
t.Fatal(err)
391+
}
392+
})
384393
}
385394
}

0 commit comments

Comments
 (0)