Documentation ¶
Index ¶
- Variables
- func AppendTokenToOutgoingContext(ctx context.Context, fieldName, token string) context.Context
- func BuildGoSource(packagePath, output string) (func() error, error)
- func GetOpenPort() (int, error)
- func GetOpenPortInRange(lowerBound, upperBound int) (int, error)
- func MakeStandardRequest(method, url string, payload interface{}) (*http.Request, error)
- func MakeTestJWT(method jwt.SigningMethod, claims jwt.Claims) (string, error)
- func RunBinary(binPath string, args ...string) (func(), error)
- func RunContainer(image string, dockerArgs, runtimeArgs []string) (func() error, error)
- func StandardTestJWT() (string, error)
- func StandardTestingContext() (context.Context, error)
- func WithMigrateDownFunction(migrateDownFunction func(*sql.DB) error) func(*PostgresDB)
- func WithMigrateUpFunction(migrateUpFunction func(*sql.DB) error) func(*PostgresDB)
- func WithName(name string) func(*PostgresDB)
- func WithPassword(password string) func(*PostgresDB)
- func WithPort(port int) func(*PostgresDB)
- func WithTimeout(timeout time.Duration) func(*PostgresDB)
- func WithUser(user string) func(*PostgresDB)
- func WithVersion(version string) func(*PostgresDB)
- type PostgresDB
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // StandardClaims is the standard payload inside a test JWT StandardClaims = jwt.MapClaims{ auth.MultiTenancyField: "TestAccount", } )
Functions ¶
func AppendTokenToOutgoingContext ¶
AppendTokenToOutgoingContext adds an authorization token to the gRPC request context metadata. The user must provide a token field name like "token" or "bearer" to this function. It is intended specifically for gRPC testing.
Example (Output) ¶
// make the jwt authToken, err := jwt.NewWithClaims( jwt.SigningMethodHS256, jwt.MapClaims{ "user": "user-test", "roles": "admin", }, ).SignedString([]byte("some-secret")) if err != nil { log.Fatalf("unable to build token: %v", err) } // add the jwt to context ctxBearer := AppendTokenToOutgoingContext( context.Background(), "Bearer", authToken, ) // check to make sure the token was added md, ok := metadata.FromOutgoingContext(ctxBearer) if !ok || len(md["authorization"]) < 1 { log.Fatalf("unable to get token from context: %v", err) } fields := strings.Split(md["authorization"][0], " ") if len(fields) < 2 { log.Fatalf("unexpected authorization metadata: %v", fields) } fmt.Println(fields[0] == "Bearer") fmt.Println(fields[1] == authToken)
Output: true true
func BuildGoSource ¶
BuildGoSource builds a target Go package and gives the resulting binary some user-defined name. The function returned by BuildGoSource will remove the binary that got created.
func GetOpenPort ¶
GetOpenPort searches for an open port on the host
func GetOpenPortInRange ¶
GetOpenPortInRange finds the first unused port within specific range
func MakeStandardRequest ¶
MakeStandardRequest issues an HTTP request a specific endpoint with Atlas-specific request data (e.g. the authorization token)
func MakeTestJWT ¶
MakeTestJWT generates a token string based on the given JWT claims
func RunContainer ¶
RunContainer launches a detached docker container on the host machine. It takes an image name, a list of "docker run" arguments, and a list of arguments that get passed to the container runtime
func StandardTestJWT ¶
StandardTestJWT builds a JWT with the standard test claims in the JWT payload
func StandardTestingContext ¶
StandardTestingContext returns an outgoing request context that includes the standard test JWT. It is intended specifically for gRPC testing.
func WithMigrateDownFunction ¶
func WithMigrateDownFunction(migrateDownFunction func(*sql.DB) error) func(*PostgresDB)
WithMigrateFunction is used to tear down the test Postgres according to a specific set of migrations. It runs on a per-test basis whenever the Reset() function is called.
func WithMigrateUpFunction ¶
func WithMigrateUpFunction(migrateUpFunction func(*sql.DB) error) func(*PostgresDB)
WithMigrateFunction is used to rebuild the test Postgres database on a per-test basis. Whenever the database is reset with the Reset() function, the migrateUp function will rebuild the tables.
func WithName ¶
func WithName(name string) func(*PostgresDB)
WithName is used to specify the name of the test Postgres database. By default the database name is "test-postgres-db"
func WithPassword ¶
func WithPassword(password string) func(*PostgresDB)
WithPassword is used to specify the password of the test Postgres database
func WithPort ¶
func WithPort(port int) func(*PostgresDB)
WithPort is used to specify the port of the test Postgres database. By default, the test database will find the first open port in the 35000+ range
func WithTimeout ¶
func WithTimeout(timeout time.Duration) func(*PostgresDB)
WithTimeout is used to specify a connection timeout to the database
func WithUser ¶
func WithUser(user string) func(*PostgresDB)
WithUser is used to specify the name of the Postgres user that owns the test database
func WithVersion ¶
func WithVersion(version string) func(*PostgresDB)
WithVersion is used to specify the version of the test Postgres database. By default the version is "latest"
Types ¶
type PostgresDB ¶
type PostgresDB struct {
// contains filtered or unexported fields
}
func NewTestPostgresDB ¶
func NewTestPostgresDB(opts ...option) (PostgresDB, error)
NewTestPostgresDB returns a test postgres database that the functional options that have been provided by the caller
func (PostgresDB) CheckConnection ¶
func (db PostgresDB) CheckConnection() error
func (PostgresDB) GetDSN ¶
func (db PostgresDB) GetDSN() string
GetDSN returns the database connection string for the test Postgres database
func (PostgresDB) GetDriverName ¶ added in v2.2.0
func (db PostgresDB) GetDriverName() string
GetDriverName returns the name of the driver used for the DSN.
func (PostgresDB) Reset ¶
func (db PostgresDB) Reset() error
Reset drops all the tables in a test database and regenerates them by running migrations. If a migration function has not been specified, then the tables are dropped but not regenerated
func (PostgresDB) RunAsDockerContainer ¶
func (db PostgresDB) RunAsDockerContainer() (func() error, error)
RunAsDockerContainer spins-up a Postgres database server as a Docker container. The test Postgres database will run inside this Docker container.