servertest

package
v0.0.0-...-261f787 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package servertest contains helpers for running server integration tests.

This package needs a lot more work. It is basically a placeholder now with a single test that makes sure OpenTelemetry stuff can start (since it broke in production before). The API is not final.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Settings

type Settings struct {
	// Options are base server options.
	//
	// In production they are usually parsed from flags, but here they should
	// be supplied explicitly (unless defaults are not good).
	//
	// Some of the fields will be forcefully overridden to make sure the server
	// behaves well in the test environment. In particular `Prod` will always be
	// false.
	//
	// If nil, some safe defaults will be used.
	Options *server.Options

	// Modules is a list of configured modules to use in the server.
	//
	// Unlike the production server, no flag parsing is done. Modules need to be
	// created using concrete options suitable for the test (i.e. do not use
	// NewModuleFromFlags, since there are no flags).
	Modules []module.Module

	// Init is called during the server initialization before starting it.
	//
	// This is a place where various routes can be registered and extra background
	// activities started.
	Init func(srv *server.Server) error

	// DisableFakeGCEMetadata can be used to **disable** fake GCE metadata server.
	//
	// The GCE metadata server is used to grab authentication tokens when making
	// calls to other services.
	//
	// By default (when DisableFakeGCEMetadata is false), the test server is
	// launched in an environment with a fake GCE metadata server that returns
	// fake tokens. It means it won't be able to make any real calls to external
	// services (and also accidentally do something bad).
	//
	// Additionally this fake GCE metadata makes the server believe it runs in a
	// GCP environment even when running on a local workstation. This will make it
	// hit more production code paths, potentially uncovering bugs there.
	//
	// If DisableFakeGCEMetadata is true the test server will use whatever is
	// available in the execution environment or configured via Options. This
	// generally means it will use real authentication tokens.
	DisableFakeGCEMetadata bool
}

Settings are parameters for launching a test server.

type TestServer

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

TestServer represents a running server-under-test.

func RunServer

func RunServer(ctx context.Context, settings *Settings) (*TestServer, error)

RunServer launches a test server and runs it in background.

It initializes the server based on the settings, launches its serving loop in a background goroutine and waits for the health check to successfully pass before returning.

The caller can then make HTTP or gRPC requests to the server via the loopback address in TestServer.

The caller is responsible to shut the server down when done with it via a call to Shutdown.

The given context is used as the root context for the server (eventually inherited by everything under it). It can be a background context if nothing extra is needed.

Note that some server's dependencies (in particular OpenTelemetry) use global variables and thus running multiple services in the same process concurrently may result in flakiness.

func (*TestServer) Context

func (ts *TestServer) Context() context.Context

Context is the server's root context with all modules initialized.

func (*TestServer) GRPCAddr

func (ts *TestServer) GRPCAddr() string

GRPCAddr is "127.0.0.1:<port>" of the server's gRPC port.

func (*TestServer) HTTPAddr

func (ts *TestServer) HTTPAddr() string

HTTPAddr is "127.0.0.1:<port>" of the server's HTTP port.

func (*TestServer) Shutdown

func (ts *TestServer) Shutdown()

Shutdown closes the server waiting for it to fully terminate.

Jump to

Keyboard shortcuts

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