GithubHelp home page GithubHelp logo

nhatthm / testcontainers-go-extra Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 229 KB

Boost testcontainers/testcontainers-go with some jet fuel! πŸš€

License: MIT License

Makefile 2.79% Go 97.21%
go golang testcontainers testcontainers-go test docker container

testcontainers-go-extra's Introduction

Testcontainers-Go Booster

GitHub Releases Build Status codecov Go Report Card GoDevDoc Donate

Boost testcontainers/testcontainers-go with some jet fuel! πŸš€

Prerequisites

  • Go >= 1.20

Install

go get go.nhat.io/testcontainers-extra

Callbacks

After successfully starting a container, you may want to do some extra operations to make it ready for your purposes. Then you could use the callbacks.

For example:

package example

import (
	"context"
	"time"

	"go.nhat.io/testcontainers-extra"
	"go.nhat.io/testcontainers-extra/wait"
)

func startPostgres(dbName, dbUser, dbPassword string) error {
	_, err := testcontainers.StartGenericContainer(context.Background(), testcontainers.ContainerRequest{
		Name:         "postgres",
		Image:        "postgres:12-alpine",
		ExposedPorts: []string{":5432"},
		Env: map[string]string{
			"LC_ALL":            "C.UTF-8",
			"POSTGRES_DB":       dbName,
			"POSTGRES_USER":     dbUser,
			"POSTGRES_PASSWORD": dbPassword,
		},
		WaitingFor: wait.ForHealthCheckCmd("pg_isready").
			WithRetries(3).
			WithStartPeriod(30 * time.Second).
			WithTestTimeout(5 * time.Second).
			WithTestInterval(10 * time.Second),
	}, testcontainers.WithCallback(func(ctx context.Context, c testcontainers.Container, r testcontainers.ContainerRequest) error {
		// Do your stuff here, for example, migration.

		return nil
	}))

	return err
}

Populating Host and Ports Envs

testcontainers.PopulateHostPortEnv is a callback that set the environment variables for the exposed ports.

For example:

package example

import (
	"context"
	"time"

	"go.nhat.io/testcontainers-extra"
	"go.nhat.io/testcontainers-extra/wait"
)

func startPostgres(dbName, dbUser, dbPassword string) error {
	_, err := testcontainers.StartGenericContainer(context.Background(), testcontainers.ContainerRequest{
		Name:         "postgres",
		Image:        "postgres:12-alpine",
		ExposedPorts: []string{":5432"},
		Env: map[string]string{
			"LC_ALL":            "C.UTF-8",
			"POSTGRES_DB":       dbName,
			"POSTGRES_USER":     dbUser,
			"POSTGRES_PASSWORD": dbPassword,
		},
		WaitingFor: wait.ForHealthCheckCmd("pg_isready").
			WithRetries(3).
			WithStartPeriod(30 * time.Second).
			WithTestTimeout(5 * time.Second).
			WithTestInterval(10 * time.Second),
	}, testcontainers.PopulateHostPortEnv)

	return err
}

After calling startPostgres(), there will be 2 variables POSTGRES_5432_HOST and POSTGRES_5432_PORT. The POSTGRES is from the container name in the request, 5432 is the exposed port. The values are

  • POSTGRES_5432_HOST: the hostname of the docker daemon where the container port is exposed.
  • POSTGRES_5432_PORT: the port that mapped to the exposed container port.

Wait Strategies

Health Check

The health check provides the same behavior as docker-compose with the configuration:

  • Start Period: Retry is only counted when time passes the start period. This is helpful for some containers that need time to get ready. The default value is 0.
  • Test Timeout: Timeout for executing the test.
  • Test Internal: If the container is unhealthy, the health check will wait for an amount of time before testing again.
  • Retries: The number of retries to test the container after start period ends.

hccmd

For example:

package example

import (
	"context"
	"time"

	"go.nhat.io/testcontainers-extra"
	"go.nhat.io/testcontainers-extra/wait"
)

func startPostgres(dbName, dbUser, dbPassword string) error {
	_, err := testcontainers.StartGenericContainer(context.Background(), testcontainers.ContainerRequest{
		Name:         "postgres",
		Image:        "postgres:12-alpine",
		ExposedPorts: []string{":5432"},
		Env: map[string]string{
			"LC_ALL":            "C.UTF-8",
			"POSTGRES_DB":       dbName,
			"POSTGRES_USER":     dbUser,
			"POSTGRES_PASSWORD": dbPassword,
		},
		WaitingFor: wait.ForHealthCheckCmd("pg_isready").
			WithRetries(3).
			WithStartPeriod(30 * time.Second).
			WithTestTimeout(5 * time.Second).
			WithTestInterval(10 * time.Second),
	})

	return err
}

Donation

If this project help you reduce time to develop, you can give me a cup of coffee :)

Paypal donation

paypal

Β Β Β Β Β Β Β or scan this

testcontainers-go-extra's People

Contributors

dependabot[bot] avatar nhatthm avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.