kubelet_client_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package client
  14. import (
  15. "encoding/json"
  16. "net/http/httptest"
  17. "net/url"
  18. "testing"
  19. "k8s.io/kubernetes/pkg/client/restclient"
  20. "k8s.io/kubernetes/pkg/probe"
  21. utiltesting "k8s.io/kubernetes/pkg/util/testing"
  22. )
  23. func TestHTTPKubeletClient(t *testing.T) {
  24. expectObj := probe.Success
  25. body, err := json.Marshal(expectObj)
  26. if err != nil {
  27. t.Errorf("unexpected error: %v", err)
  28. }
  29. fakeHandler := utiltesting.FakeHandler{
  30. StatusCode: 200,
  31. ResponseBody: string(body),
  32. }
  33. testServer := httptest.NewServer(&fakeHandler)
  34. defer testServer.Close()
  35. if _, err := url.Parse(testServer.URL); err != nil {
  36. t.Errorf("unexpected error: %v", err)
  37. }
  38. }
  39. func TestNewKubeletClient(t *testing.T) {
  40. config := &KubeletClientConfig{
  41. EnableHttps: false,
  42. }
  43. client, err := NewStaticKubeletClient(config)
  44. if err != nil {
  45. t.Errorf("Error while trying to create a client: %v", err)
  46. }
  47. if client == nil {
  48. t.Error("client is nil.")
  49. }
  50. }
  51. func TestNewKubeletClientTLSInvalid(t *testing.T) {
  52. config := &KubeletClientConfig{
  53. EnableHttps: true,
  54. //Invalid certificate and key path
  55. TLSClientConfig: restclient.TLSClientConfig{
  56. CertFile: "../../client/testdata/mycertinvalid.cer",
  57. KeyFile: "../../client/testdata/mycertinvalid.key",
  58. CAFile: "../../client/testdata/myCA.cer",
  59. },
  60. }
  61. client, err := NewStaticKubeletClient(config)
  62. if err == nil {
  63. t.Errorf("Expected an error")
  64. }
  65. if client != nil {
  66. t.Error("client should be nil as we provided invalid cert file")
  67. }
  68. }
  69. func TestNewKubeletClientTLSValid(t *testing.T) {
  70. config := &KubeletClientConfig{
  71. Port: 1234,
  72. EnableHttps: true,
  73. TLSClientConfig: restclient.TLSClientConfig{
  74. CertFile: "../../client/testdata/mycertvalid.cer",
  75. // TLS Configuration, only applies if EnableHttps is true.
  76. KeyFile: "../../client/testdata/mycertvalid.key",
  77. // TLS Configuration, only applies if EnableHttps is true.
  78. CAFile: "../../client/testdata/myCA.cer",
  79. },
  80. }
  81. client, err := NewStaticKubeletClient(config)
  82. if err != nil {
  83. t.Errorf("Not expecting an error #%v", err)
  84. }
  85. if client == nil {
  86. t.Error("client should not be nil")
  87. }
  88. {
  89. scheme, port, transport, err := client.GetConnectionInfo(nil, "foo")
  90. if err != nil {
  91. t.Errorf("Error getting info: %v", err)
  92. }
  93. if scheme != "https" {
  94. t.Errorf("Expected https, got %s", scheme)
  95. }
  96. if port != 1234 {
  97. t.Errorf("Expected 1234, got %d", port)
  98. }
  99. if transport == nil {
  100. t.Errorf("Expected transport, got nil")
  101. }
  102. }
  103. {
  104. _, _, _, err := client.GetConnectionInfo(nil, "foo bar")
  105. if err == nil {
  106. t.Errorf("Expected error getting connection info for invalid node name, got none")
  107. }
  108. }
  109. }