rigtest

package
v2.0.0-alpha.3 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package rigtest provides testing utilities for mocking functionality of the rig package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NotReceivedContains

func NotReceivedContains(t TestingT, m Receiver, substring string, msgAndArgs ...any)

NotReceivedContains asserts that a command with the given substring was not received.

func NotReceivedEqual

func NotReceivedEqual(t TestingT, m Receiver, command string, msgAndArgs ...any)

NotReceivedEqual asserts that a command was not received.

func NotReceivedMatch

func NotReceivedMatch(t TestingT, m Receiver, pattern string, msgAndArgs ...any)

NotReceivedMatch asserts that a command with the given regular expression was not received.

func NotReceivedWithPrefix

func NotReceivedWithPrefix(t TestingT, m Receiver, prefix string, msgAndArgs ...any)

NotReceivedWithPrefix asserts that a command with the given prefix was not received.

func NotReceivedWithSuffix

func NotReceivedWithSuffix(t TestingT, m Receiver, suffix string, msgAndArgs ...any)

NotReceivedWithSuffix asserts that a command with the given suffix was not received.

func ReceivedContains

func ReceivedContains(t TestingT, m Receiver, substring string, msgAndArgs ...any)

ReceivedContains asserts that a command with the given substring was received.

func ReceivedEqual

func ReceivedEqual(t TestingT, m Receiver, command string, msgAndArgs ...any)

ReceivedEqual asserts that a command was received.

func ReceivedMatch

func ReceivedMatch(t TestingT, m Receiver, pattern string, msgAndArgs ...any)

ReceivedMatch asserts that a command with the given regular expression was received.

func ReceivedWithPrefix

func ReceivedWithPrefix(t TestingT, m Receiver, prefix string, msgAndArgs ...any)

ReceivedWithPrefix asserts that a command with the given prefix was received.

func ReceivedWithSuffix

func ReceivedWithSuffix(t TestingT, m Receiver, suffix string, msgAndArgs ...any)

ReceivedWithSuffix asserts that a command with the given suffix was received.

func Trace

func Trace(fn func())

Trace allows running a function with trace logging enabled.

func TraceOff

func TraceOff()

TraceOff sets the trace logger to null.

func TraceToStderr

func TraceToStderr()

TraceToStderr sets the trace logger to log to stderr.

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

type CommandHandler func(a *A) error

CommandHandler is a function that handles a mocked command.

type CommandMatcher

type CommandMatcher func(string) bool

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) Reset

func (m *MockConnection) Reset()

Reset clears the command history.

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) Info

func (l *MockLogger) Info(t string, args ...any)

Info level log message.

func (*MockLogger) Len

func (l *MockLogger) Len() int

Len returns the number of log messages.

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) Reset

func (l *MockLogger) Reset()

Reset clears the log messages.

func (*MockLogger) Trace

func (l *MockLogger) Trace(t string, args ...any)

Trace level log message.

func (*MockLogger) Warn

func (l *MockLogger) Warn(t string, args ...any)

Warn 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 NewMockRunner

func NewMockRunner() *MockRunner

NewMockRunner creates a new mock runner.

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 NewMockStarter

func NewMockStarter() *MockStarter

NewMockStarter creates a new mock 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.

func (*MockWaiter) Close

func (m *MockWaiter) Close() error

Close the mock command.

func (*MockWaiter) Wait

func (m *MockWaiter) Wait() error

Wait simulates a process wait.

type Receiver

type Receiver interface {
	Received(matchFn CommandMatcher) error
	NotReceived(matchFn CommandMatcher) error
}

Receiver is any object in rigtest that expects to receive commands.

type TestingT

type TestingT interface {
	Errorf(format string, args ...interface{})
}

TestingT is an interface that is compatible with the testing.T.

Jump to

Keyboard shortcuts

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