unittests: Fixes unit tests for Windows (part 3)

Currently, there are some unit tests that are failing on Windows due to
various reasons:

- paths not properly joined (filepath.Join should be used).
- Proxy Mode IPVS not supported on Windows.
- DeadlineExceeded can occur when trying to read data from an UDP
  socket. This can be used to detect whether the port was closed or not.
- In Windows, with long file name support enabled, file names can have
  up to 32,767 characters. In this case, the error
  windows.ERROR_FILENAME_EXCED_RANGE will be encountered instead.
- files not closed, which means that they cannot be removed / renamed.
- time.Now() is not as precise on Windows, which means that 2
  consecutive calls may return the same timestamp.
- path.Base() will return the same path. filepath.Base() should be used
  instead.
- path.Join() will always join the paths with a / instead of the OS
  specific separator. filepath.Join() should be used instead.
This commit is contained in:
Claudiu Belu
2022-06-06 10:51:42 +03:00
parent 83415e5c9e
commit 9f95b7b18c
14 changed files with 134 additions and 53 deletions

View File

@@ -81,8 +81,8 @@ func tmpSocketDir() (socketDir, socketName, pluginSocketName string, err error)
if err != nil {
return
}
socketName = socketDir + "/server.sock"
pluginSocketName = socketDir + "/device-plugin.sock"
socketName = filepath.Join(socketDir, "server.sock")
pluginSocketName = filepath.Join(socketDir, "device-plugin.sock")
os.MkdirAll(socketDir, 0755)
return
}

View File

@@ -20,6 +20,7 @@ import (
"flag"
"fmt"
"os"
"path/filepath"
"sync"
"testing"
"time"
@@ -32,8 +33,6 @@ import (
)
var (
socketDir string
supportedVersions = []string{"v1beta1", "v1beta2"}
)
@@ -44,18 +43,17 @@ func init() {
flag.Set("alsologtostderr", fmt.Sprintf("%t", true))
flag.StringVar(&logLevel, "logLevel", "6", "test")
flag.Lookup("v").Value.Set(logLevel)
d, err := os.MkdirTemp("", "plugin_test")
if err != nil {
panic(fmt.Sprintf("Could not create a temp directory: %s", d))
}
socketDir = d
}
func cleanup(t *testing.T) {
require.NoError(t, os.RemoveAll(socketDir))
os.MkdirAll(socketDir, 0755)
func initTempDir(t *testing.T) string {
// Creating a different directory. os.RemoveAll is not atomic enough;
// os.MkdirAll can get into an "Access Denied" error on Windows.
d, err := os.MkdirTemp("", "plugin_test")
if err != nil {
t.Fatalf("Could not create a temp directory %s: %v", d, err)
}
return d
}
func waitForRegistration(
@@ -106,13 +104,14 @@ func retryWithExponentialBackOff(initialDuration time.Duration, fn wait.Conditio
}
func TestPluginRegistration(t *testing.T) {
defer cleanup(t)
socketDir := initTempDir(t)
defer os.RemoveAll(socketDir)
dsw := cache.NewDesiredStateOfWorld()
newWatcher(t, dsw, wait.NeverStop)
newWatcher(t, socketDir, dsw, wait.NeverStop)
for i := 0; i < 10; i++ {
socketPath := fmt.Sprintf("%s/plugin-%d.sock", socketDir, i)
socketPath := filepath.Join(socketDir, fmt.Sprintf("plugin-%d.sock", i))
pluginName := fmt.Sprintf("example-plugin-%d", i)
p := NewTestExamplePlugin(pluginName, registerapi.DevicePlugin, socketPath, supportedVersions...)
@@ -139,16 +138,17 @@ func TestPluginRegistration(t *testing.T) {
}
func TestPluginRegistrationSameName(t *testing.T) {
defer cleanup(t)
socketDir := initTempDir(t)
defer os.RemoveAll(socketDir)
dsw := cache.NewDesiredStateOfWorld()
newWatcher(t, dsw, wait.NeverStop)
newWatcher(t, socketDir, dsw, wait.NeverStop)
// Make 10 plugins with the same name and same type but different socket path;
// all 10 should be in desired state of world cache
pluginName := "dep-example-plugin"
for i := 0; i < 10; i++ {
socketPath := fmt.Sprintf("%s/plugin-%d.sock", socketDir, i)
socketPath := filepath.Join(socketDir, fmt.Sprintf("plugin-%d.sock", i))
p := NewTestExamplePlugin(pluginName, registerapi.DevicePlugin, socketPath, supportedVersions...)
require.NoError(t, p.Serve("v1beta1", "v1beta2"))
@@ -164,14 +164,15 @@ func TestPluginRegistrationSameName(t *testing.T) {
}
func TestPluginReRegistration(t *testing.T) {
defer cleanup(t)
socketDir := initTempDir(t)
defer os.RemoveAll(socketDir)
dsw := cache.NewDesiredStateOfWorld()
newWatcher(t, dsw, wait.NeverStop)
newWatcher(t, socketDir, dsw, wait.NeverStop)
// Create a plugin first, we are then going to remove the plugin, update the plugin with a different name
// and recreate it.
socketPath := fmt.Sprintf("%s/plugin-reregistration.sock", socketDir)
socketPath := filepath.Join(socketDir, "plugin-reregistration.sock")
pluginName := "reregister-plugin"
p := NewTestExamplePlugin(pluginName, registerapi.DevicePlugin, socketPath, supportedVersions...)
require.NoError(t, p.Serve("v1beta1", "v1beta2"))
@@ -206,12 +207,13 @@ func TestPluginReRegistration(t *testing.T) {
}
func TestPluginRegistrationAtKubeletStart(t *testing.T) {
defer cleanup(t)
socketDir := initTempDir(t)
defer os.RemoveAll(socketDir)
plugins := make([]*examplePlugin, 10)
for i := 0; i < len(plugins); i++ {
socketPath := fmt.Sprintf("%s/plugin-%d.sock", socketDir, i)
socketPath := filepath.Join(socketDir, fmt.Sprintf("plugin-%d.sock", i))
pluginName := fmt.Sprintf("example-plugin-%d", i)
p := NewTestExamplePlugin(pluginName, registerapi.DevicePlugin, socketPath, supportedVersions...)
@@ -224,7 +226,7 @@ func TestPluginRegistrationAtKubeletStart(t *testing.T) {
}
dsw := cache.NewDesiredStateOfWorld()
newWatcher(t, dsw, wait.NeverStop)
newWatcher(t, socketDir, dsw, wait.NeverStop)
var wg sync.WaitGroup
for i := 0; i < len(plugins); i++ {
@@ -252,7 +254,7 @@ func TestPluginRegistrationAtKubeletStart(t *testing.T) {
}
}
func newWatcher(t *testing.T, desiredStateOfWorldCache cache.DesiredStateOfWorld, stopCh <-chan struct{}) *Watcher {
func newWatcher(t *testing.T, socketDir string, desiredStateOfWorldCache cache.DesiredStateOfWorld, stopCh <-chan struct{}) *Watcher {
w := NewWatcher(socketDir, desiredStateOfWorldCache)
require.NoError(t, w.Start(stopCh))

View File

@@ -19,6 +19,7 @@ package reconciler
import (
"fmt"
"os"
"path/filepath"
"testing"
"time"
@@ -187,7 +188,7 @@ func Test_Run_Positive_Register(t *testing.T) {
stopChan := make(chan struct{})
defer close(stopChan)
go reconciler.Run(stopChan)
socketPath := fmt.Sprintf("%s/plugin.sock", socketDir)
socketPath := filepath.Join(socketDir, "plugin.sock")
pluginName := fmt.Sprintf("example-plugin")
p := pluginwatcher.NewTestExamplePlugin(pluginName, registerapi.DevicePlugin, socketPath, supportedVersions...)
require.NoError(t, p.Serve("v1beta1", "v1beta2"))
@@ -233,7 +234,7 @@ func Test_Run_Positive_RegisterThenUnregister(t *testing.T) {
defer close(stopChan)
go reconciler.Run(stopChan)
socketPath := fmt.Sprintf("%s/plugin.sock", socketDir)
socketPath := filepath.Join(socketDir, "plugin.sock")
pluginName := fmt.Sprintf("example-plugin")
p := pluginwatcher.NewTestExamplePlugin(pluginName, registerapi.DevicePlugin, socketPath, supportedVersions...)
require.NoError(t, p.Serve("v1beta1", "v1beta2"))
@@ -289,7 +290,7 @@ func Test_Run_Positive_ReRegister(t *testing.T) {
defer close(stopChan)
go reconciler.Run(stopChan)
socketPath := fmt.Sprintf("%s/plugin2.sock", socketDir)
socketPath := filepath.Join(socketDir, "plugin2.sock")
pluginName := fmt.Sprintf("example-plugin2")
p := pluginwatcher.NewTestExamplePlugin(pluginName, registerapi.DevicePlugin, socketPath, supportedVersions...)
require.NoError(t, p.Serve("v1beta1", "v1beta2"))