pipelinetest

package
v0.0.0-...-6f38d97 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2019 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package pipelinetest contains helper utilities for constructing test cases that exercise the components of a NEL collector pipeline.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetPayloadNames

func GetPayloadNames(basePath string) ([]string, error)

GetPayloadNames returns the PayloadNames of any input files found in a particular directory. (This makes it easy to write TestdataLoader instances that load input files from the local filesystem; all you have to do in your GetPayloadNames method is identify the correct path, and then delegate to this helper function.)

func NewTestConfigPipeline

func NewTestConfigPipeline(configString string) *collector.Pipeline

NewTestConfigPipeline constructs a new Pipeline from the contents of a TOML configuration string. We assume that configString is well-formed and panic if there are any errors parsing it, or configuring the pipeline's processors. This is especially useful in test cases, along with PipelineTest.

Types

type DefaultTestdataLoader

type DefaultTestdataLoader struct {
	// The path containing the testdata directory where we can find the files
	// containing the input report payloads.  For tests of package pipelinetest
	// itself, the default value ("") is correct.  If you want to test processors
	// in other packages, and reuse the input files from pipelinetest, set this to
	// the directory containing pipelinetest's testdata directory.
	InputPath string

	// The path containing the testdata directory where we can find the golden
	// files containing the expected output for your test case.  For most
	// packages, the default value ("") is correct.
	OutputPath string

	// Whether to update the content of the golden files with the current actual
	// test output.  You'll usually set this to the value of an `--update`
	// command-line flag.
	UpdateGoldenFiles bool
}

DefaultTestdataLoader looks for test and golden data files in `testdata` directories in the source packages being tested.

We expect the following directory structure:

[InputPath]/
  testdata/
    reports/
      [PayloadName].json
[OutputPath]/
  testdata/
    [TestName]/
      [PayloadName].[IPTag].[OutputExtension]

InputPath and OutputPath both default to the current directory if empty, which lines up with the `go test` convention of running test cases in the directory of the package being tested.

func (DefaultTestdataLoader) GetPayloadNames

func (l DefaultTestdataLoader) GetPayloadNames() ([]string, error)

GetPayloadNames returns the PayloadNames of any input files found in InputPath.

func (DefaultTestdataLoader) LoadInputFile

func (l DefaultTestdataLoader) LoadInputFile(testCase TestCase) ([]byte, error)

LoadInputFile loads the contents of an input file from InputPath.

func (DefaultTestdataLoader) LoadOutputFile

func (l DefaultTestdataLoader) LoadOutputFile(testCase TestCase, got []byte) ([]byte, error)

LoadOutputFile loads the contents of a golden file from OutputPath, updating its contents with `got` if UpdateGoldenFiles is true.

type EncodeBatchAsResult

type EncodeBatchAsResult struct{}

EncodeBatchAsResult is a pipeline processor that saves a copy of the report batch into the TestResult annotation. You can use this with PipelineTest to use the full contents of the batch (including annotations) as the output to compare in your test case.

func (EncodeBatchAsResult) ProcessReports

func (e EncodeBatchAsResult) ProcessReports(ctx context.Context, batch *collector.ReportBatch)

ProcessReports saves a copy of the report batch into the TestResult annotation.

type PipelineTest

type PipelineTest struct {
	// The name of the test case that will use this helper.  Must be unique across
	// all test cases that use the same OutputPath.
	TestName string

	// The pipeline being tested.  It should include a processor that adds a
	// []byte annotation named `TestResult` to the report batch; we'll verify the
	// contents of this annotation to determine whether each test succeeded.
	Pipeline *collector.Pipeline

	// The extension that we should use for the golden files for your test case.
	// If empty, we will use ".json".
	OutputExtension string

	// The loader that will be used to read input and output files for each test
	// case.
	Testdata TestdataLoader

	// The upload url to be passed to the processor. If no value is provided, the
	// default upload URL of https://example.com/upload/ is used.
	URL string
}

PipelineTest automates the process of running a NEL collector pipeline against a large number of test uploads.

We use testdata files to store several input report payloads, and golden files to hold the expected output of your pipeline for each of those payloads. Running the test cases with the `--update` flag will overwrite the golden files with the current output from your pipeline.

func (*PipelineTest) Run

func (p *PipelineTest) Run(t *testing.T)

Run tests your pipeline against all of the input files that we found in your InputPath, comparing the values of the TestResult annotation with the corresponding golden files in OutputPath. It uses the default upload URL of https://example.com/upload/

type SimulatedClock

type SimulatedClock struct {
	CurrentTime time.Time
}

SimulatedClock is a Clock that gives you full control over which times are reported. This can be used in test cases to give reproducible timestamps in your expected output.

func NewSimulatedClock

func NewSimulatedClock() *SimulatedClock

NewSimulatedClock returns a new SimulatedClock last initially reports the Unix expoch (midnight January 1, 1970) as the current time.

func (SimulatedClock) Now

func (c SimulatedClock) Now() time.Time

Now returns the current time according to this SimulatedClock.

type TestCase

type TestCase struct {
	// The name of the PipelineTest that created this test case.
	TestName string

	// The name of the payload file that is used as input.  This will be the base
	// filename of one of the files in the PipelineTest's InputPath, with the
	// .json extension removed.
	PayloadName string

	// Whether the payload is fake-uploaded via `ipv4` or `ipv6`.
	IPTag string

	// The golden file extension for this test case.  Will never be empty.
	OutputExtension string
}

TestCase describes one test case managed by a PipelineTest.

func (TestCase) BaseInputFilename

func (c TestCase) BaseInputFilename() string

BaseInputFilename returns the base filename of the input file for a test case.

func (TestCase) BaseOutputFilename

func (c TestCase) BaseOutputFilename() string

BaseOutputFilename returns the base filename of the output file for a test case.

func (TestCase) FullName

func (c TestCase) FullName() string

FullName returns the full name of the test case, including the overall test name.

func (TestCase) Name

func (c TestCase) Name() string

Name returns the name of the test case, relative to the test name.

type TestdataLoader

type TestdataLoader interface {
	// GetPayloadNames finds all available input files and returns their
	// PayloadNames.
	GetPayloadNames() ([]string, error)

	// LoadInputFile loads in the content of the input file for a particular test
	// case.
	LoadInputFile(testCase TestCase) ([]byte, error)

	// LoadOutputFile loads in the content of the golden output file for a
	// particular test case.  If the test is run in "update" mode (which is up to
	// you to decide, typically via an `--update` flag), then you should replace
	// any existing content with `got`, and then return `got`.
	LoadOutputFile(testCase TestCase, got []byte) ([]byte, error)
}

TestdataLoader is a helper interface that PipelineTest uses to find, read, and write the testdata and golden files for a set of test cases.

Jump to

Keyboard shortcuts

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