common_test.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 config
  14. import (
  15. "fmt"
  16. "testing"
  17. "github.com/lithammer/dedent"
  18. "k8s.io/apimachinery/pkg/runtime/schema"
  19. kubeadmapiv1beta2 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2"
  20. "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  21. kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
  22. )
  23. const KubeadmGroupName = "kubeadm.k8s.io"
  24. func TestValidateSupportedVersion(t *testing.T) {
  25. tests := []struct {
  26. gv schema.GroupVersion
  27. allowDeprecated bool
  28. expectedErr bool
  29. }{
  30. {
  31. gv: schema.GroupVersion{
  32. Group: KubeadmGroupName,
  33. Version: "v1alpha1",
  34. },
  35. expectedErr: true,
  36. },
  37. {
  38. gv: schema.GroupVersion{
  39. Group: KubeadmGroupName,
  40. Version: "v1alpha2",
  41. },
  42. expectedErr: true,
  43. },
  44. {
  45. gv: schema.GroupVersion{
  46. Group: KubeadmGroupName,
  47. Version: "v1alpha3",
  48. },
  49. expectedErr: true,
  50. },
  51. {
  52. gv: schema.GroupVersion{
  53. Group: KubeadmGroupName,
  54. Version: "v1alpha3",
  55. },
  56. allowDeprecated: true,
  57. expectedErr: true,
  58. },
  59. {
  60. gv: schema.GroupVersion{
  61. Group: KubeadmGroupName,
  62. Version: "v1beta1",
  63. },
  64. },
  65. {
  66. gv: schema.GroupVersion{
  67. Group: KubeadmGroupName,
  68. Version: "v1beta2",
  69. },
  70. },
  71. {
  72. gv: schema.GroupVersion{
  73. Group: "foo.k8s.io",
  74. Version: "v1",
  75. },
  76. },
  77. }
  78. for _, rt := range tests {
  79. t.Run(fmt.Sprintf("%s/allowDeprecated:%t", rt.gv, rt.allowDeprecated), func(t *testing.T) {
  80. err := validateSupportedVersion(rt.gv, rt.allowDeprecated)
  81. if rt.expectedErr && err == nil {
  82. t.Error("unexpected success")
  83. } else if !rt.expectedErr && err != nil {
  84. t.Errorf("unexpected failure: %v", err)
  85. }
  86. })
  87. }
  88. }
  89. func TestLowercaseSANs(t *testing.T) {
  90. tests := []struct {
  91. name string
  92. in []string
  93. out []string
  94. }{
  95. {
  96. name: "empty struct",
  97. },
  98. {
  99. name: "already lowercase",
  100. in: []string{"example.k8s.io"},
  101. out: []string{"example.k8s.io"},
  102. },
  103. {
  104. name: "ip addresses and uppercase",
  105. in: []string{"EXAMPLE.k8s.io", "10.100.0.1"},
  106. out: []string{"example.k8s.io", "10.100.0.1"},
  107. },
  108. {
  109. name: "punycode and uppercase",
  110. in: []string{"xn--7gq663byk9a.xn--fiqz9s", "ANOTHEREXAMPLE.k8s.io"},
  111. out: []string{"xn--7gq663byk9a.xn--fiqz9s", "anotherexample.k8s.io"},
  112. },
  113. }
  114. for _, test := range tests {
  115. t.Run(test.name, func(t *testing.T) {
  116. cfg := &kubeadmapiv1beta2.ClusterConfiguration{
  117. APIServer: kubeadmapiv1beta2.APIServer{
  118. CertSANs: test.in,
  119. },
  120. }
  121. LowercaseSANs(cfg.APIServer.CertSANs)
  122. if len(cfg.APIServer.CertSANs) != len(test.out) {
  123. t.Fatalf("expected %d elements, got %d", len(test.out), len(cfg.APIServer.CertSANs))
  124. }
  125. for i, expected := range test.out {
  126. if cfg.APIServer.CertSANs[i] != expected {
  127. t.Errorf("expected element %d to be %q, got %q", i, expected, cfg.APIServer.CertSANs[i])
  128. }
  129. }
  130. })
  131. }
  132. }
  133. func TestVerifyAPIServerBindAddress(t *testing.T) {
  134. tests := []struct {
  135. name string
  136. address string
  137. expectedError bool
  138. }{
  139. {
  140. name: "valid address: IPV4",
  141. address: "192.168.0.1",
  142. },
  143. {
  144. name: "valid address: IPV6",
  145. address: "2001:db8:85a3::8a2e:370:7334",
  146. },
  147. {
  148. name: "invalid address: not a global unicast 0.0.0.0",
  149. address: "0.0.0.0",
  150. expectedError: true,
  151. },
  152. {
  153. name: "invalid address: not a global unicast 127.0.0.1",
  154. address: "127.0.0.1",
  155. expectedError: true,
  156. },
  157. {
  158. name: "invalid address: not a global unicast ::",
  159. address: "::",
  160. expectedError: true,
  161. },
  162. {
  163. name: "invalid address: cannot parse IPV4",
  164. address: "255.255.255.255.255",
  165. expectedError: true,
  166. },
  167. {
  168. name: "invalid address: cannot parse IPV6",
  169. address: "2a00:800::2a00:800:10102a00",
  170. expectedError: true,
  171. },
  172. }
  173. for _, test := range tests {
  174. t.Run(test.name, func(t *testing.T) {
  175. if err := VerifyAPIServerBindAddress(test.address); (err != nil) != test.expectedError {
  176. t.Errorf("expected error: %v, got %v, error: %v", test.expectedError, (err != nil), err)
  177. }
  178. })
  179. }
  180. }
  181. func TestMigrateOldConfigFromFile(t *testing.T) {
  182. tests := []struct {
  183. desc string
  184. oldCfg string
  185. expectedKinds []string
  186. expectErr bool
  187. }{
  188. {
  189. desc: "empty file produces empty result",
  190. oldCfg: "",
  191. expectErr: false,
  192. },
  193. {
  194. desc: "bad config produces error",
  195. oldCfg: dedent.Dedent(`
  196. apiVersion: kubeadm.k8s.io/v1beta1
  197. `),
  198. expectErr: true,
  199. },
  200. {
  201. desc: "InitConfiguration only gets migrated",
  202. oldCfg: dedent.Dedent(`
  203. apiVersion: kubeadm.k8s.io/v1beta1
  204. kind: InitConfiguration
  205. `),
  206. expectedKinds: []string{
  207. constants.InitConfigurationKind,
  208. constants.ClusterConfigurationKind,
  209. },
  210. expectErr: false,
  211. },
  212. {
  213. desc: "ClusterConfiguration only gets migrated",
  214. oldCfg: dedent.Dedent(`
  215. apiVersion: kubeadm.k8s.io/v1beta1
  216. kind: ClusterConfiguration
  217. `),
  218. expectedKinds: []string{
  219. constants.InitConfigurationKind,
  220. constants.ClusterConfigurationKind,
  221. },
  222. expectErr: false,
  223. },
  224. {
  225. desc: "JoinConfiguration only gets migrated",
  226. oldCfg: dedent.Dedent(`
  227. apiVersion: kubeadm.k8s.io/v1beta1
  228. kind: JoinConfiguration
  229. discovery:
  230. bootstrapToken:
  231. token: abcdef.0123456789abcdef
  232. apiServerEndpoint: kube-apiserver:6443
  233. unsafeSkipCAVerification: true
  234. `),
  235. expectedKinds: []string{
  236. constants.JoinConfigurationKind,
  237. },
  238. expectErr: false,
  239. },
  240. {
  241. desc: "Init + Cluster Configurations are migrated",
  242. oldCfg: dedent.Dedent(`
  243. apiVersion: kubeadm.k8s.io/v1beta1
  244. kind: InitConfiguration
  245. ---
  246. apiVersion: kubeadm.k8s.io/v1beta1
  247. kind: ClusterConfiguration
  248. `),
  249. expectedKinds: []string{
  250. constants.InitConfigurationKind,
  251. constants.ClusterConfigurationKind,
  252. },
  253. expectErr: false,
  254. },
  255. {
  256. desc: "Init + Join Configurations are migrated",
  257. oldCfg: dedent.Dedent(`
  258. apiVersion: kubeadm.k8s.io/v1beta1
  259. kind: InitConfiguration
  260. ---
  261. apiVersion: kubeadm.k8s.io/v1beta1
  262. kind: JoinConfiguration
  263. discovery:
  264. bootstrapToken:
  265. token: abcdef.0123456789abcdef
  266. apiServerEndpoint: kube-apiserver:6443
  267. unsafeSkipCAVerification: true
  268. `),
  269. expectedKinds: []string{
  270. constants.InitConfigurationKind,
  271. constants.ClusterConfigurationKind,
  272. constants.JoinConfigurationKind,
  273. },
  274. expectErr: false,
  275. },
  276. {
  277. desc: "Cluster + Join Configurations are migrated",
  278. oldCfg: dedent.Dedent(`
  279. apiVersion: kubeadm.k8s.io/v1beta1
  280. kind: ClusterConfiguration
  281. ---
  282. apiVersion: kubeadm.k8s.io/v1beta1
  283. kind: JoinConfiguration
  284. discovery:
  285. bootstrapToken:
  286. token: abcdef.0123456789abcdef
  287. apiServerEndpoint: kube-apiserver:6443
  288. unsafeSkipCAVerification: true
  289. `),
  290. expectedKinds: []string{
  291. constants.InitConfigurationKind,
  292. constants.ClusterConfigurationKind,
  293. constants.JoinConfigurationKind,
  294. },
  295. expectErr: false,
  296. },
  297. {
  298. desc: "Init + Cluster + Join Configurations are migrated",
  299. oldCfg: dedent.Dedent(`
  300. apiVersion: kubeadm.k8s.io/v1beta1
  301. kind: InitConfiguration
  302. ---
  303. apiVersion: kubeadm.k8s.io/v1beta1
  304. kind: ClusterConfiguration
  305. ---
  306. apiVersion: kubeadm.k8s.io/v1beta1
  307. kind: JoinConfiguration
  308. discovery:
  309. bootstrapToken:
  310. token: abcdef.0123456789abcdef
  311. apiServerEndpoint: kube-apiserver:6443
  312. unsafeSkipCAVerification: true
  313. `),
  314. expectedKinds: []string{
  315. constants.InitConfigurationKind,
  316. constants.ClusterConfigurationKind,
  317. constants.JoinConfigurationKind,
  318. },
  319. expectErr: false,
  320. },
  321. {
  322. desc: "component configs are not migrated",
  323. oldCfg: dedent.Dedent(`
  324. apiVersion: kubeadm.k8s.io/v1beta1
  325. kind: InitConfiguration
  326. ---
  327. apiVersion: kubeadm.k8s.io/v1beta1
  328. kind: ClusterConfiguration
  329. ---
  330. apiVersion: kubeadm.k8s.io/v1beta1
  331. kind: JoinConfiguration
  332. discovery:
  333. bootstrapToken:
  334. token: abcdef.0123456789abcdef
  335. apiServerEndpoint: kube-apiserver:6443
  336. unsafeSkipCAVerification: true
  337. ---
  338. apiVersion: kubeproxy.config.k8s.io/v1alpha1
  339. kind: KubeProxyConfiguration
  340. ---
  341. apiVersion: kubelet.config.k8s.io/v1beta1
  342. kind: KubeletConfiguration
  343. `),
  344. expectedKinds: []string{
  345. constants.InitConfigurationKind,
  346. constants.ClusterConfigurationKind,
  347. constants.JoinConfigurationKind,
  348. },
  349. expectErr: false,
  350. },
  351. }
  352. for _, test := range tests {
  353. t.Run(test.desc, func(t *testing.T) {
  354. b, err := MigrateOldConfig([]byte(test.oldCfg))
  355. if test.expectErr {
  356. if err == nil {
  357. t.Fatalf("unexpected success:\n%s", b)
  358. }
  359. } else {
  360. if err != nil {
  361. t.Fatalf("unexpected failure: %v", err)
  362. }
  363. gvks, err := kubeadmutil.GroupVersionKindsFromBytes(b)
  364. if err != nil {
  365. t.Fatalf("unexpected error returned by GroupVersionKindsFromBytes: %v", err)
  366. }
  367. if len(gvks) != len(test.expectedKinds) {
  368. t.Fatalf("length mismatch between resulting gvks and expected kinds:\n\tlen(gvks)=%d\n\tlen(expectedKinds)=%d",
  369. len(gvks), len(test.expectedKinds))
  370. }
  371. for _, expectedKind := range test.expectedKinds {
  372. if !kubeadmutil.GroupVersionKindsHasKind(gvks, expectedKind) {
  373. t.Fatalf("migration failed to produce config kind: %s", expectedKind)
  374. }
  375. }
  376. }
  377. })
  378. }
  379. }