...

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

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

     1  //go:build freebsd || linux || darwin
     2  // +build freebsd linux darwin
     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  	"testing"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestParseEndpoint(t *testing.T) {
    29  	tests := []struct {
    30  		endpoint         string
    31  		expectError      bool
    32  		expectedProtocol string
    33  		expectedAddr     string
    34  	}{
    35  		{
    36  			endpoint:         "unix:///tmp/s1.sock",
    37  			expectedProtocol: "unix",
    38  			expectedAddr:     "/tmp/s1.sock",
    39  		},
    40  		{
    41  			endpoint:         "tcp://localhost:15880",
    42  			expectedProtocol: "tcp",
    43  			expectedAddr:     "localhost:15880",
    44  		},
    45  		{
    46  			endpoint:         "npipe://./pipe/mypipe",
    47  			expectedProtocol: "npipe",
    48  			expectError:      true,
    49  		},
    50  		{
    51  			endpoint:         "tcp1://abc",
    52  			expectedProtocol: "tcp1",
    53  			expectError:      true,
    54  		},
    55  		{
    56  			endpoint:    "a b c",
    57  			expectError: true,
    58  		},
    59  	}
    60  
    61  	for _, test := range tests {
    62  		protocol, addr, err := parseEndpoint(test.endpoint)
    63  		assert.Equal(t, test.expectedProtocol, protocol)
    64  		if test.expectError {
    65  			assert.NotNil(t, err, "Expect error during parsing %q", test.endpoint)
    66  			continue
    67  		}
    68  		assert.Nil(t, err, "Expect no error during parsing %q", test.endpoint)
    69  		assert.Equal(t, test.expectedAddr, addr)
    70  	}
    71  
    72  }
    73  
    74  func TestGetAddressAndDialer(t *testing.T) {
    75  	tests := []struct {
    76  		endpoint     string
    77  		expectError  bool
    78  		expectedAddr string
    79  	}{
    80  		{
    81  			endpoint:     "unix:///tmp/s1.sock",
    82  			expectError:  false,
    83  			expectedAddr: "/tmp/s1.sock",
    84  		},
    85  		{
    86  			endpoint:     "unix:///tmp/f6.sock",
    87  			expectError:  false,
    88  			expectedAddr: "/tmp/f6.sock",
    89  		},
    90  		{
    91  			endpoint:    "tcp://localhost:9090",
    92  			expectError: true,
    93  		},
    94  		{
    95  			// The misspelling is intentional to make it error
    96  			endpoint:    "htta://free-test.com",
    97  			expectError: true,
    98  		},
    99  		{
   100  			endpoint:    "https://www.youtube.com/",
   101  			expectError: true,
   102  		},
   103  		{
   104  			endpoint:    "http://www.baidu.com/",
   105  			expectError: true,
   106  		},
   107  	}
   108  	for _, test := range tests {
   109  		// just test addr and err
   110  		addr, _, err := GetAddressAndDialer(test.endpoint)
   111  		if test.expectError {
   112  			assert.NotNil(t, err, "expected error during parsing %s", test.endpoint)
   113  			continue
   114  		}
   115  		assert.Nil(t, err, "expected no error during parsing %s", test.endpoint)
   116  		assert.Equal(t, test.expectedAddr, addr)
   117  	}
   118  }
   119  
   120  func TestLocalEndpoint(t *testing.T) {
   121  	tests := []struct {
   122  		path             string
   123  		file             string
   124  		expectError      bool
   125  		expectedFullPath string
   126  	}{
   127  		{
   128  			path:             "path",
   129  			file:             "file",
   130  			expectError:      false,
   131  			expectedFullPath: "unix:/path/file.sock",
   132  		},
   133  	}
   134  	for _, test := range tests {
   135  		fullPath, err := LocalEndpoint(test.path, test.file)
   136  		if test.expectError {
   137  			assert.NotNil(t, err, "expected error")
   138  			continue
   139  		}
   140  		assert.Nil(t, err, "expected no error")
   141  		assert.Equal(t, test.expectedFullPath, fullPath)
   142  	}
   143  }
   144  

View as plain text