validation_test.go 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  1. /*
  2. Copyright 2019 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. "math"
  16. "testing"
  17. "github.com/google/go-cmp/cmp"
  18. "github.com/stretchr/testify/assert"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/util/validation/field"
  21. "k8s.io/apiserver/pkg/authentication/user"
  22. "k8s.io/kubernetes/pkg/apis/flowcontrol"
  23. )
  24. func TestFlowSchemaValidation(t *testing.T) {
  25. badExempt := flowcontrol.FlowSchemaSpec{
  26. MatchingPrecedence: 1,
  27. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  28. Name: flowcontrol.PriorityLevelConfigurationNameExempt,
  29. },
  30. Rules: []flowcontrol.PolicyRulesWithSubjects{
  31. {
  32. Subjects: []flowcontrol.Subject{
  33. {
  34. Kind: flowcontrol.SubjectKindGroup,
  35. Group: &flowcontrol.GroupSubject{Name: "system:masters"},
  36. },
  37. },
  38. ResourceRules: []flowcontrol.ResourcePolicyRule{
  39. {
  40. Verbs: []string{flowcontrol.VerbAll},
  41. APIGroups: []string{flowcontrol.APIGroupAll},
  42. Resources: []string{flowcontrol.ResourceAll},
  43. ClusterScope: true,
  44. Namespaces: []string{flowcontrol.NamespaceEvery},
  45. },
  46. },
  47. NonResourceRules: []flowcontrol.NonResourcePolicyRule{
  48. {
  49. Verbs: []string{flowcontrol.VerbAll},
  50. NonResourceURLs: []string{"/"},
  51. },
  52. },
  53. },
  54. },
  55. }
  56. badCatchAll := flowcontrol.FlowSchemaSpec{
  57. MatchingPrecedence: flowcontrol.FlowSchemaMaxMatchingPrecedence,
  58. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  59. Name: flowcontrol.PriorityLevelConfigurationNameCatchAll,
  60. },
  61. DistinguisherMethod: &flowcontrol.FlowDistinguisherMethod{Type: flowcontrol.FlowDistinguisherMethodByUserType},
  62. Rules: []flowcontrol.PolicyRulesWithSubjects{
  63. {
  64. Subjects: []flowcontrol.Subject{
  65. {
  66. Kind: flowcontrol.SubjectKindGroup,
  67. Group: &flowcontrol.GroupSubject{Name: user.AllUnauthenticated},
  68. },
  69. {
  70. Kind: flowcontrol.SubjectKindGroup,
  71. Group: &flowcontrol.GroupSubject{Name: user.AllAuthenticated},
  72. },
  73. },
  74. ResourceRules: []flowcontrol.ResourcePolicyRule{
  75. {
  76. Verbs: []string{flowcontrol.VerbAll},
  77. APIGroups: []string{flowcontrol.APIGroupAll},
  78. Resources: []string{flowcontrol.ResourceAll},
  79. ClusterScope: true,
  80. Namespaces: []string{flowcontrol.NamespaceEvery},
  81. },
  82. },
  83. NonResourceRules: []flowcontrol.NonResourcePolicyRule{
  84. {
  85. Verbs: []string{flowcontrol.VerbAll},
  86. NonResourceURLs: []string{"/"},
  87. },
  88. },
  89. },
  90. },
  91. }
  92. testCases := []struct {
  93. name string
  94. flowSchema *flowcontrol.FlowSchema
  95. expectedErrors field.ErrorList
  96. }{
  97. {
  98. name: "missing both resource and non-resource policy-rule should fail",
  99. flowSchema: &flowcontrol.FlowSchema{
  100. ObjectMeta: metav1.ObjectMeta{
  101. Name: "system-foo",
  102. },
  103. Spec: flowcontrol.FlowSchemaSpec{
  104. MatchingPrecedence: 50,
  105. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  106. Name: "system-bar",
  107. },
  108. Rules: []flowcontrol.PolicyRulesWithSubjects{
  109. {
  110. Subjects: []flowcontrol.Subject{
  111. {
  112. Kind: flowcontrol.SubjectKindUser,
  113. User: &flowcontrol.UserSubject{Name: "noxu"},
  114. },
  115. },
  116. },
  117. },
  118. },
  119. },
  120. expectedErrors: field.ErrorList{
  121. field.Required(field.NewPath("spec").Child("rules").Index(0), "at least one of resourceRules and nonResourceRules has to be non-empty"),
  122. },
  123. },
  124. {
  125. name: "normal flow-schema w/ * verbs/apiGroups/resources should work",
  126. flowSchema: &flowcontrol.FlowSchema{
  127. ObjectMeta: metav1.ObjectMeta{
  128. Name: "system-foo",
  129. },
  130. Spec: flowcontrol.FlowSchemaSpec{
  131. MatchingPrecedence: 50,
  132. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  133. Name: "system-bar",
  134. },
  135. Rules: []flowcontrol.PolicyRulesWithSubjects{
  136. {
  137. Subjects: []flowcontrol.Subject{
  138. {
  139. Kind: flowcontrol.SubjectKindGroup,
  140. Group: &flowcontrol.GroupSubject{Name: "noxu"},
  141. },
  142. },
  143. ResourceRules: []flowcontrol.ResourcePolicyRule{
  144. {
  145. Verbs: []string{flowcontrol.VerbAll},
  146. APIGroups: []string{flowcontrol.APIGroupAll},
  147. Resources: []string{flowcontrol.ResourceAll},
  148. Namespaces: []string{flowcontrol.NamespaceEvery},
  149. },
  150. },
  151. },
  152. },
  153. },
  154. },
  155. expectedErrors: field.ErrorList{},
  156. },
  157. {
  158. name: "malformed Subject union in ServiceAccount case",
  159. flowSchema: &flowcontrol.FlowSchema{
  160. ObjectMeta: metav1.ObjectMeta{
  161. Name: "system-foo",
  162. },
  163. Spec: flowcontrol.FlowSchemaSpec{
  164. MatchingPrecedence: 50,
  165. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  166. Name: "system-bar",
  167. },
  168. Rules: []flowcontrol.PolicyRulesWithSubjects{
  169. {
  170. Subjects: []flowcontrol.Subject{
  171. {
  172. Kind: flowcontrol.SubjectKindServiceAccount,
  173. User: &flowcontrol.UserSubject{Name: "fred"},
  174. Group: &flowcontrol.GroupSubject{Name: "fred"},
  175. },
  176. },
  177. NonResourceRules: []flowcontrol.NonResourcePolicyRule{
  178. {
  179. Verbs: []string{flowcontrol.VerbAll},
  180. NonResourceURLs: []string{"*"},
  181. },
  182. },
  183. },
  184. },
  185. },
  186. },
  187. expectedErrors: field.ErrorList{
  188. field.Required(field.NewPath("spec").Child("rules").Index(0).Child("subjects").Index(0).Child("serviceAccount"), "serviceAccount is required when subject kind is 'ServiceAccount'"),
  189. field.Forbidden(field.NewPath("spec").Child("rules").Index(0).Child("subjects").Index(0).Child("user"), "user is forbidden when subject kind is not 'User'"),
  190. field.Forbidden(field.NewPath("spec").Child("rules").Index(0).Child("subjects").Index(0).Child("group"), "group is forbidden when subject kind is not 'Group'"),
  191. },
  192. },
  193. {
  194. name: "Subject union malformed in User case",
  195. flowSchema: &flowcontrol.FlowSchema{
  196. ObjectMeta: metav1.ObjectMeta{
  197. Name: "system-foo",
  198. },
  199. Spec: flowcontrol.FlowSchemaSpec{
  200. MatchingPrecedence: 50,
  201. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  202. Name: "system-bar",
  203. },
  204. Rules: []flowcontrol.PolicyRulesWithSubjects{
  205. {
  206. Subjects: []flowcontrol.Subject{
  207. {
  208. Kind: flowcontrol.SubjectKindUser,
  209. Group: &flowcontrol.GroupSubject{Name: "fred"},
  210. ServiceAccount: &flowcontrol.ServiceAccountSubject{Namespace: "s", Name: "n"},
  211. },
  212. },
  213. NonResourceRules: []flowcontrol.NonResourcePolicyRule{
  214. {
  215. Verbs: []string{flowcontrol.VerbAll},
  216. NonResourceURLs: []string{"*"},
  217. },
  218. },
  219. },
  220. },
  221. },
  222. },
  223. expectedErrors: field.ErrorList{
  224. field.Forbidden(field.NewPath("spec").Child("rules").Index(0).Child("subjects").Index(0).Child("serviceAccount"), "serviceAccount is forbidden when subject kind is not 'ServiceAccount'"),
  225. field.Required(field.NewPath("spec").Child("rules").Index(0).Child("subjects").Index(0).Child("user"), "user is required when subject kind is 'User'"),
  226. field.Forbidden(field.NewPath("spec").Child("rules").Index(0).Child("subjects").Index(0).Child("group"), "group is forbidden when subject kind is not 'Group'"),
  227. },
  228. },
  229. {
  230. name: "malformed Subject union in Group case",
  231. flowSchema: &flowcontrol.FlowSchema{
  232. ObjectMeta: metav1.ObjectMeta{
  233. Name: "system-foo",
  234. },
  235. Spec: flowcontrol.FlowSchemaSpec{
  236. MatchingPrecedence: 50,
  237. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  238. Name: "system-bar",
  239. },
  240. Rules: []flowcontrol.PolicyRulesWithSubjects{
  241. {
  242. Subjects: []flowcontrol.Subject{
  243. {
  244. Kind: flowcontrol.SubjectKindGroup,
  245. User: &flowcontrol.UserSubject{Name: "fred"},
  246. ServiceAccount: &flowcontrol.ServiceAccountSubject{Namespace: "s", Name: "n"},
  247. },
  248. },
  249. NonResourceRules: []flowcontrol.NonResourcePolicyRule{
  250. {
  251. Verbs: []string{flowcontrol.VerbAll},
  252. NonResourceURLs: []string{"*"},
  253. },
  254. },
  255. },
  256. },
  257. },
  258. },
  259. expectedErrors: field.ErrorList{
  260. field.Forbidden(field.NewPath("spec").Child("rules").Index(0).Child("subjects").Index(0).Child("serviceAccount"), "serviceAccount is forbidden when subject kind is not 'ServiceAccount'"),
  261. field.Forbidden(field.NewPath("spec").Child("rules").Index(0).Child("subjects").Index(0).Child("user"), "user is forbidden when subject kind is not 'User'"),
  262. field.Required(field.NewPath("spec").Child("rules").Index(0).Child("subjects").Index(0).Child("group"), "group is required when subject kind is 'Group'"),
  263. },
  264. },
  265. {
  266. name: "exempt flow-schema should work",
  267. flowSchema: &flowcontrol.FlowSchema{
  268. ObjectMeta: metav1.ObjectMeta{
  269. Name: flowcontrol.FlowSchemaNameExempt,
  270. },
  271. Spec: flowcontrol.FlowSchemaSpec{
  272. MatchingPrecedence: 1,
  273. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  274. Name: flowcontrol.PriorityLevelConfigurationNameExempt,
  275. },
  276. Rules: []flowcontrol.PolicyRulesWithSubjects{
  277. {
  278. Subjects: []flowcontrol.Subject{
  279. {
  280. Kind: flowcontrol.SubjectKindGroup,
  281. Group: &flowcontrol.GroupSubject{Name: "system:masters"},
  282. },
  283. },
  284. ResourceRules: []flowcontrol.ResourcePolicyRule{
  285. {
  286. Verbs: []string{flowcontrol.VerbAll},
  287. APIGroups: []string{flowcontrol.APIGroupAll},
  288. Resources: []string{flowcontrol.ResourceAll},
  289. ClusterScope: true,
  290. Namespaces: []string{flowcontrol.NamespaceEvery},
  291. },
  292. },
  293. NonResourceRules: []flowcontrol.NonResourcePolicyRule{
  294. {
  295. Verbs: []string{flowcontrol.VerbAll},
  296. NonResourceURLs: []string{"*"},
  297. },
  298. },
  299. },
  300. },
  301. },
  302. },
  303. expectedErrors: field.ErrorList{},
  304. },
  305. {
  306. name: "bad exempt flow-schema should fail",
  307. flowSchema: &flowcontrol.FlowSchema{
  308. ObjectMeta: metav1.ObjectMeta{
  309. Name: flowcontrol.FlowSchemaNameExempt,
  310. },
  311. Spec: badExempt,
  312. },
  313. expectedErrors: field.ErrorList{field.Invalid(field.NewPath("spec"), badExempt, "spec of 'exempt' must equal the fixed value")},
  314. },
  315. {
  316. name: "bad catch-all flow-schema should fail",
  317. flowSchema: &flowcontrol.FlowSchema{
  318. ObjectMeta: metav1.ObjectMeta{
  319. Name: flowcontrol.FlowSchemaNameCatchAll,
  320. },
  321. Spec: badCatchAll,
  322. },
  323. expectedErrors: field.ErrorList{field.Invalid(field.NewPath("spec"), badCatchAll, "spec of 'catch-all' must equal the fixed value")},
  324. },
  325. {
  326. name: "catch-all flow-schema should work",
  327. flowSchema: &flowcontrol.FlowSchema{
  328. ObjectMeta: metav1.ObjectMeta{
  329. Name: flowcontrol.FlowSchemaNameCatchAll,
  330. },
  331. Spec: flowcontrol.FlowSchemaSpec{
  332. MatchingPrecedence: 10000,
  333. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  334. Name: flowcontrol.PriorityLevelConfigurationNameCatchAll,
  335. },
  336. DistinguisherMethod: &flowcontrol.FlowDistinguisherMethod{Type: flowcontrol.FlowDistinguisherMethodByUserType},
  337. Rules: []flowcontrol.PolicyRulesWithSubjects{
  338. {
  339. Subjects: []flowcontrol.Subject{
  340. {
  341. Kind: flowcontrol.SubjectKindGroup,
  342. Group: &flowcontrol.GroupSubject{Name: user.AllUnauthenticated},
  343. },
  344. {
  345. Kind: flowcontrol.SubjectKindGroup,
  346. Group: &flowcontrol.GroupSubject{Name: user.AllAuthenticated},
  347. },
  348. },
  349. ResourceRules: []flowcontrol.ResourcePolicyRule{
  350. {
  351. Verbs: []string{flowcontrol.VerbAll},
  352. APIGroups: []string{flowcontrol.APIGroupAll},
  353. Resources: []string{flowcontrol.ResourceAll},
  354. ClusterScope: true,
  355. Namespaces: []string{flowcontrol.NamespaceEvery},
  356. },
  357. },
  358. NonResourceRules: []flowcontrol.NonResourcePolicyRule{
  359. {
  360. Verbs: []string{flowcontrol.VerbAll},
  361. NonResourceURLs: []string{"*"},
  362. },
  363. },
  364. },
  365. },
  366. },
  367. },
  368. expectedErrors: field.ErrorList{},
  369. },
  370. {
  371. name: "non-exempt flow-schema with matchingPrecedence==1 should fail",
  372. flowSchema: &flowcontrol.FlowSchema{
  373. ObjectMeta: metav1.ObjectMeta{
  374. Name: "fred",
  375. },
  376. Spec: flowcontrol.FlowSchemaSpec{
  377. MatchingPrecedence: 1,
  378. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  379. Name: "exempt",
  380. },
  381. Rules: []flowcontrol.PolicyRulesWithSubjects{
  382. {
  383. Subjects: []flowcontrol.Subject{
  384. {
  385. Kind: flowcontrol.SubjectKindGroup,
  386. Group: &flowcontrol.GroupSubject{Name: "gorp"},
  387. },
  388. },
  389. NonResourceRules: []flowcontrol.NonResourcePolicyRule{
  390. {
  391. Verbs: []string{flowcontrol.VerbAll},
  392. NonResourceURLs: []string{"*"},
  393. },
  394. },
  395. },
  396. },
  397. },
  398. },
  399. expectedErrors: field.ErrorList{
  400. field.Invalid(field.NewPath("spec").Child("matchingPrecedence"), int32(1), "only the schema named 'exempt' may have matchingPrecedence 1")},
  401. },
  402. {
  403. name: "flow-schema mixes * verbs/apiGroups/resources should fail",
  404. flowSchema: &flowcontrol.FlowSchema{
  405. ObjectMeta: metav1.ObjectMeta{
  406. Name: "system-foo",
  407. },
  408. Spec: flowcontrol.FlowSchemaSpec{
  409. MatchingPrecedence: 50,
  410. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  411. Name: "system-bar",
  412. },
  413. Rules: []flowcontrol.PolicyRulesWithSubjects{
  414. {
  415. Subjects: []flowcontrol.Subject{
  416. {
  417. Kind: flowcontrol.SubjectKindUser,
  418. User: &flowcontrol.UserSubject{Name: "noxu"},
  419. },
  420. },
  421. ResourceRules: []flowcontrol.ResourcePolicyRule{
  422. {
  423. Verbs: []string{flowcontrol.VerbAll, "create"},
  424. APIGroups: []string{flowcontrol.APIGroupAll, "tak"},
  425. Resources: []string{flowcontrol.ResourceAll, "tok"},
  426. Namespaces: []string{flowcontrol.NamespaceEvery},
  427. },
  428. },
  429. },
  430. },
  431. },
  432. },
  433. expectedErrors: field.ErrorList{
  434. field.Invalid(field.NewPath("spec").Child("rules").Index(0).Child("resourceRules").Index(0).Child("verbs"), []string{"*", "create"}, "if '*' is present, must not specify other verbs"),
  435. field.Invalid(field.NewPath("spec").Child("rules").Index(0).Child("resourceRules").Index(0).Child("apiGroups"), []string{"*", "tak"}, "if '*' is present, must not specify other api groups"),
  436. field.Invalid(field.NewPath("spec").Child("rules").Index(0).Child("resourceRules").Index(0).Child("resources"), []string{"*", "tok"}, "if '*' is present, must not specify other resources"),
  437. },
  438. },
  439. {
  440. name: "flow-schema has both resource rules and non-resource rules should work",
  441. flowSchema: &flowcontrol.FlowSchema{
  442. ObjectMeta: metav1.ObjectMeta{
  443. Name: "system-foo",
  444. },
  445. Spec: flowcontrol.FlowSchemaSpec{
  446. MatchingPrecedence: 50,
  447. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  448. Name: "system-bar",
  449. },
  450. Rules: []flowcontrol.PolicyRulesWithSubjects{
  451. {
  452. Subjects: []flowcontrol.Subject{
  453. {
  454. Kind: flowcontrol.SubjectKindUser,
  455. User: &flowcontrol.UserSubject{Name: "noxu"},
  456. },
  457. },
  458. ResourceRules: []flowcontrol.ResourcePolicyRule{
  459. {
  460. Verbs: []string{flowcontrol.VerbAll},
  461. APIGroups: []string{flowcontrol.APIGroupAll},
  462. Resources: []string{flowcontrol.ResourceAll},
  463. Namespaces: []string{flowcontrol.NamespaceEvery},
  464. },
  465. },
  466. NonResourceRules: []flowcontrol.NonResourcePolicyRule{
  467. {
  468. Verbs: []string{flowcontrol.VerbAll},
  469. NonResourceURLs: []string{"/apis/*"},
  470. },
  471. },
  472. },
  473. },
  474. },
  475. },
  476. expectedErrors: field.ErrorList{},
  477. },
  478. {
  479. name: "flow-schema mixes * non-resource URLs should fail",
  480. flowSchema: &flowcontrol.FlowSchema{
  481. ObjectMeta: metav1.ObjectMeta{
  482. Name: "system-foo",
  483. },
  484. Spec: flowcontrol.FlowSchemaSpec{
  485. MatchingPrecedence: 50,
  486. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  487. Name: "system-bar",
  488. },
  489. Rules: []flowcontrol.PolicyRulesWithSubjects{
  490. {
  491. Subjects: []flowcontrol.Subject{
  492. {
  493. Kind: flowcontrol.SubjectKindUser,
  494. User: &flowcontrol.UserSubject{Name: "noxu"},
  495. },
  496. },
  497. NonResourceRules: []flowcontrol.NonResourcePolicyRule{
  498. {
  499. Verbs: []string{"*"},
  500. NonResourceURLs: []string{flowcontrol.NonResourceAll, "tik"},
  501. },
  502. },
  503. },
  504. },
  505. },
  506. },
  507. expectedErrors: field.ErrorList{
  508. field.Invalid(field.NewPath("spec").Child("rules").Index(0).Child("nonResourceRules").Index(0).Child("nonResourceURLs"), []string{"*", "tik"}, "if '*' is present, must not specify other non-resource URLs"),
  509. },
  510. },
  511. {
  512. name: "invalid subject kind should fail",
  513. flowSchema: &flowcontrol.FlowSchema{
  514. ObjectMeta: metav1.ObjectMeta{
  515. Name: "system-foo",
  516. },
  517. Spec: flowcontrol.FlowSchemaSpec{
  518. MatchingPrecedence: 50,
  519. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  520. Name: "system-bar",
  521. },
  522. Rules: []flowcontrol.PolicyRulesWithSubjects{
  523. {
  524. Subjects: []flowcontrol.Subject{
  525. {
  526. Kind: "FooKind",
  527. },
  528. },
  529. NonResourceRules: []flowcontrol.NonResourcePolicyRule{
  530. {
  531. Verbs: []string{"*"},
  532. NonResourceURLs: []string{flowcontrol.NonResourceAll},
  533. },
  534. },
  535. },
  536. },
  537. },
  538. },
  539. expectedErrors: field.ErrorList{
  540. field.NotSupported(field.NewPath("spec").Child("rules").Index(0).Child("subjects").Index(0).Child("kind"), flowcontrol.SubjectKind("FooKind"), supportedSubjectKinds.List()),
  541. },
  542. },
  543. {
  544. name: "flow-schema w/ invalid verb should fail",
  545. flowSchema: &flowcontrol.FlowSchema{
  546. ObjectMeta: metav1.ObjectMeta{
  547. Name: "system-foo",
  548. },
  549. Spec: flowcontrol.FlowSchemaSpec{
  550. MatchingPrecedence: 50,
  551. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  552. Name: "system-bar",
  553. },
  554. Rules: []flowcontrol.PolicyRulesWithSubjects{
  555. {
  556. Subjects: []flowcontrol.Subject{
  557. {
  558. Kind: flowcontrol.SubjectKindUser,
  559. User: &flowcontrol.UserSubject{Name: "noxu"},
  560. },
  561. },
  562. ResourceRules: []flowcontrol.ResourcePolicyRule{
  563. {
  564. Verbs: []string{"feed"},
  565. APIGroups: []string{flowcontrol.APIGroupAll},
  566. Resources: []string{flowcontrol.ResourceAll},
  567. Namespaces: []string{flowcontrol.NamespaceEvery},
  568. },
  569. },
  570. },
  571. },
  572. },
  573. },
  574. expectedErrors: field.ErrorList{
  575. field.NotSupported(field.NewPath("spec").Child("rules").Index(0).Child("resourceRules").Index(0).Child("verbs"), []string{"feed"}, supportedVerbs.List()),
  576. },
  577. },
  578. {
  579. name: "flow-schema w/ invalid priority level configuration name should fail",
  580. flowSchema: &flowcontrol.FlowSchema{
  581. ObjectMeta: metav1.ObjectMeta{
  582. Name: "system-foo",
  583. },
  584. Spec: flowcontrol.FlowSchemaSpec{
  585. MatchingPrecedence: 50,
  586. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  587. Name: "system+++$$",
  588. },
  589. Rules: []flowcontrol.PolicyRulesWithSubjects{
  590. {
  591. Subjects: []flowcontrol.Subject{
  592. {
  593. Kind: flowcontrol.SubjectKindUser,
  594. User: &flowcontrol.UserSubject{Name: "noxu"},
  595. },
  596. },
  597. ResourceRules: []flowcontrol.ResourcePolicyRule{
  598. {
  599. Verbs: []string{flowcontrol.VerbAll},
  600. APIGroups: []string{flowcontrol.APIGroupAll},
  601. Resources: []string{flowcontrol.ResourceAll},
  602. Namespaces: []string{flowcontrol.NamespaceEvery},
  603. },
  604. },
  605. },
  606. },
  607. },
  608. },
  609. expectedErrors: field.ErrorList{
  610. field.Invalid(field.NewPath("spec").Child("priorityLevelConfiguration").Child("name"), "system+++$$", `a DNS-1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')`),
  611. },
  612. },
  613. {
  614. name: "flow-schema w/ service-account kind missing namespace should fail",
  615. flowSchema: &flowcontrol.FlowSchema{
  616. ObjectMeta: metav1.ObjectMeta{
  617. Name: "system-foo",
  618. },
  619. Spec: flowcontrol.FlowSchemaSpec{
  620. MatchingPrecedence: 50,
  621. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  622. Name: "system-bar",
  623. },
  624. Rules: []flowcontrol.PolicyRulesWithSubjects{
  625. {
  626. Subjects: []flowcontrol.Subject{
  627. {
  628. Kind: flowcontrol.SubjectKindServiceAccount,
  629. ServiceAccount: &flowcontrol.ServiceAccountSubject{
  630. Name: "noxu",
  631. },
  632. },
  633. },
  634. ResourceRules: []flowcontrol.ResourcePolicyRule{
  635. {
  636. Verbs: []string{flowcontrol.VerbAll},
  637. APIGroups: []string{flowcontrol.APIGroupAll},
  638. Resources: []string{flowcontrol.ResourceAll},
  639. Namespaces: []string{flowcontrol.NamespaceEvery},
  640. },
  641. },
  642. },
  643. },
  644. },
  645. },
  646. expectedErrors: field.ErrorList{
  647. field.Required(field.NewPath("spec").Child("rules").Index(0).Child("subjects").Index(0).Child("serviceAccount").Child("namespace"), "must specify namespace for service account"),
  648. },
  649. },
  650. {
  651. name: "flow-schema missing kind should fail",
  652. flowSchema: &flowcontrol.FlowSchema{
  653. ObjectMeta: metav1.ObjectMeta{
  654. Name: "system-foo",
  655. },
  656. Spec: flowcontrol.FlowSchemaSpec{
  657. MatchingPrecedence: 50,
  658. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  659. Name: "system-bar",
  660. },
  661. Rules: []flowcontrol.PolicyRulesWithSubjects{
  662. {
  663. Subjects: []flowcontrol.Subject{
  664. {
  665. Kind: "",
  666. },
  667. },
  668. ResourceRules: []flowcontrol.ResourcePolicyRule{
  669. {
  670. Verbs: []string{flowcontrol.VerbAll},
  671. APIGroups: []string{flowcontrol.APIGroupAll},
  672. Resources: []string{flowcontrol.ResourceAll},
  673. Namespaces: []string{flowcontrol.NamespaceEvery},
  674. },
  675. },
  676. },
  677. },
  678. },
  679. },
  680. expectedErrors: field.ErrorList{
  681. field.NotSupported(field.NewPath("spec").Child("rules").Index(0).Child("subjects").Index(0).Child("kind"), flowcontrol.SubjectKind(""), supportedSubjectKinds.List()),
  682. },
  683. },
  684. {
  685. name: "Omitted ResourceRule.Namespaces should fail",
  686. flowSchema: &flowcontrol.FlowSchema{
  687. ObjectMeta: metav1.ObjectMeta{
  688. Name: "system-foo",
  689. },
  690. Spec: flowcontrol.FlowSchemaSpec{
  691. MatchingPrecedence: 50,
  692. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  693. Name: "system-bar",
  694. },
  695. Rules: []flowcontrol.PolicyRulesWithSubjects{
  696. {
  697. Subjects: []flowcontrol.Subject{
  698. {
  699. Kind: flowcontrol.SubjectKindUser,
  700. User: &flowcontrol.UserSubject{Name: "noxu"},
  701. },
  702. },
  703. ResourceRules: []flowcontrol.ResourcePolicyRule{
  704. {
  705. Verbs: []string{flowcontrol.VerbAll},
  706. APIGroups: []string{flowcontrol.APIGroupAll},
  707. Resources: []string{flowcontrol.ResourceAll},
  708. Namespaces: nil,
  709. },
  710. },
  711. },
  712. },
  713. },
  714. },
  715. expectedErrors: field.ErrorList{
  716. field.Required(field.NewPath("spec").Child("rules").Index(0).Child("resourceRules").Index(0).Child("namespaces"), "resource rules that are not cluster scoped must supply at least one namespace"),
  717. },
  718. },
  719. {
  720. name: "ClusterScope is allowed, with no Namespaces",
  721. flowSchema: &flowcontrol.FlowSchema{
  722. ObjectMeta: metav1.ObjectMeta{
  723. Name: "system-foo",
  724. },
  725. Spec: flowcontrol.FlowSchemaSpec{
  726. MatchingPrecedence: 50,
  727. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  728. Name: "system-bar",
  729. },
  730. Rules: []flowcontrol.PolicyRulesWithSubjects{
  731. {
  732. Subjects: []flowcontrol.Subject{
  733. {
  734. Kind: flowcontrol.SubjectKindUser,
  735. User: &flowcontrol.UserSubject{Name: "noxu"},
  736. },
  737. },
  738. ResourceRules: []flowcontrol.ResourcePolicyRule{
  739. {
  740. Verbs: []string{flowcontrol.VerbAll},
  741. APIGroups: []string{flowcontrol.APIGroupAll},
  742. Resources: []string{flowcontrol.ResourceAll},
  743. ClusterScope: true,
  744. },
  745. },
  746. },
  747. },
  748. },
  749. },
  750. expectedErrors: field.ErrorList{},
  751. },
  752. {
  753. name: "ClusterScope is allowed with NamespaceEvery",
  754. flowSchema: &flowcontrol.FlowSchema{
  755. ObjectMeta: metav1.ObjectMeta{
  756. Name: "system-foo",
  757. },
  758. Spec: flowcontrol.FlowSchemaSpec{
  759. MatchingPrecedence: 50,
  760. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  761. Name: "system-bar",
  762. },
  763. Rules: []flowcontrol.PolicyRulesWithSubjects{
  764. {
  765. Subjects: []flowcontrol.Subject{
  766. {
  767. Kind: flowcontrol.SubjectKindUser,
  768. User: &flowcontrol.UserSubject{Name: "noxu"},
  769. },
  770. },
  771. ResourceRules: []flowcontrol.ResourcePolicyRule{
  772. {
  773. Verbs: []string{flowcontrol.VerbAll},
  774. APIGroups: []string{flowcontrol.APIGroupAll},
  775. Resources: []string{flowcontrol.ResourceAll},
  776. ClusterScope: true,
  777. Namespaces: []string{flowcontrol.NamespaceEvery},
  778. },
  779. },
  780. },
  781. },
  782. },
  783. },
  784. expectedErrors: field.ErrorList{},
  785. },
  786. {
  787. name: "NamespaceEvery may not be combined with particulars",
  788. flowSchema: &flowcontrol.FlowSchema{
  789. ObjectMeta: metav1.ObjectMeta{
  790. Name: "system-foo",
  791. },
  792. Spec: flowcontrol.FlowSchemaSpec{
  793. MatchingPrecedence: 50,
  794. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  795. Name: "system-bar",
  796. },
  797. Rules: []flowcontrol.PolicyRulesWithSubjects{
  798. {
  799. Subjects: []flowcontrol.Subject{
  800. {
  801. Kind: flowcontrol.SubjectKindUser,
  802. User: &flowcontrol.UserSubject{Name: "noxu"},
  803. },
  804. },
  805. ResourceRules: []flowcontrol.ResourcePolicyRule{
  806. {
  807. Verbs: []string{flowcontrol.VerbAll},
  808. APIGroups: []string{flowcontrol.APIGroupAll},
  809. Resources: []string{flowcontrol.ResourceAll},
  810. Namespaces: []string{"foo", flowcontrol.NamespaceEvery},
  811. },
  812. },
  813. },
  814. },
  815. },
  816. },
  817. expectedErrors: field.ErrorList{
  818. field.Invalid(field.NewPath("spec").Child("rules").Index(0).Child("resourceRules").Index(0).Child("namespaces"), []string{"foo", flowcontrol.NamespaceEvery}, "if '*' is present, must not specify other namespaces"),
  819. },
  820. },
  821. {
  822. name: "ResourceRule.Namespaces must be well formed",
  823. flowSchema: &flowcontrol.FlowSchema{
  824. ObjectMeta: metav1.ObjectMeta{
  825. Name: "system-foo",
  826. },
  827. Spec: flowcontrol.FlowSchemaSpec{
  828. MatchingPrecedence: 50,
  829. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  830. Name: "system-bar",
  831. },
  832. Rules: []flowcontrol.PolicyRulesWithSubjects{
  833. {
  834. Subjects: []flowcontrol.Subject{
  835. {
  836. Kind: flowcontrol.SubjectKindUser,
  837. User: &flowcontrol.UserSubject{Name: "noxu"},
  838. },
  839. },
  840. ResourceRules: []flowcontrol.ResourcePolicyRule{
  841. {
  842. Verbs: []string{flowcontrol.VerbAll},
  843. APIGroups: []string{flowcontrol.APIGroupAll},
  844. Resources: []string{flowcontrol.ResourceAll},
  845. Namespaces: []string{"-foo"},
  846. },
  847. },
  848. },
  849. },
  850. },
  851. },
  852. expectedErrors: field.ErrorList{
  853. field.Invalid(field.NewPath("spec").Child("rules").Index(0).Child("resourceRules").Index(0).Child("namespaces").Index(0), "-foo", nsErrIntro+`a DNS-1123 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name', or '123-abc', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?')`),
  854. },
  855. },
  856. {
  857. name: "MatchingPrecedence must not be greater than 10000",
  858. flowSchema: &flowcontrol.FlowSchema{
  859. ObjectMeta: metav1.ObjectMeta{
  860. Name: "system-foo",
  861. },
  862. Spec: flowcontrol.FlowSchemaSpec{
  863. MatchingPrecedence: 10001,
  864. PriorityLevelConfiguration: flowcontrol.PriorityLevelConfigurationReference{
  865. Name: "system-bar",
  866. },
  867. Rules: []flowcontrol.PolicyRulesWithSubjects{
  868. {
  869. Subjects: []flowcontrol.Subject{
  870. {
  871. Kind: flowcontrol.SubjectKindUser,
  872. User: &flowcontrol.UserSubject{Name: "noxu"},
  873. },
  874. },
  875. ResourceRules: []flowcontrol.ResourcePolicyRule{
  876. {
  877. Verbs: []string{flowcontrol.VerbAll},
  878. APIGroups: []string{flowcontrol.APIGroupAll},
  879. Resources: []string{flowcontrol.ResourceAll},
  880. Namespaces: []string{flowcontrol.NamespaceEvery},
  881. },
  882. },
  883. },
  884. },
  885. },
  886. },
  887. expectedErrors: field.ErrorList{
  888. field.Invalid(field.NewPath("spec").Child("matchingPrecedence"), int32(10001), "must not be greater than 10000"),
  889. },
  890. },
  891. }
  892. for _, testCase := range testCases {
  893. t.Run(testCase.name, func(t *testing.T) {
  894. errs := ValidateFlowSchema(testCase.flowSchema)
  895. if !assert.ElementsMatch(t, testCase.expectedErrors, errs) {
  896. t.Logf("mismatch: %v", cmp.Diff(testCase.expectedErrors, errs))
  897. }
  898. })
  899. }
  900. }
  901. func TestPriorityLevelConfigurationValidation(t *testing.T) {
  902. badSpec := flowcontrol.PriorityLevelConfigurationSpec{
  903. Type: flowcontrol.PriorityLevelEnablementLimited,
  904. Limited: &flowcontrol.LimitedPriorityLevelConfiguration{
  905. AssuredConcurrencyShares: 42,
  906. LimitResponse: flowcontrol.LimitResponse{
  907. Type: flowcontrol.LimitResponseTypeReject},
  908. },
  909. }
  910. testCases := []struct {
  911. name string
  912. priorityLevelConfiguration *flowcontrol.PriorityLevelConfiguration
  913. expectedErrors field.ErrorList
  914. }{
  915. {
  916. name: "exempt should work",
  917. priorityLevelConfiguration: &flowcontrol.PriorityLevelConfiguration{
  918. ObjectMeta: metav1.ObjectMeta{
  919. Name: flowcontrol.PriorityLevelConfigurationNameExempt,
  920. },
  921. Spec: flowcontrol.PriorityLevelConfigurationSpec{
  922. Type: flowcontrol.PriorityLevelEnablementExempt,
  923. },
  924. },
  925. expectedErrors: field.ErrorList{},
  926. },
  927. {
  928. name: "wrong exempt spec should fail",
  929. priorityLevelConfiguration: &flowcontrol.PriorityLevelConfiguration{
  930. ObjectMeta: metav1.ObjectMeta{
  931. Name: flowcontrol.PriorityLevelConfigurationNameExempt,
  932. },
  933. Spec: badSpec,
  934. },
  935. expectedErrors: field.ErrorList{
  936. field.Invalid(field.NewPath("spec").Child("type"), flowcontrol.PriorityLevelEnablementLimited, "type must be 'Exempt' if and only if name is 'exempt'"),
  937. field.Invalid(field.NewPath("spec"), badSpec, "spec of 'exempt' must equal the fixed value"),
  938. },
  939. },
  940. {
  941. name: "limited requires more details",
  942. priorityLevelConfiguration: &flowcontrol.PriorityLevelConfiguration{
  943. ObjectMeta: metav1.ObjectMeta{
  944. Name: "broken-limited",
  945. },
  946. Spec: flowcontrol.PriorityLevelConfigurationSpec{
  947. Type: flowcontrol.PriorityLevelEnablementLimited,
  948. },
  949. },
  950. expectedErrors: field.ErrorList{field.Required(field.NewPath("spec").Child("limited"), "must not be empty when type is Limited")},
  951. },
  952. {
  953. name: "max-in-flight should work",
  954. priorityLevelConfiguration: &flowcontrol.PriorityLevelConfiguration{
  955. ObjectMeta: metav1.ObjectMeta{
  956. Name: "max-in-flight",
  957. },
  958. Spec: flowcontrol.PriorityLevelConfigurationSpec{
  959. Type: flowcontrol.PriorityLevelEnablementLimited,
  960. Limited: &flowcontrol.LimitedPriorityLevelConfiguration{
  961. AssuredConcurrencyShares: 42,
  962. LimitResponse: flowcontrol.LimitResponse{
  963. Type: flowcontrol.LimitResponseTypeReject},
  964. },
  965. },
  966. },
  967. expectedErrors: field.ErrorList{},
  968. },
  969. {
  970. name: "forbid queuing details when not queuing",
  971. priorityLevelConfiguration: &flowcontrol.PriorityLevelConfiguration{
  972. ObjectMeta: metav1.ObjectMeta{
  973. Name: "system-foo",
  974. },
  975. Spec: flowcontrol.PriorityLevelConfigurationSpec{
  976. Type: flowcontrol.PriorityLevelEnablementLimited,
  977. Limited: &flowcontrol.LimitedPriorityLevelConfiguration{
  978. AssuredConcurrencyShares: 100,
  979. LimitResponse: flowcontrol.LimitResponse{
  980. Type: flowcontrol.LimitResponseTypeReject,
  981. Queuing: &flowcontrol.QueuingConfiguration{
  982. Queues: 512,
  983. HandSize: 4,
  984. QueueLengthLimit: 100,
  985. }}}},
  986. },
  987. expectedErrors: field.ErrorList{field.Forbidden(field.NewPath("spec").Child("limited").Child("limitResponse").Child("queuing"), "must be nil if limited.limitResponse.type is not Limited")},
  988. },
  989. {
  990. name: "wrong backstop spec should fail",
  991. priorityLevelConfiguration: &flowcontrol.PriorityLevelConfiguration{
  992. ObjectMeta: metav1.ObjectMeta{
  993. Name: flowcontrol.PriorityLevelConfigurationNameCatchAll,
  994. },
  995. Spec: badSpec,
  996. },
  997. expectedErrors: field.ErrorList{field.Invalid(field.NewPath("spec"), badSpec, "spec of 'catch-all' must equal the fixed value")},
  998. },
  999. {
  1000. name: "backstop should work",
  1001. priorityLevelConfiguration: &flowcontrol.PriorityLevelConfiguration{
  1002. ObjectMeta: metav1.ObjectMeta{
  1003. Name: flowcontrol.PriorityLevelConfigurationNameCatchAll,
  1004. },
  1005. Spec: flowcontrol.PriorityLevelConfigurationSpec{
  1006. Type: flowcontrol.PriorityLevelEnablementLimited,
  1007. Limited: &flowcontrol.LimitedPriorityLevelConfiguration{
  1008. AssuredConcurrencyShares: 1,
  1009. LimitResponse: flowcontrol.LimitResponse{
  1010. Type: flowcontrol.LimitResponseTypeReject,
  1011. }}},
  1012. },
  1013. expectedErrors: field.ErrorList{},
  1014. },
  1015. {
  1016. name: "broken queuing level should fail",
  1017. priorityLevelConfiguration: &flowcontrol.PriorityLevelConfiguration{
  1018. ObjectMeta: metav1.ObjectMeta{
  1019. Name: "system-foo",
  1020. },
  1021. Spec: flowcontrol.PriorityLevelConfigurationSpec{
  1022. Type: flowcontrol.PriorityLevelEnablementLimited,
  1023. Limited: &flowcontrol.LimitedPriorityLevelConfiguration{
  1024. AssuredConcurrencyShares: 100,
  1025. LimitResponse: flowcontrol.LimitResponse{
  1026. Type: flowcontrol.LimitResponseTypeQueue,
  1027. }}},
  1028. },
  1029. expectedErrors: field.ErrorList{field.Required(field.NewPath("spec").Child("limited").Child("limitResponse").Child("queuing"), "must not be empty if limited.limitResponse.type is Limited")},
  1030. },
  1031. {
  1032. name: "normal customized priority level should work",
  1033. priorityLevelConfiguration: &flowcontrol.PriorityLevelConfiguration{
  1034. ObjectMeta: metav1.ObjectMeta{
  1035. Name: "system-foo",
  1036. },
  1037. Spec: flowcontrol.PriorityLevelConfigurationSpec{
  1038. Type: flowcontrol.PriorityLevelEnablementLimited,
  1039. Limited: &flowcontrol.LimitedPriorityLevelConfiguration{
  1040. AssuredConcurrencyShares: 100,
  1041. LimitResponse: flowcontrol.LimitResponse{
  1042. Type: flowcontrol.LimitResponseTypeQueue,
  1043. Queuing: &flowcontrol.QueuingConfiguration{
  1044. Queues: 512,
  1045. HandSize: 4,
  1046. QueueLengthLimit: 100,
  1047. }}}},
  1048. },
  1049. expectedErrors: field.ErrorList{},
  1050. },
  1051. {
  1052. name: "customized priority level w/ overflowing handSize/queues should fail 1",
  1053. priorityLevelConfiguration: &flowcontrol.PriorityLevelConfiguration{
  1054. ObjectMeta: metav1.ObjectMeta{
  1055. Name: "system-foo",
  1056. },
  1057. Spec: flowcontrol.PriorityLevelConfigurationSpec{
  1058. Type: flowcontrol.PriorityLevelEnablementLimited,
  1059. Limited: &flowcontrol.LimitedPriorityLevelConfiguration{
  1060. AssuredConcurrencyShares: 100,
  1061. LimitResponse: flowcontrol.LimitResponse{
  1062. Type: flowcontrol.LimitResponseTypeQueue,
  1063. Queuing: &flowcontrol.QueuingConfiguration{
  1064. QueueLengthLimit: 100,
  1065. Queues: 512,
  1066. HandSize: 8,
  1067. }}}},
  1068. },
  1069. expectedErrors: field.ErrorList{
  1070. field.Invalid(field.NewPath("spec").Child("limited").Child("limitResponse").Child("queuing").Child("handSize"), int32(8), "required entropy bits of deckSize 512 and handSize 8 should not be greater than 60"),
  1071. },
  1072. },
  1073. {
  1074. name: "customized priority level w/ overflowing handSize/queues should fail 2",
  1075. priorityLevelConfiguration: &flowcontrol.PriorityLevelConfiguration{
  1076. ObjectMeta: metav1.ObjectMeta{
  1077. Name: "system-foo",
  1078. },
  1079. Spec: flowcontrol.PriorityLevelConfigurationSpec{
  1080. Type: flowcontrol.PriorityLevelEnablementLimited,
  1081. Limited: &flowcontrol.LimitedPriorityLevelConfiguration{
  1082. AssuredConcurrencyShares: 100,
  1083. LimitResponse: flowcontrol.LimitResponse{
  1084. Type: flowcontrol.LimitResponseTypeQueue,
  1085. Queuing: &flowcontrol.QueuingConfiguration{
  1086. QueueLengthLimit: 100,
  1087. Queues: 128,
  1088. HandSize: 10,
  1089. }}}},
  1090. },
  1091. expectedErrors: field.ErrorList{
  1092. field.Invalid(field.NewPath("spec").Child("limited").Child("limitResponse").Child("queuing").Child("handSize"), int32(10), "required entropy bits of deckSize 128 and handSize 10 should not be greater than 60"),
  1093. },
  1094. },
  1095. {
  1096. name: "customized priority level w/ overflowing handSize/queues should fail 3",
  1097. priorityLevelConfiguration: &flowcontrol.PriorityLevelConfiguration{
  1098. ObjectMeta: metav1.ObjectMeta{
  1099. Name: "system-foo",
  1100. },
  1101. Spec: flowcontrol.PriorityLevelConfigurationSpec{
  1102. Type: flowcontrol.PriorityLevelEnablementLimited,
  1103. Limited: &flowcontrol.LimitedPriorityLevelConfiguration{
  1104. AssuredConcurrencyShares: 100,
  1105. LimitResponse: flowcontrol.LimitResponse{
  1106. Type: flowcontrol.LimitResponseTypeQueue,
  1107. Queuing: &flowcontrol.QueuingConfiguration{
  1108. QueueLengthLimit: 100,
  1109. Queues: math.MaxInt32,
  1110. HandSize: 3,
  1111. }}}},
  1112. },
  1113. expectedErrors: field.ErrorList{
  1114. field.Invalid(field.NewPath("spec").Child("limited").Child("limitResponse").Child("queuing").Child("handSize"), int32(3), "required entropy bits of deckSize 2147483647 and handSize 3 should not be greater than 60"),
  1115. field.Invalid(field.NewPath("spec").Child("limited").Child("limitResponse").Child("queuing").Child("queues"), int32(math.MaxInt32), "must not be greater than 10000000"),
  1116. },
  1117. },
  1118. {
  1119. name: "customized priority level w/ handSize=2 and queues=10^7 should work",
  1120. priorityLevelConfiguration: &flowcontrol.PriorityLevelConfiguration{
  1121. ObjectMeta: metav1.ObjectMeta{
  1122. Name: "system-foo",
  1123. },
  1124. Spec: flowcontrol.PriorityLevelConfigurationSpec{
  1125. Type: flowcontrol.PriorityLevelEnablementLimited,
  1126. Limited: &flowcontrol.LimitedPriorityLevelConfiguration{
  1127. AssuredConcurrencyShares: 100,
  1128. LimitResponse: flowcontrol.LimitResponse{
  1129. Type: flowcontrol.LimitResponseTypeQueue,
  1130. Queuing: &flowcontrol.QueuingConfiguration{
  1131. QueueLengthLimit: 100,
  1132. Queues: 10 * 1000 * 1000, // 10^7
  1133. HandSize: 2,
  1134. }}}},
  1135. },
  1136. expectedErrors: field.ErrorList{},
  1137. },
  1138. {
  1139. name: "customized priority level w/ handSize greater than queues should fail",
  1140. priorityLevelConfiguration: &flowcontrol.PriorityLevelConfiguration{
  1141. ObjectMeta: metav1.ObjectMeta{
  1142. Name: "system-foo",
  1143. },
  1144. Spec: flowcontrol.PriorityLevelConfigurationSpec{
  1145. Type: flowcontrol.PriorityLevelEnablementLimited,
  1146. Limited: &flowcontrol.LimitedPriorityLevelConfiguration{
  1147. AssuredConcurrencyShares: 100,
  1148. LimitResponse: flowcontrol.LimitResponse{
  1149. Type: flowcontrol.LimitResponseTypeQueue,
  1150. Queuing: &flowcontrol.QueuingConfiguration{
  1151. QueueLengthLimit: 100,
  1152. Queues: 7,
  1153. HandSize: 8,
  1154. }}}},
  1155. },
  1156. expectedErrors: field.ErrorList{
  1157. field.Invalid(field.NewPath("spec").Child("limited").Child("limitResponse").Child("queuing").Child("handSize"), int32(8), "should not be greater than queues (7)"),
  1158. },
  1159. },
  1160. }
  1161. for _, testCase := range testCases {
  1162. t.Run(testCase.name, func(t *testing.T) {
  1163. errs := ValidatePriorityLevelConfiguration(testCase.priorityLevelConfiguration)
  1164. if !assert.ElementsMatch(t, testCase.expectedErrors, errs) {
  1165. t.Logf("mismatch: %v", cmp.Diff(testCase.expectedErrors, errs))
  1166. }
  1167. })
  1168. }
  1169. }
  1170. func TestValidateFlowSchemaStatus(t *testing.T) {
  1171. testCases := []struct {
  1172. name string
  1173. status *flowcontrol.FlowSchemaStatus
  1174. expectedErrors field.ErrorList
  1175. }{
  1176. {
  1177. name: "empty status should work",
  1178. status: &flowcontrol.FlowSchemaStatus{},
  1179. expectedErrors: field.ErrorList{},
  1180. },
  1181. {
  1182. name: "duplicate key should fail",
  1183. status: &flowcontrol.FlowSchemaStatus{
  1184. Conditions: []flowcontrol.FlowSchemaCondition{
  1185. {
  1186. Type: "1",
  1187. },
  1188. {
  1189. Type: "1",
  1190. },
  1191. },
  1192. },
  1193. expectedErrors: field.ErrorList{
  1194. field.Duplicate(field.NewPath("status").Child("conditions").Index(1).Child("type"), flowcontrol.FlowSchemaConditionType("1")),
  1195. },
  1196. },
  1197. {
  1198. name: "missing key should fail",
  1199. status: &flowcontrol.FlowSchemaStatus{
  1200. Conditions: []flowcontrol.FlowSchemaCondition{
  1201. {
  1202. Type: "",
  1203. },
  1204. },
  1205. },
  1206. expectedErrors: field.ErrorList{
  1207. field.Required(field.NewPath("status").Child("conditions").Index(0).Child("type"), "must not be empty"),
  1208. },
  1209. },
  1210. }
  1211. for _, testCase := range testCases {
  1212. t.Run(testCase.name, func(t *testing.T) {
  1213. errs := ValidateFlowSchemaStatus(testCase.status, field.NewPath("status"))
  1214. if !assert.ElementsMatch(t, testCase.expectedErrors, errs) {
  1215. t.Logf("mismatch: %v", cmp.Diff(testCase.expectedErrors, errs))
  1216. }
  1217. })
  1218. }
  1219. }
  1220. func TestValidatePriorityLevelConfigurationStatus(t *testing.T) {
  1221. testCases := []struct {
  1222. name string
  1223. status *flowcontrol.PriorityLevelConfigurationStatus
  1224. expectedErrors field.ErrorList
  1225. }{
  1226. {
  1227. name: "empty status should work",
  1228. status: &flowcontrol.PriorityLevelConfigurationStatus{},
  1229. expectedErrors: field.ErrorList{},
  1230. },
  1231. {
  1232. name: "duplicate key should fail",
  1233. status: &flowcontrol.PriorityLevelConfigurationStatus{
  1234. Conditions: []flowcontrol.PriorityLevelConfigurationCondition{
  1235. {
  1236. Type: "1",
  1237. },
  1238. {
  1239. Type: "1",
  1240. },
  1241. },
  1242. },
  1243. expectedErrors: field.ErrorList{
  1244. field.Duplicate(field.NewPath("status").Child("conditions").Index(1).Child("type"), flowcontrol.PriorityLevelConfigurationConditionType("1")),
  1245. },
  1246. },
  1247. {
  1248. name: "missing key should fail",
  1249. status: &flowcontrol.PriorityLevelConfigurationStatus{
  1250. Conditions: []flowcontrol.PriorityLevelConfigurationCondition{
  1251. {
  1252. Type: "",
  1253. },
  1254. },
  1255. },
  1256. expectedErrors: field.ErrorList{
  1257. field.Required(field.NewPath("status").Child("conditions").Index(0).Child("type"), "must not be empty"),
  1258. },
  1259. },
  1260. }
  1261. for _, testCase := range testCases {
  1262. t.Run(testCase.name, func(t *testing.T) {
  1263. errs := ValidatePriorityLevelConfigurationStatus(testCase.status, field.NewPath("status"))
  1264. if !assert.ElementsMatch(t, testCase.expectedErrors, errs) {
  1265. t.Logf("mismatch: %v", cmp.Diff(testCase.expectedErrors, errs))
  1266. }
  1267. })
  1268. }
  1269. }
  1270. func TestValidateNonResourceURLPath(t *testing.T) {
  1271. testCases := []struct {
  1272. name string
  1273. path string
  1274. expectingError bool
  1275. }{
  1276. {
  1277. name: "empty string should fail",
  1278. path: "",
  1279. expectingError: true,
  1280. },
  1281. {
  1282. name: "no slash should fail",
  1283. path: "foo",
  1284. expectingError: true,
  1285. },
  1286. {
  1287. name: "single slash should work",
  1288. path: "/",
  1289. expectingError: false,
  1290. },
  1291. {
  1292. name: "continuous slash should fail",
  1293. path: "//",
  1294. expectingError: true,
  1295. },
  1296. {
  1297. name: "/foo slash should work",
  1298. path: "/foo",
  1299. expectingError: false,
  1300. },
  1301. {
  1302. name: "multiple continuous slashes should fail",
  1303. path: "/////",
  1304. expectingError: true,
  1305. },
  1306. {
  1307. name: "ending up with slash should work",
  1308. path: "/apis/",
  1309. expectingError: false,
  1310. },
  1311. {
  1312. name: "ending up with wildcard should work",
  1313. path: "/healthz/*",
  1314. expectingError: false,
  1315. },
  1316. {
  1317. name: "single wildcard inside the path should fail",
  1318. path: "/healthz/*/foo",
  1319. expectingError: true,
  1320. },
  1321. {
  1322. name: "white-space in the path should fail",
  1323. path: "/healthz/foo bar",
  1324. expectingError: true,
  1325. },
  1326. {
  1327. name: "wildcard plus plain path should fail",
  1328. path: "/health*",
  1329. expectingError: true,
  1330. },
  1331. {
  1332. name: "wildcard plus plain path should fail 2",
  1333. path: "/health*/foo",
  1334. expectingError: true,
  1335. },
  1336. {
  1337. name: "multiple wildcard internal and suffix should fail",
  1338. path: "/*/*",
  1339. expectingError: true,
  1340. },
  1341. }
  1342. for _, testCase := range testCases {
  1343. t.Run(testCase.name, func(t *testing.T) {
  1344. err := ValidateNonResourceURLPath(testCase.path, field.NewPath(""))
  1345. assert.Equal(t, testCase.expectingError, err != nil,
  1346. "actual error: %v", err)
  1347. })
  1348. }
  1349. }