initconfiguration_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. "testing"
  20. "github.com/pmezard/go-difflib/difflib"
  21. "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. kubeadmapiv1beta2 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2"
  24. "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  25. "sigs.k8s.io/yaml"
  26. )
  27. func diff(expected, actual []byte) string {
  28. // Write out the diff
  29. var diffBytes bytes.Buffer
  30. difflib.WriteUnifiedDiff(&diffBytes, difflib.UnifiedDiff{
  31. A: difflib.SplitLines(string(expected)),
  32. B: difflib.SplitLines(string(actual)),
  33. FromFile: "expected",
  34. ToFile: "actual",
  35. Context: 3,
  36. })
  37. return diffBytes.String()
  38. }
  39. func TestLoadInitConfigurationFromFile(t *testing.T) {
  40. // Create temp folder for the test case
  41. tmpdir, err := ioutil.TempDir("", "")
  42. if err != nil {
  43. t.Fatalf("Couldn't create tmpdir")
  44. }
  45. defer os.RemoveAll(tmpdir)
  46. // cfgFiles is in cluster_test.go
  47. var tests = []struct {
  48. name string
  49. fileContents []byte
  50. expectErr bool
  51. }{
  52. {
  53. name: "v1beta1.partial1",
  54. fileContents: cfgFiles["InitConfiguration_v1beta1"],
  55. },
  56. {
  57. name: "v1beta1.partial2",
  58. fileContents: cfgFiles["ClusterConfiguration_v1beta1"],
  59. },
  60. {
  61. name: "v1beta1.full",
  62. fileContents: bytes.Join([][]byte{
  63. cfgFiles["InitConfiguration_v1beta1"],
  64. cfgFiles["ClusterConfiguration_v1beta1"],
  65. cfgFiles["Kube-proxy_componentconfig"],
  66. cfgFiles["Kubelet_componentconfig"],
  67. }, []byte(constants.YAMLDocumentSeparator)),
  68. },
  69. {
  70. name: "v1beta2.partial1",
  71. fileContents: cfgFiles["InitConfiguration_v1beta2"],
  72. },
  73. {
  74. name: "v1beta2.partial2",
  75. fileContents: cfgFiles["ClusterConfiguration_v1beta2"],
  76. },
  77. {
  78. name: "v1beta2.full",
  79. fileContents: bytes.Join([][]byte{
  80. cfgFiles["InitConfiguration_v1beta2"],
  81. cfgFiles["ClusterConfiguration_v1beta2"],
  82. cfgFiles["Kube-proxy_componentconfig"],
  83. cfgFiles["Kubelet_componentconfig"],
  84. }, []byte(constants.YAMLDocumentSeparator)),
  85. },
  86. }
  87. for _, rt := range tests {
  88. t.Run(rt.name, func(t2 *testing.T) {
  89. cfgPath := filepath.Join(tmpdir, rt.name)
  90. err := ioutil.WriteFile(cfgPath, rt.fileContents, 0644)
  91. if err != nil {
  92. t.Errorf("Couldn't create file")
  93. return
  94. }
  95. obj, err := LoadInitConfigurationFromFile(cfgPath)
  96. if rt.expectErr {
  97. if err == nil {
  98. t.Error("Unexpected success")
  99. }
  100. } else {
  101. if err != nil {
  102. t.Errorf("Error reading file: %v", err)
  103. return
  104. }
  105. if obj == nil {
  106. t.Errorf("Unexpected nil return value")
  107. }
  108. }
  109. })
  110. }
  111. }
  112. func TestDefaultTaintsMarshaling(t *testing.T) {
  113. tests := []struct {
  114. desc string
  115. cfg kubeadmapiv1beta2.InitConfiguration
  116. expectedTaintCnt int
  117. }{
  118. {
  119. desc: "Uninitialized nodeRegistration field produces a single taint (the master one)",
  120. cfg: kubeadmapiv1beta2.InitConfiguration{
  121. TypeMeta: metav1.TypeMeta{
  122. APIVersion: "kubeadm.k8s.io/v1beta2",
  123. Kind: constants.InitConfigurationKind,
  124. },
  125. },
  126. expectedTaintCnt: 1,
  127. },
  128. {
  129. desc: "Uninitialized taints field produces a single taint (the master one)",
  130. cfg: kubeadmapiv1beta2.InitConfiguration{
  131. TypeMeta: metav1.TypeMeta{
  132. APIVersion: "kubeadm.k8s.io/v1beta2",
  133. Kind: constants.InitConfigurationKind,
  134. },
  135. NodeRegistration: kubeadmapiv1beta2.NodeRegistrationOptions{},
  136. },
  137. expectedTaintCnt: 1,
  138. },
  139. {
  140. desc: "Forsing taints to an empty slice produces no taints",
  141. cfg: kubeadmapiv1beta2.InitConfiguration{
  142. TypeMeta: metav1.TypeMeta{
  143. APIVersion: "kubeadm.k8s.io/v1beta2",
  144. Kind: constants.InitConfigurationKind,
  145. },
  146. NodeRegistration: kubeadmapiv1beta2.NodeRegistrationOptions{
  147. Taints: []v1.Taint{},
  148. },
  149. },
  150. expectedTaintCnt: 0,
  151. },
  152. {
  153. desc: "Custom taints are used",
  154. cfg: kubeadmapiv1beta2.InitConfiguration{
  155. TypeMeta: metav1.TypeMeta{
  156. APIVersion: "kubeadm.k8s.io/v1beta2",
  157. Kind: constants.InitConfigurationKind,
  158. },
  159. NodeRegistration: kubeadmapiv1beta2.NodeRegistrationOptions{
  160. Taints: []v1.Taint{
  161. {Key: "taint1"},
  162. {Key: "taint2"},
  163. },
  164. },
  165. },
  166. expectedTaintCnt: 2,
  167. },
  168. }
  169. for _, tc := range tests {
  170. t.Run(tc.desc, func(t *testing.T) {
  171. b, err := yaml.Marshal(tc.cfg)
  172. if err != nil {
  173. t.Fatalf("unexpected error while marshalling to YAML: %v", err)
  174. }
  175. cfg, err := BytesToInitConfiguration(b)
  176. if err != nil {
  177. t.Fatalf("unexpected error of BytesToInitConfiguration: %v\nconfig: %s", err, string(b))
  178. }
  179. if tc.expectedTaintCnt != len(cfg.NodeRegistration.Taints) {
  180. t.Fatalf("unexpected taints count\nexpected: %d\ngot: %d\ntaints: %v", tc.expectedTaintCnt, len(cfg.NodeRegistration.Taints), cfg.NodeRegistration.Taints)
  181. }
  182. })
  183. }
  184. }