-
-
Notifications
You must be signed in to change notification settings - Fork 435
/
testing.go
43 lines (37 loc) · 1.3 KB
/
testing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package testcontainers
import (
"context"
"testing"
)
// SkipIfProviderIsNotHealthWithContext is a context configurable varient of
// SkipIfProviderIsNotHealthy
func SkipIfProviderIsNotHealthyWithContext(tb testing.TB, ctx context.Context) {
provider, err := ProviderDocker.GetProvider()
if err != nil {
tb.Skipf("Docker is not running. TestContainers can't perform this work without it: %s", err)
}
err = provider.Health(ctx)
if err != nil {
tb.Skipf("Docker is not running. TestContainers can't perform this work without it: %s", err)
}
}
// SkipIfProviderIsNotHealthy is a utility function capable of skipping tests
// if the provider is not healthy, or running at all.
// This is a function designed to be used in your test, when Docker is not mandatory for CI/CD.
// In this way tests that depend on Testcontainers won't run if the provider is provisioned correctly.
func SkipIfProviderIsNotHealthy(tb testing.TB) {
SkipIfProviderIsNotHealthyWithContext(tb, context.Background())
}
// CleanupContainer terminates the passed container after testing.TB has
// ended.
func CleanupContainer(tb testing.TB, ctx context.Context, ctr Container) {
tb.Helper()
if ctr == nil {
return
}
tb.Cleanup(func() {
if err := ctr.Terminate(ctx); err != nil {
tb.Logf("failed to terminate container: %s", err)
}
})
}