test_bootstrap

package
v1.45.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2025 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package test_bootstrap implements the logic for generating Rego test mocks automatically from an OPA bundle and entrypoint list.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ASTObjectsFromRefs

func ASTObjectsFromRefs(refs []ast.Ref, defaultLeafValue *ast.Term) (*ast.Term, *ast.Term, error)

Builds Rego AST objects, based on the refs provided to it. Note(philip): I wanted to do this without needing the EditTree structure from OPA's internals, but it certainly makes some aspects of the structure generation here very straightforward.

func GetCustomAnnotationsForRefs

func GetCustomAnnotationsForRefs(compiler *ast.Compiler) (map[string][]*ast.AnnotationsRef, error)

Returns a map of targeted rule paths and their accompanying annotations refs.

func GetFileLocationForRuleRef

func GetFileLocationForRuleRef(ruleRef ast.Ref, compiler *ast.Compiler) string

Note(philip): This selects the first non-nil file location found for the rule ref. In cases where multiple files may match the ref, this results in one of those files being selected *arbitrarily*. I'm not sure how to make this less awful, but we can at least log where things will land.

func GetInputFromRuleDeps

func GetInputFromRuleDeps(rule *ast.Rule, compiler *ast.Compiler) (*ast.Term, error)

This function plugs in a rule, and attempts to extract a useful input object for the testcase.

func GetPackageForRuleRef

func GetPackageForRuleRef(ruleRef ast.Ref, compiler *ast.Compiler) string

Note(philip): This selects the first non-nil file package found for the rule.

func GetRuleCustomAnnotationWithKey

func GetRuleCustomAnnotationWithKey(key string, entrypoint ast.Ref, compiler *ast.Compiler) (*ast.AnnotationsRef, error)

func GetTestNamesToCustomAnnotations

func GetTestNamesToCustomAnnotations(compiler *ast.Compiler) (map[string][]*ast.AnnotationsRef, error)

Returns a map of targeted rule paths corresponding to the `test-bootstrap-name` custom annotation.

func LoadPolicies

func LoadPolicies(dataPaths []string, ignores []string) (*ast.Compiler, error)

Loads up all of the specified Rego sources. Returns an ast.Compiler instance.

func RefPtrToQuery

func RefPtrToQuery(rp string) string

func StartBootstrap

func StartBootstrap(_ context.Context, opt ...Opt) error

func StartNew

func StartNew(_ context.Context, opt ...Opt) error

func TestcasesForRule

func TestcasesForRule(testName string, rule *ast.Rule, compiler *ast.Compiler) (string, error)

func TestcasesFromRef

func TestcasesFromRef(ruleRef ast.Ref, generatedNames map[string]string, compiler *ast.Compiler) (string, error)

This function is the place where all the testcase generation pieces are assembled. It takes a virtual document reference, and then discovers all rules that apply to it. Each rule is then individually analyzed, and a testcase is generated for it using the testcase templates. The collected set of testcases is then returned as a string.

func VivifyTree

func VivifyTree(tree *edittree.EditTree, ref ast.Ref)

Note(philip): This ensures that if we encounter a long ref, like 'data.my.long.chain.of.refs', we can build an EditTree with the correct structure, such that an (*EditTree).InsertAtPath can reach the 'refs' leaf safely.

Types

type Opt

type Opt func(*opts)

func Annotation

func Annotation(a string) Opt

func DataPaths

func DataPaths(ds []string) Opt

func Entrypoint

func Entrypoint(e string) Opt

func Entrypoints

func Entrypoints(es []string) Opt

func Force

func Force(f bool) Opt

func Ignores

func Ignores(i []string) Opt

func Logger

func Logger(l logging.Logger) Opt

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL