https://github.com/kndndrj/go-contessa
Library for easier testing with containers.
https://github.com/kndndrj/go-contessa
Last synced: 8 months ago
JSON representation
Library for easier testing with containers.
- Host: GitHub
- URL: https://github.com/kndndrj/go-contessa
- Owner: kndndrj
- License: apache-2.0
- Created: 2023-12-30T08:27:43.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2023-12-30T09:10:42.000Z (almost 2 years ago)
- Last Synced: 2025-01-08T03:09:57.737Z (9 months ago)
- Language: Go
- Size: 14.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go Contessa
[](https://pkg.go.dev/github.com/kndndrj/go-contessa)
Library for easier testing with containers.
Originally developed for testing [nvim-dbee](https://github.com/kndndrj/nvim-dbee).Only basic functionality for now - adding fetures if needed.
## Quick Start
```go
package example_testimport (
"database/sql"
"fmt"
"testing"
"time""github.com/stretchr/testify/require"
"github.com/kndndrj/go-contessa"
)func TestExample(t *testing.T) {
r := require.New(t)
cm, err := contessa.New(contessa.WithTestingLogger(t))
r.NoError(err)// get a free port from os
port := contessa.GetFreePortOr(5177)// create a new postgres container
ready, err := cm.StartContainer("postgres:15",
// port binding
contessa.WithPortBinding(port, 5432),// env vars
contessa.WithEnvVariable("POSTGRES_USER", "postgres"),
contessa.WithEnvVariable("POSTGRES_PASSWORD", "postgres"),
contessa.WithEnvVariable("POSTGRES_DB", "postgres"),// startup probe and delay
contessa.WithStartupProbe("pg_isready"),
contessa.WithStartupProbeSkew(2*time.Second),
contessa.WithStartupDelay(10*time.Second),
)
r.NoError(err)// Wait for the container to be set-up
<-ready// connect to postgres container
dsn := fmt.Sprintf("postgres://postgres:postgres@localhost:%d/postgres?sslmode=disable", port)
db, err := sql.Open("pgx", dsn)
r.NoError(err)
defer db.Close()// do whatever you need with "db"...
}
```