k8s.io/kubernetes@v1.29.3/pkg/util/filesystem/util_test.go (about)

     1  /*
     2  Copyright 2023 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package filesystem
    18  
    19  import (
    20  	"net"
    21  	"os"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  func TestIsUnixDomainSocket(t *testing.T) {
    29  	tests := []struct {
    30  		label          string
    31  		listenOnSocket bool
    32  		expectSocket   bool
    33  		expectError    bool
    34  		invalidFile    bool
    35  	}{
    36  		{
    37  			label:          "Domain Socket file",
    38  			listenOnSocket: true,
    39  			expectSocket:   true,
    40  			expectError:    false,
    41  		},
    42  		{
    43  			label:       "Non Existent file",
    44  			invalidFile: true,
    45  			expectError: true,
    46  		},
    47  		{
    48  			label:          "Regular file",
    49  			listenOnSocket: false,
    50  			expectSocket:   false,
    51  			expectError:    false,
    52  		},
    53  	}
    54  	for _, test := range tests {
    55  		f, err := os.CreateTemp("", "test-domain-socket")
    56  		require.NoErrorf(t, err, "Failed to create file for test purposes: %v while setting up: %s", err, test.label)
    57  		addr := f.Name()
    58  		f.Close()
    59  		var ln *net.UnixListener
    60  		if test.listenOnSocket {
    61  			os.Remove(addr)
    62  			ta, err := net.ResolveUnixAddr("unix", addr)
    63  			require.NoErrorf(t, err, "Failed to ResolveUnixAddr: %v while setting up: %s", err, test.label)
    64  			ln, err = net.ListenUnix("unix", ta)
    65  			require.NoErrorf(t, err, "Failed to ListenUnix: %v while setting up: %s", err, test.label)
    66  		}
    67  		fileToTest := addr
    68  		if test.invalidFile {
    69  			fileToTest = fileToTest + ".invalid"
    70  		}
    71  		result, err := IsUnixDomainSocket(fileToTest)
    72  		if test.listenOnSocket {
    73  			// this takes care of removing the file associated with the domain socket
    74  			ln.Close()
    75  		} else {
    76  			// explicitly remove regular file
    77  			os.Remove(addr)
    78  		}
    79  		if test.expectError {
    80  			assert.Errorf(t, err, "Unexpected nil error from IsUnixDomainSocket for %s", test.label)
    81  		} else {
    82  			assert.NoErrorf(t, err, "Unexpected error invoking IsUnixDomainSocket for %s", test.label)
    83  		}
    84  		assert.Equal(t, result, test.expectSocket, "Unexpected result from IsUnixDomainSocket: %v for %s", result, test.label)
    85  	}
    86  }