...

Source file src/k8s.io/kubernetes/pkg/util/filesystem/util_windows_test.go

Documentation: k8s.io/kubernetes/pkg/util/filesystem

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

View as plain text