forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhealth_test.go
More file actions
128 lines (102 loc) · 3.9 KB
/
Copy pathhealth_test.go
File metadata and controls
128 lines (102 loc) · 3.9 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package wait_test
import (
"context"
"testing"
"time"
"github.com/moby/moby/api/types/container"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go/wait"
)
// newStateTarget creates a new mockStrategyTarget whose State method always returns the given state.
func newStateTarget(t *testing.T, state *container.State) *mockStrategyTarget {
t.Helper()
target := newMockStrategyTarget(t)
target.EXPECT().State(anyContext).Return(state, nil)
return target
}
// TestWaitForHealthTimesOutForUnhealthy confirms that an unhealthy container will eventually
// time out.
func TestWaitForHealthTimesOutForUnhealthy(t *testing.T) {
target := newStateTarget(t, &container.State{
Running: true,
Health: &container.Health{Status: container.Unhealthy},
})
wg := wait.NewHealthStrategy().WithStartupTimeout(100 * time.Millisecond)
err := wg.WaitUntilReady(context.Background(), target)
require.Error(t, err)
require.ErrorIs(t, err, context.DeadlineExceeded)
}
// TestWaitForHealthSucceeds ensures that a healthy container always succeeds.
func TestWaitForHealthSucceeds(t *testing.T) {
target := newStateTarget(t, &container.State{
Running: true,
Health: &container.Health{Status: container.Healthy},
})
wg := wait.NewHealthStrategy().WithStartupTimeout(100 * time.Millisecond)
err := wg.WaitUntilReady(context.Background(), target)
require.NoError(t, err)
}
// TestWaitForHealthWithNil checks that an initial `nil` Health will not cause a panic,
// and if the container eventually becomes healthy, the HealthStrategy will succeed.
func TestWaitForHealthWithNil(t *testing.T) {
target := newMockStrategyTarget(t)
startTime := time.Now()
target.EXPECT().State(anyContext).RunAndReturn(func(_ context.Context) (*container.State, error) {
if time.Since(startTime) >= 200*time.Millisecond {
return &container.State{Running: true, Health: &container.Health{Status: container.Healthy}}, nil
}
return &container.State{Running: true, Health: nil}, nil
})
wg := wait.NewHealthStrategy().
WithStartupTimeout(500 * time.Millisecond).
WithPollInterval(100 * time.Millisecond)
err := wg.WaitUntilReady(context.Background(), target)
require.NoError(t, err)
}
// TestWaitFailsForNilHealth checks that Health always nil fails (but will NOT cause a panic)
func TestWaitFailsForNilHealth(t *testing.T) {
target := newStateTarget(t, &container.State{
Running: true,
Health: nil,
})
wg := wait.NewHealthStrategy().
WithStartupTimeout(500 * time.Millisecond).
WithPollInterval(100 * time.Millisecond)
err := wg.WaitUntilReady(context.Background(), target)
require.Error(t, err)
require.ErrorIs(t, err, context.DeadlineExceeded)
}
func TestWaitForHealthFailsDueToOOMKilledContainer(t *testing.T) {
target := newStateTarget(t, &container.State{
OOMKilled: true,
})
wg := wait.NewHealthStrategy().
WithStartupTimeout(500 * time.Millisecond).
WithPollInterval(100 * time.Millisecond)
err := wg.WaitUntilReady(context.Background(), target)
require.Error(t, err)
require.EqualError(t, err, "container crashed with out-of-memory (OOMKilled)")
}
func TestWaitForHealthFailsDueToExitedContainer(t *testing.T) {
target := newStateTarget(t, &container.State{
Status: container.StateExited,
ExitCode: 1,
})
wg := wait.NewHealthStrategy().
WithStartupTimeout(500 * time.Millisecond).
WithPollInterval(100 * time.Millisecond)
err := wg.WaitUntilReady(context.Background(), target)
require.Error(t, err)
require.EqualError(t, err, "container exited with code 1")
}
func TestWaitForHealthFailsDueToUnexpectedContainerStatus(t *testing.T) {
target := newStateTarget(t, &container.State{
Status: container.StateDead,
})
wg := wait.NewHealthStrategy().
WithStartupTimeout(500 * time.Millisecond).
WithPollInterval(100 * time.Millisecond)
err := wg.WaitUntilReady(context.Background(), target)
require.Error(t, err)
require.EqualError(t, err, "unexpected container status \"dead\"")
}