Skip to content

Commit 12c083e

Browse files
authored
test: Add RVM compiler testing to ACI tests (microsoft#509)
code fixes: - compiler: add `is_var_bound_in_current_scope` and use it in destructuring so only the innermost scope blocks rebinding while still catching duplicates within that block. - rvm: treat `not` over undefined operands as a successful negation to match interpreter semantics. tests/aci: migrate YAML cases to `data.policy.rule` queries with `{x: …}` bindings, expand the harness to run interpreter plus RVM (with optional skipping), align results to the binding format, add readable timing output, and support a `--filter` flag for targeting cases. Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
1 parent a8a3a98 commit 12c083e

5 files changed

Lines changed: 210 additions & 56 deletions

File tree

src/languages/rego/compiler/core.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ impl<'a> Compiler<'a> {
140140
}
141141
}
142142

143+
/// Returns true when a variable is already bound in the innermost scope
144+
pub fn is_var_bound_in_current_scope(&self, var_name: &str) -> bool {
145+
self.scopes
146+
.last()
147+
.map(|scope| scope.bound_vars.contains_key(var_name))
148+
.unwrap_or(false)
149+
}
150+
143151
/// Look up a variable in all scopes starting from innermost (like interpreter's lookup_local_var)
144152
pub fn lookup_local_var(&self, var_name: &str) -> Option<Register> {
145153
self.scopes

src/languages/rego/compiler/destructuring.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ impl<'a> Compiler<'a> {
289289
return Ok(());
290290
}
291291

292-
if self.lookup_local_var(var_name).is_some() {
292+
if self.is_var_bound_in_current_scope(var_name) {
293293
bail!("Variable '{var_name}' already defined in current scope");
294294
}
295295

src/rvm/vm/dispatch.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,10 @@ impl RegoVM {
299299
let operand_value = &self.registers[operand as usize];
300300

301301
if operand_value == &Value::Undefined {
302-
self.registers[dest as usize] = Value::Undefined;
302+
// In Rego, `not expr` succeeds when `expr` has no results.
303+
// When the operand evaluates to undefined we should treat it as
304+
// a successful negation instead of propagating undefined.
305+
self.registers[dest as usize] = Value::Bool(true);
303306
return Ok(InstructionOutcome::Continue);
304307
}
305308

tests/aci/aci.yaml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ cases:
1111
target: /run/layers/p0-layer0
1212
data:
1313
metadata: {}
14-
query: data.policy.mount_device=x
14+
query: data.policy.mount_device
1515
want_result:
1616
- x:
1717
allowed: true
@@ -44,7 +44,7 @@ cases:
4444
"/run/layers/p0-layer3": 41d64cdeb347bf236b4c13b7403b633ff11f1cf94dbc7cf881a44d6da88c5156
4545
"/run/layers/p0-layer4": 4dedae42847c704da891a28c25d32201a1ae440bce2aecccfa8e6f03b97a6a6c
4646
"/run/layers/p0-layer5": fe84c9d5bfddd07a2624d00333cf13c1a9c941f3a261f13ead44fc6a93bc0e7a
47-
query: data.policy.mount_overlay=x
47+
query: data.policy.mount_overlay
4848
want_result:
4949
- x:
5050
allowed: true
@@ -68,7 +68,7 @@ cases:
6868
target: /mnt/layer6
6969
data:
7070
metadata: {}
71-
query: data.policy.scratch_mount=x
71+
query: data.policy.scratch_mount
7272
want_result:
7373
- x:
7474
allowed: true
@@ -201,7 +201,7 @@ cases:
201201
scratch_mounts:
202202
"/mnt/layer6":
203203
encrypted: true
204-
query: data.policy.create_container=x
204+
query: data.policy.create_container
205205
want_result:
206206
- x:
207207
allow_stdio_access: false
@@ -257,7 +257,7 @@ cases:
257257
started:
258258
container0:
259259
- {"privileged": false}
260-
query: data.policy.shutdown_container=x
260+
query: data.policy.shutdown_container
261261
want_result:
262262
- x:
263263
allowed: true
@@ -286,7 +286,7 @@ cases:
286286
scratch_mounts:
287287
"/mnt/layer6":
288288
encrypted: true
289-
query: data.policy.scratch_unmount=x
289+
query: data.policy.scratch_unmount
290290
want_result:
291291
- x:
292292
allowed: true
@@ -313,7 +313,7 @@ cases:
313313
overlayTargets:
314314
"/run/gcs/c/container0/rootfs": true
315315
scratch_mounts: []
316-
query: data.policy.unmount_overlay=x
316+
query: data.policy.unmount_overlay
317317
want_result:
318318
- x:
319319
allowed: true
@@ -332,7 +332,7 @@ cases:
332332
metadata:
333333
devices:
334334
"/run/layers/p0-layer0": 1b80f120dbd88e4355d6241b519c3e25290215c469516b49dece9cf07175a766
335-
query: data.policy.unmount_device=x
335+
query: data.policy.unmount_device
336336
want_result:
337337
- x:
338338
allowed: true
@@ -378,7 +378,7 @@ cases:
378378
namespace: fragment
379379
data:
380380
metadata: {}
381-
query: data.policy.load_fragment=x
381+
query: data.policy.load_fragment
382382
want_result:
383383
- x:
384384
add_module: false

0 commit comments

Comments
 (0)