1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

integration/TestUpdateCPUQUota: use exec

An implementation of exec in TestUpdateCPUQUota had a few issues,
including resource leaking and calling both ContainerExecAttach and
ContainerExecRun. The last one makes the test flaky:

	update_linux_test.go:136: expected cgroup value 20000, got: Error: Exec
	command f923baf709525f6b38f6511126addc5d9bb88fb477eeca1c22440551090fa2bb
	is already running

Fix by using the integration/internal/exec package.

While at it, use require/assert to further improve code readability.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin 2018-02-15 11:39:21 -08:00
parent 01143afe54
commit 8a7d6143fc

View file

@ -88,55 +88,17 @@ func TestUpdateCPUQUota(t *testing.T) {
}
inspect, err := client.ContainerInspect(ctx, cID)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
assert.Equal(t, test.update, inspect.HostConfig.CPUQuota)
if inspect.HostConfig.CPUQuota != test.update {
t.Fatalf("quota not updated in the API, expected %d, got: %d", test.update, inspect.HostConfig.CPUQuota)
}
res, err := container.Exec(ctx, client, cID,
[]string{"/bin/cat", "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"})
require.NoError(t, err)
require.Empty(t, res.Stderr())
require.Equal(t, 0, res.ExitCode)
execCreate, err := client.ContainerExecCreate(ctx, cID, types.ExecConfig{
Cmd: []string{"/bin/cat", "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"},
AttachStdout: true,
AttachStderr: true,
})
if err != nil {
t.Fatal(err)
}
attach, err := client.ContainerExecAttach(ctx, execCreate.ID, types.ExecStartCheck{})
if err != nil {
t.Fatal(err)
}
if err := client.ContainerExecStart(ctx, execCreate.ID, types.ExecStartCheck{}); err != nil {
t.Fatal(err)
}
buf := bytes.NewBuffer(nil)
ready := make(chan error)
go func() {
_, err := stdcopy.StdCopy(buf, buf, attach.Reader)
ready <- err
}()
select {
case <-time.After(60 * time.Second):
t.Fatal("timeout waiting for exec to complete")
case err := <-ready:
if err != nil {
t.Fatal(err)
}
}
actual := strings.TrimSpace(buf.String())
if actual != strconv.Itoa(int(test.update)) {
t.Fatalf("expected cgroup value %d, got: %s", test.update, actual)
}
assert.Equal(t, strconv.FormatInt(test.update, 10), strings.TrimSpace(res.Stdout()))
}
}
func getContainerSysFSValue(ctx context.Context, client client.APIClient, cID string, path string) (string, error) {