testutil

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package testutil provides a shared test server and helpers used across all dirio test suites (integration, admin, console).

Every constructor goes through startup.Init + startup.Prepare so the server is always initialised the same way the real binary initialises it. This prevents the class of failures where tests pass locally but fail after the Starter was introduced because RootFS / Metadata were nil.

Index

Constants

View Source
const (
	// DefaultAccessKey is the access key used by New and NewWithDataDir.
	DefaultAccessKey = "testaccess"
	// DefaultSecretKey is the secret key used by New and NewWithDataDir.
	DefaultSecretKey = "testsecretkey123"
)

Variables

This section is empty.

Functions

func CreateDataConfigWithCredentials

func CreateDataConfigWithCredentials(ts *TestServer, accessKey, secretKey string)

CreateDataConfigWithCredentials writes (or overwrites) a .dirio/config.json in the server's data directory with the given credentials. Useful for testing live credential-reload without restarting the server.

func DecodeJSON

func DecodeJSON(t *testing.T, resp *http.Response, v any)

DecodeJSON reads and JSON-decodes the response body into v, then closes it.

func DrainAndClose

func DrainAndClose(resp *http.Response)

DrainAndClose discards the response body and closes it.

func FindFreePort

func FindFreePort(t *testing.T) int

FindFreePort finds an available TCP port on 127.0.0.1 and returns it.

func SignRequestWithCredentials

func SignRequestWithCredentials(req *http.Request, body []byte, accessKey, secretKey string)

SignRequestWithCredentials signs req in-place using the provided credentials. Useful when a test needs to sign requests as a different user.

Types

type TestServer

type TestServer struct {
	// Server is the underlying server instance.  Test packages that need to
	// access server internals (e.g. the console package wiring SetConsole) can
	// use this field directly.
	Server    *server.Server
	DataDir   string
	Port      int
	BaseURL   string
	AdminURL  string
	AccessKey string
	SecretKey string
	// contains filtered or unexported fields
}

TestServer wraps a running dirio server for use in tests.

func New

func New(t *testing.T) *TestServer

New starts a server with the default test credentials (testaccess / testsecretkey123) explicitly set. This is the right constructor for the vast majority of tests.

func NewDataConfigOnly

func NewDataConfigOnly(t *testing.T, dataAccessKey, dataSecretKey string) *TestServer

NewDataConfigOnly starts a server where admin credentials come exclusively from a data config. The CLI credentials are treated as defaults (not explicitly set) and will therefore be ignored when the data config is present.

func NewDualAdmin

func NewDualAdmin(t *testing.T, cliAccessKey, cliSecretKey, dataAccessKey, dataSecretKey string) *TestServer

NewDualAdmin starts a server with both CLI credentials (explicit) and a pre-existing data config holding separate admin credentials. Both sets of credentials will be accepted.

func NewWithCanonicalDomain

func NewWithCanonicalDomain(t *testing.T, canonicalDomain string) *TestServer

NewWithCanonicalDomain starts a server with CanonicalDomain configured, enabling virtual-hosted-style routing in addition to path-style. Use the default test credentials.

func NewWithCredentials

func NewWithCredentials(t *testing.T, accessKey, secretKey string, explicit bool) *TestServer

NewWithCredentials starts a server with the given CLI credentials. Set explicit=true to simulate credentials provided via --access-key flag; set explicit=false to simulate the "no explicit credentials" startup path.

func NewWithDataDir

func NewWithDataDir(t *testing.T, dataDir string) *TestServer

NewWithDataDir starts a server against an existing, pre-populated data directory. Useful for MinIO import tests where the data dir is seeded before the server starts. The server uses the default test credentials. Call Stop() to shut down without removing the data directory.

func NewWithPreStartHook

func NewWithPreStartHook(t *testing.T, hook func(*server.Server)) *TestServer

NewWithPreStartHook starts a server with the default test credentials and calls hook on the *server.Server after New but before Start. This is used by the console test package so it can call SetConsole before the HTTP listener is opened.

func (*TestServer) AdminRequest

func (ts *TestServer) AdminRequest(t *testing.T, method, path string, body []byte) *http.Response

AdminRequest performs a signed request against the MinIO admin API. body is sent as-is (use nil for requests without a body).

func (*TestServer) BucketURL

func (ts *TestServer) BucketURL(bucket string) string

BucketURL returns the URL for a bucket.

func (*TestServer) Cleanup

func (ts *TestServer) Cleanup()

Cleanup stops the server and removes the temporary data directory. Registered automatically with t.Cleanup by all constructors.

func (*TestServer) CreateBucket

func (ts *TestServer) CreateBucket(t *testing.T, bucket string)

CreateBucket creates a bucket via the S3 API and fails the test on error.

func (*TestServer) CreateTestObjects

func (ts *TestServer) CreateTestObjects(t *testing.T, bucket string, objects map[string]string)

CreateTestObjects uploads multiple objects from a map[key]content.

func (*TestServer) DataPath

func (ts *TestServer) DataPath(parts ...string) string

DataPath returns an absolute path rooted at the server's data directory.

func (*TestServer) DecryptAdminResponse

func (ts *TestServer) DecryptAdminResponse(t *testing.T, resp *http.Response, v any)

DecryptAdminResponse reads an encrypted admin API response body, decrypts it using the test server's secret key, and unmarshals it into v.

func (*TestServer) DecryptAndDecodeJSON

func (ts *TestServer) DecryptAndDecodeJSON(t *testing.T, resp *http.Response, v any)

DecryptAndDecodeJSON decrypts a madmin-encrypted response body and JSON-decodes it into v.

func (*TestServer) EncryptedAdminRequest

func (ts *TestServer) EncryptedAdminRequest(t *testing.T, method, path string, bodyData any) *http.Response

EncryptedAdminRequest JSON-marshals bodyData, encrypts it with the admin secret key using madmin, and sends it as an admin API request.

func (*TestServer) GetUserList

func (ts *TestServer) GetUserList(t *testing.T, resp *http.Response) map[string]UserListInfo

func (*TestServer) NewRequest

func (ts *TestServer) NewRequest(method, url string, body []byte) (*http.Request, error)

NewRequest builds a signed HTTP request against the server.

func (*TestServer) NewVHostRequest

func (ts *TestServer) NewVHostRequest(method, vhostHost, path string, body []byte) (*http.Request, error)

NewVHostRequest builds a signed HTTP request that connects to the server's listener but carries vhostHost as the Host header, for exercising virtual-hosted-style bucket resolution. path should contain only the key (no bucket segment) since the bucket is conveyed via vhostHost.

func (*TestServer) ObjectURL

func (ts *TestServer) ObjectURL(bucket, key string) string

ObjectURL returns the URL for an object within a bucket.

func (*TestServer) PutObject

func (ts *TestServer) PutObject(t *testing.T, bucket, key, content string)

PutObject uploads an object via the S3 API and fails the test on error.

func (*TestServer) SetBucketPolicy

func (ts *TestServer) SetBucketPolicy(t *testing.T, bucket, policyJSON string)

SetBucketPolicy sets a bucket policy via the S3 API, failing the test on error.

func (*TestServer) SignRequest

func (ts *TestServer) SignRequest(req *http.Request, body []byte)

SignRequest signs req in-place using the server's own credentials.

func (*TestServer) Stop

func (ts *TestServer) Stop()

Stop shuts down the server gracefully and waits for it to fully stop. The data directory is NOT removed — use Cleanup for that.

func (*TestServer) URL

func (ts *TestServer) URL(path string) string

URL returns the full URL for a server-relative path (e.g. "/mybucket").

type UserListInfo

type UserListInfo struct {
	MemberOf  string    `json:"memberOf"`
	Status    string    `json:"status"`
	UpdatedAt time.Time `json:"updatedAt"`
}

Jump to

Keyboard shortcuts

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