PageRenderTime 24ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/pkg/kubelet/apis/kubeletconfig/helpers_test.go

https://gitlab.com/unofficial-mirrors/kubernetes
Go | 217 lines | 177 code | 15 blank | 25 comment | 9 complexity | 32ddba2d8975218c9e9479895df22978 MD5 | raw file
  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 kubeletconfig
  14. import (
  15. "reflect"
  16. "strings"
  17. "testing"
  18. "k8s.io/apimachinery/pkg/util/sets"
  19. "k8s.io/apimachinery/pkg/util/validation/field"
  20. )
  21. func TestKubeletConfigurationPathFields(t *testing.T) {
  22. // ensure the intersection of kubeletConfigurationPathFieldPaths and KubeletConfigurationNonPathFields is empty
  23. if i := kubeletConfigurationPathFieldPaths.Intersection(kubeletConfigurationNonPathFieldPaths); len(i) > 0 {
  24. t.Fatalf("expect the intersection of kubeletConfigurationPathFieldPaths and "+
  25. "KubeletConfigurationNonPathFields to be empty, got:\n%s",
  26. strings.Join(i.List(), "\n"))
  27. }
  28. // ensure that kubeletConfigurationPathFields U kubeletConfigurationNonPathFields == allPrimitiveFieldPaths(KubeletConfiguration)
  29. expect := sets.NewString().Union(kubeletConfigurationPathFieldPaths).Union(kubeletConfigurationNonPathFieldPaths)
  30. result := allPrimitiveFieldPaths(t, reflect.TypeOf(&KubeletConfiguration{}), nil)
  31. if !expect.Equal(result) {
  32. // expected fields missing from result
  33. missing := expect.Difference(result)
  34. // unexpected fields in result but not specified in expect
  35. unexpected := result.Difference(expect)
  36. if len(missing) > 0 {
  37. t.Errorf("the following fields were expected, but missing from the result. "+
  38. "If the field has been removed, please remove it from the kubeletConfigurationPathFieldPaths set "+
  39. "and the KubeletConfigurationPathRefs function, "+
  40. "or remove it from the kubeletConfigurationNonPathFieldPaths set, as appropriate:\n%s",
  41. strings.Join(missing.List(), "\n"))
  42. }
  43. if len(unexpected) > 0 {
  44. t.Errorf("the following fields were in the result, but unexpected. "+
  45. "If the field is new, please add it to the kubeletConfigurationPathFieldPaths set "+
  46. "and the KubeletConfigurationPathRefs function, "+
  47. "or add it to the kubeletConfigurationNonPathFieldPaths set, as appropriate:\n%s",
  48. strings.Join(unexpected.List(), "\n"))
  49. }
  50. }
  51. }
  52. func allPrimitiveFieldPaths(t *testing.T, tp reflect.Type, path *field.Path) sets.String {
  53. paths := sets.NewString()
  54. switch tp.Kind() {
  55. case reflect.Ptr:
  56. paths.Insert(allPrimitiveFieldPaths(t, tp.Elem(), path).List()...)
  57. case reflect.Struct:
  58. for i := 0; i < tp.NumField(); i++ {
  59. field := tp.Field(i)
  60. paths.Insert(allPrimitiveFieldPaths(t, field.Type, path.Child(field.Name)).List()...)
  61. }
  62. case reflect.Map, reflect.Slice:
  63. paths.Insert(allPrimitiveFieldPaths(t, tp.Elem(), path.Key("*")).List()...)
  64. case reflect.Interface:
  65. t.Fatalf("unexpected interface{} field %s", path.String())
  66. default:
  67. // if we hit a primitive type, we're at a leaf
  68. paths.Insert(path.String())
  69. }
  70. return paths
  71. }
  72. // dummy helper types
  73. type foo struct {
  74. foo int
  75. }
  76. type bar struct {
  77. str string
  78. strptr *string
  79. ints []int
  80. stringMap map[string]string
  81. foo foo
  82. fooptr *foo
  83. bars []foo
  84. barMap map[string]foo
  85. }
  86. func TestAllPrimitiveFieldPaths(t *testing.T) {
  87. expect := sets.NewString(
  88. "str",
  89. "strptr",
  90. "ints[*]",
  91. "stringMap[*]",
  92. "foo.foo",
  93. "fooptr.foo",
  94. "bars[*].foo",
  95. "barMap[*].foo",
  96. )
  97. result := allPrimitiveFieldPaths(t, reflect.TypeOf(&bar{}), nil)
  98. if !expect.Equal(result) {
  99. // expected fields missing from result
  100. missing := expect.Difference(result)
  101. // unexpected fields in result but not specified in expect
  102. unexpected := result.Difference(expect)
  103. if len(missing) > 0 {
  104. t.Errorf("the following fields were exepcted, but missing from the result:\n%s", strings.Join(missing.List(), "\n"))
  105. }
  106. if len(unexpected) > 0 {
  107. t.Errorf("the following fields were in the result, but unexpected:\n%s", strings.Join(unexpected.List(), "\n"))
  108. }
  109. }
  110. }
  111. var (
  112. // KubeletConfiguration fields that contain file paths. If you update this, also update KubeletConfigurationPathRefs!
  113. kubeletConfigurationPathFieldPaths = sets.NewString(
  114. "StaticPodPath",
  115. "Authentication.X509.ClientCAFile",
  116. "TLSCertFile",
  117. "TLSPrivateKeyFile",
  118. "ResolverConfig",
  119. )
  120. // KubeletConfiguration fields that do not contain file paths.
  121. kubeletConfigurationNonPathFieldPaths = sets.NewString(
  122. "Address",
  123. "Authentication.Anonymous.Enabled",
  124. "Authentication.Webhook.CacheTTL.Duration",
  125. "Authentication.Webhook.Enabled",
  126. "Authorization.Mode",
  127. "Authorization.Webhook.CacheAuthorizedTTL.Duration",
  128. "Authorization.Webhook.CacheUnauthorizedTTL.Duration",
  129. "CPUCFSQuota",
  130. "CPUManagerPolicy",
  131. "CPUManagerReconcilePeriod.Duration",
  132. "QOSReserved[*]",
  133. "CgroupDriver",
  134. "CgroupRoot",
  135. "CgroupsPerQOS",
  136. "ClusterDNS[*]",
  137. "ClusterDomain",
  138. "ContainerLogMaxFiles",
  139. "ContainerLogMaxSize",
  140. "ContentType",
  141. "EnableContentionProfiling",
  142. "EnableControllerAttachDetach",
  143. "EnableDebuggingHandlers",
  144. "EnforceNodeAllocatable[*]",
  145. "EventBurst",
  146. "EventRecordQPS",
  147. "EvictionHard[*]",
  148. "EvictionMaxPodGracePeriod",
  149. "EvictionMinimumReclaim[*]",
  150. "EvictionPressureTransitionPeriod.Duration",
  151. "EvictionSoft[*]",
  152. "EvictionSoftGracePeriod[*]",
  153. "FailSwapOn",
  154. "FeatureGates[*]",
  155. "FileCheckFrequency.Duration",
  156. "HTTPCheckFrequency.Duration",
  157. "HairpinMode",
  158. "HealthzBindAddress",
  159. "HealthzPort",
  160. "TLSCipherSuites[*]",
  161. "TLSMinVersion",
  162. "IPTablesDropBit",
  163. "IPTablesMasqueradeBit",
  164. "ImageGCHighThresholdPercent",
  165. "ImageGCLowThresholdPercent",
  166. "ImageMinimumGCAge.Duration",
  167. "KubeAPIBurst",
  168. "KubeAPIQPS",
  169. "KubeReservedCgroup",
  170. "KubeReserved[*]",
  171. "KubeletCgroups",
  172. "MakeIPTablesUtilChains",
  173. "ServerTLSBootstrap",
  174. "StaticPodURL",
  175. "StaticPodURLHeader[*][*]",
  176. "MaxOpenFiles",
  177. "MaxPods",
  178. "NodeStatusUpdateFrequency.Duration",
  179. "OOMScoreAdj",
  180. "PodCIDR",
  181. "PodPidsLimit",
  182. "PodsPerCore",
  183. "Port",
  184. "ProtectKernelDefaults",
  185. "ReadOnlyPort",
  186. "RegistryBurst",
  187. "RegistryPullQPS",
  188. "RuntimeRequestTimeout.Duration",
  189. "SerializeImagePulls",
  190. "StreamingConnectionIdleTimeout.Duration",
  191. "SyncFrequency.Duration",
  192. "SystemCgroups",
  193. "SystemReservedCgroup",
  194. "SystemReserved[*]",
  195. "TypeMeta.APIVersion",
  196. "TypeMeta.Kind",
  197. "VolumeStatsAggPeriod.Duration",
  198. )
  199. )