testutil

package
v0.0.0-...-2e9041e Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package testutil provides test helpers for the golang-samples repo.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateTestBucket

func CreateTestBucket(ctx context.Context, t *testing.T, client *storage.Client, projectID, prefix string) string

CreateTestBucket creates a new bucket with the given prefix and registers a cleanup function to delete the bucket and any objects it contains. It is equivalent to TestBucket but allows Storage Client re-use.

func DeleteBucketIfExists

func DeleteBucketIfExists(ctx context.Context, client *storage.Client, bucket string) error

DeleteBucketIfExists deletes a bucket and all its objects.

func DeleteExpiredBuckets

func DeleteExpiredBuckets(client *storage.Client, projectID, prefix string, expireAge time.Duration) error

DeleteExpiredBuckets deletes old testing buckets that weren't cleaned previously.

func Retry

func Retry(t *testing.T, maxAttempts int, sleep time.Duration, f func(r *R)) bool

Retry runs function f for up to maxAttempts times until f returns successfully, and reports whether f was run successfully. It will sleep for the given period between invocations of f. Use the provided *testutil.R instead of a *testing.T from the function.

func RetryWithoutTest

func RetryWithoutTest(maxAttempts int, sleep time.Duration, f func(r *R)) bool

RetryWithoutTest is a variant of Retry that does not use a testing parameter. It is meant for testing utilities that do not pass around the testing context, such as cloudrunci.

func TestBucket

func TestBucket(ctx context.Context, t *testing.T, projectID, prefix string) string

TestBucket creates a new bucket with the given prefix and registers a cleanup function to delete the bucket and any objects it contains when the test finishes. TestBucket returns the bucket name. It fails the test if bucket creation fails.

func UniqueBucketName

func UniqueBucketName(prefix string) string

UniqueBucketName returns a unique name with the test prefix.

Types

type Context

type Context struct {
	ProjectID string
	Dir       string
}

Context holds information useful for tests.

func ContextMain

func ContextMain(m *testing.M) (tc Context, ok bool)

ContextMain gets a test context from a TestMain function. Useful for initializing global variables before running parallel system tests. ok is false if the project is not set up properly for system tests.

func EndToEndTest

func EndToEndTest(t *testing.T) Context

EndToEndTest gets the test context, and sets the test as Parallel. The test is skipped if the GOLANG_SAMPLES_E2E_TEST environment variable is not set.

func SystemTest

func SystemTest(t *testing.T) Context

SystemTest gets the test context. The test is skipped if the GOLANG_SAMPLES_PROJECT_ID environment variable is not set.

func (Context) Path

func (tc Context) Path(p ...string) string

type R

type R struct {
	// The number of current attempt.
	Attempt int
	// contains filtered or unexported fields
}

R is passed to each run of a flaky test run, manages state and accumulates log statements.

func (*R) Errorf

func (r *R) Errorf(s string, v ...interface{})

Errorf is equivalent to Logf followed by Fail.

func (*R) Fail

func (r *R) Fail()

Fail marks the run as failed, and will retry once the function returns.

func (*R) Logf

func (r *R) Logf(s string, v ...interface{})

Logf formats its arguments and records it in the error log. The text is only printed for the final unsuccessful run or the first successful run.

type Runner

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

Runner holds the result of `go build`

func BuildMain

func BuildMain(t *testing.T) *Runner

BuildMain builds the main package in the current working directory. If it doesn't build, t.Fatal is called. Test methods calling BuildMain should run Runner.Cleanup.

func (*Runner) Built

func (r *Runner) Built() bool

Built reports whether the build was successful.

func (*Runner) Cleanup

func (r *Runner) Cleanup()

Cleanup removes the built binary.

func (*Runner) Run

func (r *Runner) Run(env map[string]string, timeout time.Duration, args ...string) (stdout, stderr []byte, err error)

Run executes runs the built binary until terminated or timeout has been reached, and indicates successful execution on return. You can supply extra arguments for the binary via args.

Jump to

Keyboard shortcuts

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