Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,61 @@ |
| 0 |
+package container |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "context" |
|
| 4 |
+ "testing" |
|
| 5 |
+ "time" |
|
| 6 |
+ |
|
| 7 |
+ "github.com/docker/docker/api/types" |
|
| 8 |
+ "github.com/docker/docker/api/types/container" |
|
| 9 |
+ "github.com/docker/docker/api/types/network" |
|
| 10 |
+ "github.com/docker/docker/api/types/strslice" |
|
| 11 |
+ "github.com/docker/docker/client" |
|
| 12 |
+ "github.com/docker/docker/integration/util/request" |
|
| 13 |
+ "github.com/gotestyourself/gotestyourself/poll" |
|
| 14 |
+ "github.com/stretchr/testify/require" |
|
| 15 |
+) |
|
| 16 |
+ |
|
| 17 |
+// TestHealthCheckWorkdir verifies that health-checks inherit the containers' |
|
| 18 |
+// working-dir. |
|
| 19 |
+func TestHealthCheckWorkdir(t *testing.T) {
|
|
| 20 |
+ defer setupTest(t)() |
|
| 21 |
+ ctx := context.Background() |
|
| 22 |
+ client := request.NewAPIClient(t) |
|
| 23 |
+ |
|
| 24 |
+ c, err := client.ContainerCreate(ctx, |
|
| 25 |
+ &container.Config{
|
|
| 26 |
+ Image: "busybox", |
|
| 27 |
+ Tty: true, |
|
| 28 |
+ WorkingDir: "/foo", |
|
| 29 |
+ Cmd: strslice.StrSlice([]string{"top"}),
|
|
| 30 |
+ Healthcheck: &container.HealthConfig{
|
|
| 31 |
+ Test: []string{"CMD-SHELL", "if [ \"$PWD\" = \"/foo\" ]; then exit 0; else exit 1; fi;"},
|
|
| 32 |
+ Interval: 50 * time.Millisecond, |
|
| 33 |
+ Retries: 3, |
|
| 34 |
+ }, |
|
| 35 |
+ }, |
|
| 36 |
+ &container.HostConfig{},
|
|
| 37 |
+ &network.NetworkingConfig{},
|
|
| 38 |
+ "healthtest", |
|
| 39 |
+ ) |
|
| 40 |
+ require.NoError(t, err) |
|
| 41 |
+ err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
|
|
| 42 |
+ require.NoError(t, err) |
|
| 43 |
+ |
|
| 44 |
+ poll.WaitOn(t, pollForHealthStatus(ctx, client, c.ID, types.Healthy), poll.WithDelay(100*time.Millisecond)) |
|
| 45 |
+} |
|
| 46 |
+ |
|
| 47 |
+func pollForHealthStatus(ctx context.Context, client client.APIClient, containerID string, healthStatus string) func(log poll.LogT) poll.Result {
|
|
| 48 |
+ return func(log poll.LogT) poll.Result {
|
|
| 49 |
+ inspect, err := client.ContainerInspect(ctx, containerID) |
|
| 50 |
+ |
|
| 51 |
+ switch {
|
|
| 52 |
+ case err != nil: |
|
| 53 |
+ return poll.Error(err) |
|
| 54 |
+ case inspect.State.Health.Status == healthStatus: |
|
| 55 |
+ return poll.Success() |
|
| 56 |
+ default: |
|
| 57 |
+ return poll.Continue("waiting for container to become %s", healthStatus)
|
|
| 58 |
+ } |
|
| 59 |
+ } |
|
| 60 |
+} |