mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
f77322b3fb
Go 1.23 is no longer supported as per Go release policy. Changes: - Use Go v1.24.6 as the project SDK requirement - Apply lint fixes for Go 1.24 - Fix "non-constant format string in call" issues as seen in CI. Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
87 lines
1.7 KiB
Go
87 lines
1.7 KiB
Go
// Copyright 2024 The OPA Authors. All rights reserved.
|
|
// Use of this source code is governed by an Apache2
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package sdk_test
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/open-policy-agent/opa/sdk"
|
|
sdktest "github.com/open-policy-agent/opa/v1/sdk/test"
|
|
)
|
|
|
|
func TestDefaultRegoVersion(t *testing.T) {
|
|
|
|
ctx := t.Context()
|
|
|
|
server := sdktest.MustNewServer(
|
|
sdktest.RawBundles(true),
|
|
sdktest.MockBundle("/bundles/bundle.tar.gz", map[string]string{
|
|
// v0 module
|
|
"main.rego": `
|
|
package system
|
|
|
|
main {
|
|
p[_] == "a"
|
|
}
|
|
|
|
p[x] {
|
|
x = "a"
|
|
}
|
|
|
|
str = "foo"
|
|
|
|
loopback = input
|
|
`,
|
|
}),
|
|
)
|
|
|
|
defer server.Stop()
|
|
|
|
config := fmt.Sprintf(`{
|
|
"services": {
|
|
"test": {
|
|
"url": %q
|
|
}
|
|
},
|
|
"bundles": {
|
|
"test": {
|
|
"resource": "/bundles/bundle.tar.gz"
|
|
}
|
|
}
|
|
}`, server.URL())
|
|
|
|
opa, err := sdk.New(ctx, sdk.Options{
|
|
Config: strings.NewReader(config),
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
defer opa.Stop(ctx)
|
|
|
|
if result, err := opa.Decision(ctx, sdk.DecisionOptions{}); err != nil {
|
|
t.Fatal(err)
|
|
} else if decision, ok := result.Result.(bool); !ok || !decision {
|
|
t.Fatal("expected true but got:", decision, ok)
|
|
}
|
|
|
|
if result, err := opa.Decision(ctx, sdk.DecisionOptions{Path: "/system/str"}); err != nil {
|
|
t.Fatal(err)
|
|
} else if decision, ok := result.Result.(string); !ok || decision != "foo" {
|
|
t.Fatal(`expected "foo" but got:`, decision)
|
|
}
|
|
|
|
exp := map[string]any{"foo": "bar"}
|
|
|
|
if result, err := opa.Decision(ctx, sdk.DecisionOptions{Path: "/system/loopback", Input: map[string]any{"foo": "bar"}}); err != nil {
|
|
t.Fatal(err)
|
|
} else if !reflect.DeepEqual(result.Result, exp) {
|
|
t.Fatalf("expected %v but got %v", exp, result.Result)
|
|
}
|
|
}
|