test_caddy

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: May 15, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package test_caddy provides mock types and utilities for testing caddy modules.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCaddyContext

func NewCaddyContext(t testing.TB, base context.Context, cfg caddy.Config) (caddy.Context, context.CancelFunc)

NewCaddyContext returns an instantiated caddy context. This differs from caddy.NewContext in that the context's config is set and the `apps` map is initialized. This is required to load other apps in provision for some tests.

UNSTABLE: Heavy use of reflect and unsafe pointers to access unexported struct fields.

func NewTestModule added in v0.0.3

func NewTestModule[T any, Parent caddy.Module](t testing.TB, p *Parent, fn func(Parent) *TestModule[T], module string)

NewTestModule creates and initializes a new instance of TestModule.

Types

type TestConn

type TestConn struct {
	ReadFn             func([]byte) (int, error)
	WriteFn            func([]byte) (int, error)
	CloseFn            func() error
	LocalAddrFn        func() net.Addr
	RemoteAddrFn       func() net.Addr
	SetDeadlineFn      func(time.Time) error
	SetReadDeadlineFn  func(time.Time) error
	SetWriteDeadlineFn func(time.Time) error
	// contains filtered or unexported fields
}

TestConn is a mock net.Conn.

func NewTestConn

func NewTestConn(t testing.TB) *TestConn

NewTestConn creates and initializes a new instance of TestConn.

func (*TestConn) Close

func (tc *TestConn) Close() error

Close attempts to call CloseFn. If CloseFn is not set, nil is returned.

func (*TestConn) LocalAddr

func (tc *TestConn) LocalAddr() net.Addr

LocalAddr attempts to call LocalAddrFn. If LocalAddrFn is not set, nil is returned.

func (*TestConn) Read

func (tc *TestConn) Read(b []byte) (int, error)

Read attempts to call ReadFn. If ReadFn is not set, an error is returned.

func (*TestConn) RemoteAddr

func (tc *TestConn) RemoteAddr() net.Addr

RemoteAddr attempts to call RemoteAddrFn. If RemoteAddrFn is not set, a pointer to a net.TCPAddr is returned..

func (*TestConn) SetDeadline

func (tc *TestConn) SetDeadline(t time.Time) error

SetDeadline attempts to call SetDeadlineFn. If SetDeadlineFn is not set, nil is returned.

func (*TestConn) SetReadDeadline

func (tc *TestConn) SetReadDeadline(t time.Time) error

SetReadDeadline attempts to call SetReadDeadlineFn. If SetReadDeadlineFn is not set, nil is returned.

func (*TestConn) SetWriteDeadline

func (tc *TestConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline attempts to call SetWriteDeadlineFn. If SetWriteDeadlineFn is not set, nil is returned.

func (*TestConn) Write

func (tc *TestConn) Write(b []byte) (int, error)

Write attempts to call WriteFn. If WriteFn is not set, an error is returned.

type TestDialer

type TestDialer struct {
	DialFn       func(context.Context, *net.TCPAddr) (net.Conn, error)
	DialPacketFn func(*net.UDPAddr) (net.PacketConn, error)
	// contains filtered or unexported fields
}

TestDialer is a mock pointc.Dialer.

func NewTestDialer

func NewTestDialer(t testing.TB) *TestDialer

NewTestDialer creates and initializes a new instance of TestDialer.

func (*TestDialer) Dial

func (td *TestDialer) Dial(ctx context.Context, addr *net.TCPAddr) (net.Conn, error)

Dial attempts to call DialFn. If DialFn is not set, an error is returned.

func (*TestDialer) DialPacket

func (td *TestDialer) DialPacket(addr *net.UDPAddr) (net.PacketConn, error)

DialPacket attempts to call DialPacketFn. If DialPacketFn is not set, an error is returned.

type TestListener

type TestListener struct {
	AcceptFn func() (net.Conn, error) `json:"-"`
	CloseFn  func() error             `json:"-"`
	AddrFn   func() net.Addr          `json:"-"`
	// contains filtered or unexported fields
}

TestListener is a mock net.Listener.

func NewTestListener

func NewTestListener(t testing.TB) *TestListener

NewTestListener creates and initializes a new instance of TestListener.

func (*TestListener) Accept

func (tl *TestListener) Accept() (net.Conn, error)

Accept attempts to call AcceptFn. If AcceptFn is not set, an error is returned.

func (*TestListener) Addr

func (tl *TestListener) Addr() net.Addr

Addr attempts to call AddrFn. If AddrFn is not set, a pointer to a net.TCPAddr is returned.

func (*TestListener) Close

func (tl *TestListener) Close() error

Close attempts to call CloseFn. If CloseFn is not set, nil is returned.

type TestListenerModule added in v0.0.3

type TestListenerModule[T any] struct {
	TestListener
	TestModule[T]
}

TestListenerModule is a mock net.Listener that can also be used as a Caddy module.

func NewTestListenerModule added in v0.0.3

func NewTestListenerModule[T any](t testing.TB) (v *TestListenerModule[T])

NewTestListenerModule creates and initializes a new instance of TestListenerModule.

type TestModule added in v0.0.3

type TestModule[T any] struct {
	ID                   string         `json:"-"`
	Module               caddy.ModuleID `json:"-"`
	New                  func() caddy.Module
	UnmarshalCaddyfileFn func(*caddyfile.Dispenser) error `json:"-"`
	ProvisionerFn        func(caddy.Context) error        `json:"-"`
	ValidateFn           func() error                     `json:"-"`
	CleanupFn            func() error                     `json:"-"`
	StartFn              func(T) error                    `json:"-"`
	MarshalJSONFn        func() ([]byte, error)           `json:"-"`
	UnmarshalJSONFn      func(b []byte) error             `json:"-"`
	// contains filtered or unexported fields
}

TestModule is a mock base caddy module. It implements the functions in the caddy lifecycle. It also implements lifecycler.LifeCyclable and the json marshalling methods.

func (*TestModule[T]) CaddyModule added in v0.0.3

func (t *TestModule[T]) CaddyModule() caddy.ModuleInfo

CaddyModule returns the caddy.ModuleInfo for this module. The New method returns this instance everytime.

func (*TestModule[T]) Cleanup added in v0.0.3

func (t *TestModule[T]) Cleanup() error

Cleanup attempts to call CleanupFn. If CleanupFn is not set, nil is returned.

func (*TestModule[T]) MarshalJSON added in v0.0.6

func (t *TestModule[T]) MarshalJSON() ([]byte, error)

MarshalJSON attempts to call MarshalJSONFn. If MarshalJSONFn is not set, an empty struct is marshalled and returned instead.

func (*TestModule[T]) Provision added in v0.0.3

func (t *TestModule[T]) Provision(ctx caddy.Context) error

Provision attempts to call ProvisionerFn. If ProvisionerFn is not set, nil is returned.

func (*TestModule[T]) Register added in v0.0.3

func (t *TestModule[T]) Register()

Register registers this module with caddy.

func (*TestModule[T]) Start added in v0.0.3

func (t *TestModule[T]) Start(v T) error

Start attempts to call StartFn. If StartFn is not set, nil is returned.

func (*TestModule[T]) UnmarshalCaddyfile added in v0.0.3

func (t *TestModule[T]) UnmarshalCaddyfile(d *caddyfile.Dispenser) error

UnmarshalCaddyfile attempts to call UnmarshalCaddyfileFn. If UnmarshalCaddyfileFn is not set, nil is returned.

func (*TestModule[T]) UnmarshalJSON added in v0.0.6

func (t *TestModule[T]) UnmarshalJSON(b []byte) error

UnmarshalJSON attempts to call UnmarshalJSONFn. If UnmarshalJSONFn is not set, nil is returned.

func (*TestModule[T]) Validate added in v0.0.3

func (t *TestModule[T]) Validate() error

Validate attempts to call ValidateFn. If ValidateFn is not set, nil is returned.

type TestNet

type TestNet struct {
	ListenFn       func(*net.TCPAddr) (net.Listener, error)
	ListenPacketFn func(*net.UDPAddr) (net.PacketConn, error)
	DialerFn       func(net.IP, uint16) pointc.Dialer
	LocalAddrFn    func() net.IP
	// contains filtered or unexported fields
}

TestNet is a mock point-c network net module.

func NewTestNet

func NewTestNet(t testing.TB) *TestNet

NewTestNet creates and initializes a new instance of TestNet.

func (*TestNet) Dialer

func (tn *TestNet) Dialer(laddr net.IP, port uint16) pointc.Dialer

Dialer attempts to call DialerFn. If DialerFn is not set, NewTestDialer is used to create a value.

func (*TestNet) Listen

func (tn *TestNet) Listen(addr *net.TCPAddr) (net.Listener, error)

Listen attempts to call ListenFn. If ListenFn is not set, an error is returned.

func (*TestNet) ListenPacket

func (tn *TestNet) ListenPacket(addr *net.UDPAddr) (net.PacketConn, error)

ListenPacket attempts to call ListenPacketFn. If ListenPacketFn is not set, an error is returned.

func (*TestNet) LocalAddr

func (tn *TestNet) LocalAddr() net.IP

LocalAddr attempts to call LocalAddrFn. If LocalAddrFn is not set, an error is returned.

type TestNetLookup

type TestNetLookup struct {
	LookupFn func(string) (pointc.Net, bool)
	// contains filtered or unexported fields
}

TestNetLookup implements pointc.NetLookup.

func NewTestNetLookup

func NewTestNetLookup(t testing.TB) *TestNetLookup

NewTestNetLookup creates and initializes a new instance of TestNetLookup.

func (*TestNetLookup) Lookup

func (tnl *TestNetLookup) Lookup(name string) (pointc.Net, bool)

Lookup attempts to call LookupFn. If LookupFn is not set (nil, false) is returned.

type TestNetOp

type TestNetOp struct{ TestModule[pointc.NetLookup] }

TestNetOp is a mock point-c network operation.

func NewTestNetOp

func NewTestNetOp(t testing.TB) (v *TestNetOp)

NewTestNetOp creates and initializes a new instance of TestNetOp.

type TestNetwork

type TestNetwork struct {
	TestModule[pointc.RegisterFunc]
}

TestNetwork is a mock point-c network.

func NewTestNetwork

func NewTestNetwork(t testing.TB) (v *TestNetwork)

NewTestNetwork creates and initializes a new instance of TestNetwork.

Jump to

Keyboard shortcuts

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