validation_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 validation
  14. import (
  15. "testing"
  16. "k8s.io/apimachinery/pkg/api/resource"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. utilfeature "k8s.io/apiserver/pkg/util/feature"
  19. featuregatetesting "k8s.io/component-base/featuregate/testing"
  20. "k8s.io/kubernetes/pkg/apis/core"
  21. "k8s.io/kubernetes/pkg/apis/node"
  22. "k8s.io/kubernetes/pkg/features"
  23. utilpointer "k8s.io/utils/pointer"
  24. "github.com/stretchr/testify/assert"
  25. )
  26. func TestValidateRuntimeClass(t *testing.T) {
  27. tests := []struct {
  28. name string
  29. rc node.RuntimeClass
  30. expectError bool
  31. }{{
  32. name: "invalid name",
  33. expectError: true,
  34. rc: node.RuntimeClass{
  35. ObjectMeta: metav1.ObjectMeta{Name: "&!@#"},
  36. Handler: "foo",
  37. },
  38. }, {
  39. name: "invalid Handler name",
  40. expectError: true,
  41. rc: node.RuntimeClass{
  42. ObjectMeta: metav1.ObjectMeta{Name: "foo"},
  43. Handler: "&@#$",
  44. },
  45. }, {
  46. name: "invalid empty RuntimeClass",
  47. expectError: true,
  48. rc: node.RuntimeClass{
  49. ObjectMeta: metav1.ObjectMeta{Name: "empty"},
  50. },
  51. }, {
  52. name: "valid Handler",
  53. expectError: false,
  54. rc: node.RuntimeClass{
  55. ObjectMeta: metav1.ObjectMeta{Name: "foo"},
  56. Handler: "bar-baz",
  57. },
  58. }}
  59. for _, test := range tests {
  60. t.Run(test.name, func(t *testing.T) {
  61. errs := ValidateRuntimeClass(&test.rc)
  62. if test.expectError {
  63. assert.NotEmpty(t, errs)
  64. } else {
  65. assert.Empty(t, errs)
  66. }
  67. })
  68. }
  69. }
  70. func TestValidateRuntimeUpdate(t *testing.T) {
  71. old := node.RuntimeClass{
  72. ObjectMeta: metav1.ObjectMeta{Name: "foo"},
  73. Handler: "bar",
  74. }
  75. tests := []struct {
  76. name string
  77. expectError bool
  78. old, new node.RuntimeClass
  79. }{{
  80. name: "valid metadata update",
  81. old: old,
  82. new: node.RuntimeClass{
  83. ObjectMeta: metav1.ObjectMeta{
  84. Name: "foo",
  85. Labels: map[string]string{"foo": "bar"},
  86. },
  87. Handler: "bar",
  88. },
  89. }, {
  90. name: "invalid metadata update",
  91. expectError: true,
  92. old: old,
  93. new: node.RuntimeClass{
  94. ObjectMeta: metav1.ObjectMeta{
  95. Name: "empty",
  96. ClusterName: "somethingelse", // immutable
  97. },
  98. Handler: "bar",
  99. },
  100. }, {
  101. name: "invalid Handler update",
  102. expectError: true,
  103. old: old,
  104. new: node.RuntimeClass{
  105. ObjectMeta: metav1.ObjectMeta{Name: "foo"},
  106. Handler: "somethingelse",
  107. },
  108. }}
  109. for _, test := range tests {
  110. t.Run(test.name, func(t *testing.T) {
  111. // So we don't need to write it in every test case...
  112. test.old.ObjectMeta.ResourceVersion = "1"
  113. test.new.ObjectMeta.ResourceVersion = "1"
  114. errs := ValidateRuntimeClassUpdate(&test.new, &test.old)
  115. if test.expectError {
  116. assert.NotEmpty(t, errs)
  117. } else {
  118. assert.Empty(t, errs)
  119. }
  120. })
  121. }
  122. }
  123. func TestValidateOverhead(t *testing.T) {
  124. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.PodOverhead, true)()
  125. successCase := []struct {
  126. Name string
  127. overhead *node.Overhead
  128. }{
  129. {
  130. Name: "Overhead with valid cpu and memory resources",
  131. overhead: &node.Overhead{
  132. PodFixed: core.ResourceList{
  133. core.ResourceName(core.ResourceCPU): resource.MustParse("10"),
  134. core.ResourceName(core.ResourceMemory): resource.MustParse("10G"),
  135. },
  136. },
  137. },
  138. }
  139. for _, tc := range successCase {
  140. rc := &node.RuntimeClass{
  141. ObjectMeta: metav1.ObjectMeta{Name: "foo"},
  142. Handler: "bar",
  143. Overhead: tc.overhead,
  144. }
  145. if errs := ValidateRuntimeClass(rc); len(errs) != 0 {
  146. t.Errorf("%q unexpected error: %v", tc.Name, errs)
  147. }
  148. }
  149. errorCase := []struct {
  150. Name string
  151. overhead *node.Overhead
  152. }{
  153. {
  154. Name: "Invalid Resources",
  155. overhead: &node.Overhead{
  156. PodFixed: core.ResourceList{
  157. core.ResourceName("my.org"): resource.MustParse("10m"),
  158. },
  159. },
  160. },
  161. }
  162. for _, tc := range errorCase {
  163. rc := &node.RuntimeClass{
  164. ObjectMeta: metav1.ObjectMeta{Name: "foo"},
  165. Handler: "bar",
  166. Overhead: tc.overhead,
  167. }
  168. if errs := ValidateRuntimeClass(rc); len(errs) == 0 {
  169. t.Errorf("%q expected error", tc.Name)
  170. }
  171. }
  172. }
  173. func TestValidateScheduling(t *testing.T) {
  174. tests := []struct {
  175. name string
  176. scheduling *node.Scheduling
  177. expectErrs int
  178. }{{
  179. name: "valid scheduling",
  180. scheduling: &node.Scheduling{
  181. NodeSelector: map[string]string{"valid": "yes"},
  182. Tolerations: []core.Toleration{{
  183. Key: "valid",
  184. Operator: core.TolerationOpExists,
  185. Effect: core.TaintEffectNoSchedule,
  186. }},
  187. },
  188. }, {
  189. name: "empty scheduling",
  190. scheduling: &node.Scheduling{},
  191. }, {
  192. name: "invalid nodeSelector",
  193. scheduling: &node.Scheduling{
  194. NodeSelector: map[string]string{"not a valid key!!!": "nope"},
  195. },
  196. expectErrs: 1,
  197. }, {
  198. name: "invalid toleration",
  199. scheduling: &node.Scheduling{
  200. Tolerations: []core.Toleration{{
  201. Key: "valid",
  202. Operator: core.TolerationOpExists,
  203. Effect: core.TaintEffectNoSchedule,
  204. }, {
  205. Key: "not a valid key!!!",
  206. Operator: core.TolerationOpExists,
  207. Effect: core.TaintEffectNoSchedule,
  208. }},
  209. },
  210. expectErrs: 1,
  211. }, {
  212. name: "duplicate tolerations",
  213. scheduling: &node.Scheduling{
  214. Tolerations: []core.Toleration{{
  215. Key: "valid",
  216. Operator: core.TolerationOpExists,
  217. Effect: core.TaintEffectNoExecute,
  218. TolerationSeconds: utilpointer.Int64Ptr(5),
  219. }, {
  220. Key: "valid",
  221. Operator: core.TolerationOpExists,
  222. Effect: core.TaintEffectNoExecute,
  223. TolerationSeconds: utilpointer.Int64Ptr(10),
  224. }},
  225. },
  226. expectErrs: 1,
  227. }, {
  228. name: "invalid scheduling",
  229. scheduling: &node.Scheduling{
  230. NodeSelector: map[string]string{"not a valid key!!!": "nope"},
  231. Tolerations: []core.Toleration{{
  232. Key: "valid",
  233. Operator: core.TolerationOpExists,
  234. Effect: core.TaintEffectNoSchedule,
  235. }, {
  236. Key: "not a valid toleration key!!!",
  237. Operator: core.TolerationOpExists,
  238. Effect: core.TaintEffectNoSchedule,
  239. }},
  240. },
  241. expectErrs: 2,
  242. }}
  243. for _, test := range tests {
  244. t.Run(test.name, func(t *testing.T) {
  245. rc := &node.RuntimeClass{
  246. ObjectMeta: metav1.ObjectMeta{Name: "foo"},
  247. Handler: "bar",
  248. Scheduling: test.scheduling,
  249. }
  250. assert.Len(t, ValidateRuntimeClass(rc), test.expectErrs)
  251. })
  252. }
  253. }