@@ -17,6 +17,7 @@ limitations under the License.
17
17
package cel
18
18
19
19
import (
20
+ "fmt"
20
21
"strings"
21
22
"testing"
22
23
@@ -207,10 +208,29 @@ device.attributes["dra.example.com"]["version"].isGreaterThan(semver("0.0.1"))
207
208
"expensive" : {
208
209
// The worst-case is based on the maximum number of
209
210
// 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!
214
234
},
215
235
} {
216
236
t .Run (name , func (t * testing.T ) {
0 commit comments