File: resize_test.go

package info (click to toggle)
docker.io 20.10.24%2Bdfsg1-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-proposed-updates
  • size: 60,824 kB
  • sloc: sh: 5,621; makefile: 593; ansic: 179; python: 162; asm: 7
file content (104 lines) | stat: -rw-r--r-- 2,711 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//go:build linux
// +build linux

package daemon

import (
	"context"
	"testing"

	"github.com/docker/docker/container"
	"github.com/docker/docker/daemon/exec"
	"gotest.tools/v3/assert"
)

// This test simply verify that when a wrong ID used, a specific error should be returned for exec resize.
func TestExecResizeNoSuchExec(t *testing.T) {
	n := "TestExecResize"
	d := &Daemon{
		execCommands: exec.NewStore(),
	}
	c := &container.Container{
		ExecCommands: exec.NewStore(),
	}
	ec := &exec.Config{
		ID: n,
	}
	d.registerExecCommand(c, ec)
	err := d.ContainerExecResize("nil", 24, 8)
	assert.ErrorContains(t, err, "No such exec instance")
}

type execResizeMockContainerdClient struct {
	MockContainerdClient
	ProcessID   string
	ContainerID string
	Width       int
	Height      int
}

func (c *execResizeMockContainerdClient) ResizeTerminal(ctx context.Context, containerID, processID string, width, height int) error {
	c.ProcessID = processID
	c.ContainerID = containerID
	c.Width = width
	c.Height = height
	return nil
}

// This test is to make sure that when exec context is ready, resize should call ResizeTerminal via containerd client.
func TestExecResize(t *testing.T) {
	n := "TestExecResize"
	width := 24
	height := 8
	ec := &exec.Config{
		ID:          n,
		ContainerID: n,
		Started:     make(chan struct{}),
	}
	close(ec.Started)
	mc := &execResizeMockContainerdClient{}
	d := &Daemon{
		execCommands: exec.NewStore(),
		containerd:   mc,
		containers:   container.NewMemoryStore(),
	}
	c := &container.Container{
		ExecCommands: exec.NewStore(),
		State:        &container.State{Running: true},
	}
	d.containers.Add(n, c)
	d.registerExecCommand(c, ec)
	err := d.ContainerExecResize(n, height, width)
	assert.NilError(t, err)
	assert.Equal(t, mc.Width, width)
	assert.Equal(t, mc.Height, height)
	assert.Equal(t, mc.ProcessID, n)
	assert.Equal(t, mc.ContainerID, n)
}

// This test is to make sure that when exec context is not ready, a timeout error should happen.
// TODO: the expect running time for this test is 10s, which would be too long for unit test.
func TestExecResizeTimeout(t *testing.T) {
	n := "TestExecResize"
	width := 24
	height := 8
	ec := &exec.Config{
		ID:          n,
		ContainerID: n,
		Started:     make(chan struct{}),
	}
	mc := &execResizeMockContainerdClient{}
	d := &Daemon{
		execCommands: exec.NewStore(),
		containerd:   mc,
		containers:   container.NewMemoryStore(),
	}
	c := &container.Container{
		ExecCommands: exec.NewStore(),
		State:        &container.State{Running: true},
	}
	d.containers.Add(n, c)
	d.registerExecCommand(c, ec)
	err := d.ContainerExecResize(n, height, width)
	assert.ErrorContains(t, err, "timeout waiting for exec session ready")
}