github.com/google/cloudprober@v0.11.3/rds/kubernetes/client_test.go (about)

     1  // Copyright 2019 The Cloudprober Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package kubernetes
    16  
    17  import (
    18  	"io/ioutil"
    19  	"net/http"
    20  	"os"
    21  	"testing"
    22  
    23  	"github.com/golang/protobuf/proto"
    24  	tlsconfigpb "github.com/google/cloudprober/common/tlsconfig/proto"
    25  	cpb "github.com/google/cloudprober/rds/kubernetes/proto"
    26  )
    27  
    28  var testCACert = `
    29  -----BEGIN CERTIFICATE-----
    30  MIICVjCCAb+gAwIBAgIUY0TRq/rPKnOpZ+Bbv9hBMlKgiP0wDQYJKoZIhvcNAQEL
    31  BQAwPTELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNBMSEwHwYDVQQKDBhJbnRlcm5l
    32  dCBXaWRnaXRzIFB0eSBMdGQwHhcNMTkxMjE4MDA1MDMxWhcNMjAwMTE3MDA1MDMx
    33  WjA9MQswCQYDVQQGEwJVUzELMAkGA1UECAwCQ0ExITAfBgNVBAoMGEludGVybmV0
    34  IFdpZGdpdHMgUHR5IEx0ZDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA1z5z
    35  InMY7e5AOgYnq9ACqwtIYRZZwEbBSy57Pe9kftEpUy5wfdN5YtBEiW+juy6CZLns
    36  WKQ3sZ/gnbYvdRfkHnbTU6DZdt741H/YXMnbkT28In1NJYvz/FiJWiphUxbXNEmi
    37  laHAbX+zkOnL81LX7NAArKlk0biK8iglW80oeRMCAwEAAaNTMFEwHQYDVR0OBBYE
    38  FDHgRfu3kXFxPj0f4XYsxxukRYeKMB8GA1UdIwQYMBaAFDHgRfu3kXFxPj0f4XYs
    39  xxukRYeKMA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADgYEAI4xvCBkE
    40  NbrbrZ4rPllcmYxgBrTvOUbaG8Lmhx1/qoOyx2LCca0pMQGB8cyMtT5/D7IZUlHk
    41  5IG8ts/LpaDbRhTP4MQUCoN/FgAJ4e5Y33VWJocucgEFyv4aKl0Xgg+hO4ejpaxr
    42  JeDPlMt+8OTQNVC63+SPzOvlUsOLFX74WMo=
    43  -----END CERTIFICATE-----
    44  `
    45  
    46  func testFileWithContent(t *testing.T, content string) string {
    47  	f, err := ioutil.TempFile("", "")
    48  	if err != nil {
    49  		t.Fatalf("Error creating temporary file for testing: %v", err)
    50  		return ""
    51  	}
    52  
    53  	if _, err := f.Write([]byte(content)); err != nil {
    54  		os.Remove(f.Name()) // clean up
    55  		t.Fatalf("Error writing %s to temporary file: %s", content, f.Name())
    56  		return ""
    57  	}
    58  
    59  	return f.Name()
    60  }
    61  
    62  func TestNewClientInCluster(t *testing.T) {
    63  	cacrtF := testFileWithContent(t, testCACert)
    64  	defer os.Remove(cacrtF) // clean up
    65  
    66  	oldLocalCACert := LocalCACert
    67  	LocalCACert = cacrtF
    68  	defer func() { LocalCACert = oldLocalCACert }()
    69  
    70  	testToken := "test-token"
    71  	tokenF := testFileWithContent(t, testToken)
    72  	defer os.Remove(tokenF) // clean up
    73  
    74  	oldLocalTokenFile := LocalTokenFile
    75  	LocalTokenFile = tokenF
    76  	defer func() { LocalTokenFile = oldLocalTokenFile }()
    77  
    78  	os.Setenv("KUBERNETES_SERVICE_HOST", "test-api-host")
    79  	os.Setenv("KUBERNETES_SERVICE_PORT", "4123")
    80  
    81  	tc, err := newClient(&cpb.ProviderConfig{}, nil)
    82  	if err != nil {
    83  		t.Fatalf("error while creating new client: %v", err)
    84  	}
    85  
    86  	expectedAPIHost := "test-api-host:4123"
    87  	if tc.apiHost != "test-api-host:4123" {
    88  		t.Errorf("client.apiHost: got=%s, exepcted=%s", tc.apiHost, expectedAPIHost)
    89  	}
    90  
    91  	expectedToken := "Bearer " + testToken
    92  	if tc.bearer != expectedToken {
    93  		t.Errorf("client.bearer: got=%s, exepcted=%s", tc.bearer, expectedToken)
    94  	}
    95  
    96  	if tc.httpC == nil || tc.httpC.Transport.(*http.Transport).TLSClientConfig == nil {
    97  		t.Errorf("Client's HTTP client or TLS config are unexpectedly nil.")
    98  	}
    99  }
   100  
   101  func TestNewClientWithTLS(t *testing.T) {
   102  	cacrtF := testFileWithContent(t, testCACert)
   103  	defer os.Remove(cacrtF) // clean up
   104  
   105  	oldLocalCACert := LocalCACert
   106  	LocalCACert = cacrtF
   107  	defer func() { LocalCACert = oldLocalCACert }()
   108  
   109  	testAPIServerAddr := "test-api-server-addr"
   110  	tc, err := newClient(&cpb.ProviderConfig{
   111  		ApiServerAddress: &testAPIServerAddr,
   112  		TlsConfig: &tlsconfigpb.TLSConfig{
   113  			CaCertFile: proto.String(cacrtF),
   114  		},
   115  	}, nil)
   116  	if err != nil {
   117  		t.Fatalf("error while creating new client: %v", err)
   118  	}
   119  
   120  	if tc.apiHost != testAPIServerAddr {
   121  		t.Errorf("client.apiHost: got=%s, exepcted=%s", tc.apiHost, testAPIServerAddr)
   122  	}
   123  
   124  	if tc.bearer != "" {
   125  		t.Errorf("client.bearer: got=%s, exepcted=''", tc.bearer)
   126  	}
   127  
   128  	if tc.httpC == nil || tc.httpC.Transport.(*http.Transport).TLSClientConfig == nil {
   129  		t.Errorf("Client's HTTP client or TLS config are unexpectedly nil.")
   130  	}
   131  }