unittests: Fixes hostutil.GetFileType for Windows

GetFileType is meant to return the type of the given file by using os.Stat.
However, os.Stat doesn't work on Windows for Unix Sockets, causing an error to occur:

[2-Socket Test] unexpected error :
CreateFile C:\Users\Administrator\AppData\Local\Temp\test-get-filetype-2776877299\mt.sock:
The file cannot be accessed by the system.

This is a known issue and we're already using a workaround for this in
pkg/kubelet/util/util_windows.go.

This commit fixes this issue for GetFileType on Windows.
This commit is contained in:
Claudiu Belu
2022-12-07 12:25:53 +00:00
parent 12386e2de1
commit b4d1440063
11 changed files with 326 additions and 196 deletions

View File

@@ -182,64 +182,6 @@ func TestParseEndpoint(t *testing.T) {
}
func TestIsUnixDomainSocketPipe(t *testing.T) {
generatePipeName := func(suffixLen int) string {
rand.Seed(time.Now().UnixNano())
letter := []rune("abcdef0123456789")
b := make([]rune, suffixLen)
for i := range b {
b[i] = letter[rand.Intn(len(letter))]
}
return "\\\\.\\pipe\\test-pipe" + string(b)
}
testFile := generatePipeName(4)
pipeln, err := winio.ListenPipe(testFile, &winio.PipeConfig{SecurityDescriptor: "D:P(A;;GA;;;BA)(A;;GA;;;SY)"})
defer pipeln.Close()
require.NoErrorf(t, err, "Failed to listen on named pipe for test purposes: %v", err)
result, err := IsUnixDomainSocket(testFile)
assert.NoError(t, err, "Unexpected error from IsUnixDomainSocket.")
assert.False(t, result, "Unexpected result: true from IsUnixDomainSocket.")
}
// This is required as on Windows it's possible for the socket file backing a Unix domain socket to
// exist but not be ready for socket communications yet as per
// https://github.com/kubernetes/kubernetes/issues/104584
func TestPendingUnixDomainSocket(t *testing.T) {
// Create a temporary file that will simulate the Unix domain socket file in a
// not-yet-ready state. We need this because the Kubelet keeps an eye on file
// changes and acts on them, leading to potential race issues as described in
// the referenced issue above
f, err := os.CreateTemp("", "test-domain-socket")
require.NoErrorf(t, err, "Failed to create file for test purposes: %v", err)
testFile := f.Name()
f.Close()
// Start the check at this point
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
result, err := IsUnixDomainSocket(testFile)
assert.Nil(t, err, "Unexpected error from IsUnixDomainSocket: %v", err)
assert.True(t, result, "Unexpected result: false from IsUnixDomainSocket.")
wg.Done()
}()
// Wait a sufficient amount of time to make sure the retry logic kicks in
time.Sleep(socketDialRetryPeriod)
// Replace the temporary file with an actual Unix domain socket file
os.Remove(testFile)
ta, err := net.ResolveUnixAddr("unix", testFile)
require.NoError(t, err, "Failed to ResolveUnixAddr.")
unixln, err := net.ListenUnix("unix", ta)
require.NoError(t, err, "Failed to ListenUnix.")
// Wait for the goroutine to finish, then close the socket
wg.Wait()
unixln.Close()
}
func TestNormalizePath(t *testing.T) {
tests := []struct {
originalpath string