Skip to content

Commit

Permalink
rego-v1: Future-proofing plugins tests to be 1.0 compatible (#7044)
Browse files Browse the repository at this point in the history
Signed-off-by: Johan Fylling <[email protected]>
  • Loading branch information
johanfylling authored Sep 23, 2024
1 parent 09c1bdf commit dbf980f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 29 deletions.
90 changes: 65 additions & 25 deletions plugins/bundle/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,10 +519,11 @@ func TestPluginOneShotWithAuthzSchemaVerification(t *testing.T) {

// authz rules with no error
authzModule := `package system.authz
import rego.v1
default allow := false
allow {
allow if {
input.identity == "foo"
}`

Expand Down Expand Up @@ -554,26 +555,27 @@ func TestPluginOneShotWithAuthzSchemaVerification(t *testing.T) {

// authz rules with errors
authzModule = `package system.authz
import rego.v1
default allow := false
allow {
allow if {
input.identty == "foo" # type error 1
}
allow {
allow if {
helper1
}
helper1 {
helper1 if {
helper2
}
helper2 {
helper2 if {
input.method == 123 # type error 2
}
dont_type_check_me {
dont_type_check_me if {
input.methd == "GET" # type error 3
}`

Expand Down Expand Up @@ -665,22 +667,23 @@ func TestPluginOneShotWithAuthzSchemaVerificationNonDefaultAuthzPath(t *testing.
module := "package foo\n\ncorge=1"

authzModule := `package foo.authz
import rego.v1
default allow := false
allow {
allow if {
input.identty == "foo" # type error 1
}
allow {
allow if {
helper
}
helper {
helper if {
input.method == 123 # type error 2
}
dont_type_check_me {
dont_type_check_me if {
input.methd == "GET" # type error 3
}`

Expand Down Expand Up @@ -2491,18 +2494,20 @@ func TestLoadAndActivateDepBundlesFromDisk(t *testing.T) {
module1 := `
package bar
import rego.v1
import data.foo
default allow = false
allow {
allow if {
foo.is_one(1)
}`

module2 := `
package foo
import rego.v1
is_one(x) {
is_one(x) if {
x == 1
}`

Expand Down Expand Up @@ -2589,11 +2594,12 @@ func TestLoadAndActivateDepBundlesFromDiskMaxAttempts(t *testing.T) {
module := `
package bar
import rego.v1
import data.foo
default allow = false
allow {
allow if {
foo.is_one(1)
}`

Expand Down Expand Up @@ -2646,7 +2652,10 @@ func TestPluginOneShotCompileError(t *testing.T) {

ensurePluginState(t, plugin, plugins.StateNotReady)

raw1 := "package foo\n\np[x] { x = 1 }"
raw1 := `package foo
import rego.v1
p contains x if { x = 1 }`

b1 := &bundle.Bundle{
Data: map[string]interface{}{"a": "b"},
Expand All @@ -2668,8 +2677,11 @@ func TestPluginOneShotCompileError(t *testing.T) {
Data: map[string]interface{}{"a": "b"},
Modules: []bundle.ModuleFile{
{
Path: "/example2.rego",
Parsed: ast.MustParseModule("package foo\n\np[x]"),
Path: "/example2.rego",
Parsed: ast.MustParseModule(`package foo
import rego.v1
p contains x`),
},
},
}
Expand Down Expand Up @@ -2980,7 +2992,10 @@ func TestPluginListener(t *testing.T) {
t.Fatal("Listener not properly registered")
}

module := "package gork\np[x] { x = 1 }"
module := `package gork
import rego.v1
p contains x if { x = 1 }`

b := bundle.Bundle{
Manifest: bundle.Manifest{
Expand All @@ -3005,7 +3020,10 @@ func TestPluginListener(t *testing.T) {

validateStatus(t, s1, "quickbrownfaux", false)

module = "package gork\np[x]"
module = `package gork
import rego.v1
p contains x`

b.Manifest.Revision = "slowgreenburd"
b.Modules[0] = bundle.ModuleFile{
Expand All @@ -3020,7 +3038,10 @@ func TestPluginListener(t *testing.T) {

validateStatus(t, s2, "quickbrownfaux", true)

module = "package gork\np[1]"
module = `package gork
import rego.v1
p contains 1`
b.Manifest.Revision = "fancybluederg"
b.Modules[0] = bundle.ModuleFile{
Path: "/foo.rego",
Expand Down Expand Up @@ -3146,7 +3167,10 @@ func TestPluginBulkListener(t *testing.T) {
t.Fatal("Bulk listener not properly registered")
}

module := "package gork\np[x] { x = 1 }"
module := `package gork
import rego.v1
p contains x if { x = 1 }`

b := bundle.Bundle{
Manifest: bundle.Manifest{
Expand Down Expand Up @@ -3187,7 +3211,10 @@ func TestPluginBulkListener(t *testing.T) {
}
}

module = "package gork\np[x]"
module = `package gork
import rego.v1
p contains x`

b.Manifest.Revision = "slowgreenburd"
b.Modules[0] = bundle.ModuleFile{
Expand Down Expand Up @@ -3217,7 +3244,10 @@ func TestPluginBulkListener(t *testing.T) {
}
}

module = "package gork\np[1]"
module = `package gork
import rego.v1
p contains 1`
b.Manifest.Revision = "fancybluederg"
b.Modules[0] = bundle.ModuleFile{
Path: "/foo.rego",
Expand Down Expand Up @@ -3256,7 +3286,10 @@ func TestPluginBulkListener(t *testing.T) {
}

// Test updates the other bundles
module = "package p1\np[x] { x = 1 }"
module = `package p1
import rego.v1
p contains x if { x = 1 }`

b1 := bundle.Bundle{
Manifest: bundle.Manifest{
Expand Down Expand Up @@ -3330,7 +3363,10 @@ func TestPluginBulkListenerStatusCopyOnly(t *testing.T) {
bulkChan <- status
})

module := "package gork\np[x] { x = 1 }"
module := `package gork
import rego.v1
p contains x if { x = 1 }`

b := bundle.Bundle{
Manifest: bundle.Manifest{
Expand Down Expand Up @@ -6246,7 +6282,11 @@ func writeTestBundleToDisk(t *testing.T, srcDir string, signed bool) bundle.Bund
func getTestBundle(t *testing.T) bundle.Bundle {
t.Helper()

module := "package gork\np[x] { x = 1 }"
module := `package gork
import rego.v1
p contains x if { x = 1 }`

b := bundle.Bundle{
Manifest: bundle.Manifest{
Expand Down
3 changes: 2 additions & 1 deletion plugins/discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ func TestEvaluateBundle(t *testing.T) {

sampleModule := `
package foo.bar
import rego.v1
bundle = {
"name": rt.name,
"service": "example"
} {
} if {
rt := opa.runtime()
}
`
Expand Down
6 changes: 3 additions & 3 deletions plugins/plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ func TestManagerWithOPATelemetryUpdateLoop(t *testing.T) {
t.Fatalf("Unexpected error: %s", err)
}

// add a policy to the store to trigger a telemetry update
// add a policy to the store to trigger a telemetry update (v0.36.0)
module := `package x
p { array.reverse([1,2,3]) }`
p := array.reverse([1,2,3])`

err = storage.Txn(ctx, m.Store, storage.WriteParams, func(txn storage.Transaction) error {
return m.Store.UpsertPolicy(ctx, txn, "policy.rego", []byte(module))
Expand All @@ -256,7 +256,7 @@ func TestManagerWithOPATelemetryUpdateLoop(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}

// add a bundle with some policy to trigger a telemetry update
// add a bundle with some policy to trigger a telemetry update (v0.46.0)
txn := storage.NewTransactionOrDie(ctx, m.Store, storage.WriteParams)

var archiveFiles = map[string]string{
Expand Down

0 comments on commit dbf980f

Please sign in to comment.