test

package
v4.14.1 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2023 License: MIT Imports: 30 Imported by: 0

Documentation

Overview

Package test implements the Benthos service unit testing command.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CliCommand

func CliCommand() *cli.Command

CliCommand is a cli.Command definition for unit testing.

func GetPathPair

func GetPathPair(fullPath, testSuffix string) (configPath, definitionPath string)

GetPathPair returns the config path and expected accompanying test definition path for a given syntax and a path for either file.

func GetTestTargets

func GetTestTargets(targetPaths []string, testSuffix string) (map[string]Definition, error)

GetTestTargets searches for test definition targets in a path with a given test suffix.

func OptAddResourcesPaths

func OptAddResourcesPaths(paths []string) func(*ProcessorsProvider)

OptAddResourcesPaths adds paths to files where resources should be parsed.

func OptProcessorsProviderSetLogger

func OptProcessorsProviderSetLogger(logger log.Modular) func(*ProcessorsProvider)

OptProcessorsProviderSetLogger sets the logger used by tested components.

func RunAll

func RunAll(paths []string, testSuffix string, lint bool, logger log.Modular, resourcesPaths []string) bool

RunAll executes the test command for a slice of paths. The path can either be a config file, a config files test definition file, a directory, or the wildcard pattern './...'.

Types

type Case

type Case struct {
	Name             string               `yaml:"name"`
	Environment      map[string]string    `yaml:"environment"`
	TargetProcessors string               `yaml:"target_processors"`
	TargetMapping    string               `yaml:"target_mapping"`
	Mocks            map[string]yaml.Node `yaml:"mocks"`
	InputBatch       []InputPart          `yaml:"input_batch"`
	InputBatches     [][]InputPart        `yaml:"input_batches"`
	OutputBatches    [][]ConditionsMap    `yaml:"output_batches"`
	// contains filtered or unexported fields
}

Case contains a definition of a single Benthos config test case.

func NewCase

func NewCase() Case

NewCase returns a default test case.

func (Case) AtLine

func (c Case) AtLine(l int) Case

AtLine returns a test case at a given line.

func (*Case) ExecuteFrom

func (c *Case) ExecuteFrom(dir string, provider ProcProvider) (failures []CaseFailure, err error)

ExecuteFrom executes a test case from the perspective of a given directory, which is used for obtaining relative condition file imports.

func (*Case) UnmarshalYAML

func (c *Case) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML extracts a Case from a YAML node.

type CaseFailure

type CaseFailure struct {
	Name     string
	TestLine int
	Reason   string
}

CaseFailure encapsulates information about a failed test case.

func (CaseFailure) String

func (c CaseFailure) String() string

String returns a string representation of the case failure.

type Condition

type Condition interface {
	Check(part *message.Part) error
}

Condition is a test case against a message part.

type ConditionsMap

type ConditionsMap map[string]Condition

ConditionsMap contains a map of conditions to condition string types.

func (ConditionsMap) CheckAll

func (c ConditionsMap) CheckAll(dir string, part *message.Part) (errs []error)

CheckAll checks all conditions against a message part. Conditions are executed in alphabetical order.

func (*ConditionsMap) UnmarshalYAML

func (c *ConditionsMap) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML extracts a ConditionsMap from a YAML node.

type ContentEqualsCondition

type ContentEqualsCondition string

ContentEqualsCondition is a string condition that tests the string against the contents of a message.

func (ContentEqualsCondition) Check

Check this condition against a message part.

type ContentJSONContainsCondition

type ContentJSONContainsCondition string

ContentJSONContainsCondition is a string condition that tests the string against the contents of a message using JSON comparison and is true if the expected and actual documents are both valid JSON, and the actual is a superset of the expected.

func (ContentJSONContainsCondition) Check

Check this condition against a message part.

type ContentJSONEqualsCondition

type ContentJSONEqualsCondition string

ContentJSONEqualsCondition is a string condition that tests the string against the contents of a message using JSON comparison and is true if the expected and actual documents are both valid JSON and deeply equal.

func (ContentJSONEqualsCondition) Check

Check this condition against a message part.

type ContentMatchesCondition

type ContentMatchesCondition string

ContentMatchesCondition is a string condition that tests parses the string as a regular expression and tests that regular expression against the contents of a message.

func (ContentMatchesCondition) Check

Check this condition against a message part.

type Definition

type Definition struct {
	Cases []Case `yaml:"tests"`
}

Definition of a group of tests for a Benthos config file.

func (Definition) Execute

func (d Definition) Execute(testFilePath string, resourcesPaths []string, logger log.Modular) ([]CaseFailure, error)

Execute the test definition.

type FileEqualsCondition

type FileEqualsCondition string

FileEqualsCondition is a string condition that reads a file at the string path and compares it against the contents of a message.

func (FileEqualsCondition) Check

func (c FileEqualsCondition) Check(p *message.Part) error

Check this condition against a message part.

type FileJSONContainsCondition

type FileJSONContainsCondition string

FileJSONContainsCondition is a string condition that tests the contents of the file against the contents of a message using JSON comparison and is true if the expected and actual documents are both valid JSON and the actual is a superset of the expected.

func (FileJSONContainsCondition) Check

Check this condition against a message part.

type FileJSONEqualsCondition

type FileJSONEqualsCondition string

FileJSONEqualsCondition is a string condition that tests the contents of the file against the contents of a message using JSON comparison and is true if the expected and actual documents are both valid JSON and deeply equal.

func (FileJSONEqualsCondition) Check

Check this condition against a message part.

type InputPart

type InputPart struct {
	Content  string         `yaml:"content"`
	Metadata map[string]any `yaml:"metadata"`
	// contains filtered or unexported fields
}

InputPart defines an input part for a test case.

func (*InputPart) UnmarshalYAML

func (i *InputPart) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML extracts an InputPart from a YAML node.

type MetadataEqualsCondition

type MetadataEqualsCondition map[string]any

MetadataEqualsCondition checks whether a metadata keys contents matches a value.

func (MetadataEqualsCondition) Check

Check this condition against a message part.

type ProcProvider

type ProcProvider interface {
	Provide(jsonPtr string, environment map[string]string, mocks map[string]yaml.Node) ([]iprocessor.V1, error)
	ProvideBloblang(path string) ([]iprocessor.V1, error)
}

ProcProvider returns compiled processors extracted from a Benthos config using a JSON Pointer.

type ProcessorsProvider

type ProcessorsProvider struct {
	// contains filtered or unexported fields
}

ProcessorsProvider consumes a Benthos config and, given a JSON Pointer, extracts and constructs the target processors from the config file.

func NewProcessorsProvider

func NewProcessorsProvider(targetPath string, opts ...func(*ProcessorsProvider)) *ProcessorsProvider

NewProcessorsProvider returns a new processors provider aimed at a filepath.

func (*ProcessorsProvider) Provide

func (p *ProcessorsProvider) Provide(jsonPtr string, environment map[string]string, mocks map[string]yaml.Node) ([]processor.V1, error)

Provide attempts to extract an array of processors from a Benthos config. Supports injected mocked components in the parsed config. If the JSON Pointer targets a single processor config it will be constructed and returned as an array of one element.

func (*ProcessorsProvider) ProvideBloblang

func (p *ProcessorsProvider) ProvideBloblang(pathStr string) ([]processor.V1, error)

ProvideBloblang attempts to parse a Bloblang mapping and returns a processor slice that executes it.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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