Add test coverage of result size of string operations

This commit is contained in:
Joe Betz 2023-08-07 12:41:52 -04:00
parent 69a5a52896
commit e4d16f34c1
4 changed files with 53 additions and 2 deletions

View File

@ -9315,6 +9315,32 @@ func TestValidateCustomResourceDefinitionValidation(t *testing.T) {
forbidden("spec.validation.openAPIV3Schema.properties[f].x-kubernetes-validations[0].messageExpression"),
},
},
{
name: "x-kubernetes-validations rule with lowerAscii check should be within estimated cost limit",
opts: validationOptions{requireStructuralSchema: true},
input: apiextensions.CustomResourceValidation{
OpenAPIV3Schema: &apiextensions.JSONSchemaProps{
Type: "object",
Properties: map[string]apiextensions.JSONSchemaProps{
"f": {
Type: "array",
MaxItems: pointer.Int64(5),
Items: &apiextensions.JSONSchemaPropsOrArray{
Schema: &apiextensions.JSONSchemaProps{
Type: "string",
MaxLength: pointer.Int64(5),
},
},
XValidations: apiextensions.ValidationRules{
{
Rule: "self.all(x, self.exists_one(y, x.lowerAscii() == y.lowerAscii()))",
},
},
},
},
},
},
},
{
name: "x-kubernetes-validations rule invalidated by messageExpression exceeding per-CRD estimated cost limit",
opts: validationOptions{requireStructuralSchema: true},

View File

@ -115,6 +115,7 @@ func TestCelCostStability(t *testing.T) {
"self.val1.substring(4, 10).trim() == 'takes'": 6,
"self.val1.upperAscii() == 'ROOK TAKES 👑'": 6,
"self.val1.lowerAscii() == 'rook takes 👑'": 6,
"self.val1.lowerAscii() == self.val1.lowerAscii()": 10,
},
},
{name: "escaped strings",

View File

@ -1691,9 +1691,9 @@ func TestCostEstimation(t *testing.T) {
objType = withRule(objType, "self.str.replace(self.before, self.after) == 'does not matter'")
return &objType
},
expectedCalcCost: 629154,
expectedCalcCost: 629152, // cost is based on the result size of the replace() call
setMaxElements: 10,
expectedSetCost: 16,
expectedSetCost: 14,
},
{
name: "extended library split",

View File

@ -227,12 +227,36 @@ func TestStringLibrary(t *testing.T) {
expectEsimatedCost: checker.CostEstimate{Min: 3, Max: 3},
expectRuntimeCost: 3,
},
{
name: "lowerAsciiEquals",
expr: "'ABCDEFGHIJ abcdefghij'.lowerAscii() == 'abcdefghij ABCDEFGHIJ'.lowerAscii()",
expectEsimatedCost: checker.CostEstimate{Min: 7, Max: 9},
expectRuntimeCost: 9,
},
{
name: "upperAscii",
expr: "'ABCDEFGHIJ abcdefghij'.upperAscii()",
expectEsimatedCost: checker.CostEstimate{Min: 3, Max: 3},
expectRuntimeCost: 3,
},
{
name: "upperAsciiEquals",
expr: "'ABCDEFGHIJ abcdefghij'.upperAscii() == 'abcdefghij ABCDEFGHIJ'.upperAscii()",
expectEsimatedCost: checker.CostEstimate{Min: 7, Max: 9},
expectRuntimeCost: 9,
},
{
name: "quote",
expr: "strings.quote('ABCDEFGHIJ abcdefghij')",
expectEsimatedCost: checker.CostEstimate{Min: 3, Max: 3},
expectRuntimeCost: 3,
},
{
name: "quoteEquals",
expr: "strings.quote('ABCDEFGHIJ abcdefghij') == strings.quote('ABCDEFGHIJ abcdefghij')",
expectEsimatedCost: checker.CostEstimate{Min: 7, Max: 11},
expectRuntimeCost: 9,
},
{
name: "replace",
expr: "'abc 123 def 123'.replace('123', '456')",