Skip to content

Commit

Permalink
cmd: Adding --v0-compatible flag (#7065)
Browse files Browse the repository at this point in the history
To make OPA behave as v0.x post v1.0 release.
If used simultaneously with `--v1-compatible` flag, the `--v0-compatible` flag takes precedence.

Also, future-proofing `cmd` package tests for 1.0.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
  • Loading branch information
johanfylling authored Sep 25, 2024
1 parent 6e8b765 commit 5521453
Show file tree
Hide file tree
Showing 31 changed files with 2,091 additions and 688 deletions.
11 changes: 6 additions & 5 deletions ast/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2696,18 +2696,19 @@ func (p *Parser) regoV1Import(imp *Import) {
return
}

if p.po.RegoVersion == RegoV1 {
// We're parsing for Rego v1, where the 'rego.v1' import is a no-op.
return
}

path := imp.Path.Value.(Ref)

// v1 is only valid option
if len(path) == 1 || !path[1].Equal(RegoV1CompatibleRef[1]) || len(path) > 2 {
p.errorf(imp.Path.Location, "invalid import `%s`, must be `%s`", path, RegoV1CompatibleRef)
return
}

if p.po.RegoVersion == RegoV1 {
// We're parsing for Rego v1, where the 'rego.v1' import is a no-op.
return
}

if imp.Alias != "" {
p.errorf(imp.Path.Location, "`rego` imports cannot be aliased")
return
Expand Down
2 changes: 2 additions & 0 deletions cmd/bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ The optional "gobench" output format conforms to the Go Benchmark Data Format.
addIgnoreFlag(benchCommand.Flags(), &params.ignore)
addSchemaFlags(benchCommand.Flags(), params.schema)
addTargetFlag(benchCommand.Flags(), params.target)
addV0CompatibleFlag(benchCommand.Flags(), &params.v0Compatible, false)
addV1CompatibleFlag(benchCommand.Flags(), &params.v1Compatible, false)

// Shared benchmark flags
Expand Down Expand Up @@ -304,6 +305,7 @@ func benchE2E(ctx context.Context, args []string, params benchmarkCommandParams,
GracefulShutdownPeriod: params.gracefulShutdownPeriod,
ShutdownWaitPeriod: params.shutdownWaitPeriod,
ConfigFile: params.configFile,
V0Compatible: params.v0Compatible,
V1Compatible: params.v1Compatible,
}

Expand Down
30 changes: 23 additions & 7 deletions cmd/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,8 +683,9 @@ func TestBenchMainWithDataE2E(t *testing.T) {
params.e2e = true

mod := `package a.b
import rego.v1
x {
x if {
data.a.b.c == 42
}
`
Expand Down Expand Up @@ -731,25 +732,28 @@ func TestBenchMainBadQueryE2E(t *testing.T) {
}
}

func TestBenchMainV1Compatible(t *testing.T) {
func TestBenchMainCompatibleFlags(t *testing.T) {
tests := []struct {
note string
v0Compatible bool
v1Compatible bool
module string
query string
expErrs []string
}{
// These tests are slow, so we're not being completely exhaustive here.
{
note: "v0.x, keywords not used",
note: "v0, keywords not used",
v0Compatible: true,
module: `package test
a[4] {
1 == 1
}`,
query: `data.test.a`,
},
{
note: "v0.x, no keywords imported",
note: "v0, no keywords imported",
v0Compatible: true,
module: `package test
a contains 4 if {
1 == 1
Expand All @@ -761,7 +765,7 @@ a contains 4 if {
},
},
{
note: "v1.0, keywords not used",
note: "v1, keywords not used",
v1Compatible: true,
module: `package test
a[4] {
Expand All @@ -774,11 +778,21 @@ a[4] {
},
},
{
note: "v1.0, no keywords imported",
note: "v1, no keywords imported",
v1Compatible: true,
module: `package test
a contains 4 if {
1 == 1
}`,
query: `data.test.a`,
},
{
note: "v0+v1, keywords not used (v0 takes precedence)",
v0Compatible: true,
v1Compatible: true,
module: `package test
a[4] {
1 == 1
}`,
query: `data.test.a`,
},
Expand Down Expand Up @@ -807,6 +821,7 @@ a contains 4 if {
test.WithTempFS(files, func(path string) {
params := testBenchParams()
_ = params.outputFormat.Set(evalPrettyOutput)
params.v0Compatible = tc.v0Compatible
params.v1Compatible = tc.v1Compatible
params.e2e = mode.e2e

Expand Down Expand Up @@ -1303,8 +1318,9 @@ func fakeBenchResults() testing.BenchmarkResult {

func testBundle() bundle.Bundle {
mod := `package a.b
import rego.v1
x {
x if {
data.a.b.c == 42
}
`
Expand Down
11 changes: 9 additions & 2 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type buildParams struct {
excludeVerifyFiles []string
plugin string
ns string
v0Compatible bool
v1Compatible bool
followSymlinks bool
}
Expand Down Expand Up @@ -257,6 +258,7 @@ against OPA v0.22.0:
addSigningPluginFlag(buildCommand.Flags(), &buildParams.plugin)
addClaimsFileFlag(buildCommand.Flags(), &buildParams.claimsFile)

addV0CompatibleFlag(buildCommand.Flags(), &buildParams.v0Compatible, false)
addV1CompatibleFlag(buildCommand.Flags(), &buildParams.v1Compatible, false)

RootCommand.AddCommand(buildCommand)
Expand Down Expand Up @@ -307,9 +309,14 @@ func dobuild(params buildParams, args []string) error {
WithPartialNamespace(params.ns).
WithFollowSymlinks(params.followSymlinks)

if params.v1Compatible {
compiler = compiler.WithRegoVersion(ast.RegoV1)
regoVersion := ast.DefaultRegoVersion
if params.v0Compatible {
// v0 takes precedence over v1
regoVersion = ast.RegoV0
} else if params.v1Compatible {
regoVersion = ast.RegoV1
}
compiler = compiler.WithRegoVersion(regoVersion)

if params.revision.isSet {
compiler = compiler.WithRevision(*params.revision.v)
Expand Down
Loading

0 comments on commit 5521453

Please sign in to comment.