etcd_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. Copyright 2018 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 etcd
  14. import (
  15. "fmt"
  16. "strconv"
  17. "testing"
  18. kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  19. "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  20. )
  21. func TestCheckConfigurationIsHA(t *testing.T) {
  22. var tests = []struct {
  23. name string
  24. cfg *kubeadmapi.Etcd
  25. expected bool
  26. }{
  27. {
  28. name: "HA etcd",
  29. cfg: &kubeadmapi.Etcd{
  30. External: &kubeadmapi.ExternalEtcd{
  31. Endpoints: []string{"10.100.0.1:2379", "10.100.0.2:2379", "10.100.0.3:2379"},
  32. },
  33. },
  34. expected: true,
  35. },
  36. {
  37. name: "single External etcd",
  38. cfg: &kubeadmapi.Etcd{
  39. External: &kubeadmapi.ExternalEtcd{
  40. Endpoints: []string{"10.100.0.1:2379"},
  41. },
  42. },
  43. expected: false,
  44. },
  45. {
  46. name: "local etcd",
  47. cfg: &kubeadmapi.Etcd{
  48. Local: &kubeadmapi.LocalEtcd{},
  49. },
  50. expected: false,
  51. },
  52. {
  53. name: "empty etcd struct",
  54. cfg: &kubeadmapi.Etcd{},
  55. expected: false,
  56. },
  57. }
  58. for _, test := range tests {
  59. t.Run(test.name, func(t *testing.T) {
  60. if isHA := CheckConfigurationIsHA(test.cfg); isHA != test.expected {
  61. t.Errorf("expected isHA to be %v, got %v", test.expected, isHA)
  62. }
  63. })
  64. }
  65. }
  66. func testGetURL(t *testing.T, getURLFunc func(*kubeadmapi.APIEndpoint) string, port int) {
  67. portStr := strconv.Itoa(port)
  68. var tests = []struct {
  69. name string
  70. advertiseAddress string
  71. expectedURL string
  72. }{
  73. {
  74. name: "IPv4",
  75. advertiseAddress: "10.10.10.10",
  76. expectedURL: fmt.Sprintf("https://10.10.10.10:%s", portStr),
  77. },
  78. {
  79. name: "IPv6",
  80. advertiseAddress: "2001:db8::2",
  81. expectedURL: fmt.Sprintf("https://[2001:db8::2]:%s", portStr),
  82. },
  83. {
  84. name: "IPv4 localhost",
  85. advertiseAddress: "127.0.0.1",
  86. expectedURL: fmt.Sprintf("https://127.0.0.1:%s", portStr),
  87. },
  88. {
  89. name: "IPv6 localhost",
  90. advertiseAddress: "::1",
  91. expectedURL: fmt.Sprintf("https://[::1]:%s", portStr),
  92. },
  93. }
  94. for _, test := range tests {
  95. url := getURLFunc(&kubeadmapi.APIEndpoint{AdvertiseAddress: test.advertiseAddress})
  96. if url != test.expectedURL {
  97. t.Errorf("expected %s, got %s", test.expectedURL, url)
  98. }
  99. }
  100. }
  101. func TestGetClientURL(t *testing.T) {
  102. testGetURL(t, GetClientURL, constants.EtcdListenClientPort)
  103. }
  104. func TestGetPeerURL(t *testing.T) {
  105. testGetURL(t, GetClientURL, constants.EtcdListenClientPort)
  106. }
  107. func TestGetClientURLByIP(t *testing.T) {
  108. portStr := strconv.Itoa(constants.EtcdListenClientPort)
  109. var tests = []struct {
  110. name string
  111. ip string
  112. expectedURL string
  113. }{
  114. {
  115. name: "IPv4",
  116. ip: "10.10.10.10",
  117. expectedURL: fmt.Sprintf("https://10.10.10.10:%s", portStr),
  118. },
  119. {
  120. name: "IPv6",
  121. ip: "2001:db8::2",
  122. expectedURL: fmt.Sprintf("https://[2001:db8::2]:%s", portStr),
  123. },
  124. {
  125. name: "IPv4 localhost",
  126. ip: "127.0.0.1",
  127. expectedURL: fmt.Sprintf("https://127.0.0.1:%s", portStr),
  128. },
  129. {
  130. name: "IPv6 localhost",
  131. ip: "::1",
  132. expectedURL: fmt.Sprintf("https://[::1]:%s", portStr),
  133. },
  134. }
  135. for _, test := range tests {
  136. url := GetClientURLByIP(test.ip)
  137. if url != test.expectedURL {
  138. t.Errorf("expected %s, got %s", test.expectedURL, url)
  139. }
  140. }
  141. }