harness

package
v0.28.1 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2024 License: Apache-2.0, MIT, Apache-2.0, + 1 more Imports: 30 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RunFuncStart = (*exec.Cmd).Start

Functions

func CreatePeerNodes added in v0.24.0

func CreatePeerNodes(t *testing.T, n int, peerings []Peering) (*Harness, Nodes)

func EnableDebugLogging

func EnableDebugLogging()

TODO: use zaptest.NewLogger(t) instead

func NewRandPort added in v0.24.0

func NewRandPort() int

func WaitForFile

func WaitForFile(path string, timeout time.Duration) error

Types

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer is a thread-safe byte buffer.

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

func (*Buffer) Lines

func (b *Buffer) Lines() []string

func (*Buffer) String

func (b *Buffer) String() string

func (*Buffer) Trimmed

func (b *Buffer) Trimmed() string

Trimmed returns the bytes as a string, but with the trailing newline removed. This only removes a single trailing newline, not all whitespace.

func (*Buffer) Write

func (b *Buffer) Write(p []byte) (n int, err error)

type CmdOpt

type CmdOpt func(*exec.Cmd)

func RunWithEnv

func RunWithEnv(env map[string]string) CmdOpt

func RunWithPath

func RunWithPath(path string) CmdOpt

func RunWithStderr added in v0.24.0

func RunWithStderr(writer io.Writer) CmdOpt

func RunWithStdin

func RunWithStdin(reader io.Reader) CmdOpt

func RunWithStdinStr

func RunWithStdinStr(s string) CmdOpt

func RunWithStdout added in v0.24.0

func RunWithStdout(writer io.Writer) CmdOpt

type HTTPClient

type HTTPClient struct {
	Client  *http.Client
	BaseURL string

	Timeout      time.Duration
	TemplateData any
}

HTTPClient is an HTTP client with some conveniences for testing. URLs are constructed from a base URL. The response body is buffered into a string. Internal errors cause panics so that tests don't need to check errors. The paths are evaluated as Go templates for readable string interpolation.

func (*HTTPClient) BuildURL

func (c *HTTPClient) BuildURL(urlPath string) string

BuildURL constructs a request URL from the given path by interpolating the string and then appending it to the base URL.

func (*HTTPClient) DisableRedirects

func (c *HTTPClient) DisableRedirects() *HTTPClient

func (*HTTPClient) Do

func (c *HTTPClient) Do(req *http.Request) *HTTPResponse

Do executes the request unchanged.

func (*HTTPClient) Get

func (c *HTTPClient) Get(urlPath string, opts ...func(*http.Request)) *HTTPResponse

func (*HTTPClient) Head

func (c *HTTPClient) Head(urlPath string, opts ...func(*http.Request)) *HTTPResponse

func (*HTTPClient) Post

func (c *HTTPClient) Post(urlPath string, body io.Reader, opts ...func(*http.Request)) *HTTPResponse

func (*HTTPClient) PostStr

func (c *HTTPClient) PostStr(urlpath, body string, opts ...func(*http.Request)) *HTTPResponse

func (*HTTPClient) WithHeader

func (c *HTTPClient) WithHeader(k, v string) func(h *http.Request)

type HTTPResponse

type HTTPResponse struct {
	Body       string
	StatusCode int
	Headers    http.Header

	// The raw response. The body will be closed on this response.
	Resp *http.Response
}

type Harness

type Harness struct {
	Dir       string
	IPFSBin   string
	Runner    *Runner
	NodesRoot string
	Nodes     Nodes
}

Harness tracks state for a test, such as temp dirs and IFPS nodes, and cleans them up after the test.

func New

func New(options ...func(h *Harness)) *Harness

func NewT

func NewT(t *testing.T, options ...func(h *Harness)) *Harness

NewT constructs a harness that cleans up after the given test is done.

func (*Harness) Cleanup

func (h *Harness) Cleanup()

func (*Harness) ExtractPeerID added in v0.24.0

func (h *Harness) ExtractPeerID(m multiaddr.Multiaddr) peer.ID

ExtractPeerID extracts a peer ID from the given multiaddr, and fatals if it does not contain a peer ID.

func (*Harness) Mkdirs

func (h *Harness) Mkdirs(paths ...string)

func (*Harness) NewNode

func (h *Harness) NewNode() *Node

func (*Harness) NewNodes

func (h *Harness) NewNodes(count int) Nodes

func (*Harness) Sh

func (h *Harness) Sh(expr string) *RunResult

func (*Harness) TempFile

func (h *Harness) TempFile() *os.File

TempFile creates a new unique temp file.

func (*Harness) WriteFile

func (h *Harness) WriteFile(filename, contents string)

WriteFile writes a file given a filename and its contents. The filename must be a relative path, or this panics.

func (*Harness) WriteToTemp

func (h *Harness) WriteToTemp(contents string) string

WriteToTemp writes the given contents to a guaranteed-unique temp file, returning its path.

type Node

type Node struct {
	ID  int
	Dir string

	APIListenAddr     multiaddr.Multiaddr
	GatewayListenAddr multiaddr.Multiaddr
	SwarmAddr         multiaddr.Multiaddr
	EnableMDNS        bool

	IPFSBin string
	Runner  *Runner

	Daemon *RunResult
}

Node is a single Kubo node. Each node has its own config and can run its own Kubo daemon.

func BuildNode

func BuildNode(ipfsBin, baseDir string, id int) *Node

func (*Node) APIAddr

func (n *Node) APIAddr() multiaddr.Multiaddr

func (*Node) APIClient

func (n *Node) APIClient() *HTTPClient

func (*Node) APIURL

func (n *Node) APIURL() string

func (*Node) ConfigFile added in v0.24.0

func (n *Node) ConfigFile() string

func (*Node) Connect

func (n *Node) Connect(other *Node) *Node

func (*Node) Disconnect added in v0.24.0

func (n *Node) Disconnect(other *Node)

func (*Node) GatewayClient

func (n *Node) GatewayClient() *HTTPClient

func (*Node) GatewayURL

func (n *Node) GatewayURL() string

GatewayURL waits for the gateway file and then returns its contents or times out.

func (*Node) GetIPFSConfig

func (n *Node) GetIPFSConfig(key string, val interface{})

func (*Node) IPFS

func (n *Node) IPFS(args ...string) *RunResult

func (*Node) IPFSAdd

func (n *Node) IPFSAdd(content io.Reader, args ...string) string

func (*Node) IPFSAddStr

func (n *Node) IPFSAddStr(content string, args ...string) string

func (*Node) IPFSCommands

func (n *Node) IPFSCommands() []string

func (*Node) IPFSDagImport added in v0.24.0

func (n *Node) IPFSDagImport(content io.Reader, cid string, args ...string) error

func (*Node) Init

func (n *Node) Init(ipfsArgs ...string) *Node

Init initializes and configures the IPFS node, after which it is ready to run.

func (*Node) IsAlive

func (n *Node) IsAlive() bool

func (*Node) PeerID

func (n *Node) PeerID() peer.ID

func (*Node) PeerWith added in v0.24.0

func (n *Node) PeerWith(other *Node)

func (*Node) Peers

func (n *Node) Peers() []multiaddr.Multiaddr

func (*Node) PipeStrToIPFS

func (n *Node) PipeStrToIPFS(s string, args ...string) *RunResult

func (*Node) PipeToIPFS

func (n *Node) PipeToIPFS(reader io.Reader, args ...string) *RunResult

func (*Node) ReadConfig

func (n *Node) ReadConfig() *config.Config

func (*Node) ReadFile added in v0.24.0

func (n *Node) ReadFile(filename string) string

ReadFile reads the specific file. If it is relative, it is relative the node's root dir.

func (*Node) ReadUserResourceOverrides added in v0.24.0

func (n *Node) ReadUserResourceOverrides() *rcmgr.PartialLimitConfig

func (*Node) RunIPFS

func (n *Node) RunIPFS(args ...string) *RunResult

func (*Node) RunPipeToIPFS

func (n *Node) RunPipeToIPFS(reader io.Reader, args ...string) *RunResult

func (*Node) SetIPFSConfig

func (n *Node) SetIPFSConfig(key string, val interface{}, flags ...string)

func (*Node) StartDaemon

func (n *Node) StartDaemon(ipfsArgs ...string) *Node

func (*Node) StartDaemonWithAuthorization added in v0.28.1

func (n *Node) StartDaemonWithAuthorization(secret string, ipfsArgs ...string) *Node

func (*Node) StartDaemonWithReq added in v0.24.0

func (n *Node) StartDaemonWithReq(req RunRequest, authorization string) *Node

StartDaemonWithReq runs a Kubo daemon with the given request. This overwrites the request Path with the Kubo bin path.

For example, if you want to run the daemon and see stderr and stdout to debug:

 node.StartDaemonWithReq(harness.RunRequest{
 	 CmdOpts: []harness.CmdOpt{
		harness.RunWithStderr(os.Stdout),
		harness.RunWithStdout(os.Stdout),
	 },
 })

func (*Node) StopDaemon

func (n *Node) StopDaemon() *Node

func (*Node) SwarmAddrs

func (n *Node) SwarmAddrs() []multiaddr.Multiaddr

func (*Node) SwarmAddrsWithPeerIDs added in v0.24.0

func (n *Node) SwarmAddrsWithPeerIDs() []multiaddr.Multiaddr

func (*Node) SwarmAddrsWithoutPeerIDs added in v0.24.0

func (n *Node) SwarmAddrsWithoutPeerIDs() []multiaddr.Multiaddr

func (*Node) TryAPIAddr

func (n *Node) TryAPIAddr() (multiaddr.Multiaddr, error)

func (*Node) UpdateConfig

func (n *Node) UpdateConfig(f func(cfg *config.Config))

func (*Node) UpdateUserSuppliedResourceManagerOverrides added in v0.24.0

func (n *Node) UpdateUserSuppliedResourceManagerOverrides(f func(overrides *rcmgr.PartialLimitConfig))

func (*Node) WaitOnAPI

func (n *Node) WaitOnAPI(authorization string) *Node

func (*Node) WriteBytes added in v0.24.0

func (n *Node) WriteBytes(filename string, b []byte)

func (*Node) WriteConfig

func (n *Node) WriteConfig(c *config.Config)

func (*Node) WriteUserSuppliedResourceOverrides added in v0.24.0

func (n *Node) WriteUserSuppliedResourceOverrides(c *rcmgr.PartialLimitConfig)

type Nodes

type Nodes []*Node

Nodes is a collection of Kubo nodes along with operations on groups of nodes.

func (Nodes) Connect

func (n Nodes) Connect() Nodes

func (Nodes) ForEachPar added in v0.24.0

func (n Nodes) ForEachPar(f func(*Node))

func (Nodes) Init

func (n Nodes) Init(args ...string) Nodes

func (Nodes) StartDaemons

func (n Nodes) StartDaemons(args ...string) Nodes

func (Nodes) StopDaemons

func (n Nodes) StopDaemons() Nodes

type Peering added in v0.24.0

type Peering struct {
	From int
	To   int
}

type RunFunc

type RunFunc func(*exec.Cmd) error

type RunRequest

type RunRequest struct {
	Path string
	Args []string
	// Options that are applied to the exec.Cmd just before running it
	CmdOpts []CmdOpt
	// Function to use to run the command.
	// If not specified, defaults to cmd.Run
	RunFunc func(*exec.Cmd) error
	Verbose bool
}

type RunResult

type RunResult struct {
	Stdout  *Buffer
	Stderr  *Buffer
	Err     error
	ExitErr *exec.ExitError
	Cmd     *exec.Cmd
}

func (*RunResult) ExitCode added in v0.24.0

func (r *RunResult) ExitCode() int

type Runner

type Runner struct {
	Env     map[string]string
	Dir     string
	Verbose bool
}

Runner is a process runner which can run subprocesses and aggregate output.

func (*Runner) AssertNoError

func (r *Runner) AssertNoError(result *RunResult)

func (*Runner) MustRun

func (r *Runner) MustRun(req RunRequest) *RunResult

MustRun runs the command and fails the test if the command fails.

func (*Runner) Run

func (r *Runner) Run(req RunRequest) *RunResult

type TestLogger added in v0.24.0

type TestLogger struct {
	// contains filtered or unexported fields
}

TestLogger is a logger for tests. It buffers output and only writes the output if the test fails or output is explicitly turned on. The purpose of this logger is to allow Go test to run with the verbose flag without printing logs. The verbose flag is useful since it streams test progress, but also printing logs makes the output too verbose.

You can also add prefixes that are prepended to each log message, for extra logging context.

This is implemented as a hierarchy of loggers, with children flushing log entries back to parents. This works because t.Cleanup() processes entries in LIFO order, so children always flush first.

Obviously this logger should never be used in production systems.

func NewTestLogger added in v0.24.0

func NewTestLogger(t *testing.T) *TestLogger

func (*TestLogger) AddPrefix added in v0.24.0

func (t *TestLogger) AddPrefix(prefix string) *TestLogger

func (*TestLogger) EnableLogs added in v0.24.0

func (t *TestLogger) EnableLogs()

func (*TestLogger) Fatal added in v0.24.0

func (t *TestLogger) Fatal(args ...any)

func (*TestLogger) Fatalf added in v0.24.0

func (t *TestLogger) Fatalf(format string, args ...any)

func (*TestLogger) Log added in v0.24.0

func (t *TestLogger) Log(args ...any)

func (*TestLogger) Logf added in v0.24.0

func (t *TestLogger) Logf(format string, args ...any)

Jump to

Keyboard shortcuts

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