Browse code

Migrate container pause tests to api tests

This fix migrates container pause tests from integration-cli
to api tests in integration/.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

Yong Tang authored on 2018/02/10 09:13:26
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,98 @@
0
+package container // import "github.com/docker/docker/integration/container"
1
+
2
+import (
3
+	"context"
4
+	"io"
5
+	"testing"
6
+	"time"
7
+
8
+	"github.com/docker/docker/api/types"
9
+	"github.com/docker/docker/api/types/events"
10
+	"github.com/docker/docker/api/types/filters"
11
+	"github.com/docker/docker/integration/internal/container"
12
+	"github.com/docker/docker/integration/internal/request"
13
+	"github.com/docker/docker/internal/testutil"
14
+	"github.com/gotestyourself/gotestyourself/poll"
15
+	"github.com/gotestyourself/gotestyourself/skip"
16
+	"github.com/stretchr/testify/assert"
17
+	"github.com/stretchr/testify/require"
18
+)
19
+
20
+func TestPause(t *testing.T) {
21
+	skip.If(t, testEnv.DaemonInfo.OSType == "windows" && testEnv.DaemonInfo.Isolation == "process")
22
+
23
+	defer setupTest(t)()
24
+	client := request.NewAPIClient(t)
25
+	ctx := context.Background()
26
+
27
+	name := "testeventpause"
28
+	cID := container.Run(t, ctx, client, container.WithName(name))
29
+	poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
30
+
31
+	since := request.DaemonUnixTime(ctx, t, client, testEnv)
32
+
33
+	err := client.ContainerPause(ctx, name)
34
+	require.NoError(t, err)
35
+
36
+	inspect, err := client.ContainerInspect(ctx, cID)
37
+	require.NoError(t, err)
38
+	assert.Equal(t, inspect.State.Paused, true)
39
+
40
+	err = client.ContainerUnpause(ctx, name)
41
+	require.NoError(t, err)
42
+
43
+	until := request.DaemonUnixTime(ctx, t, client, testEnv)
44
+
45
+	messages, errs := client.Events(ctx, types.EventsOptions{
46
+		Since:   since,
47
+		Until:   until,
48
+		Filters: filters.NewArgs(filters.Arg("container", name)),
49
+	})
50
+	assert.Equal(t, getEventActions(t, messages, errs), []string{"pause", "unpause"})
51
+}
52
+
53
+func TestPauseFailsOnWindowsServerContainers(t *testing.T) {
54
+	skip.If(t, (testEnv.DaemonInfo.OSType != "windows" || testEnv.DaemonInfo.Isolation != "process"))
55
+
56
+	defer setupTest(t)()
57
+	client := request.NewAPIClient(t)
58
+	ctx := context.Background()
59
+
60
+	cID := container.Run(t, ctx, client)
61
+	poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
62
+
63
+	err := client.ContainerPause(ctx, cID)
64
+	testutil.ErrorContains(t, err, "cannot pause Windows Server Containers")
65
+}
66
+
67
+func TestPauseStopPausedContainer(t *testing.T) {
68
+	skip.If(t, testEnv.DaemonInfo.OSType != "linux")
69
+
70
+	defer setupTest(t)()
71
+	client := request.NewAPIClient(t)
72
+	ctx := context.Background()
73
+
74
+	cID := container.Run(t, ctx, client)
75
+	poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
76
+
77
+	err := client.ContainerPause(ctx, cID)
78
+	require.NoError(t, err)
79
+
80
+	err = client.ContainerStop(ctx, cID, nil)
81
+	require.NoError(t, err)
82
+
83
+	poll.WaitOn(t, containerIsStopped(ctx, client, cID), poll.WithDelay(100*time.Millisecond))
84
+}
85
+
86
+func getEventActions(t *testing.T, messages <-chan events.Message, errs <-chan error) []string {
87
+	actions := []string{}
88
+	for {
89
+		select {
90
+		case err := <-errs:
91
+			assert.Equal(t, err == nil || err == io.EOF, true)
92
+			return actions
93
+		case e := <-messages:
94
+			actions = append(actions, e.Status)
95
+		}
96
+	}
97
+}
... ...
@@ -1,12 +1,16 @@
1 1
 package request // import "github.com/docker/docker/integration/internal/request"
2 2
 
3 3
 import (
4
+	"fmt"
4 5
 	"net"
5 6
 	"net/http"
6 7
 	"testing"
7 8
 	"time"
8 9
 
10
+	"golang.org/x/net/context"
11
+
9 12
 	"github.com/docker/docker/client"
13
+	"github.com/docker/docker/internal/test/environment"
10 14
 	"github.com/docker/go-connections/sockets"
11 15
 	"github.com/docker/go-connections/tlsconfig"
12 16
 	"github.com/stretchr/testify/require"
... ...
@@ -49,3 +53,24 @@ func NewTLSAPIClient(t *testing.T, host, cacertPath, certPath, keyPath string) (
49 49
 	}
50 50
 	return client.NewClientWithOpts(client.WithHost(host), client.WithHTTPClient(httpClient))
51 51
 }
52
+
53
+// daemonTime provides the current time on the daemon host
54
+func daemonTime(ctx context.Context, t *testing.T, client client.APIClient, testEnv *environment.Execution) time.Time {
55
+	if testEnv.IsLocalDaemon() {
56
+		return time.Now()
57
+	}
58
+
59
+	info, err := client.Info(ctx)
60
+	require.NoError(t, err)
61
+
62
+	dt, err := time.Parse(time.RFC3339Nano, info.SystemTime)
63
+	require.NoError(t, err, "invalid time format in GET /info response")
64
+	return dt
65
+}
66
+
67
+// DaemonUnixTime returns the current time on the daemon host with nanoseconds precision.
68
+// It return the time formatted how the client sends timestamps to the server.
69
+func DaemonUnixTime(ctx context.Context, t *testing.T, client client.APIClient, testEnv *environment.Execution) string {
70
+	dt := daemonTime(ctx, t, client, testEnv)
71
+	return fmt.Sprintf("%d.%09d", dt.Unix(), int64(dt.Nanosecond()))
72
+}