...

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

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

     1  //go:build windows
     2  // +build windows
     3  
     4  /*
     5  Copyright 2018 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 util
    21  
    22  import (
    23  	"fmt"
    24  	"reflect"
    25  	"runtime"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  func TestGetAddressAndDialer(t *testing.T) {
    33  
    34  	// Compare dialer function by pointer
    35  	tcpDialPointer := reflect.ValueOf(tcpDial).Pointer()
    36  	npipeDialPointer := reflect.ValueOf(npipeDial).Pointer()
    37  	var nilDialPointer uintptr = 0x0
    38  
    39  	tests := []struct {
    40  		endpoint      string
    41  		expectedAddr  string
    42  		expectedDial  uintptr
    43  		expectedError bool
    44  	}{
    45  		{
    46  			endpoint:      "tcp://localhost:15880",
    47  			expectedAddr:  "localhost:15880",
    48  			expectedDial:  tcpDialPointer,
    49  			expectedError: false,
    50  		},
    51  		{
    52  			endpoint:      "npipe://./pipe/mypipe",
    53  			expectedAddr:  "//./pipe/mypipe",
    54  			expectedDial:  npipeDialPointer,
    55  			expectedError: false,
    56  		},
    57  		{
    58  			endpoint:      "npipe:\\\\.\\pipe\\mypipe",
    59  			expectedAddr:  "//./pipe/mypipe",
    60  			expectedDial:  npipeDialPointer,
    61  			expectedError: false,
    62  		},
    63  		{
    64  			endpoint:      "unix:///tmp/s1.sock",
    65  			expectedAddr:  "",
    66  			expectedDial:  nilDialPointer,
    67  			expectedError: true,
    68  		},
    69  		{
    70  			endpoint:      "tcp1://abc",
    71  			expectedAddr:  "",
    72  			expectedDial:  nilDialPointer,
    73  			expectedError: true,
    74  		},
    75  		{
    76  			endpoint:      "a b c",
    77  			expectedAddr:  "",
    78  			expectedDial:  nilDialPointer,
    79  			expectedError: true,
    80  		},
    81  	}
    82  
    83  	for _, test := range tests {
    84  		expectedDialerName := runtime.FuncForPC(test.expectedDial).Name()
    85  		if expectedDialerName == "" {
    86  			expectedDialerName = "nilDial"
    87  		}
    88  		t.Run(fmt.Sprintf("Endpoint is %s, addr is %s and dialer is %s",
    89  			test.endpoint, test.expectedAddr, expectedDialerName),
    90  			func(t *testing.T) {
    91  				address, dialer, err := GetAddressAndDialer(test.endpoint)
    92  
    93  				dialerPointer := reflect.ValueOf(dialer).Pointer()
    94  				actualDialerName := runtime.FuncForPC(dialerPointer).Name()
    95  				if actualDialerName == "" {
    96  					actualDialerName = "nilDial"
    97  				}
    98  
    99  				assert.Equalf(t, test.expectedDial, dialerPointer,
   100  					"Expected dialer %s, but get %s", expectedDialerName, actualDialerName)
   101  
   102  				assert.Equal(t, test.expectedAddr, address)
   103  
   104  				if test.expectedError {
   105  					assert.NotNil(t, err, "Expect error during parsing %q", test.endpoint)
   106  				} else {
   107  					assert.Nil(t, err, "Expect no error during parsing %q", test.endpoint)
   108  				}
   109  			})
   110  	}
   111  }
   112  
   113  func TestParseEndpoint(t *testing.T) {
   114  	tests := []struct {
   115  		endpoint         string
   116  		expectedError    bool
   117  		expectedProtocol string
   118  		expectedAddr     string
   119  	}{
   120  		{
   121  			endpoint:         "unix:///tmp/s1.sock",
   122  			expectedProtocol: "unix",
   123  			expectedError:    true,
   124  		},
   125  		{
   126  			endpoint:         "tcp://localhost:15880",
   127  			expectedProtocol: "tcp",
   128  			expectedAddr:     "localhost:15880",
   129  		},
   130  		{
   131  			endpoint:         "npipe://./pipe/mypipe",
   132  			expectedProtocol: "npipe",
   133  			expectedAddr:     "//./pipe/mypipe",
   134  		},
   135  		{
   136  			endpoint:         "npipe:////./pipe/mypipe2",
   137  			expectedProtocol: "npipe",
   138  			expectedAddr:     "//./pipe/mypipe2",
   139  		},
   140  		{
   141  			endpoint:         "npipe:/pipe/mypipe3",
   142  			expectedProtocol: "npipe",
   143  			expectedAddr:     "//./pipe/mypipe3",
   144  		},
   145  		{
   146  			endpoint:         "npipe:\\\\.\\pipe\\mypipe4",
   147  			expectedProtocol: "npipe",
   148  			expectedAddr:     "//./pipe/mypipe4",
   149  		},
   150  		{
   151  			endpoint:         "npipe:\\pipe\\mypipe5",
   152  			expectedProtocol: "npipe",
   153  			expectedAddr:     "//./pipe/mypipe5",
   154  		},
   155  		{
   156  			endpoint:         "tcp1://abc",
   157  			expectedProtocol: "tcp1",
   158  			expectedError:    true,
   159  		},
   160  		{
   161  			endpoint:      "a b c",
   162  			expectedError: true,
   163  		},
   164  	}
   165  
   166  	for _, test := range tests {
   167  		protocol, addr, err := parseEndpoint(test.endpoint)
   168  		assert.Equal(t, test.expectedProtocol, protocol)
   169  		if test.expectedError {
   170  			assert.NotNil(t, err, "Expect error during parsing %q", test.endpoint)
   171  			continue
   172  		}
   173  		require.Nil(t, err, "Expect no error during parsing %q", test.endpoint)
   174  		assert.Equal(t, test.expectedAddr, addr)
   175  	}
   176  
   177  }
   178  
   179  func TestNormalizePath(t *testing.T) {
   180  	tests := []struct {
   181  		originalpath   string
   182  		normalizedPath string
   183  	}{
   184  		{
   185  			originalpath:   "\\path\\to\\file",
   186  			normalizedPath: "c:\\path\\to\\file",
   187  		},
   188  		{
   189  			originalpath:   "/path/to/file",
   190  			normalizedPath: "c:\\path\\to\\file",
   191  		},
   192  		{
   193  			originalpath:   "/path/to/dir/",
   194  			normalizedPath: "c:\\path\\to\\dir\\",
   195  		},
   196  		{
   197  			originalpath:   "\\path\\to\\dir\\",
   198  			normalizedPath: "c:\\path\\to\\dir\\",
   199  		},
   200  		{
   201  			originalpath:   "/file",
   202  			normalizedPath: "c:\\file",
   203  		},
   204  		{
   205  			originalpath:   "\\file",
   206  			normalizedPath: "c:\\file",
   207  		},
   208  		{
   209  			originalpath:   "fileonly",
   210  			normalizedPath: "fileonly",
   211  		},
   212  	}
   213  
   214  	for _, test := range tests {
   215  		assert.Equal(t, test.normalizedPath, NormalizePath(test.originalpath))
   216  	}
   217  }
   218  
   219  func TestLocalEndpoint(t *testing.T) {
   220  	tests := []struct {
   221  		path             string
   222  		file             string
   223  		expectError      bool
   224  		expectedFullPath string
   225  	}{
   226  		{
   227  			path:             "/var/lib/kubelet/pod-resources",
   228  			file:             "kube.sock", // this is not the default, but it's not relevant here
   229  			expectError:      false,
   230  			expectedFullPath: `npipe://\\.\pipe\kubelet-pod-resources`,
   231  		},
   232  	}
   233  	for _, test := range tests {
   234  		fullPath, err := LocalEndpoint(test.path, test.file)
   235  		if test.expectError {
   236  			assert.NotNil(t, err, "expected error")
   237  			continue
   238  		}
   239  		assert.Nil(t, err, "expected no error")
   240  		assert.Equal(t, test.expectedFullPath, fullPath)
   241  	}
   242  }
   243  
   244  func TestLocalEndpointRoundTrip(t *testing.T) {
   245  	npipeDialPointer := reflect.ValueOf(npipeDial).Pointer()
   246  	expectedDialerName := runtime.FuncForPC(npipeDialPointer).Name()
   247  	expectedAddress := "//./pipe/kubelet-pod-resources"
   248  
   249  	fullPath, err := LocalEndpoint(`pod-resources`, "kubelet")
   250  	require.NoErrorf(t, err, "Failed to create the local endpoint path")
   251  
   252  	address, dialer, err := GetAddressAndDialer(fullPath)
   253  	require.NoErrorf(t, err, "Failed to parse the endpoint path and get back address and dialer (path=%q)", fullPath)
   254  
   255  	dialerPointer := reflect.ValueOf(dialer).Pointer()
   256  	actualDialerName := runtime.FuncForPC(dialerPointer).Name()
   257  
   258  	assert.Equalf(t, npipeDialPointer, dialerPointer,
   259  		"Expected dialer %s, but get %s", expectedDialerName, actualDialerName)
   260  
   261  	assert.Equal(t, expectedAddress, address)
   262  }
   263  

View as plain text