testutil

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package testutil provides test utilities for integration testing

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertEqual

func AssertEqual(t *testing.T, expected, actual interface{}, msg string)

AssertEqual fails test if expected != actual

func AssertLenEqual

func AssertLenEqual(t *testing.T, expected int, slice interface{}, msg string)

AssertLenEqual fails test if slice length != expected

func AssertNotEmpty

func AssertNotEmpty(t *testing.T, value string, msg string)

AssertNotEmpty fails test if value is empty

func Cleanup

func Cleanup(t *testing.T, name string, fn func() error)

Cleanup registers a cleanup function that logs on error

func CompareJSON

func CompareJSON(t *testing.T, expected, actual []byte)

CompareJSON compares two JSON byte arrays for equality

func ExpectedSessionsCount

func ExpectedSessionsCount(t *testing.T) int

ExpectedSessionsCount returns expected number of sessions in "occtl -j show sessions all" fixture

func ExpectedUsersCount

func ExpectedUsersCount(t *testing.T) int

ExpectedUsersCount returns expected number of users in "occtl -j show users" fixture

func FixturesPath

func FixturesPath(t *testing.T) string

FixturesPath returns the absolute path to fixtures directory

func GetFixtureString

func GetFixtureString(t *testing.T, name string) string

GetFixtureString returns fixture as string (for text fixtures)

func IsSystemdAvailable

func IsSystemdAvailable() bool

IsSystemdAvailable checks if systemd is available on the system

func LoadFixture

func LoadFixture(t *testing.T, name string) []byte

LoadFixture loads a fixture file and returns its contents

func LoadFixtureJSON

func LoadFixtureJSON(t *testing.T, name string) []byte

LoadFixtureJSON loads a fixture and validates it's valid JSON

func NewTestContext

func NewTestContext(t *testing.T, timeout time.Duration) (context.Context, context.CancelFunc)

NewTestContext creates a context with timeout for testing

func NewTestLogger

func NewTestLogger(t *testing.T) zerolog.Logger

NewTestLogger creates a logger for testing

func RequireError

func RequireError(t *testing.T, err error, msg string)

RequireError fails test if error is nil

func RequireNoError

func RequireNoError(t *testing.T, err error, msg string)

RequireNoError fails test if error is not nil

func RetryWithTimeout

func RetryWithTimeout(t *testing.T, timeout time.Duration, interval time.Duration, fn func() error) error

RetryWithTimeout retries a function until it succeeds or timeout

func SkipIfShort

func SkipIfShort(t *testing.T, reason string)

SkipIfShort skips test if -short flag is set

func UnmarshalFixture

func UnmarshalFixture(t *testing.T, name string, target interface{})

UnmarshalFixture loads and unmarshals a JSON fixture into target

func ValidateAllFixtures

func ValidateAllFixtures(t *testing.T)

ValidateAllFixtures validates all expected fixture files exist and are valid JSON

func ValidateFixture

func ValidateFixture(t *testing.T, name string)

ValidateFixture validates a fixture file exists and is valid JSON

Types

type MockSocket

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

MockSocket represents a mock ocserv socket server for testing

func NewMockSocket

func NewMockSocket(t *testing.T, cfg MockSocketConfig) *MockSocket

NewMockSocket creates a new mock socket helper

For compose-based testing (recommended):

mock := NewMockSocket(t, MockSocketConfig{UseCompose: true})
defer mock.Close()

For local testing (development only):

mock := NewMockSocket(t, MockSocketConfig{SocketPath: "/tmp/test.socket"})
defer mock.Close()

func (*MockSocket) Close

func (m *MockSocket) Close()

Close cleans up mock socket resources

func (*MockSocket) SocketPath

func (m *MockSocket) SocketPath() string

SocketPath returns the Unix socket path for connecting to mock server

func (*MockSocket) WaitReady

func (m *MockSocket) WaitReady(t *testing.T) error

WaitReady waits for socket to become available

type MockSocketConfig

type MockSocketConfig struct {
	// SocketPath is the path to Unix socket (default: /tmp/occtl-test-{random}.socket)
	SocketPath string

	// FixturesPath is the path to fixtures directory (default: ../../test/fixtures/ocserv/occtl)
	FixturesPath string

	// StartTimeout is max time to wait for socket to become ready (default: 5s)
	StartTimeout time.Duration

	// UseCompose indicates whether to use podman-compose mock-ocserv service
	// If true, assumes mock-ocserv container is running with socket at /var/run/occtl.socket
	UseCompose bool

	// ComposeSocketVolume is the volume name for shared socket (default: mock-socket)
	ComposeSocketVolume string
}

MockSocketConfig holds configuration for mock socket server

type SystemdTestHelper

type SystemdTestHelper struct {
	ServiceName string
	ServiceFile string
	UserMode    bool
	UnitDir     string
	// contains filtered or unexported fields
}

SystemdTestHelper helps manage test systemd services

func NewSystemdTestHelper

func NewSystemdTestHelper(t *testing.T, serviceName string) *SystemdTestHelper

NewSystemdTestHelper creates a new systemd test helper

func (*SystemdTestHelper) Cleanup

func (h *SystemdTestHelper) Cleanup(t *testing.T)

Cleanup removes the test service

func (*SystemdTestHelper) GetServiceName

func (h *SystemdTestHelper) GetServiceName() string

GetServiceName returns the full service name

func (*SystemdTestHelper) RunSystemctl

func (h *SystemdTestHelper) RunSystemctl(ctx context.Context, args ...string) (string, string, error)

RunSystemctl runs a systemctl command for testing

func (*SystemdTestHelper) Setup

func (h *SystemdTestHelper) Setup(t *testing.T) error

Setup prepares the test service

func (*SystemdTestHelper) WaitForState

func (h *SystemdTestHelper) WaitForState(ctx context.Context, expectedState string, maxWait time.Duration) error

WaitForState waits for the service to reach a specific state

Jump to

Keyboard shortcuts

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