@@ -61,6 +61,7 @@ func TestValidationExpressions(t *testing.T) {
61
61
costBudget int64
62
62
isRoot bool
63
63
expectSkipped bool
64
+ expectedCost int64
64
65
}{
65
66
// tests where val1 and val2 are equal but val3 is different
66
67
// equality, comparisons and type specific functions
@@ -2006,14 +2007,48 @@ func TestValidationExpressions(t *testing.T) {
2006
2007
`quantity(self.val1).isInteger()` ,
2007
2008
},
2008
2009
},
2010
+ {name : "cost for extended lib calculated correctly: isSorted" ,
2011
+ obj : objs ("20" , "200M" ),
2012
+ schema : schemas (stringType , stringType ),
2013
+ valid : []string {
2014
+ "[1,2,3,4].isSorted()" ,
2015
+ },
2016
+ expectedCost : 4 ,
2017
+ },
2018
+ {name : "cost for extended lib calculated correctly: url" ,
2019
+ obj : objs ("20" , "200M" ),
2020
+ schema : schemas (stringType , stringType ),
2021
+ valid : []string {
2022
+ "url('https:://kubernetes.io/').getHostname() != 'test'" ,
2023
+ },
2024
+ expectedCost : 4 ,
2025
+ },
2026
+ {name : "cost for extended lib calculated correctly: split" ,
2027
+ obj : objs ("20" , "200M" ),
2028
+ schema : schemas (stringType , stringType ),
2029
+ valid : []string {
2030
+ "size('abc 123 def 123'.split(' ')) > 0" ,
2031
+ },
2032
+ expectedCost : 5 ,
2033
+ },
2034
+ {name : "cost for extended lib calculated correctly: join" ,
2035
+ obj : objs ("20" , "200M" ),
2036
+ schema : schemas (stringType , stringType ),
2037
+ valid : []string {
2038
+ "size(['aa', 'bb', 'cc', 'd', 'e', 'f', 'g', 'h', 'i', 'j'].join(' ')) > 0" ,
2039
+ },
2040
+ expectedCost : 7 ,
2041
+ },
2009
2042
}
2010
2043
2011
2044
for i := range tests {
2012
2045
i := i
2013
2046
t .Run (tests [i ].name , func (t * testing.T ) {
2014
2047
t .Parallel ()
2015
2048
tt := tests [i ]
2016
- tt .costBudget = celconfig .RuntimeCELCostBudget
2049
+ if tt .costBudget == 0 {
2050
+ tt .costBudget = celconfig .RuntimeCELCostBudget
2051
+ }
2017
2052
ctx := context .TODO ()
2018
2053
for j := range tt .valid {
2019
2054
validRule := tt .valid [j ]
@@ -2032,6 +2067,13 @@ func TestValidationExpressions(t *testing.T) {
2032
2067
for _ , err := range errs {
2033
2068
t .Errorf ("unexpected error: %v" , err )
2034
2069
}
2070
+
2071
+ if tt .expectedCost != 0 {
2072
+ if remainingBudget != tt .costBudget - tt .expectedCost {
2073
+ t .Errorf ("expected cost to be %d, but got %d" , tt .expectedCost , tt .costBudget - remainingBudget )
2074
+ }
2075
+ return
2076
+ }
2035
2077
if tt .expectSkipped {
2036
2078
// Skipped validations should have no cost. The only possible false positive here would be the CEL expression 'true'.
2037
2079
if remainingBudget != tt .costBudget {
0 commit comments