initconfiguration_test.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. "bytes"
  16. "io/ioutil"
  17. "os"
  18. "path/filepath"
  19. "reflect"
  20. "testing"
  21. "github.com/pmezard/go-difflib/difflib"
  22. "k8s.io/api/core/v1"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. kubeadmapiv1beta2 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2"
  25. "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  26. "sigs.k8s.io/yaml"
  27. )
  28. func diff(expected, actual []byte) string {
  29. // Write out the diff
  30. var diffBytes bytes.Buffer
  31. difflib.WriteUnifiedDiff(&diffBytes, difflib.UnifiedDiff{
  32. A: difflib.SplitLines(string(expected)),
  33. B: difflib.SplitLines(string(actual)),
  34. FromFile: "expected",
  35. ToFile: "actual",
  36. Context: 3,
  37. })
  38. return diffBytes.String()
  39. }
  40. func TestLoadInitConfigurationFromFile(t *testing.T) {
  41. // Create temp folder for the test case
  42. tmpdir, err := ioutil.TempDir("", "")
  43. if err != nil {
  44. t.Fatalf("Couldn't create tmpdir")
  45. }
  46. defer os.RemoveAll(tmpdir)
  47. // cfgFiles is in cluster_test.go
  48. var tests = []struct {
  49. name string
  50. fileContents []byte
  51. expectErr bool
  52. }{
  53. {
  54. name: "v1beta1.partial1",
  55. fileContents: cfgFiles["InitConfiguration_v1beta1"],
  56. },
  57. {
  58. name: "v1beta1.partial2",
  59. fileContents: cfgFiles["ClusterConfiguration_v1beta1"],
  60. },
  61. {
  62. name: "v1beta1.full",
  63. fileContents: bytes.Join([][]byte{
  64. cfgFiles["InitConfiguration_v1beta1"],
  65. cfgFiles["ClusterConfiguration_v1beta1"],
  66. cfgFiles["Kube-proxy_componentconfig"],
  67. cfgFiles["Kubelet_componentconfig"],
  68. }, []byte(constants.YAMLDocumentSeparator)),
  69. },
  70. {
  71. name: "v1beta2.partial1",
  72. fileContents: cfgFiles["InitConfiguration_v1beta2"],
  73. },
  74. {
  75. name: "v1beta2.partial2",
  76. fileContents: cfgFiles["ClusterConfiguration_v1beta2"],
  77. },
  78. {
  79. name: "v1beta2.full",
  80. fileContents: bytes.Join([][]byte{
  81. cfgFiles["InitConfiguration_v1beta2"],
  82. cfgFiles["ClusterConfiguration_v1beta2"],
  83. cfgFiles["Kube-proxy_componentconfig"],
  84. cfgFiles["Kubelet_componentconfig"],
  85. }, []byte(constants.YAMLDocumentSeparator)),
  86. },
  87. }
  88. for _, rt := range tests {
  89. t.Run(rt.name, func(t2 *testing.T) {
  90. cfgPath := filepath.Join(tmpdir, rt.name)
  91. err := ioutil.WriteFile(cfgPath, rt.fileContents, 0644)
  92. if err != nil {
  93. t.Errorf("Couldn't create file")
  94. return
  95. }
  96. obj, err := LoadInitConfigurationFromFile(cfgPath)
  97. if rt.expectErr {
  98. if err == nil {
  99. t.Error("Unexpected success")
  100. }
  101. } else {
  102. if err != nil {
  103. t.Errorf("Error reading file: %v", err)
  104. return
  105. }
  106. if obj == nil {
  107. t.Errorf("Unexpected nil return value")
  108. }
  109. }
  110. })
  111. }
  112. }
  113. /*
  114. func TestInitConfigurationMarshallingFromFile(t *testing.T) {
  115. controlPlaneV1beta1YAMLAbstracted := controlPlaneV1beta1YAML
  116. controlPlaneInternalYAMLAbstracted := controlPlaneInternalYAML
  117. controlPlaneDefaultedYAMLAbstracted := controlPlaneDefaultedYAML
  118. if runtime.GOOS != "linux" {
  119. controlPlaneV1beta1YAMLAbstracted = controlPlaneV1beta1YAMLNonLinux
  120. controlPlaneInternalYAMLAbstracted = controlPlaneInternalYAMLNonLinux
  121. controlPlaneDefaultedYAMLAbstracted = controlPlaneDefaultedYAMLNonLinux
  122. }
  123. var tests = []struct {
  124. name, in, out string
  125. groupVersion schema.GroupVersion
  126. expectedErr bool
  127. }{
  128. // These tests are reading one file, loading it using LoadInitConfigurationFromFile that all of kubeadm is using for unmarshal of our API types,
  129. // and then marshals the internal object to the expected groupVersion
  130. //{ // v1beta1 -> internal NB. test commented after changes required for upgrading to go v1.12
  131. // name: "v1beta1ToInternal",
  132. // in: controlPlaneV1beta1YAMLAbstracted,
  133. // out: controlPlaneInternalYAMLAbstracted,
  134. // groupVersion: kubeadm.SchemeGroupVersion,
  135. //},
  136. { // v1beta1 -> internal -> v1beta1
  137. name: "v1beta1Tov1beta1",
  138. in: controlPlaneV1beta1YAMLAbstracted,
  139. out: controlPlaneV1beta1YAMLAbstracted,
  140. groupVersion: kubeadmapiv1beta1.SchemeGroupVersion,
  141. },
  142. // These tests are reading one file that has only a subset of the fields populated, loading it using LoadInitConfigurationFromFile,
  143. // and then marshals the internal object to the expected groupVersion
  144. { // v1beta1 -> default -> validate -> internal -> v1beta1
  145. name: "incompleteYAMLToDefaultedv1beta1",
  146. in: controlPlaneIncompleteYAML,
  147. out: controlPlaneDefaultedYAMLAbstracted,
  148. groupVersion: kubeadmapiv1beta1.SchemeGroupVersion,
  149. },
  150. { // v1beta1 -> validation should fail
  151. name: "invalidYAMLShouldFail",
  152. in: controlPlaneInvalidYAML,
  153. expectedErr: true,
  154. },
  155. }
  156. for _, rt := range tests {
  157. t.Run(rt.name, func(t2 *testing.T) {
  158. internalcfg, err := LoadInitConfigurationFromFile(rt.in)
  159. if err != nil {
  160. if rt.expectedErr {
  161. return
  162. }
  163. t2.Fatalf("couldn't unmarshal test data: %v", err)
  164. }
  165. actual, err := MarshalInitConfigurationToBytes(internalcfg, rt.groupVersion)
  166. if err != nil {
  167. t2.Fatalf("couldn't marshal internal object: %v", err)
  168. }
  169. expected, err := ioutil.ReadFile(rt.out)
  170. if err != nil {
  171. t2.Fatalf("couldn't read test data: %v", err)
  172. }
  173. if !bytes.Equal(expected, actual) {
  174. t2.Errorf("the expected and actual output differs.\n\tin: %s\n\tout: %s\n\tgroupversion: %s\n\tdiff: \n%s\n",
  175. rt.in, rt.out, rt.groupVersion.String(), diff(expected, actual))
  176. }
  177. })
  178. }
  179. }
  180. */
  181. func TestConsistentOrderByteSlice(t *testing.T) {
  182. var (
  183. aKind = "Akind"
  184. aFile = []byte(`
  185. kind: Akind
  186. apiVersion: foo.k8s.io/v1
  187. `)
  188. aaKind = "Aakind"
  189. aaFile = []byte(`
  190. kind: Aakind
  191. apiVersion: foo.k8s.io/v1
  192. `)
  193. abKind = "Abkind"
  194. abFile = []byte(`
  195. kind: Abkind
  196. apiVersion: foo.k8s.io/v1
  197. `)
  198. )
  199. var tests = []struct {
  200. name string
  201. in map[string][]byte
  202. expected [][]byte
  203. }{
  204. {
  205. name: "a_aa_ab",
  206. in: map[string][]byte{
  207. aKind: aFile,
  208. aaKind: aaFile,
  209. abKind: abFile,
  210. },
  211. expected: [][]byte{aaFile, abFile, aFile},
  212. },
  213. {
  214. name: "a_ab_aa",
  215. in: map[string][]byte{
  216. aKind: aFile,
  217. abKind: abFile,
  218. aaKind: aaFile,
  219. },
  220. expected: [][]byte{aaFile, abFile, aFile},
  221. },
  222. {
  223. name: "aa_a_ab",
  224. in: map[string][]byte{
  225. aaKind: aaFile,
  226. aKind: aFile,
  227. abKind: abFile,
  228. },
  229. expected: [][]byte{aaFile, abFile, aFile},
  230. },
  231. {
  232. name: "aa_ab_a",
  233. in: map[string][]byte{
  234. aaKind: aaFile,
  235. abKind: abFile,
  236. aKind: aFile,
  237. },
  238. expected: [][]byte{aaFile, abFile, aFile},
  239. },
  240. {
  241. name: "ab_a_aa",
  242. in: map[string][]byte{
  243. abKind: abFile,
  244. aKind: aFile,
  245. aaKind: aaFile,
  246. },
  247. expected: [][]byte{aaFile, abFile, aFile},
  248. },
  249. {
  250. name: "ab_aa_a",
  251. in: map[string][]byte{
  252. abKind: abFile,
  253. aaKind: aaFile,
  254. aKind: aFile,
  255. },
  256. expected: [][]byte{aaFile, abFile, aFile},
  257. },
  258. }
  259. for _, rt := range tests {
  260. t.Run(rt.name, func(t2 *testing.T) {
  261. actual := consistentOrderByteSlice(rt.in)
  262. if !reflect.DeepEqual(rt.expected, actual) {
  263. t2.Errorf("the expected and actual output differs.\n\texpected: %s\n\tout: %s\n", rt.expected, actual)
  264. }
  265. })
  266. }
  267. }
  268. func TestDefaultTaintsMarshaling(t *testing.T) {
  269. tests := []struct {
  270. desc string
  271. cfg kubeadmapiv1beta2.InitConfiguration
  272. expectedTaintCnt int
  273. }{
  274. {
  275. desc: "Uninitialized nodeRegistration field produces a single taint (the master one)",
  276. cfg: kubeadmapiv1beta2.InitConfiguration{
  277. TypeMeta: metav1.TypeMeta{
  278. APIVersion: "kubeadm.k8s.io/v1beta2",
  279. Kind: constants.InitConfigurationKind,
  280. },
  281. },
  282. expectedTaintCnt: 1,
  283. },
  284. {
  285. desc: "Uninitialized taints field produces a single taint (the master one)",
  286. cfg: kubeadmapiv1beta2.InitConfiguration{
  287. TypeMeta: metav1.TypeMeta{
  288. APIVersion: "kubeadm.k8s.io/v1beta2",
  289. Kind: constants.InitConfigurationKind,
  290. },
  291. NodeRegistration: kubeadmapiv1beta2.NodeRegistrationOptions{},
  292. },
  293. expectedTaintCnt: 1,
  294. },
  295. {
  296. desc: "Forsing taints to an empty slice produces no taints",
  297. cfg: kubeadmapiv1beta2.InitConfiguration{
  298. TypeMeta: metav1.TypeMeta{
  299. APIVersion: "kubeadm.k8s.io/v1beta2",
  300. Kind: constants.InitConfigurationKind,
  301. },
  302. NodeRegistration: kubeadmapiv1beta2.NodeRegistrationOptions{
  303. Taints: []v1.Taint{},
  304. },
  305. },
  306. expectedTaintCnt: 0,
  307. },
  308. {
  309. desc: "Custom taints are used",
  310. cfg: kubeadmapiv1beta2.InitConfiguration{
  311. TypeMeta: metav1.TypeMeta{
  312. APIVersion: "kubeadm.k8s.io/v1beta2",
  313. Kind: constants.InitConfigurationKind,
  314. },
  315. NodeRegistration: kubeadmapiv1beta2.NodeRegistrationOptions{
  316. Taints: []v1.Taint{
  317. {Key: "taint1"},
  318. {Key: "taint2"},
  319. },
  320. },
  321. },
  322. expectedTaintCnt: 2,
  323. },
  324. }
  325. for _, tc := range tests {
  326. t.Run(tc.desc, func(t *testing.T) {
  327. b, err := yaml.Marshal(tc.cfg)
  328. if err != nil {
  329. t.Fatalf("unexpected error while marshalling to YAML: %v", err)
  330. }
  331. cfg, err := BytesToInitConfiguration(b)
  332. if err != nil {
  333. t.Fatalf("unexpected error of BytesToInitConfiguration: %v\nconfig: %s", err, string(b))
  334. }
  335. if tc.expectedTaintCnt != len(cfg.NodeRegistration.Taints) {
  336. t.Fatalf("unexpected taints count\nexpected: %d\ngot: %d\ntaints: %v", tc.expectedTaintCnt, len(cfg.NodeRegistration.Taints), cfg.NodeRegistration.Taints)
  337. }
  338. })
  339. }
  340. }