...

Package postgres

import "edge-infra.dev/test/f2/x/postgres"
Overview
Index

Overview ▾

Package postgres implements an f2 extension for working with PostgreSQL databases

For L1 tests an embedded-postgres binary is started, while for L2 tests an external PostgreSQL cluster with a configured database is expected to be running and connection details passed to the test binary. Required flags can be discovered by running the example test using the --help argument, and searching for `postgres`

rosa //test/f2/examples/pgtest/... --test_arg=--help

Test isolation is achieved by creating a new unique schema within the database for each test. Test authors should ensure tests only modify data within the configured schema. Helper methods have been created to ensure tests have easy access to the required schema using the Postgres.DB method and the Postgres.Schema method. If tests require using the default schema they can disable unique schema creation using the SkipSchemaIsolation option, however these tests should generally be considered disruptive. The schema is automatically removed by the framework during the test teardown.

Any non schema scoped resources, such as postgres roles, should use the f2.Context's RunID to ensure they will not cause conflicts with concurrent tests, and authors should ensure all of these resources are cleaned up during teardown.

Options

Option that can be passed include:

  1. ApplySeedModel: Provides a method to migrate the database to the full edgesql schema.
  2. SkipSchemaIsolation: this provides a method to avoid tests generating their own pgsql schemas

Usage

Examples can be found under `/test/f2/examples/pgtest` and can be run with:

rosa //test/f2/examples/pgtest/...

func WithData

func WithData(seedData []plugin.Seed, msgAndArgs ...interface{}) f2.StepFn

WithData is a f2.StepFn that can be used to add data to the initialised database. Requires ApplySeedModel to be specified.

The edge-infra SeededPostgres package includes a Seed variable which can be used as an example to create Seed data or can be passed directly to this function.

type Option

type Option func(*options)

func ApplySeedModel

func ApplySeedModel() Option

ApplySeedModel should be passed in order to apply edgesql migrations to database

func SkipSchemaIsolation

func SkipSchemaIsolation() Option

SkipSchemaIsolution should be passed if you don't want the framework to create isolated schemas for every test

type Postgres

Postgres is a f2 extension that sets up an embedded Postgres server

type Postgres struct {
    ConnectionName string
    Host           string
    Port           uint
    User           string
    Password       string
    Database       string
    MaxConns       int
    MaxIdleConns   int
    // contains filtered or unexported fields
}

func FromContext

func FromContext(ctx fctx.Context) (*Postgres, error)

FromContext attempts to fetch an instance of Postgres from the test context and returns an error if it is not discovered.

func FromContextT

func FromContextT(ctx fctx.Context, t *testing.T) *Postgres

FromContextT is a testing variant of FromContext that immediately fails the test if Postgres isnt presnt in the testing context.

func New

func New(opts ...Option) *Postgres

New initialises a new Postgres struct. The extension will be initialised with migrations from edgesql by default. if data with custom data structures are needed and these would conflict with edgesql tables, SkipSeedModel Option should be passed:

pg := postgres.New(postgres.SkipSeedModel())

func (*Postgres) BindFlags

func (pg *Postgres) BindFlags(fs *flag.FlagSet)

BindFlags registers test flags for the framework extension.

func (*Postgres) DB

func (pg *Postgres) DB() *sql.DB

DB returns an initialised *sql.DB with the search_path set to help isolate tests

DB returns a single sql.DB instance for every call within a given test. Tests generally shouldn't call db.Close manually.

func (*Postgres) DSN

func (pg *Postgres) DSN() string

DSN returns a DSN string for the extension. Can be used to connect via database/sql.Open.

As integration tests are expected to run against both CloudSQL and regular postgres databases, it is recommended that tests use Postgres.DB to fetch a preconfigured sql.DB struct, rather than manually calling databas/sql.Open

func (*Postgres) IntoContext

func (pg *Postgres) IntoContext(ctx fctx.Context) fctx.Context

IntoContext stores the framework extension in the test context.

func (*Postgres) K8SHost

func (pg *Postgres) K8SHost() string

K8SHost returns the hostname to be used to connect to the DB when connecting from a pod within the ktest cluster. The [Postgres.Host] field should be used in most cases during normal connection from the test binary.

func (*Postgres) RegisterFns

func (pg *Postgres) RegisterFns(f f2.Framework)

func (*Postgres) Schema

func (pg *Postgres) Schema() string

Returns the schema to be used by the individual test for test isolation