Skip to content

Commit

Permalink
Adding back v0 API test utils
Browse files Browse the repository at this point in the history
Signed-off-by: Johan Fylling <[email protected]>
  • Loading branch information
johanfylling committed Dec 9, 2024
1 parent 925583a commit 32690be
Show file tree
Hide file tree
Showing 13 changed files with 288 additions and 6 deletions.
8 changes: 8 additions & 0 deletions sdk/test/doc.go
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
65 changes: 65 additions & 0 deletions sdk/test/test.go
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
}
8 changes: 8 additions & 0 deletions storage/inmem/test/doc.go
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
22 changes: 22 additions & 0 deletions storage/inmem/test/testutil.go
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)
}
8 changes: 8 additions & 0 deletions test/authz/doc.go
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
43 changes: 43 additions & 0 deletions test/authz/testing.go
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)
}
26 changes: 26 additions & 0 deletions test/cases/cases.go
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)
}
8 changes: 8 additions & 0 deletions test/cases/doc.go
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
8 changes: 8 additions & 0 deletions test/e2e/doc.go
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
8 changes: 8 additions & 0 deletions test/e2e/logs/doc.go
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
16 changes: 16 additions & 0 deletions test/e2e/logs/utils.go
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
49 changes: 49 additions & 0 deletions test/e2e/testing.go
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)
}
25 changes: 19 additions & 6 deletions v1/sdk/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,21 @@ func Ready(ch chan struct{}) func(*Server) error {
}
}

// ParserOptions sets the ast.ParserOptions to use when parsing modules when preparing bundles.
func ParserOptions(popts ast.ParserOptions) func(*Server) error {
return func(s *Server) error {
s.parserOptions = popts
return nil
}
}

// Server provides a mock HTTP server for testing the SDK and integrations.
type Server struct {
server *httptest.Server
ready chan struct{}
bundles map[string]map[string]string
rawBundles bool
server *httptest.Server
ready chan struct{}
bundles map[string]map[string]string
rawBundles bool
parserOptions ast.ParserOptions
}

// MustNewServer returns a new Server for test purposes or panics if an error occurs.
Expand Down Expand Up @@ -100,6 +109,10 @@ func RawBundles(raw bool) func(*Server) error {
}
}

func (s *Server) ParserOptions() ast.ParserOptions {
return s.parserOptions
}

// WithTestBundle adds a bundle to the server at the specified endpoint.
func (s *Server) WithTestBundle(endpoint string, policies map[string]string) *Server {
s.bundles[endpoint] = policies
Expand All @@ -121,7 +134,7 @@ func (s *Server) buildBundles(ref string, policies map[string]string) error {
// Prepare the modules to include in the bundle. Sort them so bundles are deterministic.
modules := make([]bundle.ModuleFile, 0, len(policies))
for url, str := range policies {
module, err := ast.ParseModule(url, str)
module, err := ast.ParseModuleWithOpts(url, str, s.parserOptions)
if err != nil {
return fmt.Errorf("failed to parse module: %v", err)
}
Expand Down Expand Up @@ -402,7 +415,7 @@ func (s *Server) handleBundles(w http.ResponseWriter, r *http.Request) {
return
}
case strings.HasSuffix(url, ".rego"):
module, err := ast.ParseModule(url, str)
module, err := ast.ParseModuleWithOpts(url, str, s.parserOptions)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
Expand Down

0 comments on commit 32690be

Please sign in to comment.