testutils

package
v1.24.1 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2025 License: MIT Imports: 36 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Password = "password"
	Secret   = "0123456789101112"
)

Variables

View Source
var (
	HashSecret         = sha256.Sum256([]byte(Secret))
	SupportedUILocales = []language.Tag{language.English}
)

Functions

func ConnectToManagementInterface added in v1.23.0

func ConnectToManagementInterface(tb testing.TB, managementInterface net.Listener, openVPNClient *openvpn.Client) (net.Conn, <-chan error, error)

ConnectToManagementInterface establishes a connection to the given management interface and returns the accepted net.Conn and an error channel for the OpenVPN client.

func ExpectMessage

func ExpectMessage(tb testing.TB, conn net.Conn, reader *bufio.Reader, expectMessage string)

ExpectMessage reads from the connection and compares the output with the expected message.

func ExpectVersionAndReleaseHold

func ExpectVersionAndReleaseHold(tb testing.TB, conn net.Conn, reader *bufio.Reader)

ExpectVersionAndReleaseHold performs the initial handshake with the mocked management interface. It checks for a version query and hold release.

func GetGIDOfFile added in v1.22.6

func GetGIDOfFile(fileName string) (int, error)

GetGIDOfFile returns the numeric group ID of the given file.

func GetPermissionsOfFile added in v1.22.6

func GetPermissionsOfFile(fileName string) (string, error)

GetPermissionsOfFile returns the permission bits of the given file as a string, for example "drwxr-xr-x".

func ReadLine

func ReadLine(tb testing.TB, conn net.Conn, reader *bufio.Reader) string

ReadLine reads a single line from the connection with a timeout.

func SendAndExpectMessage

func SendAndExpectMessage(tb testing.TB, conn net.Conn, reader *bufio.Reader, sendMessage, expectMessage string)

SendAndExpectMessage sends a message and immediately validates the response.

func SendMessagef added in v1.23.1

func SendMessagef(tb testing.TB, conn net.Conn, sendMessage string, args ...any)

SendMessagef sends a formatted string to the management interface connection.

func SetupOpenVPNOAuth2Clients added in v1.22.6

func SetupOpenVPNOAuth2Clients(
	ctx context.Context, tb testing.TB, conf config.Config, logger *slog.Logger, httpClient *http.Client, tokenStorage tokenstorage.Storage,
) (*oauth2.Client, *openvpn.Client)

SetupOpenVPNOAuth2Clients creates mocked OpenVPN and OAuth2 clients using the provided configuration.

func SetupResourceServer

func SetupResourceServer(tb testing.TB, clientListener net.Listener, logger *slog.Logger, opConfig *op.Config) (
	*httptest.Server, types.URL, config.OAuth2Client, error,
)

SetupResourceServer starts a minimal OIDC server used for integration tests.

func WaitUntilListening

func WaitUntilListening(tb testing.TB, network, address string) (net.Conn, error)

WaitUntilListening tries to connect to a network address until it is available. It returns the connection or an error after several retries.

Types

type FakeOpenVPNClient added in v1.22.6

type FakeOpenVPNClient struct{}

FakeOpenVPNClient implements the parts of the OpenVPN client interface used in tests. It does not perform any actions.

func NewFakeOpenVPNClient added in v1.22.6

func NewFakeOpenVPNClient() FakeOpenVPNClient

NewFakeOpenVPNClient returns a FakeOpenVPNClient.

func (FakeOpenVPNClient) AcceptClient added in v1.22.6

func (FakeOpenVPNClient) AcceptClient(_ *slog.Logger, _ state.ClientIdentifier, _ bool, _ string)

AcceptClient is a no-op implementation of the real method.

func (FakeOpenVPNClient) DenyClient added in v1.22.6

DenyClient is a no-op implementation of the real method.

type FakeStorage added in v1.22.6

type FakeStorage struct{}

FakeStorage is a no-op implementation of the token storage interface used in tests.

func NewFakeStorage added in v1.22.6

func NewFakeStorage() *FakeStorage

NewFakeStorage returns a new FakeStorage instance.

func (FakeStorage) Close added in v1.24.0

func (FakeStorage) Close() error

Close is a no-op for FakeStorage.

func (FakeStorage) Delete added in v1.22.6

func (FakeStorage) Delete(_ string) error

Delete is a no-op for FakeStorage.

func (FakeStorage) Get added in v1.22.6

func (FakeStorage) Get(_ string) (string, error)

Get returns an empty token.

func (FakeStorage) Set added in v1.22.6

func (FakeStorage) Set(_, _ string) error

Set is a no-op for FakeStorage.

type Logger

type Logger struct {
	*slog.Logger
	*SyncBuffer
}

Logger combines slog.Logger with a synchronized buffer used in tests.

func NewTestLogger

func NewTestLogger() *Logger

NewTestLogger returns a logger that stores all logs in memory so tests can inspect them.

func SetupMockEnvironment

func SetupMockEnvironment(ctx context.Context, tb testing.TB, conf config.Config, rt http.RoundTripper, opConf *op.Config) (
	config.Config, *openvpn.Client, net.Listener, *oauth2.Client, *httptest.Server, *http.Client, *Logger,
)

SetupMockEnvironment sets up an OpenVPN management interface and a mock OIDC provider. It returns the adjusted configuration and helper instances used in tests.

func (Logger) GetLogs

func (l Logger) GetLogs() string

GetLogs returns the accumulated log output as a string.

type MockRoundTripper

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

MockRoundTripper mocks selected HTTP calls while delegating everything else to the provided RoundTripper.

func NewMockRoundTripper

func NewMockRoundTripper(rt http.RoundTripper) *MockRoundTripper

NewMockRoundTripper creates a MockRoundTripper. If rt is nil the default transport is used.

func (*MockRoundTripper) RoundTrip

func (f *MockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper and returns mocked responses for specific hosts.

type RoundTripperFunc

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

RoundTripperFunc wraps an http.RoundTripper and allows a custom function to intercept each request.

func NewRoundTripperFunc

func NewRoundTripperFunc(rt http.RoundTripper, fn func(rt http.RoundTripper, req *http.Request) (*http.Response, error)) *RoundTripperFunc

NewRoundTripperFunc returns a RoundTripperFunc that calls fn with the wrapped RoundTripper for each request.

func (*RoundTripperFunc) RoundTrip

func (f *RoundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper.

type SyncBuffer added in v1.22.4

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

SyncBuffer is a bytes.Buffer protected by a mutex for concurrent writes.

func (*SyncBuffer) String added in v1.22.4

func (s *SyncBuffer) String() string

String returns the buffered data as a string.

func (*SyncBuffer) Write added in v1.22.4

func (s *SyncBuffer) Write(p []byte) (int, error)

Write appends bytes to the buffer. It is safe for concurrent use.

Jump to

Keyboard shortcuts

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