210 lines
7.4 KiB
Go
210 lines
7.4 KiB
Go
//go:build integration
|
|
|
|
package integration_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/testcontainers/testcontainers-go"
|
|
)
|
|
|
|
const rootlessPortBindFailureMessage = "Error response from daemon: failed to set up container networking: driver failed programming external connectivity on endpoint lincodex (0123456789abcdef): error while calling RootlessKit PortManager.AddPort(): listen tcp4 0.0.0.0:32912: bind: address already in use"
|
|
|
|
type retryTestContainer struct {
|
|
testcontainers.Container
|
|
terminateCalls int
|
|
terminateErr error
|
|
}
|
|
|
|
func (container *retryTestContainer) Terminate(context.Context, ...testcontainers.TerminateOption) error {
|
|
container.terminateCalls++
|
|
return container.terminateErr
|
|
}
|
|
|
|
func TestIsRootlessPortBindFailure(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
err error
|
|
want bool
|
|
}{
|
|
{name: "rootless host port collision", err: errors.New(rootlessPortBindFailureMessage), want: true},
|
|
{name: "unrelated address collision", err: errors.New("listen tcp4 0.0.0.0:32912: bind: address already in use")},
|
|
{name: "rootless service bind failure", err: errors.New("Error response from daemon: failed to set up container networking: driver failed programming external connectivity on endpoint lincodex (0123456789abcdef): error while calling RootlessKit PortManager.AddPort(): listen tcp4 0.0.0.0:32912: bind: permission denied")},
|
|
{name: "rootless IPv6 bind failure", err: errors.New("Error response from daemon: failed to set up container networking: driver failed programming external connectivity on endpoint lincodex (0123456789abcdef): error while calling RootlessKit PortManager.AddPort(): listen tcp6 [::]:32912: bind: address already in use")},
|
|
{name: "nil error", err: nil},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
if got := isRootlessPortBindFailure(test.err); got != test.want {
|
|
t.Fatalf("isRootlessPortBindFailure(%v) = %t, want %t", test.err, got, test.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestStartWithRootlessPortRetryCleansPartialContainer(t *testing.T) {
|
|
first := &retryTestContainer{}
|
|
second := &retryTestContainer{}
|
|
attempts := 0
|
|
container, err := startWithRootlessPortRetry(
|
|
context.Background(),
|
|
func(context.Context) (testcontainers.Container, error) {
|
|
attempts++
|
|
if attempts == 1 {
|
|
return first, errors.New(rootlessPortBindFailureMessage)
|
|
}
|
|
return second, nil
|
|
},
|
|
func(ctx context.Context, container testcontainers.Container) error {
|
|
return container.Terminate(ctx)
|
|
},
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("startWithRootlessPortRetry() error = %v", err)
|
|
}
|
|
if container != second {
|
|
t.Fatalf("startWithRootlessPortRetry() container = %v, want second container", container)
|
|
}
|
|
if attempts != 2 {
|
|
t.Fatalf("start attempts = %d, want 2", attempts)
|
|
}
|
|
if first.terminateCalls != 1 {
|
|
t.Fatalf("first container cleanup calls = %d, want 1", first.terminateCalls)
|
|
}
|
|
if second.terminateCalls != 0 {
|
|
t.Fatalf("successful container cleanup calls = %d, want 0", second.terminateCalls)
|
|
}
|
|
}
|
|
|
|
func TestStartWithRootlessPortRetryStopsAtLimit(t *testing.T) {
|
|
startErr := errors.New(rootlessPortBindFailureMessage)
|
|
attempts := 0
|
|
containers := make([]*retryTestContainer, 0, rootlessPortStartLimit)
|
|
container, err := startWithRootlessPortRetry(
|
|
context.Background(),
|
|
func(context.Context) (testcontainers.Container, error) {
|
|
attempts++
|
|
partial := &retryTestContainer{}
|
|
containers = append(containers, partial)
|
|
return partial, startErr
|
|
},
|
|
func(ctx context.Context, container testcontainers.Container) error {
|
|
return container.Terminate(ctx)
|
|
},
|
|
)
|
|
if err == nil {
|
|
t.Fatal("startWithRootlessPortRetry() error = nil, want attempt limit error")
|
|
}
|
|
if !errors.Is(err, startErr) {
|
|
t.Fatalf("startWithRootlessPortRetry() error = %v, want startup cause", err)
|
|
}
|
|
if container != containers[len(containers)-1] {
|
|
t.Fatalf("startWithRootlessPortRetry() container = %v, want final partial container", container)
|
|
}
|
|
if attempts != rootlessPortStartLimit {
|
|
t.Fatalf("start attempts = %d, want %d", attempts, rootlessPortStartLimit)
|
|
}
|
|
for index, partial := range containers[:len(containers)-1] {
|
|
if partial.terminateCalls != 1 {
|
|
t.Fatalf("retried container %d cleanup calls = %d, want 1", index, partial.terminateCalls)
|
|
}
|
|
}
|
|
final := containers[len(containers)-1]
|
|
if final.terminateCalls != 0 {
|
|
t.Fatalf("final container cleanup calls = %d, want caller-owned cleanup", final.terminateCalls)
|
|
}
|
|
if err := final.Terminate(context.Background()); err != nil {
|
|
t.Fatalf("caller cleanup of final container: %v", err)
|
|
}
|
|
if final.terminateCalls != 1 {
|
|
t.Fatalf("final container cleanup calls after caller cleanup = %d, want 1", final.terminateCalls)
|
|
}
|
|
}
|
|
|
|
func TestStartWithRootlessPortRetryPreservesCleanupFailure(t *testing.T) {
|
|
startErr := errors.New(rootlessPortBindFailureMessage)
|
|
cleanupErr := errors.New("container remove failed")
|
|
partial := &retryTestContainer{}
|
|
attempts := 0
|
|
container, err := startWithRootlessPortRetry(
|
|
context.Background(),
|
|
func(context.Context) (testcontainers.Container, error) {
|
|
attempts++
|
|
return partial, startErr
|
|
},
|
|
func(context.Context, testcontainers.Container) error {
|
|
return cleanupErr
|
|
},
|
|
)
|
|
if !errors.Is(err, startErr) || !errors.Is(err, cleanupErr) {
|
|
t.Fatalf("startWithRootlessPortRetry() error = %v, want start and cleanup causes", err)
|
|
}
|
|
if container != partial {
|
|
t.Fatalf("startWithRootlessPortRetry() container = %v, want partial container for final cleanup", container)
|
|
}
|
|
if attempts != 1 {
|
|
t.Fatalf("start attempts = %d, want 1 after cleanup failure", attempts)
|
|
}
|
|
}
|
|
|
|
func TestStartWithRootlessPortRetryDoesNotRetryStartupFailure(t *testing.T) {
|
|
startErr := errors.New("service readiness failed")
|
|
partial := &retryTestContainer{}
|
|
attempts := 0
|
|
container, err := startWithRootlessPortRetry(
|
|
context.Background(),
|
|
func(context.Context) (testcontainers.Container, error) {
|
|
attempts++
|
|
return partial, startErr
|
|
},
|
|
func(ctx context.Context, container testcontainers.Container) error {
|
|
return container.Terminate(ctx)
|
|
},
|
|
)
|
|
if !errors.Is(err, startErr) {
|
|
t.Fatalf("startWithRootlessPortRetry() error = %v, want startup error", err)
|
|
}
|
|
if container != partial {
|
|
t.Fatalf("startWithRootlessPortRetry() container = %v, want partial container for caller cleanup", container)
|
|
}
|
|
if attempts != 1 {
|
|
t.Fatalf("start attempts = %d, want 1", attempts)
|
|
}
|
|
if partial.terminateCalls != 0 {
|
|
t.Fatalf("partial container cleanup calls = %d, want caller-owned cleanup", partial.terminateCalls)
|
|
}
|
|
}
|
|
|
|
func TestStartWithRootlessPortRetryStopsWhenContextIsCanceled(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
startErr := errors.New(rootlessPortBindFailureMessage)
|
|
partial := &retryTestContainer{}
|
|
attempts := 0
|
|
container, err := startWithRootlessPortRetry(
|
|
ctx,
|
|
func(context.Context) (testcontainers.Container, error) {
|
|
attempts++
|
|
cancel()
|
|
return partial, startErr
|
|
},
|
|
func(ctx context.Context, container testcontainers.Container) error {
|
|
return container.Terminate(ctx)
|
|
},
|
|
)
|
|
if !errors.Is(err, context.Canceled) || !errors.Is(err, startErr) {
|
|
t.Fatalf("startWithRootlessPortRetry() error = %v, want cancellation and startup cause", err)
|
|
}
|
|
if container != nil {
|
|
t.Fatalf("startWithRootlessPortRetry() container = %v, want nil after cleanup", container)
|
|
}
|
|
if attempts != 1 {
|
|
t.Fatalf("start attempts = %d, want 1", attempts)
|
|
}
|
|
if partial.terminateCalls != 1 {
|
|
t.Fatalf("partial container cleanup calls = %d, want 1", partial.terminateCalls)
|
|
}
|
|
}
|