testdb

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: Apache-2.0 Imports: 27 Imported by: 0

README

Postgres Support

You can use VCService with a local Postgres instance as an alternative to Yugabyte.

Run VCService with the following environment variables set and make sure your Postgres instance is running.

export DB_DEPLOYMENT=local

Start Postgres

Note that VCService uses yugabyte:yugabye as default user and password, and runs on port 5344. Please make sure you set up your Postgres instance accordingly.

For local testing with Postgres in docker you can simply use the following snipped.

docker run --name sc_postgres_unit_tests \
  -e POSTGRES_PASSWORD=yugabyte \
  -e POSTGRES_USER=yugabyte \
  -p 5433:5432 \
  -d postgres:16.9-alpine3.21

You can kill the instance by running:

docker ps -aq -f name=sc_postgres_unit_tests | xargs docker rm -f

Testing

Once Postgres is up and running you can run the tests of VC service.

DB_DEPLOYMENT=local go test ./service/vc/dbtest/...

Documentation

Index

Constants

View Source
const (

	// DefaultPostgresImage is the official PostgreSQL image used across unit and integration tests.
	// Must match the version in scripts/get-and-start-postgres.sh.
	DefaultPostgresImage = "postgres:16.9-alpine3.21"

	// YugabytedReadinessOutput is the output indicating that a Yugabyted node is ready.
	YugabytedReadinessOutput = "Data placement constraint successfully verified"
	// YugabyteTabletNodeReadinessOutput is the output indicating that a yugabyte's tablet node is ready.
	YugabyteTabletNodeReadinessOutput = "syncing data to disk ... ok"
	// PostgresReadinesssOutput is the output indicating that a PostgreSQL node is ready.
	PostgresReadinesssOutput = "database system is ready to accept connections"
	// SecondaryPostgresNodeReadinessOutput is the output indicating that a secondary PostgreSQL node is ready.
	SecondaryPostgresNodeReadinessOutput = "started streaming WAL from primary"
)
View Source
const (

	// YugaDBType represents the usage of Yugabyte DB.
	YugaDBType = "yugabyte"
	// PostgresDBType represents the usage of PostgreSQL DB.
	PostgresDBType = "postgres"
)

Variables

View Source
var (
	// YugabyteCMD starts yugabyte without fault tolerance (single server).
	YugabyteCMD = []string{
		"bin/yugabyted", "start",
		"--callhome", "false",
		"--background", "false",
		"--ui", "false",
		"--tserver_flags",
		"ysql_max_connections=500," +
			"tablet_replicas_per_gib_limit=4000," +
			"yb_num_shards_per_tserver=1," +
			"minloglevel=3," +
			"yb_enable_read_committed_isolation=true",
	}
)

Functions

func CleanupSharedContainer

func CleanupSharedContainer()

CleanupSharedContainer stops and removes the shared test container. Call this from TestMain after m.Run(). It is safe to call even when no container was created (e.g. DB_DEPLOYMENT=local).

func ConnectAndQueryTest

func ConnectAndQueryTest(t *testing.T, connections *Connection)

ConnectAndQueryTest is an exported function for testing purpose.

func RunTestMain

func RunTestMain(m *testing.M)

RunTestMain is a convenience wrapper for TestMain functions that only need the shared container lifecycle. It sets up the container, runs all tests, and cleans up. For TestMain functions with additional setup (e.g. building binaries), call SetupSharedContainer/CleanupSharedContainer directly.

func SetupSharedContainer

func SetupSharedContainer() error

SetupSharedContainer creates a fresh test container for the current test. Call this from TestMain before m.Run(). The container uses a unique name to avoid interfering with developer containers or other test runs. For DB_DEPLOYMENT=local, this is a no-op (returns nil).

Design decisions:

  • Unique name (UUID suffix): never touches existing containers. A developer may have a YugabyteDB container running for other purposes — we must not stop, remove, or reuse it.
  • Auto-assigned host port: avoids port conflicts with any other service on the host, including other test runs.
  • CleanupSharedContainer (called after m.Run) guarantees removal. In the rare case of SIGKILL, `make kill-test-docker` cleans up all containers matching the "sc_test" prefix.

Types

type Connection

type Connection struct {
	Endpoints   []*connection.Endpoint
	User        string
	Password    string
	Database    string
	LoadBalance bool
	TLS         dbconn.DatabaseTLSConfig
}

Connection facilities connecting to a YugabyteDB instance.

func NewConnection

func NewConnection(endpoints ...*connection.Endpoint) *Connection

NewConnection returns a connection parameters with the specified host:port, and the default values for the other parameters.

func PrepareTestEnv

func PrepareTestEnv(t *testing.T) *Connection

PrepareTestEnv initializes a test environment for an existing or uncontrollable db instance.

func PrepareTestEnvWithConnection

func PrepareTestEnvWithConnection(t *testing.T, conn *Connection) *Connection

PrepareTestEnvWithConnection initializes a test environment given a db connection.

type DatabaseContainer

type DatabaseContainer struct {
	Name         string
	Image        string
	HostIP       string
	Network      string
	Hostname     string
	DatabaseType string
	Tag          string
	Role         string
	Cmd          []string
	Entrypoint   []string
	Env          []string
	Binds        []string
	HostPort     int
	DbPort       docker.Port
	PortMap      docker.Port
	PortBinds    map[docker.Port][]docker.PortBinding
	NetToIP      map[string]*docker.EndpointConfig
	AutoRm       bool
	// TLSConfig holds the node TLS certificates.
	// If TLSConfig isn't available (is nil), we fallback to insecure mode.
	TLSConfig *connection.TLSConfig
	// contains filtered or unexported fields
}

DatabaseContainer manages the execution of an instance of a dockerized DB for tests.

func (*DatabaseContainer) ContainerID

func (dc *DatabaseContainer) ContainerID() string

ContainerID returns the container ID.

func (*DatabaseContainer) EnsureNodeReadinessByLogs

func (dc *DatabaseContainer) EnsureNodeReadinessByLogs(t *testing.T, requiredOutput string)

EnsureNodeReadinessByLogs checks the container's readiness by monitoring its logs and ensure its running correctly.

func (*DatabaseContainer) ExecuteCommand

func (dc *DatabaseContainer) ExecuteCommand(t *testing.T, cmd []string) string

ExecuteCommand executes a command and returns the container output.

func (*DatabaseContainer) ExposePort

func (dc *DatabaseContainer) ExposePort(port string)

ExposePort adds a host port mapping for the given container port (e.g. "5433") using a pre-allocated ephemeral host port.

func (*DatabaseContainer) GetConnectionOptions

func (dc *DatabaseContainer) GetConnectionOptions(ctx context.Context, t *testing.T) *Connection

GetConnectionOptions inspect the container and fetches the available connection options.

func (*DatabaseContainer) GetContainerConnectionDetails

func (dc *DatabaseContainer) GetContainerConnectionDetails(
	t *testing.T,
) *connection.Endpoint

GetContainerConnectionDetails inspect the container and fetches its connection to an endpoint.

func (*DatabaseContainer) GetContainerLogs

func (dc *DatabaseContainer) GetContainerLogs(t *testing.T) string

GetContainerLogs return the output of the DatabaseContainer.

func (*DatabaseContainer) GetHostMappedEndpoint

func (dc *DatabaseContainer) GetHostMappedEndpoint(t *testing.T) *connection.Endpoint

GetHostMappedEndpoint inspects the container and returns a host-accessible endpoint using the auto-assigned host port mapping.

func (*DatabaseContainer) ReadPasswordFromContainer

func (dc *DatabaseContainer) ReadPasswordFromContainer(t *testing.T, filePath string) string

ReadPasswordFromContainer extracts the randomly generated password from a file inside the container. This is required because YugabyteDB, when running in secure mode, doesn't allow default passwords and instead generates a random one at startup. This method being called only when a secured Yugabyted node is started. If the file doesn’t exist or doesn't contain a password, the test should fail.

func (*DatabaseContainer) StartContainer

func (dc *DatabaseContainer) StartContainer(ctx context.Context, t *testing.T)

StartContainer runs a DB container, if no specific container details provided, default values will be set.

func (*DatabaseContainer) StopAndRemoveContainer

func (dc *DatabaseContainer) StopAndRemoveContainer(t *testing.T)

StopAndRemoveContainer stops and removes the db container from the docker engine.

func (*DatabaseContainer) StopContainer

func (dc *DatabaseContainer) StopContainer(t *testing.T)

StopContainer stops db container.

Jump to

Keyboard shortcuts

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