mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-12-06 16:22:15 +00:00
test(integration): reuse default containers across scenarios, one scenario at a time
This commit is contained in:
@@ -37,6 +37,7 @@ Available tags:
|
||||
3. For new tags, add them to both the `TAGS` comment block in the `Makefile` and the table above.
|
||||
4. For new step definitions, add the function to `scenario/scenario.go` (or `scenario/pdf.go` for PDF assertions), register it in `InitializeScenario`, and add the step pattern to the step reference below.
|
||||
5. Test data goes in `test/integration/testdata/`.
|
||||
6. `I have a default Gotenberg container` reuses a container an earlier scenario left, one scenario at a time. A scenario that reads the container's logs, stops it, or sends concurrent or sequential requests gets its own. Tag a scenario `@isolated` to give it its own container for another reason. A container with environment variables is never reused.
|
||||
|
||||
## Step reference
|
||||
|
||||
|
||||
@@ -88,6 +88,12 @@ func TestMain(m *testing.M) {
|
||||
},
|
||||
}.Run()
|
||||
|
||||
// Pooled containers live across scenarios, not across runs: a retry
|
||||
// starts from fresh ones.
|
||||
if err := scenario.TerminateDefaultContainers(context.Background()); err != nil {
|
||||
fmt.Fprintf(colors.Colored(os.Stdout), "warning: %v\n", err)
|
||||
}
|
||||
|
||||
if code == 0 {
|
||||
if len(flaky) > 0 {
|
||||
fmt.Fprintf(colors.Colored(os.Stdout), "\n--- %d flaky scenario(s), passed on a retry ---\n%s\n", len(flaky), strings.Join(flaky, "\n"))
|
||||
|
||||
111
test/integration/scenario/pool.go
Normal file
111
test/integration/scenario/pool.go
Normal file
@@ -0,0 +1,111 @@
|
||||
package scenario
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/cucumber/godog"
|
||||
"github.com/testcontainers/testcontainers-go"
|
||||
)
|
||||
|
||||
// defaultContainers pools the containers "I have a default Gotenberg
|
||||
// container" starts, so that a scenario reuses one an earlier scenario left
|
||||
// instead of paying for a start. A pooled container serves one scenario at a
|
||||
// time: concurrent scenarios never share one.
|
||||
var defaultContainers struct {
|
||||
mu sync.Mutex
|
||||
idle []testcontainers.Container
|
||||
all []testcontainers.Container
|
||||
}
|
||||
|
||||
// acquireDefaultContainer returns an idle pooled container, or starts one.
|
||||
func acquireDefaultContainer(ctx context.Context) (testcontainers.Container, error) {
|
||||
defaultContainers.mu.Lock()
|
||||
if n := len(defaultContainers.idle); n > 0 {
|
||||
c := defaultContainers.idle[n-1]
|
||||
defaultContainers.idle = defaultContainers.idle[:n-1]
|
||||
defaultContainers.mu.Unlock()
|
||||
return c, nil
|
||||
}
|
||||
defaultContainers.mu.Unlock()
|
||||
|
||||
c, err := startGotenbergContainer(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defaultContainers.mu.Lock()
|
||||
defer defaultContainers.mu.Unlock()
|
||||
defaultContainers.all = append(defaultContainers.all, c)
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// releaseDefaultContainer hands a pooled container back for another scenario.
|
||||
func releaseDefaultContainer(c testcontainers.Container) {
|
||||
defaultContainers.mu.Lock()
|
||||
defer defaultContainers.mu.Unlock()
|
||||
defaultContainers.idle = append(defaultContainers.idle, c)
|
||||
}
|
||||
|
||||
// discardDefaultContainer terminates a pooled container that must not serve
|
||||
// another scenario.
|
||||
func discardDefaultContainer(ctx context.Context, c testcontainers.Container) error {
|
||||
defaultContainers.mu.Lock()
|
||||
defaultContainers.all = slices.DeleteFunc(defaultContainers.all, func(o testcontainers.Container) bool { return o == c })
|
||||
defaultContainers.mu.Unlock()
|
||||
|
||||
return c.Terminate(ctx, testcontainers.StopTimeout(0))
|
||||
}
|
||||
|
||||
// TerminateDefaultContainers terminates every pooled container. Call it once a
|
||||
// run of the suite ends.
|
||||
func TerminateDefaultContainers(ctx context.Context) error {
|
||||
defaultContainers.mu.Lock()
|
||||
all := defaultContainers.all
|
||||
defaultContainers.all, defaultContainers.idle = nil, nil
|
||||
defaultContainers.mu.Unlock()
|
||||
|
||||
var errs []error
|
||||
for _, c := range all {
|
||||
err := c.Terminate(ctx, testcontainers.StopTimeout(0))
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("terminate pooled Gotenberg container: %w", err))
|
||||
}
|
||||
}
|
||||
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
// isolatedSteps are the step texts whose assertions depend on a container no
|
||||
// other scenario used: they read its logs, stop it, or load it on purpose.
|
||||
var isolatedSteps = []string{
|
||||
"log the following entries",
|
||||
"I stop the Gotenberg container",
|
||||
"concurrent",
|
||||
"sequential",
|
||||
}
|
||||
|
||||
// needsOwnContainer reports whether sc must not reuse, nor hand back, a pooled
|
||||
// container: it is tagged @isolated or has one of [isolatedSteps].
|
||||
func needsOwnContainer(sc *godog.Scenario) bool {
|
||||
for _, tag := range sc.Tags {
|
||||
if tag.Name == "@isolated" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
for _, step := range sc.Steps {
|
||||
for _, text := range isolatedSteps {
|
||||
if strings.Contains(step.Text, text) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
@@ -19,8 +19,12 @@ type scenario struct {
|
||||
workdir string
|
||||
teststoreDir string
|
||||
gotenbergContainer testcontainers.Container
|
||||
server *server
|
||||
hostPort int
|
||||
// ownContainer forbids the scenario a pooled default container, and
|
||||
// pooledContainer tells that its container came from the pool.
|
||||
ownContainer bool
|
||||
pooledContainer bool
|
||||
server *server
|
||||
hostPort int
|
||||
}
|
||||
|
||||
func (s *scenario) reset(ctx context.Context) error {
|
||||
@@ -57,6 +61,7 @@ func InitializeScenario(ctx *godog.ScenarioContext) {
|
||||
return ctx, fmt.Errorf("create working directory: %w", err)
|
||||
}
|
||||
s.workdir = workdir
|
||||
s.ownContainer = needsOwnContainer(sc)
|
||||
return ctx, nil
|
||||
})
|
||||
ctx.Given(`^I have a default Gotenberg container$`, s.iHaveADefaultGotenbergContainer)
|
||||
@@ -111,11 +116,27 @@ func InitializeScenario(ctx *godog.ScenarioContext) {
|
||||
ctx.Then(`^the "([^"]*)" image pixel at (\d+),(\d+) should be "([^"]*)"$`, s.theImagePixelShouldBe)
|
||||
ctx.Then(`^the "([^"]*)" file size should be (less|greater) than (\d+) KB$`, s.theFileSizeShouldBe)
|
||||
ctx.After(func(ctx context.Context, sc *godog.Scenario, err error) (context.Context, error) {
|
||||
if s.gotenbergContainer != nil {
|
||||
errTerminate := s.gotenbergContainer.Terminate(ctx, testcontainers.StopTimeout(0))
|
||||
if errTerminate != nil {
|
||||
return ctx, fmt.Errorf("terminate Gotenberg container: %w", errTerminate)
|
||||
if s.gotenbergContainer == nil {
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
if s.pooledContainer {
|
||||
// A failed scenario may leave its container in a bad state.
|
||||
if err == nil {
|
||||
releaseDefaultContainer(s.gotenbergContainer)
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
errDiscard := discardDefaultContainer(ctx, s.gotenbergContainer)
|
||||
if errDiscard != nil {
|
||||
return ctx, fmt.Errorf("terminate pooled Gotenberg container: %w", errDiscard)
|
||||
}
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
errTerminate := s.gotenbergContainer.Terminate(ctx, testcontainers.StopTimeout(0))
|
||||
if errTerminate != nil {
|
||||
return ctx, fmt.Errorf("terminate Gotenberg container: %w", errTerminate)
|
||||
}
|
||||
return ctx, nil
|
||||
})
|
||||
|
||||
@@ -15,11 +15,21 @@ import (
|
||||
)
|
||||
|
||||
func (s *scenario) iHaveADefaultGotenbergContainer(ctx context.Context) error {
|
||||
c, err := startGotenbergContainer(ctx, nil)
|
||||
if s.ownContainer {
|
||||
c, err := startGotenbergContainer(ctx, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create Gotenberg container: %s", err)
|
||||
}
|
||||
s.gotenbergContainer = c
|
||||
return nil
|
||||
}
|
||||
|
||||
c, err := acquireDefaultContainer(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create Gotenberg container: %s", err)
|
||||
}
|
||||
s.gotenbergContainer = c
|
||||
s.pooledContainer = true
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user