mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-03-17 20:47:50 +02:00
Maybe 3rd time is the charm! This makes the CI build run on windows too, and fix broken tests/featuers on Windows. Most of the changes are related to ignoring certain tests on windows, or making sure to use the right path separators. More work to do in the future, probably! #4293 --------- Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
82 lines
1.7 KiB
Go
82 lines
1.7 KiB
Go
package testlib
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/ory/dockertest/v3"
|
|
"github.com/ory/dockertest/v3/docker"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
var (
|
|
dockerPoolOnce sync.Once
|
|
dockerPool *dockertest.Pool
|
|
)
|
|
|
|
type skipper func(args ...any)
|
|
|
|
func (s skipper) Fatal(...any) {
|
|
s("docker is not available")
|
|
}
|
|
|
|
func CheckDocker(tb testing.TB) {
|
|
tb.Helper()
|
|
MustDockerPool(skipper(tb.Skip))
|
|
}
|
|
|
|
// MustDockerPool gets a single dockertet.Pool.
|
|
func MustDockerPool(f Fataler) *dockertest.Pool {
|
|
dockerPoolOnce.Do(func() {
|
|
pool, err := dockertest.NewPool("")
|
|
if err != nil {
|
|
f.Fatal(err)
|
|
}
|
|
if err := pool.Client.Ping(); err != nil {
|
|
f.Fatal(err)
|
|
}
|
|
dockerPool = pool
|
|
})
|
|
return dockerPool
|
|
}
|
|
|
|
// MustKillContainer kills the given container by name if it exists in the
|
|
// current dockertest.Pool.
|
|
func MustKillContainer(f Fataler, name string) {
|
|
pool := MustDockerPool(f)
|
|
if trash, ok := pool.ContainerByName(name); ok {
|
|
if err := pool.Purge(trash); err != nil {
|
|
f.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fataler interface, can be a log.Default() or testing.TB, for example.
|
|
type Fataler interface {
|
|
Fatal(args ...any)
|
|
}
|
|
|
|
// StartRegistry starts a registry with the given name in the given port, and
|
|
// sets up its deletion on test.Cleanup.
|
|
func StartRegistry(tb testing.TB, name, port string) {
|
|
tb.Helper()
|
|
|
|
pool := MustDockerPool(tb)
|
|
MustKillContainer(tb, name)
|
|
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
|
|
Name: name,
|
|
Repository: "registry",
|
|
Tag: "2",
|
|
PortBindings: map[docker.Port][]docker.PortBinding{
|
|
docker.Port("5000/tcp"): {{HostPort: port}},
|
|
},
|
|
}, func(hc *docker.HostConfig) {
|
|
hc.AutoRemove = true
|
|
})
|
|
require.NoError(tb, err)
|
|
|
|
tb.Cleanup(func() {
|
|
require.NoError(tb, pool.Purge(resource))
|
|
})
|
|
}
|