test

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2021 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const NamePrefix = "cli-test"

NamePrefix is the prefix used in generated names.

Variables

View Source
var NoCleanupFunc = func() error { return nil }

NoCleanupFunc is cleanup function that just returns nil.

Functions

func ContainsAnyString

func ContainsAnyString(stdOut *bytes.Buffer, contains []string) error

ContainsAnyString checks that the given buffer contains any of the given set of strings.

func ContainsString

func ContainsString(stdOut *bytes.Buffer, contains string) error

ContainsString checks that the given buffer contains the string.

func Exec

func Exec(command string) (stdOut, stdErr *bytes.Buffer, err error)

Exec the command, exit on error

func ExecContainsAnyString

func ExecContainsAnyString(command string, contains []string) error

ExecContainsAnyString checks that the given command output contains any of the given set of strings.

func ExecContainsErrorString

func ExecContainsErrorString(command, contains string) error

ExecContainsErrorString checks that the given command stdErr output contains the string

func ExecContainsString

func ExecContainsString(command, contains string) error

ExecContainsString checks that the given command output contains the string.

func FlagSet

func FlagSet() *pflag.FlagSet

FlagSet returns the default flagset values for cli tests.

func GenerateName

func GenerateName() string

GenerateName returns a name for a cli test.

Types

type CleanupFunc

type CleanupFunc func() error

CleanupFunc is executed at the end of the test.

type Main

type Main struct {
	// Name of the main test.
	Name string
	// Tests in this main.
	Tests []*Test
	// Report holds the report for the test.
	Report *Report
	// Cleanup function.
	Cleanup CleanupFunc

	// Whether to defer the resource deletion.
	DeferDelete bool
	// contains filtered or unexported fields
}

Main holds state for multiple command tests.

func NewMain

func NewMain(name string, c *cobra.Command, cleanup CleanupFunc) *Main

NewMain returns a new CLI test.

func (*Main) AddTest

func (m *Main) AddTest(t *Test)

AddTest adds a test to the main.

func (*Main) BuildReport

func (m *Main) BuildReport()

BuildReport will build the report with the current commands.

func (*Main) Finish

func (m *Main) Finish()

Finish test.

func (*Main) NewTest

func (m *Main) NewTest(name, command string, run func(t *Test) error) *Test

NewTest creates a new Test and adds it to the main.

func (*Main) PrintFailure

func (m *Main) PrintFailure()

PrintFailure prints a main test failure message.

func (*Main) PrintReport

func (m *Main) PrintReport(format string) error

PrintReport prints the report in json|yaml.

func (*Main) PrintStart

func (m *Main) PrintStart()

PrintStart prints a main test start message.

func (*Main) PrintSuccess

func (m *Main) PrintSuccess()

PrintSuccess prints a successful main test message.

func (*Main) ReportError

func (m *Main) ReportError(cmd string, err error)

ReportError adds an error result to the report.

func (*Main) ReportResult

func (m *Main) ReportResult(res *Result)

ReportResult adds a result to the report.

func (*Main) ReportSuccess

func (m *Main) ReportSuccess(cmd string)

ReportSuccess adds an error result to the report.

func (*Main) ReportTestResult

func (m *Main) ReportTestResult(t *Test)

ReportTestResult adds an error result to the report.

func (*Main) RunTest

func (m *Main) RunTest(name, command string, run func(t *Test) error) error

RunTest will create a test and run it.

type Report

type Report struct {
	// Name of the report.
	TestName string `json:"testName"`
	// TimeStart at which the test was started.
	TimeStart time.Time `json:"timeStart"`
	// TimeEnd at which the test was started.
	TimeEnd time.Time `json:"timeEnd"`
	// Pass tells whether all the tests passed.
	Pass bool `json:"pass"`
	// Results of the test.
	Results []*Result `json:"results"`
}

Report is the report generated from running a CLI test.

type Result

type Result struct {
	// Command executed.
	Command string `json:"command"`
	// Pass tells if command passed execution.
	Pass bool `json:"pass"`
	// Err holds any error produced.
	Err error `json:"err"`
}

Result is the result of an individual CLI test.

func (*Result) Error

func (r *Result) Error(err error)

Error fails a result and reports the error.

func (*Result) Success

func (r *Result) Success()

Success passes a result.

type Test

type Test struct {
	// Name of the test.
	Name string
	// Command to test.
	Command string

	// Result of command test.
	Result *Result
	// contains filtered or unexported fields
}

Test is a cli test to run.

func NewTest

func NewTest(name, command string, run func(t *Test) error) *Test

NewTest returns a new command

func (*Test) Exec

func (t *Test) Exec() (err error)

Exec will execute the test command.

func (*Test) ExecContainsAnyString

func (t *Test) ExecContainsAnyString(contains ...string) error

ExecContainsAnyString executes the command and checks if the output contains any of the given set of strings.

func (*Test) ExecContainsErrorString

func (t *Test) ExecContainsErrorString(contains string) error

ExecContainsErrorString executes the command and checks if the output contains the given string.

func (*Test) ExecContainsString

func (t *Test) ExecContainsString(contains string) error

ExecContainsString executes the command and checks if the output contains the given string.

func (*Test) PrintSuccess

func (t *Test) PrintSuccess()

PrintSuccess will print a success message.

func (*Test) Run

func (t *Test) Run() error

Run the 'run' function within the context of the test updating the result accordingly.

func (*Test) StdErr

func (t *Test) StdErr() *bytes.Buffer

StdErr from executing the test command.

func (*Test) StdOut

func (t *Test) StdOut() *bytes.Buffer

StdOut from executing the test command.

func (*Test) Wrap

func (t *Test) Wrap(f func(t *Test) error) error

Wrap will wrap the execution of the given function in the context of the test and update the results accordingly.

Jump to

Keyboard shortcuts

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