зеркало из https://github.com/microsoft/docker.git
Merge pull request #36177 from yongtang/02012018-docker_api_resize_test
Migrate several resize tests from integration-cli to integration
This commit is contained in:
Коммит
81e651c6d2
|
@ -1,58 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/docker/docker/integration-cli/checker"
|
||||
"github.com/docker/docker/integration-cli/request"
|
||||
"github.com/go-check/check"
|
||||
)
|
||||
|
||||
func (s *DockerSuite) TestResizeAPIResponse(c *check.C) {
|
||||
out := runSleepingContainer(c, "-d")
|
||||
cleanedContainerID := strings.TrimSpace(out)
|
||||
cli, err := client.NewEnvClient()
|
||||
c.Assert(err, checker.IsNil)
|
||||
defer cli.Close()
|
||||
|
||||
options := types.ResizeOptions{
|
||||
Height: 40,
|
||||
Width: 40,
|
||||
}
|
||||
err = cli.ContainerResize(context.Background(), cleanedContainerID, options)
|
||||
c.Assert(err, check.IsNil)
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestResizeAPIHeightWidthNoInt(c *check.C) {
|
||||
out := runSleepingContainer(c, "-d")
|
||||
cleanedContainerID := strings.TrimSpace(out)
|
||||
|
||||
endpoint := "/containers/" + cleanedContainerID + "/resize?h=foo&w=bar"
|
||||
res, _, err := request.Post(endpoint)
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusBadRequest)
|
||||
c.Assert(err, check.IsNil)
|
||||
}
|
||||
|
||||
func (s *DockerSuite) TestResizeAPIResponseWhenContainerNotStarted(c *check.C) {
|
||||
out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
|
||||
cleanedContainerID := strings.TrimSpace(out)
|
||||
|
||||
// make sure the exited container is not running
|
||||
dockerCmd(c, "wait", cleanedContainerID)
|
||||
|
||||
cli, err := client.NewEnvClient()
|
||||
c.Assert(err, checker.IsNil)
|
||||
defer cli.Close()
|
||||
|
||||
options := types.ResizeOptions{
|
||||
Height: 40,
|
||||
Width: 40,
|
||||
}
|
||||
|
||||
err = cli.ContainerResize(context.Background(), cleanedContainerID, options)
|
||||
c.Assert(err.Error(), checker.Contains, "is not running")
|
||||
}
|
|
@ -1,11 +1,17 @@
|
|||
package container
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/network"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/docker/docker/internal/test/environment"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var testEnv *environment.Execution
|
||||
|
@ -31,3 +37,32 @@ func setupTest(t *testing.T) func() {
|
|||
environment.ProtectAll(t, testEnv)
|
||||
return func() { testEnv.Clean(t) }
|
||||
}
|
||||
|
||||
type containerConstructor func(config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig)
|
||||
|
||||
func createSimpleContainer(ctx context.Context, t *testing.T, client client.APIClient, name string, f ...containerConstructor) string {
|
||||
config := &container.Config{
|
||||
Cmd: []string{"top"},
|
||||
Image: "busybox",
|
||||
}
|
||||
hostConfig := &container.HostConfig{}
|
||||
networkingConfig := &network.NetworkingConfig{}
|
||||
|
||||
for _, fn := range f {
|
||||
fn(config, hostConfig, networkingConfig)
|
||||
}
|
||||
|
||||
c, err := client.ContainerCreate(ctx, config, hostConfig, networkingConfig, name)
|
||||
require.NoError(t, err)
|
||||
|
||||
return c.ID
|
||||
}
|
||||
|
||||
func runSimpleContainer(ctx context.Context, t *testing.T, client client.APIClient, name string, f ...containerConstructor) string {
|
||||
cID := createSimpleContainer(ctx, t, client, name, f...)
|
||||
|
||||
err := client.ContainerStart(ctx, cID, types.ContainerStartOptions{})
|
||||
require.NoError(t, err)
|
||||
|
||||
return cID
|
||||
}
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
package container
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/network"
|
||||
req "github.com/docker/docker/integration-cli/request"
|
||||
"github.com/docker/docker/integration/util/request"
|
||||
"github.com/docker/docker/internal/testutil"
|
||||
"github.com/gotestyourself/gotestyourself/poll"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestResize(t *testing.T) {
|
||||
defer setupTest(t)()
|
||||
client := request.NewAPIClient(t)
|
||||
ctx := context.Background()
|
||||
|
||||
cID := runSimpleContainer(ctx, t, client, "")
|
||||
|
||||
poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
|
||||
|
||||
err := client.ContainerResize(ctx, cID, types.ResizeOptions{
|
||||
Height: 40,
|
||||
Width: 40,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestResizeWithInvalidSize(t *testing.T) {
|
||||
defer setupTest(t)()
|
||||
client := request.NewAPIClient(t)
|
||||
ctx := context.Background()
|
||||
|
||||
cID := runSimpleContainer(ctx, t, client, "")
|
||||
|
||||
poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
|
||||
|
||||
endpoint := "/containers/" + cID + "/resize?h=foo&w=bar"
|
||||
res, _, err := req.Post(endpoint)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, res.StatusCode, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
func TestResizeWhenContainerNotStarted(t *testing.T) {
|
||||
defer setupTest(t)()
|
||||
client := request.NewAPIClient(t)
|
||||
ctx := context.Background()
|
||||
|
||||
cID := runSimpleContainer(ctx, t, client, "", func(config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig) {
|
||||
config.Cmd = []string{"echo"}
|
||||
})
|
||||
|
||||
poll.WaitOn(t, containerIsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond))
|
||||
|
||||
err := client.ContainerResize(ctx, cID, types.ResizeOptions{
|
||||
Height: 40,
|
||||
Width: 40,
|
||||
})
|
||||
testutil.ErrorContains(t, err, "is not running")
|
||||
}
|
Загрузка…
Ссылка в новой задаче