etcd_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 testGetURL(t *testing.T, getURLFunc func(*kubeadmapi.APIEndpoint) string, port int) {
  22. portStr := strconv.Itoa(port)
  23. var tests = []struct {
  24. name string
  25. advertiseAddress string
  26. expectedURL string
  27. }{
  28. {
  29. name: "IPv4",
  30. advertiseAddress: "10.10.10.10",
  31. expectedURL: fmt.Sprintf("https://10.10.10.10:%s", portStr),
  32. },
  33. {
  34. name: "IPv6",
  35. advertiseAddress: "2001:db8::2",
  36. expectedURL: fmt.Sprintf("https://[2001:db8::2]:%s", portStr),
  37. },
  38. {
  39. name: "IPv4 localhost",
  40. advertiseAddress: "127.0.0.1",
  41. expectedURL: fmt.Sprintf("https://127.0.0.1:%s", portStr),
  42. },
  43. {
  44. name: "IPv6 localhost",
  45. advertiseAddress: "::1",
  46. expectedURL: fmt.Sprintf("https://[::1]:%s", portStr),
  47. },
  48. }
  49. for _, test := range tests {
  50. url := getURLFunc(&kubeadmapi.APIEndpoint{AdvertiseAddress: test.advertiseAddress})
  51. if url != test.expectedURL {
  52. t.Errorf("expected %s, got %s", test.expectedURL, url)
  53. }
  54. }
  55. }
  56. func TestGetClientURL(t *testing.T) {
  57. testGetURL(t, GetClientURL, constants.EtcdListenClientPort)
  58. }
  59. func TestGetPeerURL(t *testing.T) {
  60. testGetURL(t, GetClientURL, constants.EtcdListenClientPort)
  61. }
  62. func TestGetClientURLByIP(t *testing.T) {
  63. portStr := strconv.Itoa(constants.EtcdListenClientPort)
  64. var tests = []struct {
  65. name string
  66. ip string
  67. expectedURL string
  68. }{
  69. {
  70. name: "IPv4",
  71. ip: "10.10.10.10",
  72. expectedURL: fmt.Sprintf("https://10.10.10.10:%s", portStr),
  73. },
  74. {
  75. name: "IPv6",
  76. ip: "2001:db8::2",
  77. expectedURL: fmt.Sprintf("https://[2001:db8::2]:%s", portStr),
  78. },
  79. {
  80. name: "IPv4 localhost",
  81. ip: "127.0.0.1",
  82. expectedURL: fmt.Sprintf("https://127.0.0.1:%s", portStr),
  83. },
  84. {
  85. name: "IPv6 localhost",
  86. ip: "::1",
  87. expectedURL: fmt.Sprintf("https://[::1]:%s", portStr),
  88. },
  89. }
  90. for _, test := range tests {
  91. url := GetClientURLByIP(test.ip)
  92. if url != test.expectedURL {
  93. t.Errorf("expected %s, got %s", test.expectedURL, url)
  94. }
  95. }
  96. }