...

Source file src/k8s.io/kubernetes/pkg/kubelet/client/kubelet_client_test.go

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

     1  /*
     2  Copyright 2014 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 client
    18  
    19  import (
    20  	"net"
    21  	"net/http"
    22  	"net/http/httptest"
    23  	"net/http/httputil"
    24  	"net/url"
    25  	"strconv"
    26  	"testing"
    27  )
    28  
    29  func TestMakeTransportInvalid(t *testing.T) {
    30  	config := &KubeletClientConfig{
    31  		// Invalid certificate and key path
    32  		TLSClientConfig: KubeletTLSConfig{
    33  			CertFile: "../../client/testdata/mycertinvalid.cer",
    34  			KeyFile:  "../../client/testdata/mycertinvalid.key",
    35  			CAFile:   "../../client/testdata/myCA.cer",
    36  		},
    37  	}
    38  
    39  	rt, err := MakeTransport(config)
    40  	if err == nil {
    41  		t.Errorf("Expected an error")
    42  	}
    43  	if rt != nil {
    44  		t.Error("rt should be nil as we provided invalid cert file")
    45  	}
    46  }
    47  
    48  func TestMakeTransportValid(t *testing.T) {
    49  	config := &KubeletClientConfig{
    50  		Port: 1234,
    51  		TLSClientConfig: KubeletTLSConfig{
    52  			CertFile: "../../client/testdata/mycertvalid.cer",
    53  			// TLS Configuration
    54  			KeyFile: "../../client/testdata/mycertvalid.key",
    55  			// TLS Configuration
    56  			CAFile: "../../client/testdata/myCA.cer",
    57  		},
    58  	}
    59  
    60  	rt, err := MakeTransport(config)
    61  	if err != nil {
    62  		t.Errorf("Not expecting an error %#v", err)
    63  	}
    64  	if rt == nil {
    65  		t.Error("rt should not be nil")
    66  	}
    67  }
    68  
    69  func TestMakeInsecureTransport(t *testing.T) {
    70  	testServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    71  		w.WriteHeader(http.StatusOK)
    72  	}))
    73  	defer testServer.Close()
    74  
    75  	testURL, err := url.Parse(testServer.URL)
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  	_, portStr, err := net.SplitHostPort(testURL.Host)
    80  	if err != nil {
    81  		t.Fatal(err)
    82  	}
    83  	port, err := strconv.ParseUint(portStr, 10, 32)
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  
    88  	config := &KubeletClientConfig{
    89  		Port: uint(port),
    90  		TLSClientConfig: KubeletTLSConfig{
    91  			CertFile: "../../client/testdata/mycertvalid.cer",
    92  			// TLS Configuration
    93  			KeyFile: "../../client/testdata/mycertvalid.key",
    94  			// TLS Configuration
    95  			CAFile: "../../client/testdata/myCA.cer",
    96  		},
    97  	}
    98  
    99  	rt, err := MakeInsecureTransport(config)
   100  	if err != nil {
   101  		t.Errorf("Not expecting an error #%v", err)
   102  	}
   103  	if rt == nil {
   104  		t.Error("rt should not be nil")
   105  	}
   106  
   107  	req, err := http.NewRequest(http.MethodGet, testServer.URL, nil)
   108  	if err != nil {
   109  		t.Fatal(err)
   110  	}
   111  	response, err := rt.RoundTrip(req)
   112  	if err != nil {
   113  		t.Fatal(err)
   114  	}
   115  	if response.StatusCode != http.StatusOK {
   116  		dump, err := httputil.DumpResponse(response, true)
   117  		if err != nil {
   118  			t.Fatal(err)
   119  		}
   120  		t.Fatal(string(dump))
   121  	}
   122  }
   123  

View as plain text