node_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. Copyright 2016 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 node
  14. import (
  15. "testing"
  16. "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. )
  19. func TestGetPreferredAddress(t *testing.T) {
  20. testcases := map[string]struct {
  21. Labels map[string]string
  22. Addresses []v1.NodeAddress
  23. Preferences []v1.NodeAddressType
  24. ExpectErr string
  25. ExpectAddress string
  26. }{
  27. "no addresses": {
  28. ExpectErr: "no preferred addresses found; known addresses: []",
  29. },
  30. "missing address": {
  31. Addresses: []v1.NodeAddress{
  32. {Type: v1.NodeInternalIP, Address: "1.2.3.4"},
  33. },
  34. Preferences: []v1.NodeAddressType{v1.NodeHostName},
  35. ExpectErr: "no preferred addresses found; known addresses: [{InternalIP 1.2.3.4}]",
  36. },
  37. "found address": {
  38. Addresses: []v1.NodeAddress{
  39. {Type: v1.NodeInternalIP, Address: "1.2.3.4"},
  40. {Type: v1.NodeExternalIP, Address: "1.2.3.5"},
  41. {Type: v1.NodeExternalIP, Address: "1.2.3.7"},
  42. },
  43. Preferences: []v1.NodeAddressType{v1.NodeHostName, v1.NodeExternalIP},
  44. ExpectAddress: "1.2.3.5",
  45. },
  46. "found hostname address": {
  47. Labels: map[string]string{v1.LabelHostname: "label-hostname"},
  48. Addresses: []v1.NodeAddress{
  49. {Type: v1.NodeExternalIP, Address: "1.2.3.5"},
  50. {Type: v1.NodeHostName, Address: "status-hostname"},
  51. },
  52. Preferences: []v1.NodeAddressType{v1.NodeHostName, v1.NodeExternalIP},
  53. ExpectAddress: "status-hostname",
  54. },
  55. "label address ignored": {
  56. Labels: map[string]string{v1.LabelHostname: "label-hostname"},
  57. Addresses: []v1.NodeAddress{
  58. {Type: v1.NodeExternalIP, Address: "1.2.3.5"},
  59. },
  60. Preferences: []v1.NodeAddressType{v1.NodeHostName, v1.NodeExternalIP},
  61. ExpectAddress: "1.2.3.5",
  62. },
  63. }
  64. for k, tc := range testcases {
  65. node := &v1.Node{
  66. ObjectMeta: metav1.ObjectMeta{Labels: tc.Labels},
  67. Status: v1.NodeStatus{Addresses: tc.Addresses},
  68. }
  69. address, err := GetPreferredNodeAddress(node, tc.Preferences)
  70. errString := ""
  71. if err != nil {
  72. errString = err.Error()
  73. }
  74. if errString != tc.ExpectErr {
  75. t.Errorf("%s: expected err=%q, got %q", k, tc.ExpectErr, errString)
  76. }
  77. if address != tc.ExpectAddress {
  78. t.Errorf("%s: expected address=%q, got %q", k, tc.ExpectAddress, address)
  79. }
  80. }
  81. }
  82. func TestGetHostname(t *testing.T) {
  83. testCases := []struct {
  84. hostName string
  85. expectedHostName string
  86. expectError bool
  87. }{
  88. {
  89. hostName: " ",
  90. expectError: true,
  91. },
  92. {
  93. hostName: " abc ",
  94. expectedHostName: "abc",
  95. expectError: false,
  96. },
  97. }
  98. for idx, test := range testCases {
  99. hostName, err := GetHostname(test.hostName)
  100. if err != nil && !test.expectError {
  101. t.Errorf("[%d]: unexpected error: %s", idx, err)
  102. }
  103. if err == nil && test.expectError {
  104. t.Errorf("[%d]: expected error, got none", idx)
  105. }
  106. if test.expectedHostName != hostName {
  107. t.Errorf("[%d]: expected output %q, got %q", idx, test.expectedHostName, hostName)
  108. }
  109. }
  110. }
  111. func Test_GetZoneKey(t *testing.T) {
  112. tests := []struct {
  113. name string
  114. node *v1.Node
  115. zone string
  116. }{
  117. {
  118. name: "has no zone or region keys",
  119. node: &v1.Node{
  120. ObjectMeta: metav1.ObjectMeta{
  121. Labels: map[string]string{},
  122. },
  123. },
  124. zone: "",
  125. },
  126. {
  127. name: "has beta zone and region keys",
  128. node: &v1.Node{
  129. ObjectMeta: metav1.ObjectMeta{
  130. Labels: map[string]string{
  131. v1.LabelZoneFailureDomain: "zone1",
  132. v1.LabelZoneRegion: "region1",
  133. },
  134. },
  135. },
  136. zone: "region1:\x00:zone1",
  137. },
  138. {
  139. name: "has GA zone and region keys",
  140. node: &v1.Node{
  141. ObjectMeta: metav1.ObjectMeta{
  142. Labels: map[string]string{
  143. v1.LabelZoneFailureDomainStable: "zone1",
  144. v1.LabelZoneRegionStable: "region1",
  145. },
  146. },
  147. },
  148. zone: "region1:\x00:zone1",
  149. },
  150. {
  151. name: "has both beta and GA zone and region keys",
  152. node: &v1.Node{
  153. ObjectMeta: metav1.ObjectMeta{
  154. Labels: map[string]string{
  155. v1.LabelZoneFailureDomainStable: "zone1",
  156. v1.LabelZoneRegionStable: "region1",
  157. v1.LabelZoneFailureDomain: "zone1",
  158. v1.LabelZoneRegion: "region1",
  159. },
  160. },
  161. },
  162. zone: "region1:\x00:zone1",
  163. },
  164. {
  165. name: "has both beta and GA zone and region keys, beta labels take precedent",
  166. node: &v1.Node{
  167. ObjectMeta: metav1.ObjectMeta{
  168. Labels: map[string]string{
  169. v1.LabelZoneFailureDomainStable: "zone1",
  170. v1.LabelZoneRegionStable: "region1",
  171. v1.LabelZoneFailureDomain: "zone2",
  172. v1.LabelZoneRegion: "region2",
  173. },
  174. },
  175. },
  176. zone: "region2:\x00:zone2",
  177. },
  178. }
  179. for _, test := range tests {
  180. t.Run(test.name, func(t *testing.T) {
  181. zone := GetZoneKey(test.node)
  182. if zone != test.zone {
  183. t.Logf("actual zone key: %q", zone)
  184. t.Logf("expected zone key: %q", test.zone)
  185. t.Errorf("unexpected zone key")
  186. }
  187. })
  188. }
  189. }