Skip to content

Commit 7995b6f

Browse files
committed
DRA CEL: add test case for runtime cost limit check
At the moment, the cost also gets checked at runtime. This test case ensures that this check is really active.
1 parent f548fc2 commit 7995b6f

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

staging/src/k8s.io/dynamic-resource-allocation/cel/compile_test.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package cel
1818

1919
import (
20+
"fmt"
2021
"strings"
2122
"testing"
2223

@@ -207,10 +208,29 @@ device.attributes["dra.example.com"]["version"].isGreaterThan(semver("0.0.1"))
207208
"expensive": {
208209
// The worst-case is based on the maximum number of
209210
// attributes and the maximum attribute name length.
210-
expression: `device.attributes["dra.example.com"].map(s, s.lowerAscii()).map(s, s.size()).sum() == 0`,
211-
driver: "dra.example.com",
212-
expectMatch: true,
213-
expectCost: 18446744073709551615, // Exceeds limit!
211+
// To actually reach that expected cost at runtime, we must
212+
// have many attributes.
213+
attributes: func() map[resourceapi.QualifiedName]resourceapi.DeviceAttribute {
214+
attributes := make(map[resourceapi.QualifiedName]resourceapi.DeviceAttribute)
215+
prefix := "dra.example.com/"
216+
attribute := resourceapi.DeviceAttribute{
217+
StringValue: ptr.To("abc"),
218+
}
219+
// If the cost estimate was accurate, using exactly as many attributes
220+
// as allowed at most should exceed the limit. In practice, the estimate
221+
// is an upper bound and significantly more attributes are needed before
222+
// the runtime cost becomes too large.
223+
for i := 0; i < 1000*resourceapi.ResourceSliceMaxAttributesAndCapacitiesPerDevice; i++ {
224+
suffix := fmt.Sprintf("-%d", i)
225+
name := prefix + strings.Repeat("x", resourceapi.DeviceMaxIDLength-len(suffix)) + suffix
226+
attributes[resourceapi.QualifiedName(name)] = attribute
227+
}
228+
return attributes
229+
}(),
230+
expression: `device.attributes["dra.example.com"].map(s, s.lowerAscii()).map(s, s.size()).sum() == 0`,
231+
driver: "dra.example.com",
232+
expectMatchError: "actual cost limit exceeded",
233+
expectCost: 18446744073709551615, // Exceeds limit!
214234
},
215235
} {
216236
t.Run(name, func(t *testing.T) {

0 commit comments

Comments
 (0)