kubelet_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 certificate
  14. import (
  15. "net"
  16. "reflect"
  17. "testing"
  18. "k8s.io/api/core/v1"
  19. )
  20. func TestAddressesToHostnamesAndIPs(t *testing.T) {
  21. tests := []struct {
  22. name string
  23. addresses []v1.NodeAddress
  24. wantDNSNames []string
  25. wantIPs []net.IP
  26. }{
  27. {
  28. name: "empty",
  29. addresses: nil,
  30. wantDNSNames: nil,
  31. wantIPs: nil,
  32. },
  33. {
  34. name: "ignore empty values",
  35. addresses: []v1.NodeAddress{{Type: v1.NodeHostName, Address: ""}},
  36. wantDNSNames: nil,
  37. wantIPs: nil,
  38. },
  39. {
  40. name: "ignore invalid IPs",
  41. addresses: []v1.NodeAddress{
  42. {Type: v1.NodeInternalIP, Address: "1.2"},
  43. {Type: v1.NodeExternalIP, Address: "3.4"},
  44. },
  45. wantDNSNames: nil,
  46. wantIPs: nil,
  47. },
  48. {
  49. name: "dedupe values",
  50. addresses: []v1.NodeAddress{
  51. {Type: v1.NodeHostName, Address: "hostname"},
  52. {Type: v1.NodeExternalDNS, Address: "hostname"},
  53. {Type: v1.NodeInternalDNS, Address: "hostname"},
  54. {Type: v1.NodeInternalIP, Address: "1.1.1.1"},
  55. {Type: v1.NodeExternalIP, Address: "1.1.1.1"},
  56. },
  57. wantDNSNames: []string{"hostname"},
  58. wantIPs: []net.IP{net.ParseIP("1.1.1.1")},
  59. },
  60. {
  61. name: "order values",
  62. addresses: []v1.NodeAddress{
  63. {Type: v1.NodeHostName, Address: "hostname-2"},
  64. {Type: v1.NodeExternalDNS, Address: "hostname-1"},
  65. {Type: v1.NodeInternalDNS, Address: "hostname-3"},
  66. {Type: v1.NodeInternalIP, Address: "2.2.2.2"},
  67. {Type: v1.NodeExternalIP, Address: "1.1.1.1"},
  68. {Type: v1.NodeInternalIP, Address: "3.3.3.3"},
  69. },
  70. wantDNSNames: []string{"hostname-1", "hostname-2", "hostname-3"},
  71. wantIPs: []net.IP{net.ParseIP("1.1.1.1"), net.ParseIP("2.2.2.2"), net.ParseIP("3.3.3.3")},
  72. },
  73. {
  74. name: "handle IP and DNS hostnames",
  75. addresses: []v1.NodeAddress{
  76. {Type: v1.NodeHostName, Address: "hostname"},
  77. {Type: v1.NodeHostName, Address: "1.1.1.1"},
  78. },
  79. wantDNSNames: []string{"hostname"},
  80. wantIPs: []net.IP{net.ParseIP("1.1.1.1")},
  81. },
  82. }
  83. for _, tt := range tests {
  84. t.Run(tt.name, func(t *testing.T) {
  85. gotDNSNames, gotIPs := addressesToHostnamesAndIPs(tt.addresses)
  86. if !reflect.DeepEqual(gotDNSNames, tt.wantDNSNames) {
  87. t.Errorf("addressesToHostnamesAndIPs() gotDNSNames = %v, want %v", gotDNSNames, tt.wantDNSNames)
  88. }
  89. if !reflect.DeepEqual(gotIPs, tt.wantIPs) {
  90. t.Errorf("addressesToHostnamesAndIPs() gotIPs = %v, want %v", gotIPs, tt.wantIPs)
  91. }
  92. })
  93. }
  94. }