test(container): fix broken restart test
This test started failing for mysterious reasons; it can be improved by testing our more recent hand-rolled restart logic instead of the previous Docker integration.
This commit is contained in:
parent
3d5f438ff9
commit
59e7c757d8
@ -4,7 +4,7 @@ package container_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -167,7 +167,9 @@ func TestIntegrationClientRemoveContainers(t *testing.T) {
|
|||||||
assert.NoError(t, <-err3C)
|
assert.NoError(t, <-err3C)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContainerRestart(t *testing.T) {
|
func TestIntegrationContainerRestart(t *testing.T) {
|
||||||
|
const wantRestartCount = 3
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
t.Cleanup(cancel)
|
t.Cleanup(cancel)
|
||||||
|
|
||||||
@ -185,41 +187,45 @@ func TestContainerRestart(t *testing.T) {
|
|||||||
Name: containerName,
|
Name: containerName,
|
||||||
ChanSize: 1,
|
ChanSize: 1,
|
||||||
ContainerConfig: &typescontainer.Config{
|
ContainerConfig: &typescontainer.Config{
|
||||||
Image: "alpine:latest",
|
Image: "alpine:3.18",
|
||||||
Cmd: []string{"sleep", "1"},
|
Entrypoint: []string{"sleep", "1"},
|
||||||
Labels: map[string]string{container.LabelComponent: component},
|
Cmd: []string{"1"}, // 1 second
|
||||||
|
Labels: map[string]string{container.LabelComponent: component},
|
||||||
},
|
},
|
||||||
HostConfig: &typescontainer.HostConfig{
|
HostConfig: &typescontainer.HostConfig{
|
||||||
NetworkMode: "default",
|
NetworkMode: "default",
|
||||||
RestartPolicy: typescontainer.RestartPolicy{Name: "always"},
|
|
||||||
},
|
},
|
||||||
|
ShouldRestart: func(_ int64, restartCount int, _ [][]byte, _ time.Duration) (bool, error) {
|
||||||
|
return restartCount < wantRestartCount, nil
|
||||||
|
},
|
||||||
|
RestartInterval: 1 * time.Second,
|
||||||
})
|
})
|
||||||
testhelpers.ChanRequireNoError(t, errC)
|
testhelpers.ChanRequireNoError(t, errC)
|
||||||
|
|
||||||
containerState := <-containerStateC
|
outer:
|
||||||
assert.Equal(t, "pulling", containerState.Status)
|
for {
|
||||||
containerState = <-containerStateC
|
select {
|
||||||
assert.Equal(t, "created", containerState.Status)
|
case containerState := <-containerStateC:
|
||||||
containerState = <-containerStateC
|
if containerState.Status == "running" {
|
||||||
assert.Equal(t, "running", containerState.Status)
|
break outer
|
||||||
|
}
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
require.Fail(t, "timeout waiting for container")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err = nil // reset error
|
err = nil // reset error
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
defer close(done)
|
defer close(done)
|
||||||
|
|
||||||
var count int
|
|
||||||
for {
|
for {
|
||||||
containerState = <-containerStateC
|
containerState := <-containerStateC
|
||||||
if containerState.Status == domain.ContainerStatusRestarting {
|
if containerState.Status == domain.ContainerStatusExited {
|
||||||
|
if containerState.RestartCount != wantRestartCount {
|
||||||
|
err = fmt.Errorf("expected %d restarts, got %d", wantRestartCount, containerState.RestartCount)
|
||||||
|
}
|
||||||
break
|
break
|
||||||
} else if containerState.Status == domain.ContainerStatusExited {
|
|
||||||
err = errors.New("container exited unexpectedly")
|
|
||||||
} else if count >= 5 {
|
|
||||||
err = errors.New("container did not enter restarting state")
|
|
||||||
} else {
|
|
||||||
// wait for a few state changes
|
|
||||||
count++
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user