Skip to content

Commit

Permalink
rego-v1: Future-proofing test pkg tests to be 1.0 compatible (#7030)
Browse files Browse the repository at this point in the history
Signed-off-by: Johan Fylling <[email protected]>
  • Loading branch information
johanfylling authored Sep 18, 2024
1 parent 8de480e commit 36dd26a
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 58 deletions.
13 changes: 11 additions & 2 deletions sdk/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,18 @@ func (s *Server) buildBundles(ref string, policies map[string]string) error {

// Compile the bundle out into a buffer
buf := bytes.NewBuffer(nil)

// We need to explicitly set the global bundle rego-version, as an unassigned version will be
// interpreted as v0 on the receiving end, which will cause problems if modules are parsed/compiled
// as v1 on this end, which will drop 'rego.v1' and 'future.keywords' imports.
bundleManifest := bundle.Manifest{}
bundleManifest.SetRegoVersion(ast.DefaultRegoVersion)
bundleManifest.Init()

err := compile.New().WithOutput(buf).WithBundle(&bundle.Bundle{
Data: map[string]interface{}{},
Modules: modules,
Data: map[string]interface{}{},
Modules: modules,
Manifest: bundleManifest,
}).Build(context.Background())
if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions test/authz/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ import (
// Policy is a test rego policy for a token based authz system
const Policy = `package policy.restauthz
import rego.v1
import data.restauthz.tokens
default allow = false
allow {
allow if {
tokens[input.token_id] = token
token.authz_profiles[_] = authz
re_match(authz.path, input.path)
regex.match(authz.path, input.path)
authz.methods[_] = input.method
}`

Expand Down
6 changes: 4 additions & 2 deletions test/e2e/concurrency/concurrency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ func TestConcurrencyCompile(t *testing.T) {

policy := `
package test
import rego.v1
f(_)
p {
p if {
not q
}
q {
q if {
not f(input.foo)
}
`
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/logs/console/console_decision_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ func TestConsoleDecisionLogWithInput(t *testing.T) {

policy := `
package test
import rego.v1
default allow = false
allow {
allow if {
input.x == 1
}
`
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ func TestRequestWithInstrumentationV1CompileAPI(t *testing.T) {

policy := `
package test
p {input.x >= data.y}
import rego.v1
p if {input.x >= data.y}
`

err := testRuntime.UploadPolicy(t.Name(), strings.NewReader(policy))
Expand Down
12 changes: 7 additions & 5 deletions test/e2e/oci/oci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,22 @@ func (b *SafeBuffer) String() string {

func TestEnablePrintStatementsForBundles(t *testing.T) {
ref := "registry.io/someorg/somerepo:tag"
server := test_sdk.MustNewServer(test_sdk.MockOCIBundle(ref, map[string]string{
"post.rego": `
server := test_sdk.MustNewServer(
test_sdk.MockOCIBundle(ref, map[string]string{
"post.rego": `
package peoplefinder.POST.api.users
import future.keywords.if
import input.user.properties as user_props
default allowed = false
allowed {
allowed if {
user_props.department == "Operations"
user_props.title == "IT Manager"
}
`,
}))
}))
params := e2e.NewAPIServerTestParams()

buf := SafeBuffer{}
Expand Down
14 changes: 9 additions & 5 deletions test/e2e/print/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ func TestEnablePrintStatementsForFilesystemPolicies(t *testing.T) {
func TestEnablePrintStatementsForHTTPAPIPushedPolicies(t *testing.T) {
policy := `
package test
import rego.v1
p {
p if {
print("hello world")
}
`
Expand Down Expand Up @@ -100,15 +101,18 @@ func TestEnablePrintStatementsForHTTPAPIPushedPolicies(t *testing.T) {

func TestEnablePrintStatementsForBundles(t *testing.T) {

server := test_sdk.MustNewServer(test_sdk.MockBundle("/bundles/bundle.tar.gz", map[string]string{
"test.rego": `
server := test_sdk.MustNewServer(
test_sdk.RawBundles(true),
test_sdk.MockBundle("/bundles/bundle.tar.gz", map[string]string{
"test.rego": `
package test
import rego.v1
p {
p if {
print("hello world")
}
`,
}))
}))

params := e2e.NewAPIServerTestParams()

Expand Down
Loading

0 comments on commit 36dd26a

Please sign in to comment.