k8s.io/kubernetes@v1.29.3/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 "net" 24 "os" 25 "sync" 26 "testing" 27 "time" 28 29 "github.com/stretchr/testify/assert" 30 "github.com/stretchr/testify/require" 31 ) 32 33 func TestIsUnixDomainSocketPipe(t *testing.T) { 34 generatePipeName := func(suffixLen int) string { 35 rand.Seed(time.Now().UnixNano()) 36 letter := []rune("abcdef0123456789") 37 b := make([]rune, suffixLen) 38 for i := range b { 39 b[i] = letter[rand.Intn(len(letter))] 40 } 41 return "\\\\.\\pipe\\test-pipe" + string(b) 42 } 43 testFile := generatePipeName(4) 44 pipeln, err := winio.ListenPipe(testFile, &winio.PipeConfig{SecurityDescriptor: "D:P(A;;GA;;;BA)(A;;GA;;;SY)"}) 45 defer pipeln.Close() 46 47 require.NoErrorf(t, err, "Failed to listen on named pipe for test purposes: %v", err) 48 result, err := IsUnixDomainSocket(testFile) 49 assert.NoError(t, err, "Unexpected error from IsUnixDomainSocket.") 50 assert.False(t, result, "Unexpected result: true from IsUnixDomainSocket.") 51 } 52 53 // This is required as on Windows it's possible for the socket file backing a Unix domain socket to 54 // exist but not be ready for socket communications yet as per 55 // https://github.com/kubernetes/kubernetes/issues/104584 56 func TestPendingUnixDomainSocket(t *testing.T) { 57 // Create a temporary file that will simulate the Unix domain socket file in a 58 // not-yet-ready state. We need this because the Kubelet keeps an eye on file 59 // changes and acts on them, leading to potential race issues as described in 60 // the referenced issue above 61 f, err := os.CreateTemp("", "test-domain-socket") 62 require.NoErrorf(t, err, "Failed to create file for test purposes: %v", err) 63 testFile := f.Name() 64 f.Close() 65 66 // Start the check at this point 67 wg := sync.WaitGroup{} 68 wg.Add(1) 69 go func() { 70 result, err := IsUnixDomainSocket(testFile) 71 assert.Nil(t, err, "Unexpected error from IsUnixDomainSocket: %v", err) 72 assert.True(t, result, "Unexpected result: false from IsUnixDomainSocket.") 73 wg.Done() 74 }() 75 76 // Wait a sufficient amount of time to make sure the retry logic kicks in 77 time.Sleep(socketDialRetryPeriod) 78 79 // Replace the temporary file with an actual Unix domain socket file 80 os.Remove(testFile) 81 ta, err := net.ResolveUnixAddr("unix", testFile) 82 require.NoError(t, err, "Failed to ResolveUnixAddr.") 83 unixln, err := net.ListenUnix("unix", ta) 84 require.NoError(t, err, "Failed to ListenUnix.") 85 86 // Wait for the goroutine to finish, then close the socket 87 wg.Wait() 88 unixln.Close() 89 }