-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Johan Fylling <[email protected]>
- Loading branch information
1 parent
925583a
commit 32690be
Showing
13 changed files
with
288 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// 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. | ||
|
||
// Deprecated: This package is intended for older projects transitioning from OPA v0.x and will remain for the lifetime of OPA v1.x, but its use is not recommended. | ||
// For newer features and behaviours, such as defaulting to the Rego v1 syntax, use the corresponding components in the [github.com/open-policy-agent/opa/v1] package instead. | ||
// See https://www.openpolicyagent.org/docs/latest/v0-compatibility/ for more information. | ||
package test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package test | ||
|
||
import ( | ||
"github.com/open-policy-agent/opa/ast" | ||
v1 "github.com/open-policy-agent/opa/v1/sdk/test" | ||
) | ||
|
||
// MockBundle sets a bundle named file on the test server containing the given | ||
// policies. | ||
func MockBundle(file string, policies map[string]string) func(*Server) error { | ||
return v1.MockBundle(file, policies) | ||
} | ||
|
||
// MockOCIBundle prepares the server to allow serving "/v2" OCI responses from the supplied policies | ||
// Ref parameter must be in the form of <registry>/<org>/<repo>:<tag> that will be used in detecting future calls | ||
func MockOCIBundle(ref string, policies map[string]string) func(*Server) error { | ||
return v1.MockOCIBundle(ref, policies) | ||
} | ||
|
||
// Ready provides a channel that the server will use to gate readiness. The | ||
// caller can provide this channel to prevent the server from becoming ready. | ||
// The server will response with HTTP 500 responses until ready. The caller | ||
// should close the channel to indicate readiness. | ||
func Ready(ch chan struct{}) func(*Server) error { | ||
return v1.Ready(ch) | ||
} | ||
|
||
// Server provides a mock HTTP server for testing the SDK and integrations. | ||
type Server = v1.Server | ||
|
||
// MustNewServer returns a new Server for test purposes or panics if an error occurs. | ||
func MustNewServer(opts ...func(*Server) error) *Server { | ||
return v1.MustNewServer(setRegoVersion(opts)...) | ||
} | ||
|
||
// NewServer returns a new Server for test purposes. | ||
func NewServer(opts ...func(*Server) error) (*Server, error) { | ||
return v1.NewServer(setRegoVersion(opts)...) | ||
} | ||
|
||
func RawBundles(raw bool) func(*Server) error { | ||
return v1.RawBundles(raw) | ||
} | ||
|
||
// ParserOptions sets the ast.ParserOptions to use when parsing modules when preparing bundles. | ||
func ParserOptions(popts ast.ParserOptions) func(*Server) error { | ||
return v1.ParserOptions(popts) | ||
} | ||
|
||
func setRegoVersion(opts []func(*Server) error) []func(*v1.Server) error { | ||
cpy := make([]func(*v1.Server) error, 0, len(opts)+1) | ||
cpy = append(cpy, opts...) | ||
|
||
// Sets rego-version to default (v0) if not set. | ||
// Must be last in list of options. | ||
cpy = append(cpy, func(s *v1.Server) error { | ||
if popts := s.ParserOptions(); popts.RegoVersion == ast.RegoUndefined { | ||
popts.RegoVersion = ast.DefaultRegoVersion | ||
return ParserOptions(popts)(s) | ||
} | ||
return nil | ||
}) | ||
|
||
return cpy | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright 2016 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. | ||
|
||
// Deprecated: This package is intended for older projects transitioning from OPA v0.x and will remain for the lifetime of OPA v1.x, but its use is not recommended. | ||
// For newer features and behaviours, such as defaulting to the Rego v1 syntax, use the corresponding components in the [github.com/open-policy-agent/opa/v1] package instead. | ||
// See https://www.openpolicyagent.org/docs/latest/v0-compatibility/ for more information. | ||
package test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2022 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 test | ||
|
||
import ( | ||
"github.com/open-policy-agent/opa/storage" | ||
v1 "github.com/open-policy-agent/opa/v1/storage/inmem/test" | ||
) | ||
|
||
// New returns an inmem store with some common options set: opt-out of write | ||
// roundtripping. | ||
func New() storage.Store { | ||
return v1.New() | ||
} | ||
|
||
// NewFromObject returns an inmem store from the passed object, with some | ||
// common options set: opt-out of write roundtripping. | ||
func NewFromObject(x map[string]interface{}) storage.Store { | ||
return v1.NewFromObject(x) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// 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. | ||
|
||
// Deprecated: This package is intended for older projects transitioning from OPA v0.x and will remain for the lifetime of OPA v1.x, but its use is not recommended. | ||
// For newer features and behaviours, such as defaulting to the Rego v1 syntax, use the corresponding components in the [github.com/open-policy-agent/opa/v1] package instead. | ||
// See https://www.openpolicyagent.org/docs/latest/v0-compatibility/ for more information. | ||
package authz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2019 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 authz contains unit and benchmark tests for authz use-cases | ||
// The public (non-test) APIs are meant to be used as helpers for | ||
// other tests to build off of. | ||
package authz | ||
|
||
import ( | ||
v1 "github.com/open-policy-agent/opa/v1/test/authz" | ||
) | ||
|
||
// Policy is a test rego policy for a token based authz system | ||
const Policy = v1.Policy | ||
|
||
// AllowQuery is the test query that goes with the Policy | ||
// defined in this package | ||
const AllowQuery = v1.AllowQuery | ||
|
||
// DataSetProfile defines how the test data should be generated | ||
type DataSetProfile = v1.DataSetProfile | ||
|
||
// InputMode defines what type of inputs to generate for testings | ||
type InputMode = v1.InputMode | ||
|
||
// InputMode types supported by GenerateInput | ||
const ( | ||
ForbidIdentity = v1.ForbidIdentity | ||
ForbidPath = v1.ForbidPath | ||
ForbidMethod = v1.ForbidMethod | ||
Allow = v1.Allow | ||
) | ||
|
||
// GenerateInput will use a dataset profile and desired InputMode to generate inputs for testing | ||
func GenerateInput(profile DataSetProfile, mode InputMode) (interface{}, interface{}) { | ||
return v1.GenerateInput(profile, mode) | ||
} | ||
|
||
// GenerateDataset will generate a dataset for the given DatasetProfile | ||
func GenerateDataset(profile DataSetProfile) map[string]interface{} { | ||
return v1.GenerateDataset(profile) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2020 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 cases contains utilities for evaluation test cases. | ||
package cases | ||
|
||
import ( | ||
v1 "github.com/open-policy-agent/opa/v1/test/cases" | ||
) | ||
|
||
// Set represents a collection of test cases. | ||
type Set = v1.Set | ||
|
||
// TestCase represents a single test case. | ||
type TestCase = v1.TestCase | ||
|
||
// Load returns a set of built-in test cases. | ||
func Load(path string) (Set, error) { | ||
return v1.Load(path) | ||
} | ||
|
||
// MustLoad returns a set of built-in test cases or panics if an error occurs. | ||
func MustLoad(path string) Set { | ||
return v1.MustLoad(path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// 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. | ||
|
||
// Deprecated: This package is intended for older projects transitioning from OPA v0.x and will remain for the lifetime of OPA v1.x, but its use is not recommended. | ||
// For newer features and behaviours, such as defaulting to the Rego v1 syntax, use the corresponding components in the [github.com/open-policy-agent/opa/v1] package instead. | ||
// See https://www.openpolicyagent.org/docs/latest/v0-compatibility/ for more information. | ||
package cases |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// 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. | ||
|
||
// Deprecated: This package is intended for older projects transitioning from OPA v0.x and will remain for the lifetime of OPA v1.x, but its use is not recommended. | ||
// For newer features and behaviours, such as defaulting to the Rego v1 syntax, use the corresponding components in the [github.com/open-policy-agent/opa/v1] package instead. | ||
// See https://www.openpolicyagent.org/docs/latest/v0-compatibility/ for more information. | ||
package e2e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// 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. | ||
|
||
// Deprecated: This package is intended for older projects transitioning from OPA v0.x and will remain for the lifetime of OPA v1.x, but its use is not recommended. | ||
// For newer features and behaviours, such as defaulting to the Rego v1 syntax, use the corresponding components in the [github.com/open-policy-agent/opa/v1] package instead. | ||
// See https://www.openpolicyagent.org/docs/latest/v0-compatibility/ for more information. | ||
package logs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package logs | ||
|
||
import ( | ||
v1 "github.com/open-policy-agent/opa/v1/test/e2e/logs" | ||
) | ||
|
||
// GeneratePolicy generates a policy for use in Decision Log e2e tests. The | ||
// `ruleCounts` determine how many total rules to generate, and the `ruleHits` | ||
// are the number of them that will be evaluated. This is keyed off of | ||
// the `input.hit` boolean value. | ||
func GeneratePolicy(ruleCounts int, ruleHits int) string { | ||
return v1.GeneratePolicy(ruleCounts, ruleHits) | ||
} | ||
|
||
// TestLogServer implements the decision log endpoint for e2e testing. | ||
type TestLogServer = v1.TestLogServer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2019 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 e2e | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/open-policy-agent/opa/v1/runtime" | ||
v1 "github.com/open-policy-agent/opa/v1/test/e2e" | ||
) | ||
|
||
// NewAPIServerTestParams creates a new set of runtime.Params with enough | ||
// default values filled in to start the server. Options can/should | ||
// be customized for the test case. | ||
func NewAPIServerTestParams() runtime.Params { | ||
return v1.NewAPIServerTestParams() | ||
} | ||
|
||
// TestRuntime holds metadata and provides helper methods | ||
// to interact with the runtime being tested. | ||
type TestRuntime = v1.TestRuntime | ||
|
||
// NewTestRuntime returns a new TestRuntime. | ||
func NewTestRuntime(params runtime.Params) (*TestRuntime, error) { | ||
return v1.NewTestRuntime(params) | ||
} | ||
|
||
// NewTestRuntimeWithOpts returns a new TestRuntime. | ||
func NewTestRuntimeWithOpts(opts TestRuntimeOpts, params runtime.Params) (*TestRuntime, error) { | ||
return v1.NewTestRuntimeWithOpts(opts, params) | ||
} | ||
|
||
// WrapRuntime creates a new TestRuntime by wrapping an existing runtime | ||
func WrapRuntime(ctx context.Context, cancel context.CancelFunc, rt *runtime.Runtime) *TestRuntime { | ||
return v1.WrapRuntime(ctx, cancel, rt) | ||
} | ||
|
||
// TestRuntimeOpts contains parameters for the test runtime. | ||
type TestRuntimeOpts = v1.TestRuntimeOpts | ||
|
||
// WithRuntime invokes f with a new TestRuntime after waiting for server | ||
// readiness. This function can be called inside of each test that requires a | ||
// runtime as opposed to RunTests which can only be called once. | ||
func WithRuntime(t *testing.T, opts TestRuntimeOpts, params runtime.Params, f func(rt *TestRuntime)) { | ||
v1.WithRuntime(t, opts, params, f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters