test_utils

package
v0.0.0-...-977fb78 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 40 Imported by: 1 module, 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

Index

Constants

View Source
const UnresolvableHost = "noserverexists.invalid"

UnresolvableHost is a name under the reserved .invalid TLD (RFC 6761), which can never resolve.

Variables

This section is empty.

Functions

func ChownToDaemon

func ChownToDaemon(t testing.TB, paths ...string)

ChownToDaemon changes ownership of the given paths to the XRootD daemon user. When not running as root this is a no-op (the daemon user is the current user).

func FailLookupsOf

func FailLookupsOf(t *testing.T, host string)

FailLookupsOf makes connections through the global transport (config.GetTransport and the clients built on it) to host fail the way a lookup of a nonexistent name does -- "dial tcp: lookup <host>: no such host" -- without asking a resolver. A real lookup depends on the network and can take longer than a test is prepared to wait. Connections to any other host are dialed normally. The real dialer is restored when the test ends.

func FileOwner

func FileOwner(t *testing.T, path string) (uid, gid uint32)

FileOwner returns the UID and GID of the file at path.

func GenerateJWK

func GenerateJWK() (jwk.Key, jwk.Set, string, error)

GenerateJWK generates a JWK private key and a corresponding JWKS public key, and the string representation of the public key

func GenerateJWKS

func GenerateJWKS() (string, error)

func GetPelicanBinary

func GetPelicanBinary(t testing.TB) string

GetPelicanBinary returns the path to a `pelican` binary. If $TEST_PELICAN_BINARY is set, it must be the absolute path to an existing binary, which is used as is.

func GetPelicanServerBinary

func GetPelicanServerBinary(t testing.TB) string

GetPelicanServerBinary returns the path to a `pelican-server` binary. If $TEST_PELICAN_SERVER_BINARY is set, it must be the absolute path to an existing binary, which is used as is.

func GetSampleMetadataServerBinary

func GetSampleMetadataServerBinary(t testing.TB) string

GetSampleMetadataServerBinary returns the path to a `sample_metadata_server` binary. If $TEST_PELICAN_SAMPLE_METADATA_SERVER_BINARY is set, it must be the absolute path to an existing binary, which is used as is.

func GetTmpStoragePrefixDir

func GetTmpStoragePrefixDir(t *testing.T) string

Helper function for other tests who call server_utils.GetOriginExports() internally. This function gets a temp dir for export StoragePrefixes, whose existence is validated by server_utils.GetOriginExports(). The directory is created with 0777 permissions so the XRootD daemon user (which is "others" relative to the test process owner) has the read, write, and execute permissions needed for all origin capabilities.

func GetUniqueAvailablePorts

func GetUniqueAvailablePorts(count int) ([]int, error)

getUniqueAvailablePorts returns `count` unique, available ports. **WARNING**: There is a small race condition between getting the list of available ports and actually binding to them in whatever service uses these values. Be warned they may (but are hopefully unlikely to) disappear before you can use them!

func InitClient

func InitClient(t *testing.T, initCfg map[param.Param]any)

Initialize the client for a unit test

Will set the configuration to a temporary directory (to avoid pulling in global configuration) and set some arbitrary param configurations. The initCfg map uses typed param constants as keys; each value is set via the param's typed Set method where possible (StringParam, BoolParam, etc.), falling back to param.Set for OpaqueParam or unknown types. Panics from type mismatches are caught and reported as test failures.

func MockFederationRoot

func MockFederationRoot(t *testing.T, fInfo *pelican_url.FederationDiscovery, kSet *jwk.Set)

Create a mock federation root that can respond to requests for metadata and federation keys

func MockIssuer

func MockIssuer(t *testing.T, kSet *jwk.Set) string

Create a mock issuer that responds to request for /.well-known/openid-configuration and /.well-known/issuer.jwks

func RegistryMockup

func RegistryMockup(t *testing.T, prefix string) *httptest.Server

For these tests, we only need to lookup key locations. Create a dummy registry that only returns the jwks_uri location for the given key. Once a server is instantiated, it will only return locations for the provided prefix. To change prefixes, create a new registry mockup.

func RemoveTestBinaries

func RemoveTestBinaries()

RemoveTestBinaries removes the binaries built by this test process. Call it from TestMain after m.Run returns.

func SetupGlobalTestLogging

func SetupGlobalTestLogging() func()

SetupGlobalTestLogging silences logrus output for an entire package's tests (for use in TestMain). It preserves existing logger settings and restores them when the returned cleanup is called.

func SetupTestLogging

func SetupTestLogging(t testing.TB) func()

SetupTestLogging configures logrus to write to the test's log buffer. This should be called at the beginning of tests to ensure clean output. Returns a cleanup function that should be called with defer.

func SkipIfNoMinio

func SkipIfNoMinio(t *testing.T)

SkipIfNoMinio skips the test if the minio binary is not available on PATH, or fails it if requireMinioEnv is set.

func SkipIfShort

func SkipIfShort(t testing.TB, reason string)

SkipIfShort skips the calling test when "go test -short" is in effect.

Reserve this for tests that measurement shows are not worth their time on every run, or that need something esoteric (HTCondor, rclone, minio, root). AGENTS.md describes the criteria. Call it as the first statement of the test, before any setup starts.

func SkipUnlessPrivileged

func SkipUnlessPrivileged(t *testing.T)

SkipUnlessPrivileged skips the test if the process lacks CAP_SETUID or CAP_SETGID in its effective capability set.

func SkipUnlessTestUsers

func SkipUnlessTestUsers(t *testing.T, usernames ...string)

SkipUnlessTestUsers skips the test if any of the given usernames cannot be resolved via the system user database.

func StartMinio

func StartMinio(t *testing.T, bucket string) (endpoint, accessKey, secretKey string)

StartMinio launches a minio server bound to 127.0.0.1:0 (OS-assigned port), parses the actual listening port from minio's log output, and returns the endpoint URL, access key, and secret key. The server is killed when the test completes.

This avoids the TOCTOU race inherent in picking a free port first and then passing it to minio.

func TestContext

func TestContext(ictx context.Context, t testing.TB) (ctx context.Context, cancel context.CancelFunc, egrp *errgroup.Group)

func WriteBigBuffer

func WriteBigBuffer(t *testing.T, fp io.WriteCloser, sizeMB int) (size int)

Writes a file at least the specified size in MB

func WriteJWKSFile

func WriteJWKSFile(t testing.TB, dir, name string, keys ...jwk.Key) string

WriteJWKSFile serializes keys into a JWKS JSON file at dir/name and returns the full path. Fails the test on any error. This is a thin shim over configtest.WriteJWKSFile.

Types

type StubFederation

type StubFederation struct {
	// URL is the federation's discovery URL, which is also where its
	// director and object server answer.
	URL string
	// CAFile is the CA that signed the server's certificate.
	CAFile string
	// DirectorQueries counts the questions put to the director.
	DirectorQueries atomic.Int64
	// contains filtered or unexported fields
}

StubFederation is one TLS server playing every federation role a client transfer touches: the discovery document, the director, and the object server the director redirects to. Every object under the namespace /test exists and holds the same contents.

It is for exercising the client against a federation without starting one. MockFederationRoot covers discovery alone and turns certificate verification off to do it; this covers the director and the object server as well, and is verified the way a client verifies a production federation.

func NewStubFederation

func NewStubFederation(t *testing.T, opts StubFederationOptions) *StubFederation

NewStubFederation starts a stub federation for the duration of t.

func (*StubFederation) InitClient

func (sf *StubFederation) InitClient(t *testing.T, extra map[param.Param]any)

InitClient initializes the client against the stub, discovering through it and trusting the CA that signed its certificate. extra adds the test's own parameters.

type StubFederationOptions

type StubFederationOptions struct {
	// Contents is what every object holds.
	Contents string
	// RequireToken makes the director say the namespace wants a credential.
	RequireToken bool
	// OnFetch, if set, is called on each object fetch with the object's
	// federation path, before anything is written.  A non-zero return fails
	// the fetch with that status; zero lets it succeed.  Fetches run
	// concurrently, so it must be safe for concurrent use.
	OnFetch func(objectPath string) int
}

StubFederationOptions configures a StubFederation.

type TestLogHook

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

TestLogHook forwards log entries to the test log buffer so they appear under the test's output (visible with -v or on failure) and never hit stdout/stderr directly.

func NewTestLogHook

func NewTestLogHook(t testing.TB) *TestLogHook

NewTestLogHook creates a new TestLogHook that writes to testing.TB's log buffer

func (*TestLogHook) Fire

func (hook *TestLogHook) Fire(entry *logrus.Entry) error

Fire is called on every log entry

func (*TestLogHook) Levels

func (hook *TestLogHook) Levels() []logrus.Level

Levels defines which log levels this hook applies to

Jump to

Keyboard shortcuts

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