Documentation
¶
Overview ¶
Package rigtest provides testing utilities for mocking functionality of the rig package.
Index ¶
- func NotReceivedContains(t TestingT, m Receiver, substring string, msgAndArgs ...any)
- func NotReceivedEqual(t TestingT, m Receiver, command string, msgAndArgs ...any)
- func NotReceivedMatch(t TestingT, m Receiver, pattern string, msgAndArgs ...any)
- func NotReceivedWithPrefix(t TestingT, m Receiver, prefix string, msgAndArgs ...any)
- func NotReceivedWithSuffix(t TestingT, m Receiver, suffix string, msgAndArgs ...any)
- func ReceivedContains(t TestingT, m Receiver, substring string, msgAndArgs ...any)
- func ReceivedEqual(t TestingT, m Receiver, command string, msgAndArgs ...any)
- func ReceivedMatch(t TestingT, m Receiver, pattern string, msgAndArgs ...any)
- func ReceivedWithPrefix(t TestingT, m Receiver, prefix string, msgAndArgs ...any)
- func ReceivedWithSuffix(t TestingT, m Receiver, suffix string, msgAndArgs ...any)
- func Trace(fn func())
- func TraceOff()
- func TraceToStderr()
- type A
- type CommandHandler
- type CommandMatcher
- type MockConnection
- func (m *MockConnection) Commands() []string
- func (m *MockConnection) IPAddress() string
- func (m *MockConnection) IsWindows() bool
- func (m *MockConnection) LastCommand() string
- func (m *MockConnection) Len() int
- func (m *MockConnection) NotReceived(matchFn CommandMatcher) error
- func (m *MockConnection) Protocol() string
- func (m *MockConnection) Received(matchFn CommandMatcher) error
- func (m *MockConnection) Reset()
- func (m *MockConnection) StartProcess(ctx context.Context, cmd string, stdin io.Reader, stdout io.Writer, ...) (protocol.Waiter, error)
- func (m *MockConnection) String() string
- type MockLogMessage
- type MockLogger
- func (l *MockLogger) Debug(t string, args ...any)
- func (l *MockLogger) Error(t string, args ...any)
- func (l *MockLogger) Info(t string, args ...any)
- func (l *MockLogger) Len() int
- func (l *MockLogger) Messages() []MockLogMessage
- func (l *MockLogger) Received(regex regexp.Regexp) bool
- func (l *MockLogger) ReceivedString(message string) bool
- func (l *MockLogger) ReceivedSubstring(substring string) bool
- func (l *MockLogger) Reset()
- func (l *MockLogger) Trace(t string, args ...any)
- func (l *MockLogger) Warn(t string, args ...any)
- type MockRunner
- type MockStarter
- func (m *MockStarter) AddCommand(matchFn CommandMatcher, waitFn CommandHandler)
- func (m *MockStarter) AddCommandFailure(matchFn CommandMatcher, err error)
- func (m *MockStarter) AddCommandOutput(matchFn CommandMatcher, output string)
- func (m *MockStarter) AddCommandSuccess(matchFn CommandMatcher)
- func (m *MockStarter) StartProcess(ctx context.Context, cmd string, stdin io.Reader, stdout io.Writer, ...) (protocol.Waiter, error)
- type MockWaiter
- type Receiver
- type TestingT
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NotReceivedContains ¶
NotReceivedContains asserts that a command with the given substring was not received.
func NotReceivedEqual ¶
NotReceivedEqual asserts that a command was not received.
func NotReceivedMatch ¶
NotReceivedMatch asserts that a command with the given regular expression was not received.
func NotReceivedWithPrefix ¶
NotReceivedWithPrefix asserts that a command with the given prefix was not received.
func NotReceivedWithSuffix ¶
NotReceivedWithSuffix asserts that a command with the given suffix was not received.
func ReceivedContains ¶
ReceivedContains asserts that a command with the given substring was received.
func ReceivedEqual ¶
ReceivedEqual asserts that a command was received.
func ReceivedMatch ¶
ReceivedMatch asserts that a command with the given regular expression was received.
func ReceivedWithPrefix ¶
ReceivedWithPrefix asserts that a command with the given prefix was received.
func ReceivedWithSuffix ¶
ReceivedWithSuffix asserts that a command with the given suffix was received.
Types ¶
type A ¶
type A struct {
// Ctx is the context passed to the command
Ctx context.Context //nolint:containedctx
// Stdin is the standard input of the command
Stdin io.Reader
// Stdout is the standard output of the command
Stdout io.Writer
// Stderr is the standard error of the command
Stderr io.Writer
// Command is the command line
Command string
}
A is the struct passed to the command handling functions.
type CommandHandler ¶
CommandHandler is a function that handles a mocked command.
type CommandMatcher ¶
CommandMatcher is a function that checks if a command matches a certain criteria.
func Contains ¶
func Contains(substring string) CommandMatcher
Contains returns a CommandMatcher that checks if a command contains a given substring.
func Equal ¶
func Equal(str string) CommandMatcher
Equal returns a CommandMatcher that checks if a command equals a given string.
func HasPrefix ¶
func HasPrefix(prefix string) CommandMatcher
HasPrefix returns a CommandMatcher that checks if a command starts with a given prefix.
func HasSuffix ¶
func HasSuffix(suffix string) CommandMatcher
HasSuffix returns a CommandMatcher that checks if a command ends with a given suffix.
func Match ¶
func Match(pattern string) CommandMatcher
Match returns a CommandMatcher that checks if a command matches a given regular expression.
func Not ¶
func Not(matchFn CommandMatcher) CommandMatcher
Not returns a CommandMatcher that negates the result of the given match function.
type MockConnection ¶
type MockConnection struct {
log.LoggerInjectable
*MockStarter
Windows bool
// contains filtered or unexported fields
}
MockConnection is a mock client. It can be used to simulate a client in tests.
func NewMockConnection ¶
func NewMockConnection() *MockConnection
NewMockConnection creates a new mock connection.
func (*MockConnection) Commands ¶
func (m *MockConnection) Commands() []string
Commands returns a copy of the commands received.
func (*MockConnection) IPAddress ¶
func (m *MockConnection) IPAddress() string
IPAddress returns the IP address of the client.
func (*MockConnection) IsWindows ¶
func (m *MockConnection) IsWindows() bool
IsWindows returns true if the client is set to be a Windows client.
func (*MockConnection) LastCommand ¶
func (m *MockConnection) LastCommand() string
LastCommand returns the last command received.
func (*MockConnection) Len ¶
func (m *MockConnection) Len() int
Len returns the number of commands received.
func (*MockConnection) NotReceived ¶
func (m *MockConnection) NotReceived(matchFn CommandMatcher) error
NotReceived returns an error if a command matching the given regular expression was received.
func (*MockConnection) Protocol ¶
func (m *MockConnection) Protocol() string
Protocol returns the protocol of the client.
func (*MockConnection) Received ¶
func (m *MockConnection) Received(matchFn CommandMatcher) error
Received returns an error unless a command matching the given matcher was received.
func (*MockConnection) StartProcess ¶
func (m *MockConnection) StartProcess(ctx context.Context, cmd string, stdin io.Reader, stdout io.Writer, stderr io.Writer) (protocol.Waiter, error)
StartProcess simulates a start of a process on the client.
func (*MockConnection) String ¶
func (m *MockConnection) String() string
String returns the string representation of the client.
type MockLogMessage ¶
type MockLogMessage struct {
// contains filtered or unexported fields
}
MockLogMessage is a mock log message.
func (MockLogMessage) KeysAndValues ¶
func (m MockLogMessage) KeysAndValues() []any
KeysAndValues returns the log message's keys and values.
func (MockLogMessage) Level ¶
func (m MockLogMessage) Level() int
Level returns the log level of the message.
func (MockLogMessage) Message ¶
func (m MockLogMessage) Message() string
Message returns the log message.
func (MockLogMessage) String ¶
func (m MockLogMessage) String() string
String returns the log message as a string.
type MockLogger ¶
type MockLogger struct {
// contains filtered or unexported fields
}
MockLogger is a mock logger.
func (*MockLogger) Debug ¶
func (l *MockLogger) Debug(t string, args ...any)
Debug level log message.
func (*MockLogger) Error ¶
func (l *MockLogger) Error(t string, args ...any)
Error level log message.
func (*MockLogger) Messages ¶
func (l *MockLogger) Messages() []MockLogMessage
Messages returns a copy of the log messages.
func (*MockLogger) Received ¶
func (l *MockLogger) Received(regex regexp.Regexp) bool
Received returns true if a log message with the given level and message was received.
func (*MockLogger) ReceivedString ¶
func (l *MockLogger) ReceivedString(message string) bool
ReceivedString returns true if a log message with the given level and message was received.
func (*MockLogger) ReceivedSubstring ¶
func (l *MockLogger) ReceivedSubstring(substring string) bool
ReceivedSubstring returns true if a log message with the given level and message was received.
func (*MockLogger) Trace ¶
func (l *MockLogger) Trace(t string, args ...any)
Trace level log message.
type MockRunner ¶
type MockRunner struct {
log.LoggerInjectable
cmd.Runner
*MockConnection
*MockStarter
}
MockRunner runs commands on a mock connection.
Example ¶
ExampleMockRunner demonstrates how to mock a connection to match the "ls" command and return a list of fake filenames.
package main
import (
"fmt"
"github.com/k0sproject/rig/v2/rigtest"
)
func main() {
runner := rigtest.NewMockRunner()
runner.AddCommand(rigtest.Equal("ls"), func(a *rigtest.A) error {
fmt.Fprintln(a.Stdout, "file1")
fmt.Fprintln(a.Stdout, "file2")
return nil
})
out, err := runner.ExecOutput("ls")
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(out)
}
Output: file1 file2
func (*MockRunner) IsWindows ¶
func (m *MockRunner) IsWindows() bool
IsWindows returns true if the runner's connection is set to be a Windows client.
func (*MockRunner) StartProcess ¶
func (m *MockRunner) StartProcess(ctx context.Context, cmd string, stdin io.Reader, stdout io.Writer, stderr io.Writer) (protocol.Waiter, error)
StartProcess simulates a start of a process on the client.
func (*MockRunner) String ¶
func (m *MockRunner) String() string
String returns the string representation of the runner.
type MockStarter ¶
type MockStarter struct {
ErrImmediate bool
ErrDefault error
// contains filtered or unexported fields
}
MockStarter is a mock process starter.
func (*MockStarter) AddCommand ¶
func (m *MockStarter) AddCommand(matchFn CommandMatcher, waitFn CommandHandler)
AddCommand adds a mocked command handler which is called when the matcher matches the command line.
func (*MockStarter) AddCommandFailure ¶
func (m *MockStarter) AddCommandFailure(matchFn CommandMatcher, err error)
AddCommandFailure adds a command matcher that should always fail with the given error.
func (*MockStarter) AddCommandOutput ¶
func (m *MockStarter) AddCommandOutput(matchFn CommandMatcher, output string)
AddCommandOutput adds a matcher and a function to the starter that writes the given output to the stdout of the process.
func (*MockStarter) AddCommandSuccess ¶
func (m *MockStarter) AddCommandSuccess(matchFn CommandMatcher)
AddCommandSuccess adds a command matcher that should always succeed.
func (*MockStarter) StartProcess ¶
func (m *MockStarter) StartProcess(ctx context.Context, cmd string, stdin io.Reader, stdout io.Writer, stderr io.Writer) (protocol.Waiter, error)
StartProcess simulates a start of a process. You can add matchers to the starter to simulate different behaviors for different commands.
type MockWaiter ¶
type MockWaiter struct {
// contains filtered or unexported fields
}
MockWaiter is a mock process waiter.
type Receiver ¶
type Receiver interface {
Received(matchFn CommandMatcher) error
NotReceived(matchFn CommandMatcher) error
}
Receiver is any object in rigtest that expects to receive commands.