Browse Source

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 7 years ago
parent
commit
ea2f076ca9
2 changed files with 123 additions and 0 deletions
  1. 98 0
      integration/container/pause_test.go
  2. 25 0
      integration/internal/request/client.go

+ 98 - 0
integration/container/pause_test.go

@@ -0,0 +1,98 @@
+package container // import "github.com/docker/docker/integration/container"
+
+import (
+	"context"
+	"io"
+	"testing"
+	"time"
+
+	"github.com/docker/docker/api/types"
+	"github.com/docker/docker/api/types/events"
+	"github.com/docker/docker/api/types/filters"
+	"github.com/docker/docker/integration/internal/container"
+	"github.com/docker/docker/integration/internal/request"
+	"github.com/docker/docker/internal/testutil"
+	"github.com/gotestyourself/gotestyourself/poll"
+	"github.com/gotestyourself/gotestyourself/skip"
+	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
+)
+
+func TestPause(t *testing.T) {
+	skip.If(t, testEnv.DaemonInfo.OSType == "windows" && testEnv.DaemonInfo.Isolation == "process")
+
+	defer setupTest(t)()
+	client := request.NewAPIClient(t)
+	ctx := context.Background()
+
+	name := "testeventpause"
+	cID := container.Run(t, ctx, client, container.WithName(name))
+	poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
+
+	since := request.DaemonUnixTime(ctx, t, client, testEnv)
+
+	err := client.ContainerPause(ctx, name)
+	require.NoError(t, err)
+
+	inspect, err := client.ContainerInspect(ctx, cID)
+	require.NoError(t, err)
+	assert.Equal(t, inspect.State.Paused, true)
+
+	err = client.ContainerUnpause(ctx, name)
+	require.NoError(t, err)
+
+	until := request.DaemonUnixTime(ctx, t, client, testEnv)
+
+	messages, errs := client.Events(ctx, types.EventsOptions{
+		Since:   since,
+		Until:   until,
+		Filters: filters.NewArgs(filters.Arg("container", name)),
+	})
+	assert.Equal(t, getEventActions(t, messages, errs), []string{"pause", "unpause"})
+}
+
+func TestPauseFailsOnWindowsServerContainers(t *testing.T) {
+	skip.If(t, (testEnv.DaemonInfo.OSType != "windows" || testEnv.DaemonInfo.Isolation != "process"))
+
+	defer setupTest(t)()
+	client := request.NewAPIClient(t)
+	ctx := context.Background()
+
+	cID := container.Run(t, ctx, client)
+	poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
+
+	err := client.ContainerPause(ctx, cID)
+	testutil.ErrorContains(t, err, "cannot pause Windows Server Containers")
+}
+
+func TestPauseStopPausedContainer(t *testing.T) {
+	skip.If(t, testEnv.DaemonInfo.OSType != "linux")
+
+	defer setupTest(t)()
+	client := request.NewAPIClient(t)
+	ctx := context.Background()
+
+	cID := container.Run(t, ctx, client)
+	poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
+
+	err := client.ContainerPause(ctx, cID)
+	require.NoError(t, err)
+
+	err = client.ContainerStop(ctx, cID, nil)
+	require.NoError(t, err)
+
+	poll.WaitOn(t, containerIsStopped(ctx, client, cID), poll.WithDelay(100*time.Millisecond))
+}
+
+func getEventActions(t *testing.T, messages <-chan events.Message, errs <-chan error) []string {
+	actions := []string{}
+	for {
+		select {
+		case err := <-errs:
+			assert.Equal(t, err == nil || err == io.EOF, true)
+			return actions
+		case e := <-messages:
+			actions = append(actions, e.Status)
+		}
+	}
+}

+ 25 - 0
integration/internal/request/client.go

@@ -1,12 +1,16 @@
 package request // import "github.com/docker/docker/integration/internal/request"
 
 import (
+	"fmt"
 	"net"
 	"net/http"
 	"testing"
 	"time"
 
+	"golang.org/x/net/context"
+
 	"github.com/docker/docker/client"
+	"github.com/docker/docker/internal/test/environment"
 	"github.com/docker/go-connections/sockets"
 	"github.com/docker/go-connections/tlsconfig"
 	"github.com/stretchr/testify/require"
@@ -49,3 +53,24 @@ func NewTLSAPIClient(t *testing.T, host, cacertPath, certPath, keyPath string) (
 	}
 	return client.NewClientWithOpts(client.WithHost(host), client.WithHTTPClient(httpClient))
 }
+
+// daemonTime provides the current time on the daemon host
+func daemonTime(ctx context.Context, t *testing.T, client client.APIClient, testEnv *environment.Execution) time.Time {
+	if testEnv.IsLocalDaemon() {
+		return time.Now()
+	}
+
+	info, err := client.Info(ctx)
+	require.NoError(t, err)
+
+	dt, err := time.Parse(time.RFC3339Nano, info.SystemTime)
+	require.NoError(t, err, "invalid time format in GET /info response")
+	return dt
+}
+
+// DaemonUnixTime returns the current time on the daemon host with nanoseconds precision.
+// It return the time formatted how the client sends timestamps to the server.
+func DaemonUnixTime(ctx context.Context, t *testing.T, client client.APIClient, testEnv *environment.Execution) string {
+	dt := daemonTime(ctx, t, client, testEnv)
+	return fmt.Sprintf("%d.%09d", dt.Unix(), int64(dt.Nanosecond()))
+}