Merge pull request #122885 from claudiubelu/unittests-10

unittests: Fixes unit tests for Windows (part 10)
This commit is contained in:
Kubernetes Prow Robot 2024-02-28 05:38:40 -08:00 committed by GitHub
commit f139450e9b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 22 additions and 8 deletions

View File

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

View File

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

View File

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

View File

@ -20,6 +20,7 @@ import (
"os" "os"
"reflect" "reflect"
"strings" "strings"
"syscall"
"testing" "testing"
"time" "time"
@ -905,7 +906,7 @@ func TestLoadAuthenticationConfig(t *testing.T) {
{ {
name: "missing file", name: "missing file",
file: func() string { return "bogus-missing-file" }, file: func() string { return "bogus-missing-file" },
expectErr: "no such file or directory", expectErr: syscall.Errno(syscall.ENOENT).Error(),
expectedConfig: nil, expectedConfig: nil,
}, },
{ {
@ -1037,6 +1038,10 @@ func writeTempFile(t *testing.T, content string) string {
t.Fatal(err) t.Fatal(err)
} }
t.Cleanup(func() { 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 { if err := os.Remove(file.Name()); err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -64,7 +64,7 @@ func TestSummaryProvider(t *testing.T) {
mockStatsProvider.EXPECT().GetPodCgroupRoot().Return(cgroupRoot).AnyTimes() mockStatsProvider.EXPECT().GetPodCgroupRoot().Return(cgroupRoot).AnyTimes()
mockStatsProvider.EXPECT().ListPodStats(ctx).Return(podStats, nil).AnyTimes() mockStatsProvider.EXPECT().ListPodStats(ctx).Return(podStats, nil).AnyTimes()
mockStatsProvider.EXPECT().ListPodStatsAndUpdateCPUNanoCoreUsage(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().RootFsStats().Return(rootFsStats, nil).AnyTimes()
mockStatsProvider.EXPECT().RlimitStats().Return(nil, nil).AnyTimes() mockStatsProvider.EXPECT().RlimitStats().Return(nil, nil).AnyTimes()
mockStatsProvider.EXPECT().GetCgroupStats("/", true).Return(cgroupStatsMap["/"].cs, cgroupStatsMap["/"].ns, 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.Memory, cgroupStatsMap["/"].cs.Memory)
assert.Equal(summary.Node.Network, cgroupStatsMap["/"].ns) assert.Equal(summary.Node.Network, cgroupStatsMap["/"].ns)
assert.Equal(summary.Node.Fs, rootFsStats) 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(len(summary.Node.SystemContainers), 1)
assert.Equal(summary.Node.SystemContainers[0].Name, "pods") assert.Equal(summary.Node.SystemContainers[0].Name, "pods")

View File

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

View File

@ -105,7 +105,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 // 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. // 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 { if isSocket, errSocket := filesystem.IsUnixDomainSocket(pathname); errSocket == nil && isSocket {
return FileTypeSocket, nil return FileTypeSocket, nil
} }