k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/util/filesystem/util_windows_test.go (about) 1 //go:build windows 2 // +build windows 3 4 /* 5 Copyright 2023 The Kubernetes Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 */ 19 20 package filesystem 21 22 import ( 23 "math/rand" 24 "net" 25 "os" 26 "sync" 27 "testing" 28 "time" 29 30 winio "github.com/Microsoft/go-winio" 31 "github.com/stretchr/testify/assert" 32 "github.com/stretchr/testify/require" 33 ) 34 35 func TestIsUnixDomainSocketPipe(t *testing.T) { 36 generatePipeName := func(suffixLen int) string { 37 letter := []rune("abcdef0123456789") 38 b := make([]rune, suffixLen) 39 for i := range b { 40 b[i] = letter[rand.Intn(len(letter))] 41 } 42 return "\\\\.\\pipe\\test-pipe" + string(b) 43 } 44 testFile := generatePipeName(4) 45 pipeln, err := winio.ListenPipe(testFile, &winio.PipeConfig{SecurityDescriptor: "D:P(A;;GA;;;BA)(A;;GA;;;SY)"}) 46 defer pipeln.Close() 47 48 require.NoErrorf(t, err, "Failed to listen on named pipe for test purposes: %v", err) 49 result, err := IsUnixDomainSocket(testFile) 50 assert.NoError(t, err, "Unexpected error from IsUnixDomainSocket.") 51 assert.False(t, result, "Unexpected result: true from IsUnixDomainSocket.") 52 } 53 54 // This is required as on Windows it's possible for the socket file backing a Unix domain socket to 55 // exist but not be ready for socket communications yet as per 56 // https://github.com/kubernetes/kubernetes/issues/104584 57 func TestPendingUnixDomainSocket(t *testing.T) { 58 // Create a temporary file that will simulate the Unix domain socket file in a 59 // not-yet-ready state. We need this because the Kubelet keeps an eye on file 60 // changes and acts on them, leading to potential race issues as described in 61 // the referenced issue above 62 f, err := os.CreateTemp("", "test-domain-socket") 63 require.NoErrorf(t, err, "Failed to create file for test purposes: %v", err) 64 testFile := f.Name() 65 f.Close() 66 67 // Start the check at this point 68 wg := sync.WaitGroup{} 69 wg.Add(1) 70 go func() { 71 result, err := IsUnixDomainSocket(testFile) 72 assert.Nil(t, err, "Unexpected error from IsUnixDomainSocket: %v", err) 73 assert.True(t, result, "Unexpected result: false from IsUnixDomainSocket.") 74 wg.Done() 75 }() 76 77 // Wait a sufficient amount of time to make sure the retry logic kicks in 78 time.Sleep(socketDialRetryPeriod) 79 80 // Replace the temporary file with an actual Unix domain socket file 81 os.Remove(testFile) 82 ta, err := net.ResolveUnixAddr("unix", testFile) 83 require.NoError(t, err, "Failed to ResolveUnixAddr.") 84 unixln, err := net.ListenUnix("unix", ta) 85 require.NoError(t, err, "Failed to ListenUnix.") 86 87 // Wait for the goroutine to finish, then close the socket 88 wg.Wait() 89 unixln.Close() 90 } 91 92 func TestAbsWithSlash(t *testing.T) { 93 // On Windows, filepath.IsAbs will not return True for paths prefixed with a slash 94 assert.True(t, IsAbs("/test")) 95 assert.True(t, IsAbs("\\test")) 96 97 assert.False(t, IsAbs("./local")) 98 assert.False(t, IsAbs("local")) 99 }