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:
		@@ -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}}`,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -42,6 +42,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
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,6 +20,7 @@ import (
 | 
				
			|||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"reflect"
 | 
						"reflect"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
						"syscall"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -866,7 +867,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,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
@@ -998,6 +999,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)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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")
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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
 | 
				
			||||||
@@ -108,5 +112,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
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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
 | 
						// 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
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user