test

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package test defines helper variables and functions for testing policy templates and instances.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Decls represent the standard variables and functions available for testing templates.
	Decls = cel.Declarations(
		decls.NewVar("destination.ip", decls.String),
		decls.NewVar("origin.ip", decls.String),
		decls.NewVar("request.auth.claims", decls.NewMapType(decls.String, decls.Dyn)),
		decls.NewVar("request.time", decls.Timestamp),
		decls.NewVar("resource.name", decls.String),
		decls.NewVar("resource.type", decls.String),
		decls.NewVar("resource.labels", decls.NewMapType(decls.String, decls.String)),
		decls.NewFunction("locationCode",
			decls.NewOverload("location_code_string",
				[]*exprpb.Type{decls.String},
				decls.String,
			),
		),
		decls.NewFunction("rangeLow",
			decls.NewOverload("range_low",
				[]*exprpb.Type{decls.String},
				decls.Int,
			),
		),
		decls.NewFunction("rangeHigh",
			decls.NewOverload("range_high",
				[]*exprpb.Type{decls.String},
				decls.Int,
			),
		),
	)

	// Funcs are the custom function implementations used within templates.
	Funcs = []*functions.Overload{
		{
			Operator: "location_code_string",
			Unary: func(ip ref.Val) ref.Val {
				switch ip.(types.String) {
				case types.String("10.0.0.1"):
					return types.String("us")

				case types.String("10.0.0.2"):
					return types.String("de")
				default:
					return types.String("ir")
				}
			},
		},
		{
			Operator: "range_low",
			Unary: func(arg ref.Val) ref.Val {
				r := arg.(types.String).Value().(string)
				r = strings.TrimSpace(r)
				if r == "" {
					return types.Int(0)
				}
				if strings.Contains(r, "-") {
					rs := strings.Split(r, "-")
					if len(rs) != 2 {
						return types.ValOrErr(arg, "invalid port")
					}
					r = rs[0]
				}
				v, err := strconv.Atoi(r)
				if err != nil {
					return types.ValOrErr(arg, "invalid port")
				}
				return types.Int(v)
			},
		},
		{
			Operator: "range_high",
			Unary: func(arg ref.Val) ref.Val {
				r := arg.(types.String).Value().(string)
				r = strings.TrimSpace(r)
				if r == "" {
					return types.Int(65535)
				}
				if strings.Contains(r, "-") {
					rs := strings.Split(r, "-")
					if len(rs) != 2 {
						return types.ValOrErr(arg, "invalid port")
					}
					r = rs[1]
				}
				v, err := strconv.Atoi(r)
				if err != nil {
					return types.ValOrErr(arg, "invalid port")
				}
				return types.Int(v)
			},
		},
	}
)

Functions

func NewReader

func NewReader(relDir string) *reader

NewReader constructs a new test reader with the relative location of the testdata.

Types

type Case

type Case struct {
	// ID is a human-readable short-name for the test case.
	ID string

	// Kind is the type of test case this is, either template or instance.
	Kind string

	// In contains a reference to the source under test.
	In *model.Source

	// Out represents the output value expected from the test in a positive case.
	//
	// Note, positive test cases bear the '.out' suffix within testdata folders.
	Out string

	// Err is the error expected from the negative case of the test.
	//
	// Note, negative test cases bear the '.err' suffix within testdata folders.
	Err string
}

Case is a test case for use with parsing, compiling, or evaluating.

Jump to

Keyboard shortcuts

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