Documentation ¶
Overview ¶
Package testutil contains helper functions for writing tests.
Index ¶
- func CanReplay(replayFilename string) bool
- func Diff(x, y interface{}, opts ...cmp.Option) string
- func Equal(x, y interface{}, opts ...cmp.Option) bool
- func JWTConfig() (*jwt.Config, error)
- func NewRand(t time.Time) *rand.Rand
- func PageBounds(pageSize int, pageToken string, length int) (from, to int, nextPageToken string, err error)
- func ProjID() string
- func TokenSource(ctx context.Context, scopes ...string) oauth2.TokenSource
- func TokenSourceEnv(ctx context.Context, envVar string, scopes ...string) oauth2.TokenSource
- type Server
- type TestExporter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CanReplay ¶ added in v0.25.0
CanReplay reports whether an integration test can be run in replay mode. The replay file must exist, and the GCLOUD_TESTS_GOLANG_ENABLE_REPLAY environment variable must be non-empty.
func Diff ¶ added in v0.11.0
Diff reports the differences between two values. Diff(x, y) == "" iff Equal(x, y).
func JWTConfig ¶ added in v0.17.0
JWTConfig reads the JSON private key file whose name is in the default environment variable, and returns the jwt.Config it contains. It ignores scopes. If the environment variable is empty, it returns (nil, nil).
func NewRand ¶ added in v0.25.0
NewRand creates a new *rand.Rand seeded with t. The return value is safe for use with multiple goroutines.
func PageBounds ¶ added in v0.18.0
func PageBounds(pageSize int, pageToken string, length int) (from, to int, nextPageToken string, err error)
PageBounds converts an incoming page size and token from an RPC request into slice bounds and the outgoing next-page token.
PageBounds assumes that the complete, unpaginated list of items exists as a single slice. In addition to the page size and token, PageBounds needs the length of that slice.
PageBounds's first two return values should be used to construct a sub-slice of the complete, unpaginated slice. E.g. if the complete slice is s, then s[from:to] is the desired page. Its third return value should be set as the NextPageToken field of the RPC response.
func ProjID ¶
func ProjID() string
ProjID returns the project ID to use in integration tests, or the empty string if none is configured.
func TokenSource ¶
func TokenSource(ctx context.Context, scopes ...string) oauth2.TokenSource
TokenSource returns the OAuth2 token source to use in integration tests, or nil if none is configured. It uses the standard environment variable for tests in this repo.
func TokenSourceEnv ¶ added in v0.15.0
TokenSourceEnv returns the OAuth2 token source to use in integration tests. or nil if none is configured. It tries to get credentials from the filename in the environment variable envVar. If the environment variable is unset, TokenSourceEnv will try to find 'Application Default Credentials'. Else, TokenSourceEnv will return nil. TokenSourceEnv will log.Fatal if the token source is specified but missing or invalid.
Types ¶
type Server ¶
A Server is an in-process gRPC server, listening on a system-chosen port on the local loopback interface. Servers are for testing only and are not intended to be used in production code.
To create a server, make a new Server, register your handlers, then call Start:
srv, err := NewServer() ... mypb.RegisterMyServiceServer(srv.Gsrv, &myHandler) .... srv.Start()
Clients should connect to the server with no security:
conn, err := grpc.Dial(srv.Addr, grpc.WithInsecure()) ...
type TestExporter ¶ added in v0.21.0
func NewTestExporter ¶ added in v0.21.0
func NewTestExporter() *TestExporter
func (*TestExporter) ExportSpan ¶ added in v0.21.0
func (te *TestExporter) ExportSpan(s *trace.SpanData)
func (*TestExporter) ExportView ¶ added in v0.21.0
func (te *TestExporter) ExportView(vd *view.Data)
func (*TestExporter) Unregister ¶ added in v0.21.0
func (te *TestExporter) Unregister()