From 06942c6c299861a94d7f50c2fa1999f4382d2d71 Mon Sep 17 00:00:00 2001 From: Johan Fylling Date: Wed, 18 Sep 2024 13:10:46 +0200 Subject: [PATCH] rego-v1: Future-proofing `ir`, `loader`, `profiler`, and `refactor` pkg tests to be 1.0 compatible (#7021) Updating tests in packages: * ir * loader * profiler * refactor Signed-off-by: Johan Fylling --- ir/encoding/encoding_test.go | 3 +- loader/internal/embedtest/bar/bar.rego | 3 +- loader/loader_test.go | 3 +- profiler/profiler_test.go | 55 ++++++++++++++----------- refactor/refactor_test.go | 57 ++++++++++++++++---------- 5 files changed, 73 insertions(+), 48 deletions(-) diff --git a/ir/encoding/encoding_test.go b/ir/encoding/encoding_test.go index a14e42fb40..de073f5aff 100644 --- a/ir/encoding/encoding_test.go +++ b/ir/encoding/encoding_test.go @@ -15,8 +15,9 @@ func TestRoundTrip(t *testing.T) { c, err := ast.CompileModules(map[string]string{ "test.rego": ` package test + import rego.v1 - p { + p if { input.foo == 7 } `, diff --git a/loader/internal/embedtest/bar/bar.rego b/loader/internal/embedtest/bar/bar.rego index 08bbfb0ac6..7b51d41cd4 100644 --- a/loader/internal/embedtest/bar/bar.rego +++ b/loader/internal/embedtest/bar/bar.rego @@ -1,3 +1,4 @@ package bar +import rego.v1 -p = true { true } +p = true if { true } diff --git a/loader/loader_test.go b/loader/loader_test.go index 01c4d2e21e..a3f3d51f81 100644 --- a/loader/loader_test.go +++ b/loader/loader_test.go @@ -52,8 +52,9 @@ func TestLoadRego(t *testing.T) { files := map[string]string{ "/foo.rego": `package ex +import rego.v1 -p = true { true }`} +p = true if { true }`} test.WithTempFS(files, func(rootDir string) { moduleFile := filepath.Join(rootDir, "foo.rego") diff --git a/profiler/profiler_test.go b/profiler/profiler_test.go index a706415960..cc021fd241 100644 --- a/profiler/profiler_test.go +++ b/profiler/profiler_test.go @@ -21,33 +21,34 @@ import ( func TestProfilerLargeArray(t *testing.T) { profiler := New() module := `package test +import rego.v1 -foo { +foo if { p bar not baz bee } -bee { +bee if { nums = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i", "b", "c", "d", "e", "f", "g", "h", "i"] num = nums[_] contains(num, "test") } -bar { +bar if { a := 1 b := 2 a != b } -baz { +baz if { true false true } -p { +p if { a := 1 b := 2 c := 3 @@ -86,7 +87,7 @@ p { expectedNumEval := []int{1, 1, 2, 1, 1, 1, 1633, 1, 1, 1, 1, 1, 1, 1, 1, 3} expectedNumRedo := []int{1, 1, 0, 0, 1, 1633, 0, 1, 1, 1, 1, 0, 1, 1, 1, 3} - expectedRow := []int{4, 5, 6, 7, 11, 12, 13, 17, 18, 19, 23, 24, 29, 30, 31, 32} + expectedRow := []int{5, 6, 7, 8, 12, 13, 14, 18, 19, 20, 24, 25, 30, 31, 32, 33} expectedNumGenExpr := []int{1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3} for idx, actualExprStat := range fr.Result { @@ -126,8 +127,9 @@ func TestProfileCheckExprDuration(t *testing.T) { }) module := `package test + import rego.v1 - foo { + foo if { test.sleep("100ms") }` @@ -173,26 +175,27 @@ func TestProfileCheckExprDuration(t *testing.T) { func TestProfilerReportTopNResultsNoCriteria(t *testing.T) { profiler := New() module := `package test +import rego.v1 -foo { +foo if { bar not baz bee } -bee { +bee if { nums = ["a", "b", "c", "d"] num = nums[_] contains(num, "test") } -bar { +bar if { a := 1 b := 2 a != b } -baz { +baz if { true false true @@ -227,26 +230,27 @@ baz { func TestProfilerReportTopNResultsOneCriteria(t *testing.T) { profiler := New() module := `package test +import rego.v1 -foo { +foo if { bar not baz bee } -bee { +bee if { nums = ["a", "b", "c", "d"] num = nums[_] contains(num, "test") } -bar { +bar if { a := 1 b := 2 a != b } -baz { +baz if { true false true @@ -288,26 +292,27 @@ baz { func TestProfilerReportTopNResultsTwoCriteria(t *testing.T) { profiler := New() module := `package test +import rego.v1 -foo { +foo if { bar not baz bee } -bee { +bee if { nums = ["a", "b", "c", "d"] num = nums[_] contains(num, "test") } -bar { +bar if { a := 1 b := 2 a != b } -baz { +baz if { true false true @@ -355,26 +360,27 @@ baz { func TestProfilerReportTopNResultsThreeCriteria(t *testing.T) { profiler := New() module := `package test +import rego.v1 -foo { +foo if { bar not baz bee } -bee { +bee if { nums = ["a", "b", "c", "d"] num = nums[_] contains(num, "test") } -bar { +bar if { a := 1 b := 2 a != b } -baz { +baz if { true false true @@ -430,10 +436,11 @@ func TestProfilerWithPartialEval(t *testing.T) { profiler := New() module := `package test +import rego.v1 default foo = false -foo = true { +foo = true if { op = allowed_operations[_] input.method = op.method input.resource = op.resource diff --git a/refactor/refactor_test.go b/refactor/refactor_test.go index 6a06318142..1294183daf 100644 --- a/refactor/refactor_test.go +++ b/refactor/refactor_test.go @@ -9,10 +9,11 @@ import ( func TestMoveRenamePackage(t *testing.T) { module := ast.MustParseModule(`package lib.foo +import rego.v1 default allow = false -allow { +allow if { input.message == "hello" }`) @@ -35,10 +36,11 @@ allow { actual := result.Result["policy.rego"] expected := ast.MustParseModule(`package baz.bar +import rego.v1 default allow = false -allow { +allow if { input.message == "hello" }`) @@ -49,16 +51,18 @@ allow { func TestMoveRenamePackagePrefix(t *testing.T) { module1 := ast.MustParseModule(`package lib.foo +import rego.v1 default allow = false -allow { +allow if { input.message == "hello" }`) module2 := ast.MustParseModule(`package lib.bar +import rego.v1 -allow { +allow if { input.message == "world" }`) @@ -83,16 +87,18 @@ allow { actual2 := result.Result["policy2.rego"] expected1 := ast.MustParseModule(`package hidden.foo +import rego.v1 default allow = false -allow { +allow if { input.message == "hello" }`) expected2 := ast.MustParseModule(`package hidden.bar +import rego.v1 -allow { +allow if { input.message == "world" }`) @@ -107,8 +113,9 @@ allow { func TestMovePrefixInjection(t *testing.T) { module1 := ast.MustParseModule(`package a.b +import rego.v1 -p { data.x.q }`) +p if { data.x.q }`) module2 := ast.MustParseModule(`package x @@ -135,8 +142,9 @@ q = true`) actual2 := result.Result["policy2.rego"] expected1 := ast.MustParseModule(`package deadbeef.a.b +import rego.v1 -p { +p if { data.deadbeef.x.q }`) @@ -155,14 +163,15 @@ q = true`) func TestMoveWithKeyword(t *testing.T) { module1 := ast.MustParseModule(`package a.b - +import rego.v1 import data.x.q as r -p { r with data.foo as 7 }`) +p if{ r with data.foo as 7 }`) module2 := ast.MustParseModule(`package x +import rego.v1 -q { data.foo == 7 }`) +q if { data.foo == 7 }`) modules := map[string]*ast.Module{ "policy1.rego": module1, @@ -185,16 +194,17 @@ q { data.foo == 7 }`) actual2 := result.Result["policy2.rego"] expected1 := ast.MustParseModule(`package deadbeef.a.b - +import rego.v1 import data.deadbeef.x.q as r -p { +p if { r with data.deadbeef.foo as 7 }`) expected2 := ast.MustParseModule(`package deadbeef.x +import rego.v1 -q { +q if { data.deadbeef.foo == 7 }`) @@ -241,12 +251,13 @@ p = data.foo`) func TestMovePrefixEmpty(t *testing.T) { module1 := ast.MustParseModule(`package foo.bar.v1 +import rego.v1 -helper_1 { +helper_1 if { to_number(split(input.baz, ".")[1]) >= 1 } -helper_2 { +helper_2 if { to_number(split(data.bar, ".")[1]) >= 1 }`) @@ -270,12 +281,13 @@ helper_2 { actual := result.Result["policy1.rego"] expected := ast.MustParseModule(`package hidden.name["hello:0.1"].bar.v1 +import rego.v1 -helper_1 { +helper_1 if{ to_number(split(input.baz, ".")[1]) >= 1 } -helper_2 { +helper_2 if { to_number(split(data.hello, ".")[1]) >= 1 }`) @@ -286,8 +298,9 @@ helper_2 { func TestMoveConflictingRulesNoValidation(t *testing.T) { module1 := ast.MustParseModule(`package a.b +import rego.v1 -p[1]`) +p contains 1`) module2 := ast.MustParseModule(`package b @@ -313,8 +326,9 @@ p = 7`) func TestMoveConflictingRulesWithValidation(t *testing.T) { module1 := ast.MustParseModule(`package a.b +import rego.v1 -p[1]`) +p contains 1`) module2 := ast.MustParseModule(`package b @@ -345,10 +359,11 @@ p = 7`) func TestMoveBadSourceMapping(t *testing.T) { module := ast.MustParseModule(`package lib.foo +import rego.v1 default allow = false -allow { +allow if { input.message == "hello" }`)