unittests: Fixes unit tests for Windows (part 10)

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

- Different "File not found" error messages on Windows.
- Files need to be closed on Windows before removing them.
- The default RootHnsEndpointName (root-hnsendpoint-name) flag value is 'cbr0'
- On Windows, Unix Domain sockets are not checked in the same way in golang, which is why
  hostutils_windows.go checks for it differently. GetFileType will return an error in this
  case. We need to check for it, and see if it's actually a Unix Domain Socket.
This commit is contained in:
Claudiu Belu 2024-01-20 00:40:46 +00:00
parent 909faa3a9b
commit b8df7e7684
7 changed files with 22 additions and 8 deletions

View File

@ -21,6 +21,7 @@ import (
"os"
"path"
"strings"
"syscall"
"testing"
"github.com/stretchr/testify/assert"
@ -28,6 +29,7 @@ import (
)
func TestGenerate(t *testing.T) {
noFileErr := os.PathError{Op: "open", Path: "no-such-file.txt", Err: syscall.Errno(syscall.ENOENT)}
for name, tt := range map[string]struct {
in string
data map[string]string
@ -37,7 +39,7 @@ func TestGenerate(t *testing.T) {
}{
"missing-file": {
in: `{{include "no-such-file.txt"}}`,
expectedErr: "open no-such-file.txt: no such file or directory",
expectedErr: noFileErr.Error(),
},
"data": {
in: `{{.Hello}} {{.World}}`,

View File

@ -42,6 +42,9 @@ func (o *Options) platformApplyDefaults(config *proxyconfigapi.KubeProxyConfigur
if config.Mode == "" {
config.Mode = proxyconfigapi.ProxyModeKernelspace
}
if config.Winkernel.RootHnsEndpointName == "" {
config.Winkernel.RootHnsEndpointName = "cbr0"
}
}
// platformSetup is called after setting up the ProxyServer, but before creating the

View File

@ -21,6 +21,7 @@ import (
"fmt"
"os"
"path/filepath"
"syscall"
"testing"
"github.com/stretchr/testify/assert"
@ -32,7 +33,6 @@ const (
apiVersionMissing = "'apiVersion' is missing"
apiVersionTooOld = "no kind \"KubeSchedulerConfiguration\" is registered for" +
" version \"kubescheduler.config.k8s.io/v1alpha1\""
fileNotFound = "no such file or directory"
// schedulerConfigMinimalCorrect is the minimal
// correct scheduler config
@ -91,7 +91,7 @@ func TestLoadConfigFromFile(t *testing.T) {
{
name: "Empty scheduler config file path",
path: "",
expectedErr: fmt.Errorf(fileNotFound),
expectedErr: syscall.Errno(syscall.ENOENT),
expectedConfig: nil,
},
{

View File

@ -20,6 +20,7 @@ import (
"os"
"reflect"
"strings"
"syscall"
"testing"
"time"
@ -866,7 +867,7 @@ func TestLoadAuthenticationConfig(t *testing.T) {
{
name: "missing file",
file: func() string { return "bogus-missing-file" },
expectErr: "no such file or directory",
expectErr: syscall.Errno(syscall.ENOENT).Error(),
expectedConfig: nil,
},
{
@ -998,6 +999,10 @@ func writeTempFile(t *testing.T, content string) string {
t.Fatal(err)
}
t.Cleanup(func() {
// An open file cannot be removed on Windows. Close it first.
if err := file.Close(); err != nil {
t.Fatal(err)
}
if err := os.Remove(file.Name()); err != nil {
t.Fatal(err)
}

View File

@ -64,7 +64,7 @@ func TestSummaryProvider(t *testing.T) {
mockStatsProvider.EXPECT().GetPodCgroupRoot().Return(cgroupRoot).AnyTimes()
mockStatsProvider.EXPECT().ListPodStats(ctx).Return(podStats, nil).AnyTimes()
mockStatsProvider.EXPECT().ListPodStatsAndUpdateCPUNanoCoreUsage(ctx).Return(podStats, nil).AnyTimes()
mockStatsProvider.EXPECT().ImageFsStats(ctx).Return(imageFsStats, nil).AnyTimes()
mockStatsProvider.EXPECT().ImageFsStats(ctx).Return(imageFsStats, ImageFsStats, nil).AnyTimes()
mockStatsProvider.EXPECT().RootFsStats().Return(rootFsStats, nil).AnyTimes()
mockStatsProvider.EXPECT().RlimitStats().Return(nil, nil).AnyTimes()
mockStatsProvider.EXPECT().GetCgroupStats("/", true).Return(cgroupStatsMap["/"].cs, cgroupStatsMap["/"].ns, nil).AnyTimes()
@ -81,7 +81,7 @@ func TestSummaryProvider(t *testing.T) {
assert.Equal(summary.Node.Memory, cgroupStatsMap["/"].cs.Memory)
assert.Equal(summary.Node.Network, cgroupStatsMap["/"].ns)
assert.Equal(summary.Node.Fs, rootFsStats)
assert.Equal(summary.Node.Runtime, &statsapi.RuntimeStats{ImageFs: imageFsStats})
assert.Equal(summary.Node.Runtime, &statsapi.RuntimeStats{ContainerFs: imageFsStats, ImageFs: imageFsStats})
assert.Equal(len(summary.Node.SystemContainers), 1)
assert.Equal(summary.Node.SystemContainers[0].Name, "pods")

View File

@ -41,6 +41,10 @@ const (
FileTypeUnknown FileType = ""
)
var (
errUnknownFileType = fmt.Errorf("only recognise file, directory, socket, block device and character device")
)
// HostUtils defines the set of methods for interacting with paths on a host.
type HostUtils interface {
// DeviceOpened determines if the device (e.g. /dev/sdc) is in use elsewhere
@ -108,5 +112,5 @@ func getFileType(pathname string) (FileType, error) {
return FileTypeBlockDev, nil
}
return pathType, fmt.Errorf("only recognise file, directory, socket, block device and character device")
return pathType, errUnknownFileType
}

View File

@ -106,7 +106,7 @@ func (hu *(HostUtil)) GetFileType(pathname string) (FileType, error) {
// os.Stat will return a 1920 error (windows.ERROR_CANT_ACCESS_FILE) if we use it on a Unix Socket
// on Windows. In this case, we need to use a different method to check if it's a Unix Socket.
if isSystemCannotAccessErr(err) {
if err == errUnknownFileType || isSystemCannotAccessErr(err) {
if isSocket, errSocket := filesystem.IsUnixDomainSocket(pathname); errSocket == nil && isSocket {
return FileTypeSocket, nil
}