testhelper

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2018 License: BSD-2-Clause Imports: 22 Imported by: 0

Documentation

Overview

Package greenery/testhelper is a helper library used to test greenery-based applications.

Testhelper is a package containing many utility functions intended to make unit testing of greenery applications simpler, see the greenery unit tests for a lot of usage examples.

Index

Constants

This section is empty.

Variables

View Source
var ExtraConfigCustomComparers = map[string]CompareFunc{
	"FlagIP":      CompareGetterToGetter,
	"FlagEnum":    CompareGetterToGetter,
	"FlagInt":     CompareGetterToGetter,
	"FlagCString": CompareGetterToGetter,
	"NameValue":   ExtraConfigCustomCompareNameValue,
	"NameEnum":    ExtraConfigCustomCompareNameEnum,
}

ExtraConfigCustomComparers is a set of compare functions used for the built-in base config types

View Source
var ExtraConfigCustomVars = []string{
	"custom.namevalue",
	"custom.nameenum",
}

ExtraConfigCustomVars is the names of the custom structs to deserialize

View Source
var ExtraDocs = greenery.DocSet{
	Usage: map[string]*greenery.CmdHelp{
		"int": &greenery.CmdHelp{
			Short: "int flags",
		},
		"bool": &greenery.CmdHelp{
			Short: "bool flags",
		},
		"float": &greenery.CmdHelp{
			Short: "float flags",
		},
		"string": &greenery.CmdHelp{
			Short: "string flags",
		},
		"flag": &greenery.CmdHelp{
			Short: "flag flags",
		},
		"custom": &greenery.CmdHelp{
			Short: "custom config values",
		},
	},
	CmdLine: map[string]string{
		"Bool": "cmdline bool",

		"FlagCString": "cmdline cstring",
		"FlagEnum":    "cmdline enum",
		"FlagIP":      "cmdline ip",
		"FlagInt":     "cmdline int",
		"FlagPort":    "cmdline port",

		"Float32": "cmdline float32",
		"Float64": "cmdline float64",

		"Int":    "cmdline int",
		"Int16":  "cmdline int16",
		"Int32":  "cmdline int32",
		"Int64":  "cmdline int64",
		"Int8":   "cmdline int8",
		"Uint":   "cmdline uint",
		"Uint16": "cmdline uint16",
		"Uint32": "cmdline uint32",
		"Uint64": "cmdline uint64",
		"Uint8":  "cmdline uint8",

		"String": "cmdline string",
	},
	ConfigFile: map[string]string{
		greenery.DocConfigHeader: "Config generated while testing",

		"bool.": "config bool section",
		"Bool":  "config bool",

		"custom.": "config custom section",
		"NameValue": `# some example custom parameter name values
[[custom.namevalue]]
name = "k1"
value = "v1"

[[custom.namevalue]]
name = "k2"
value = "v2"

[[custom.namevalue]]
name = "k3"
value = "v3"`,
		"NameEnum": `# some example custom parameter name enum, valid values a,b,c
[[custom.nameenum]]
name = "k1"
enum = "a"

[[custom.nameenum]]
name = "k2"
enum = "b"

[[custom.nameenum]]
name = "k3"
enum = "c"`,

		"flag.":       "config flag section",
		"FlagCString": "config cstring",
		"FlagEnum":    "config enum",
		"FlagIP":      "config ip",
		"FlagInt":     "config int",
		"FlagPort":    "config port",

		"Float32": "config float32",
		"Float64": "config float64",

		"int.":     "config int section",
		"Int":      "config int",
		"Int16":    "config int16",
		"Int32":    "config int32",
		"Int64":    "config int64",
		"Int8":     "config int8",
		"Uint":     "config uint",
		"Uint16":   "config uint16",
		"Uint32":   "config uint32",
		"Uint64":   "config uint64",
		"Uint8":    "config uint8",
		"Time":     "config time",
		"PTime":    "config ptime",
		"Duration": "config duration",

		"string.": "config string section",
		"String":  "config string",

		"slice.":      "config slice section",
		"SliceString": "config slice string",
		"SliceInt":    "config slice int",
	},
}

ExtraDocs is a documentation list for the config with all possible types of fields

View Source
var ForceNoParallel = flag.Bool("test.force-no-parallel", false, "force no parallel")

ForceNoParallel is a flag that if set will disable parallel test execution

View Source
var ForceTrace = flag.Bool("test.force-trace", false, "force trace")

ForceTrace is the flag controlling if the test should be run with tracing enabled for test debugging purposes

View Source
var GoldUpdate = flag.Bool("test.update-gold", false, "update golden files")

GoldUpdate is the flag controlling whether the gold files should be updated

Functions

func CompareGetterToGetter

func CompareGetterToGetter(t *testing.T, name string, expected, current interface{}) error

CompareGetterToGetter is a comparer that will compare the values returned by the GetTyped method for this particular flag

func CompareIgnoreTmp

func CompareIgnoreTmp(t *testing.T, gold, found []byte) bool

CompareIgnoreTmp is a function that will compare two files ignoring any temporary files listed in them. This is typically used when comparing configuration files where we want to ignore the log file line, say, is /tmp/xxx in the currently executing test and /tmp/yyy in the gold file

func CompareValueToGetter

func CompareValueToGetter(t *testing.T, name string, expected, current interface{}) error

CompareValueToGetter is a comparer that will compare the value of the flag as returned by the GetTyped method, to the expected value as-is

func ExtraConfigCustomCompareNameEnum

func ExtraConfigCustomCompareNameEnum(t *testing.T, name string, vl, vr interface{}) error

ExtraConfigCustomCompareNameEnum is a comparer function for the ExtraConfigCustomNameEnum type

func ExtraConfigCustomCompareNameValue

func ExtraConfigCustomCompareNameValue(t *testing.T, name string, vl, vr interface{}) error

ExtraConfigCustomCompareNameValue is a comparer function for the ExtraConfigCustomNameValue type

func ExtraConfigCustomParse

func ExtraConfigCustomParse(lcfg greenery.Config, vals map[string]interface{}) (processed []string, err error)

ExtraConfigCustomParse is a parser for the custom structs in the config

func LoggingTester

func LoggingTester(cfg greenery.Config, args []string) error

LoggingTester is a greenery handler that will output a log message at all possible levels / verbosities.

func NewExtraConfig

func NewExtraConfig() greenery.Config

NewExtraConfig instantiates the configuration with all possible types of field

func NewGrabber

func NewGrabber() *outGrabber

NewGrabber creates a new grabber struct

func NewSimpleConfig

func NewSimpleConfig() greenery.Config

NewSimpleConfig returns an instance of the configuration

func NopNoArgs

func NopNoArgs(lcfg greenery.Config, args []string) error

NopNoArgs is a command handler that doesn't do anything

func RunTestCases

func RunTestCases(t *testing.T, tcs []TestCase, global TestRunnerOptions) error

RunTestCases will run the declared test cases in .Run() subtests

func TempFileT

func TempFileT(t *testing.T, af afero.Fs, prefix, suffix, contents string, cleanup *[]string, mtx sync.Locker, real bool) (name string, err error)

TempFileT is a utility function typically used to create temporary configuration files to test with.

func UcFirstAndUpcase

func UcFirstAndUpcase(name, s string, data interface{}) (string, error)

UcFirstAndUpcase is an arbitrary validation function used by the custom string flag in ExtraConfig, it will fail if the value does not start with an uppercase character, and will return an uppercased version of the value

Types

type CompareFunc

type CompareFunc func(*testing.T, string, interface{}, interface{}) error

CompareFunc is an interface that is fulfilled by compare functions, used when verifying config struct values

type Comparer

type Comparer struct {
	Value        interface{}
	Accessor     string
	Compare      CompareFunc
	DefaultValue bool
}

Comparer is a struct containing a single expected value, how to retrieve it, and how to compare it.

type ExtraConfig

type ExtraConfig struct {
	*greenery.BaseConfig

	Bool bool `greenery:"bool|bool|,    bool.bool, BOOL"`

	NameValue []ExtraConfigCustomNameValue `greenery:"||custom,  custom.namevalue,"`
	NameEnum  []ExtraConfigCustomNameEnum  `greenery:"||custom,  custom.nameenum,"`

	FlagIP      *greenery.IPValue           `greenery:"flag|ip|,      flag.ip,      FLAGIP"`
	FlagPort    greenery.PortValue          `greenery:"flag|port|,    flag.port,    FLAGPORT"`
	FlagEnum    *greenery.EnumValue         `greenery:"flag|enum|,    flag.enum,    FLAGENUM"`
	FlagInt     *greenery.IntValue          `greenery:"flag|int|,     flag.int,     FLAGINT"`
	FlagCString *greenery.CustomStringValue `greenery:"flag|cstring|, flag.cstring, FLAGCSTRING"`

	Float32 float32 `greenery:"float|float32|,        float.float32,     FLOAT32"`
	Float64 float64 `greenery:"float|float64|,        float.float64,     FLOAT64"`

	Int      int           `greenery:"int|int|,          int.int,       INT"`
	Int8     int8          `greenery:"int|int8|,         int.int8,      INT8"`
	Int16    int16         `greenery:"int|int16|,        int.int16,     INT16"`
	Int32    int32         `greenery:"int|int32|,        int.int32,     INT32"`
	Int64    int64         `greenery:"int|int64|,        int.int64,     INT64"`
	Uint     uint          `greenery:"int|uint|,         int.uint,      UINT"`
	Uint8    uint8         `greenery:"int|uint8|,        int.uint8,     UINT8"`
	Uint16   uint16        `greenery:"int|uint16|,       int.uint16,    UINT16"`
	Uint32   uint32        `greenery:"int|uint32|,       int.uint32,    UINT32"`
	Uint64   uint64        `greenery:"int|uint64|,       int.uint64,    UINT64"`
	Time     time.Time     `greenery:"||none,            int.time,      TIME"`
	PTime    *time.Time    `greenery:"||none,            int.ptime,     PTIME"`
	Duration time.Duration `greenery:"||none,            int.duration,  DURATION"`

	String string `greenery:"string|string|,    string.string, STRING"`

	SliceString []string `greenery:"||none,    slice.string, "`
	SliceInt    []int    `greenery:"||none,    slice.int, "`
	// contains filtered or unexported fields
}

ExtraConfig is a config with all possible types of field

type ExtraConfigCustomNameEnum

type ExtraConfigCustomNameEnum struct {
	Name string
	Enum *greenery.EnumValue
}

ExtraConfigCustomNameEnum is a sample struct for custom deserialization

func (*ExtraConfigCustomNameEnum) GetTyped

func (e *ExtraConfigCustomNameEnum) GetTyped() []string

GetTyped is an accessor returning the name/value pair as a string list

type ExtraConfigCustomNameValue

type ExtraConfigCustomNameValue struct {
	Name  string
	Value string
}

ExtraConfigCustomNameValue is a sample struct for custom deserialization

func (*ExtraConfigCustomNameValue) GetTyped

func (e *ExtraConfigCustomNameValue) GetTyped() []string

GetTyped is an accessor returning the name/value pair as a string list

type LogLine

type LogLine struct {
	Level       string
	Msg         string
	MsgRegex    string
	Custom      map[string]interface{}
	CustomRegex map[string]string
}

LogLine contains an expected log line to compare to. Note that lines pass through json which will deserialize things like time.Time as strings etc.

type SimpleConfig

type SimpleConfig struct {
	*greenery.BaseConfig
}

SimpleConfig is the simplest configuration, only base fields

type TestCase

type TestCase struct {
	// Name contains the name of the test, will be used as t.Run()'s name
	Name string

	// Env is a map of strings containing the environment for this test case
	Env map[string]string

	// CmdLine is the command line passed to our executable
	CmdLine []string

	// CfgContents is a string containing the configuration file contents for
	// this test
	CfgContents string

	// ExecError is the expected exec function error (if any) this will be
	// matched as a substring
	ExecError string

	// ExecErrorRegex is the expected exec function error (if any) this will be
	// matched as a regex
	ExecErrorRegex string

	// ExecErrorOutput will cause the output of the program (stdout/err/logs)
	// to be checked even if the execution had an error
	ExecErrorOutput bool

	// ExpectedValues is a map of interface{} containing the values that are
	// to be expected to be set in the config object after the environment,
	// command line and configuration file are taken into account. Any config
	// object fields not included here will be validated to remain their
	// default value as set by the config generator function. This will be
	// ignored if Validate is set
	ExpectedValues map[string]Comparer

	// ValuesValidator contains a custom validation function, which will be used
	// instead of the internal configuration values validation. It will be
	// passed the configuration, it is expected to fail the test itself if the
	// validation is unsuccessful.
	ValuesValidator func(*testing.T, greenery.Config)

	// Trace is used to output trace log messages, trace is an undocumented
	// log level used primarily during development.
	Trace bool

	// CmdlineCfgName contains a name to be passed as the config file name to
	// the test, it is typically used to validate a non-existent configuration
	// file, because configuration files are passed via CfgContents
	CmdlineCfgName string

	// ConfigGen is the function that will return the configuration struct to
	// be used
	ConfigGen func() greenery.Config

	// OverrideHandlerMap will enable overriding of the test handler map via
	// HandlerMap
	OverrideHandlerMap bool

	// HandlerMap is a map of handlers, that will be used on the passed in
	// configuration.
	HandlerMap map[string]greenery.Handler

	// OverrideBuiltinHandlers will enable overriding the test default
	// handlers for the configuration
	OverrideBuiltinHandlers bool

	// BuiltinHandlers is a map of override builtin handlers, that will be
	// used on the passed in configuration. Note the key of the map is a
	// string, not an OverrideHandler type to allow arbitrary test overrides.
	BuiltinHandlers map[string]greenery.Handler

	// OverrideUserDocList allows overriding of the test user doc list
	OverrideUserDocList bool

	// UserDocList is a map with the application user documents, if the map
	// contains a "" key, that will be meant to indicate the default language
	// docset.
	UserDocList map[string]*greenery.DocSet
	CfgFile     string

	// GoldStdOut contains the gold expected standard output for this test case
	GoldStdOut *TestFile

	// GoldStdErr contains the gold expected standard error for this test case
	GoldStdErr *TestFile

	// GoldStdOut contains the gold expected logfile for this test case
	GoldLog string

	// NoValidateConfigValues if set will cause the test to not look at the
	// configuration values
	NoValidateConfigValues bool

	// ConfigDefaults allows specifying config defaults via SetOptions on a
	// per-testcase basis
	ConfigDefaults *greenery.BaseConfigOptions

	// GoldFiles is a list of expected gold files
	GoldFiles []TestFile

	// PrecreateFiles is a list of files to be created in the test environment
	// before executing the test
	PrecreateFiles []TestFile

	// RealFilesystem controls whether the test executes on the filesystem or
	// on an in-memory cache
	RealFilesystem bool

	// RemoveFiles contains a list of files to be removed after the test
	// execution
	RemoveFiles []string

	// CustomVars is a list of expected custom variables in the config file
	CustomVars []string

	// CustomParser contains a custom parser to be used for custom variables
	CustomParser func(greenery.Config, map[string]interface{}) ([]string, error)

	// OverrideTraceLogger controls overriding of the test default trace
	// logger creation function
	OverrideTraceLogger bool

	// TraceLogger contains the MakeTraceLogger function to use
	TraceLogger greenery.MakeTraceLogger

	// OverridePrettyLogger controls overriding of the test default pretty
	// logger creation function
	OverridePrettyLogger bool

	// PrettyLogger contains the MakePrettyLogger function to use
	PrettyLogger greenery.MakeLogger

	// OverrideStructuredLogger controls overriding of the test default
	// structured logger creation function
	OverrideStructuredLogger bool

	// StructuredLogger contains the MakeStructuredLogger function to use
	StructuredLogger greenery.MakeLogger

	// OutStdOut is the expected stdout output from the executed command
	OutStdOut string

	// OutStdOutRegex will be matched against the output from the executed command
	OutStdOutRegex string

	// OutStdErr is the expected stderr output from the executed command
	OutStdErr string

	// OutStdErrRegex will be matched against the stderr output from the executed command
	OutStdErrRegex string

	// OutLogAllLines means one regex per expected log line will be tested
	OutLogAllLines bool

	// OutLogLines contains the expected log lines. All regexes must match at
	// least one of the log lines, done in sequence so go through the log
	// lines until the first regex matches, then the second and so on
	OutLogLines []LogLine

	// OutLogRegex will be matched against the log output from the executed command
	OutLogRegex string

	// Pretty is set to true if the expected logging output is 'pretty'
	// otherwise it's assumed to be standard JSON
	Pretty bool
	// Parallel makes this test case run parallel, parallel tests do not
	// have their stdout/stderr captured, and only OutLog directives are taken
	// into account.
	Parallel bool
	// contains filtered or unexported fields
}

TestCase represent a single test case.

type TestFile

type TestFile struct {
	Location string
	Source   string
	Contents []byte
	Perms    os.FileMode
	Custom   func(*testing.T, []byte, []byte) bool
}

TestFile describes a file that is expected to be created by the application being tested.

type TestRunnerOptions

type TestRunnerOptions struct {
	ConfigGen                func() greenery.Config
	Parallel                 bool
	HandlerMap               map[string]greenery.Handler
	OverrideHandlerMap       bool
	UserDocList              map[string]*greenery.DocSet
	OverrideTraceLogger      bool
	TraceLogger              greenery.MakeTraceLogger
	OverridePrettyLogger     bool
	PrettyLogger             greenery.MakeLogger
	OverrideStructuredLogger bool
	StructuredLogger         greenery.MakeLogger
	CompareMap               map[string]CompareFunc
	OverrideBuiltinHandlers  bool
	BuiltinHandlers          map[string]greenery.Handler
}

TestRunnerOptions is a struct containing options for this test runner, see the TestCase documentation for information on the fields available.

Jump to

Keyboard shortcuts

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