endpoint_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. Copyright 2017 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 util
  14. import (
  15. "testing"
  16. kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  17. )
  18. func TestGetControlPlaneEndpoint(t *testing.T) {
  19. var tests = []struct {
  20. name string
  21. cfg *kubeadmapi.InitConfiguration
  22. expectedEndpoint string
  23. expectedError bool
  24. }{
  25. {
  26. name: "use ControlPlaneEndpoint (dns) if fully defined",
  27. cfg: &kubeadmapi.InitConfiguration{
  28. LocalAPIEndpoint: kubeadmapi.APIEndpoint{
  29. BindPort: 4567,
  30. AdvertiseAddress: "4.5.6.7",
  31. },
  32. ClusterConfiguration: kubeadmapi.ClusterConfiguration{
  33. ControlPlaneEndpoint: "cp.k8s.io:1234",
  34. },
  35. },
  36. expectedEndpoint: "https://cp.k8s.io:1234",
  37. },
  38. {
  39. name: "use ControlPlaneEndpoint (ipv4) if fully defined",
  40. cfg: &kubeadmapi.InitConfiguration{
  41. LocalAPIEndpoint: kubeadmapi.APIEndpoint{
  42. BindPort: 4567,
  43. AdvertiseAddress: "4.5.6.7",
  44. },
  45. ClusterConfiguration: kubeadmapi.ClusterConfiguration{
  46. ControlPlaneEndpoint: "1.2.3.4:1234",
  47. },
  48. },
  49. expectedEndpoint: "https://1.2.3.4:1234",
  50. },
  51. {
  52. name: "use ControlPlaneEndpoint (ipv6) if fully defined",
  53. cfg: &kubeadmapi.InitConfiguration{
  54. LocalAPIEndpoint: kubeadmapi.APIEndpoint{
  55. BindPort: 4567,
  56. AdvertiseAddress: "4.5.6.7",
  57. },
  58. ClusterConfiguration: kubeadmapi.ClusterConfiguration{
  59. ControlPlaneEndpoint: "[2001:db8::1]:1234",
  60. },
  61. },
  62. expectedEndpoint: "https://[2001:db8::1]:1234",
  63. },
  64. {
  65. name: "use ControlPlaneEndpoint (dns) + BindPort if ControlPlaneEndpoint defined without port",
  66. cfg: &kubeadmapi.InitConfiguration{
  67. LocalAPIEndpoint: kubeadmapi.APIEndpoint{
  68. BindPort: 4567,
  69. AdvertiseAddress: "4.5.6.7",
  70. },
  71. ClusterConfiguration: kubeadmapi.ClusterConfiguration{
  72. ControlPlaneEndpoint: "cp.k8s.io",
  73. },
  74. },
  75. expectedEndpoint: "https://cp.k8s.io:4567",
  76. },
  77. {
  78. name: "use ControlPlaneEndpoint (ipv4) + BindPort if ControlPlaneEndpoint defined without port",
  79. cfg: &kubeadmapi.InitConfiguration{
  80. LocalAPIEndpoint: kubeadmapi.APIEndpoint{
  81. BindPort: 4567,
  82. AdvertiseAddress: "4.5.6.7",
  83. },
  84. ClusterConfiguration: kubeadmapi.ClusterConfiguration{
  85. ControlPlaneEndpoint: "1.2.3.4",
  86. },
  87. },
  88. expectedEndpoint: "https://1.2.3.4:4567",
  89. },
  90. {
  91. name: "use ControlPlaneEndpoint (ipv6) + BindPort if ControlPlaneEndpoint defined without port",
  92. cfg: &kubeadmapi.InitConfiguration{
  93. LocalAPIEndpoint: kubeadmapi.APIEndpoint{
  94. BindPort: 4567,
  95. AdvertiseAddress: "4.5.6.7",
  96. },
  97. ClusterConfiguration: kubeadmapi.ClusterConfiguration{
  98. ControlPlaneEndpoint: "2001:db8::1",
  99. },
  100. },
  101. expectedEndpoint: "https://[2001:db8::1]:4567",
  102. },
  103. {
  104. name: "use AdvertiseAddress (ipv4) + BindPort if ControlPlaneEndpoint is not defined",
  105. cfg: &kubeadmapi.InitConfiguration{
  106. LocalAPIEndpoint: kubeadmapi.APIEndpoint{
  107. BindPort: 4567,
  108. AdvertiseAddress: "4.5.6.7",
  109. },
  110. },
  111. expectedEndpoint: "https://4.5.6.7:4567",
  112. },
  113. {
  114. name: "use AdvertiseAddress (ipv6) + BindPort if ControlPlaneEndpoint is not defined",
  115. cfg: &kubeadmapi.InitConfiguration{
  116. LocalAPIEndpoint: kubeadmapi.APIEndpoint{
  117. BindPort: 4567,
  118. AdvertiseAddress: "2001:db8::1",
  119. },
  120. },
  121. expectedEndpoint: "https://[2001:db8::1]:4567",
  122. },
  123. {
  124. name: "fail if invalid BindPort",
  125. cfg: &kubeadmapi.InitConfiguration{
  126. LocalAPIEndpoint: kubeadmapi.APIEndpoint{
  127. BindPort: 0,
  128. },
  129. },
  130. expectedError: true,
  131. },
  132. {
  133. name: "fail if invalid ControlPlaneEndpoint (dns)",
  134. cfg: &kubeadmapi.InitConfiguration{
  135. ClusterConfiguration: kubeadmapi.ClusterConfiguration{
  136. ControlPlaneEndpoint: "bad!!.cp.k8s.io",
  137. },
  138. },
  139. expectedError: true,
  140. },
  141. {
  142. name: "fail if invalid ControlPlaneEndpoint (ip4)",
  143. cfg: &kubeadmapi.InitConfiguration{
  144. ClusterConfiguration: kubeadmapi.ClusterConfiguration{
  145. ControlPlaneEndpoint: "1..0",
  146. },
  147. },
  148. expectedError: true,
  149. },
  150. {
  151. name: "fail if invalid ControlPlaneEndpoint (ip6)",
  152. cfg: &kubeadmapi.InitConfiguration{
  153. ClusterConfiguration: kubeadmapi.ClusterConfiguration{
  154. ControlPlaneEndpoint: "1200::AB00:1234::2552:7777:1313",
  155. },
  156. },
  157. expectedError: true,
  158. },
  159. {
  160. name: "fail if invalid ControlPlaneEndpoint (port)",
  161. cfg: &kubeadmapi.InitConfiguration{
  162. ClusterConfiguration: kubeadmapi.ClusterConfiguration{
  163. ControlPlaneEndpoint: "cp.k8s.io:0",
  164. },
  165. },
  166. expectedError: true,
  167. },
  168. {
  169. name: "fail if invalid AdvertiseAddress (ip4)",
  170. cfg: &kubeadmapi.InitConfiguration{
  171. LocalAPIEndpoint: kubeadmapi.APIEndpoint{
  172. AdvertiseAddress: "1..0",
  173. BindPort: 4567,
  174. },
  175. },
  176. expectedError: true,
  177. },
  178. {
  179. name: "fail if invalid AdvertiseAddress (ip6)",
  180. cfg: &kubeadmapi.InitConfiguration{
  181. LocalAPIEndpoint: kubeadmapi.APIEndpoint{
  182. AdvertiseAddress: "1200::AB00:1234::2552:7777:1313",
  183. BindPort: 4567,
  184. },
  185. },
  186. expectedError: true,
  187. },
  188. }
  189. for _, rt := range tests {
  190. t.Run(rt.name, func(t *testing.T) {
  191. actualEndpoint, actualError := GetControlPlaneEndpoint(rt.cfg.ControlPlaneEndpoint, &rt.cfg.LocalAPIEndpoint)
  192. if (actualError != nil) && !rt.expectedError {
  193. t.Errorf("%s unexpected failure: %v", rt.name, actualError)
  194. return
  195. } else if (actualError == nil) && rt.expectedError {
  196. t.Errorf("%s passed when expected to fail", rt.name)
  197. return
  198. }
  199. if actualEndpoint != rt.expectedEndpoint {
  200. t.Errorf("%s returned invalid endpoint %s, expected %s", rt.name, actualEndpoint, rt.expectedEndpoint)
  201. }
  202. })
  203. }
  204. }
  205. func TestParseHostPort(t *testing.T) {
  206. var tests = []struct {
  207. name string
  208. hostport string
  209. expectedHost string
  210. expectedPort string
  211. expectedError bool
  212. }{
  213. {
  214. name: "valid dns",
  215. hostport: "cp.k8s.io",
  216. expectedHost: "cp.k8s.io",
  217. expectedPort: "",
  218. },
  219. {
  220. name: "valid dns:port",
  221. hostport: "cp.k8s.io:1234",
  222. expectedHost: "cp.k8s.io",
  223. expectedPort: "1234",
  224. },
  225. {
  226. name: "valid ip4",
  227. hostport: "1.2.3.4",
  228. expectedHost: "1.2.3.4",
  229. expectedPort: "",
  230. },
  231. {
  232. name: "valid ipv4:port",
  233. hostport: "1.2.3.4:1234",
  234. expectedHost: "1.2.3.4",
  235. expectedPort: "1234",
  236. },
  237. {
  238. name: "valid ipv6",
  239. hostport: "2001:db8::1",
  240. expectedHost: "2001:db8::1",
  241. expectedPort: "",
  242. },
  243. {
  244. name: "valid ipv6:port",
  245. hostport: "[2001:db8::1]:1234",
  246. expectedHost: "2001:db8::1",
  247. expectedPort: "1234",
  248. },
  249. {
  250. name: "invalid port(not a number)",
  251. hostport: "cp.k8s.io:aaa",
  252. expectedError: true,
  253. },
  254. {
  255. name: "invalid port(out of range, positive port number)",
  256. hostport: "cp.k8s.io:987654321",
  257. expectedError: true,
  258. },
  259. {
  260. name: "invalid port(out of range, negative port number)",
  261. hostport: "cp.k8s.io:-987654321",
  262. expectedError: true,
  263. },
  264. {
  265. name: "invalid port(out of range, negative port number)",
  266. hostport: "cp.k8s.io:123:123",
  267. expectedError: true,
  268. },
  269. {
  270. name: "invalid dns",
  271. hostport: "bad!!cp.k8s.io",
  272. expectedError: true,
  273. },
  274. {
  275. name: "invalid valid dns:port",
  276. hostport: "bad!!cp.k8s.io:1234",
  277. expectedError: true,
  278. },
  279. {
  280. name: "invalid ip4, but valid DNS",
  281. hostport: "259.2.3.4",
  282. expectedHost: "259.2.3.4",
  283. },
  284. {
  285. name: "invalid ip4",
  286. hostport: "1..3.4",
  287. expectedError: true,
  288. },
  289. {
  290. name: "invalid ip4(2):port",
  291. hostport: "1..3.4:1234",
  292. expectedError: true,
  293. },
  294. {
  295. name: "invalid ipv6",
  296. hostport: "1200::AB00:1234::2552:7777:1313",
  297. expectedError: true,
  298. },
  299. {
  300. name: "invalid ipv6:port",
  301. hostport: "[1200::AB00:1234::2552:7777:1313]:1234",
  302. expectedError: true,
  303. },
  304. }
  305. for _, rt := range tests {
  306. t.Run(rt.name, func(t *testing.T) {
  307. actualHost, actualPort, actualError := ParseHostPort(rt.hostport)
  308. if (actualError != nil) && !rt.expectedError {
  309. t.Errorf("%s unexpected failure: %v", rt.name, actualError)
  310. return
  311. } else if (actualError == nil) && rt.expectedError {
  312. t.Errorf("%s passed when expected to fail", rt.name)
  313. return
  314. }
  315. if actualHost != rt.expectedHost {
  316. t.Errorf("%s returned invalid host %s, expected %s", rt.name, actualHost, rt.expectedHost)
  317. return
  318. }
  319. if actualPort != rt.expectedPort {
  320. t.Errorf("%s returned invalid port %s, expected %s", rt.name, actualPort, rt.expectedPort)
  321. }
  322. })
  323. }
  324. }
  325. func TestParsePort(t *testing.T) {
  326. var tests = []struct {
  327. name string
  328. port string
  329. expectedPort int
  330. expectedError bool
  331. }{
  332. {
  333. name: "valid port",
  334. port: "1234",
  335. expectedPort: 1234,
  336. },
  337. {
  338. name: "invalid port (not a number)",
  339. port: "a",
  340. expectedError: true,
  341. },
  342. {
  343. name: "invalid port (<1)",
  344. port: "-10",
  345. expectedError: true,
  346. },
  347. {
  348. name: "invalid port (>65535)",
  349. port: "66535",
  350. expectedError: true,
  351. },
  352. }
  353. for _, rt := range tests {
  354. t.Run(rt.name, func(t *testing.T) {
  355. actualPort, actualError := ParsePort(rt.port)
  356. if (actualError != nil) && !rt.expectedError {
  357. t.Errorf("%s unexpected failure: %v", rt.name, actualError)
  358. return
  359. } else if (actualError == nil) && rt.expectedError {
  360. t.Errorf("%s passed when expected to fail", rt.name)
  361. return
  362. }
  363. if actualPort != rt.expectedPort {
  364. t.Errorf("%s returned invalid port %d, expected %d", rt.name, actualPort, rt.expectedPort)
  365. }
  366. })
  367. }
  368. }