package e2e

import "github.com/open-policy-agent/opa/v1/test/e2e"

Index

Functions

func NewAPIServerTestParams

func NewAPIServerTestParams() runtime.Params

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 WithRuntime

func WithRuntime(t *testing.T, opts TestRuntimeOpts, params runtime.Params, f func(rt *TestRuntime))

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.

Types

type TestRuntime

type TestRuntime struct {
	Params        runtime.Params
	Runtime       *runtime.Runtime
	Ctx           context.Context
	Cancel        context.CancelFunc
	Client        *http.Client
	ConsoleLogger *test.Logger
	// contains filtered or unexported fields
}

TestRuntime holds metadata and provides helper methods to interact with the runtime being tested.

func NewTestRuntime

func NewTestRuntime(params runtime.Params) (*TestRuntime, error)

NewTestRuntime returns a new TestRuntime.

func NewTestRuntimeWithOpts

func NewTestRuntimeWithOpts(opts TestRuntimeOpts, params runtime.Params) (*TestRuntime, error)

NewTestRuntimeWithOpts returns a new TestRuntime.

func WrapRuntime

func WrapRuntime(ctx context.Context, cancel context.CancelFunc, rt *runtime.Runtime) *TestRuntime

WrapRuntime creates a new TestRuntime by wrapping an existing runtime

func (*TestRuntime) AddrToURL

func (t *TestRuntime) AddrToURL(addr string) (string, error)

AddrToURL generates a full URL from an address, as configured on the runtime. This can include fully qualified urls, just host/ip, with port, or only port (eg, "localhost", ":8181", "http://foo", etc). If the runtime is configured with HTTPS certs it will generate an appropriate URL.

func (*TestRuntime) CompileRequest

func (t *TestRuntime) CompileRequest(req types.CompileRequestV1) (*types.CompileResponseV1, error)

CompileRequest will use the v1 compile API and POST with the given request.

func (*TestRuntime) CompileRequestWithInstrumentation

func (t *TestRuntime) CompileRequestWithInstrumentation(req types.CompileRequestV1) (*types.CompileResponseV1, error)

CompileRequestWithInstrumentation will use the v1 compile API and POST with the given request and instrumentation enabled.

func (*TestRuntime) DeletePolicy

func (t *TestRuntime) DeletePolicy(name string) error

DeletePolicy will delete the given policy in the runtime via the v1 policy API

func (*TestRuntime) GetData

func (t *TestRuntime) GetData(url string) (io.ReadCloser, error)

GetData will use the v1 data API and GET without input. The returned value is the full response body.

func (*TestRuntime) GetDataWithInput

func (t *TestRuntime) GetDataWithInput(path string, input any) ([]byte, error)

GetDataWithInput will use the v1 data API and POST with the given input. The returned value is the full response body.

func (*TestRuntime) GetDataWithInputTyped

func (t *TestRuntime) GetDataWithInputTyped(path string, input any, response any) error

GetDataWithInputTyped returns an unmarshalled response from GetDataWithInput.

func (*TestRuntime) GetDataWithRawInput

func (t *TestRuntime) GetDataWithRawInput(url string, input io.Reader) (io.ReadCloser, error)

GetDataWithRawInput will use the v1 data API and POST with the given input. The returned value is the full response body.

func (*TestRuntime) HealthCheck

func (t *TestRuntime) HealthCheck(url string) error

HealthCheck will query /health and return an error if the server is not healthy

func (*TestRuntime) RunAPIServerBenchmarks

func (t *TestRuntime) RunAPIServerBenchmarks(m *testing.M) int

RunAPIServerBenchmarks will start the OPA runtime and do `m.Run()` similar to how RunAPIServerTests works. This will suppress logging output on stdout to prevent the tests from being overly verbose. If log output is desired set the `test.v` flag. Deprecated: Use RunTests instead

func (*TestRuntime) RunAPIServerTests

func (t *TestRuntime) RunAPIServerTests(m *testing.M) int

RunAPIServerTests will start the OPA runtime serving with a given configuration. This is essentially a wrapper for `m.Run()` that handles starting and stopping the local API server. The return value is what should be used as the code in `os.Exit` in the `TestMain` function. Deprecated: Use RunTests instead

func (*TestRuntime) RunTests

func (t *TestRuntime) RunTests(m *testing.M) int

RunTests will start the OPA runtime serving with a given configuration. This is essentially a wrapper for `m.Run()` that handles starting and stopping the local API server. The return value is what should be used as the code in `os.Exit` in the `TestMain` function.

func (*TestRuntime) URL

func (t *TestRuntime) URL() string

URL will return the URL that the server is listening on. If the server hasn't started listening this will return an empty string. It is not expected for the URL to change throughout the lifetime of the TestRuntime. Runtimes configured with >1 address will only get the first URL.

func (*TestRuntime) UploadData

func (t *TestRuntime) UploadData(data io.Reader) error

UploadData will upload the given data to the runtime via the v1 data API

func (*TestRuntime) UploadDataToPath

func (t *TestRuntime) UploadDataToPath(path string, data io.Reader) error

UploadDataToPath will upload the given data to the runtime via the v1 data API

func (*TestRuntime) UploadPolicy

func (t *TestRuntime) UploadPolicy(name string, policy io.Reader) error

UploadPolicy will upload the given policy to the runtime via the v1 policy API

func (*TestRuntime) WaitForServer

func (t *TestRuntime) WaitForServer() error

WaitForServer will block until the server is running and passes a health check.

func (*TestRuntime) WaitForServerStatus

func (t *TestRuntime) WaitForServerStatus(status runtime.ServerStatus) error

type TestRuntimeOpts

type TestRuntimeOpts struct {
	WaitForBundles   bool // indicates if readiness check should depend on bundle activation
	PostServeActions func(rt *TestRuntime) error
}

TestRuntimeOpts contains parameters for the test runtime.

Source Files

testing.go

Directories

PathSynopsis
v1/test/e2e/authznolint: deadcode,unused // build tags confuse these linters
v1/test/e2e/certrefresh
v1/test/e2e/concurrency
v1/test/e2e/diagnostics
v1/test/e2e/distributedtracing
v1/test/e2e/h2c
v1/test/e2e/http
v1/test/e2e/logs
v1/test/e2e/logs/console
v1/test/e2e/metrics
v1/test/e2e/oci
v1/test/e2e/print
v1/test/e2e/shutdown
v1/test/e2e/tls
v1/test/e2e/wasm
v1/test/e2e/wasm/authznolint: deadcode,unused // build tags confuse these linters
Version
v1.5.1 (latest)
Published
Jun 4, 2025
Platform
linux/amd64
Imports
20 packages
Last checked
2 days ago

Tools for package owners.