github.com/moby/docker@v26.1.3+incompatible/daemon/resize_test.go (about)

     1  //go:build linux
     2  
     3  package daemon
     4  
     5  import (
     6  	"context"
     7  	"testing"
     8  
     9  	"github.com/docker/docker/container"
    10  	"github.com/docker/docker/libcontainerd/types"
    11  	"gotest.tools/v3/assert"
    12  )
    13  
    14  // This test simply verify that when a wrong ID used, a specific error should be returned for exec resize.
    15  func TestExecResizeNoSuchExec(t *testing.T) {
    16  	n := "TestExecResize"
    17  	d := &Daemon{
    18  		execCommands: container.NewExecStore(),
    19  	}
    20  	c := &container.Container{
    21  		ExecCommands: container.NewExecStore(),
    22  	}
    23  	ec := &container.ExecConfig{
    24  		ID:        n,
    25  		Container: c,
    26  	}
    27  	d.registerExecCommand(c, ec)
    28  	err := d.ContainerExecResize("nil", 24, 8)
    29  	assert.ErrorContains(t, err, "No such exec instance")
    30  }
    31  
    32  type execResizeMockProcess struct {
    33  	types.Process
    34  	Width, Height int
    35  }
    36  
    37  func (p *execResizeMockProcess) Resize(ctx context.Context, width, height uint32) error {
    38  	p.Width = int(width)
    39  	p.Height = int(height)
    40  	return nil
    41  }
    42  
    43  // This test is to make sure that when exec context is ready, resize should call ResizeTerminal via containerd client.
    44  func TestExecResize(t *testing.T) {
    45  	n := "TestExecResize"
    46  	width := 24
    47  	height := 8
    48  	mp := &execResizeMockProcess{}
    49  	d := &Daemon{
    50  		execCommands: container.NewExecStore(),
    51  		containers:   container.NewMemoryStore(),
    52  	}
    53  	c := &container.Container{
    54  		ID:           n,
    55  		ExecCommands: container.NewExecStore(),
    56  		State:        &container.State{Running: true},
    57  	}
    58  	ec := &container.ExecConfig{
    59  		ID:        n,
    60  		Container: c,
    61  		Process:   mp,
    62  		Started:   make(chan struct{}),
    63  	}
    64  	close(ec.Started)
    65  	d.containers.Add(n, c)
    66  	d.registerExecCommand(c, ec)
    67  	err := d.ContainerExecResize(n, height, width)
    68  	assert.NilError(t, err)
    69  	assert.Equal(t, mp.Width, width)
    70  	assert.Equal(t, mp.Height, height)
    71  }
    72  
    73  // This test is to make sure that when exec context is not ready, a timeout error should happen.
    74  // TODO: the expect running time for this test is 10s, which would be too long for unit test.
    75  func TestExecResizeTimeout(t *testing.T) {
    76  	n := "TestExecResize"
    77  	width := 24
    78  	height := 8
    79  	mp := &execResizeMockProcess{}
    80  	d := &Daemon{
    81  		execCommands: container.NewExecStore(),
    82  		containers:   container.NewMemoryStore(),
    83  	}
    84  	c := &container.Container{
    85  		ID:           n,
    86  		ExecCommands: container.NewExecStore(),
    87  		State:        &container.State{Running: true},
    88  	}
    89  	ec := &container.ExecConfig{
    90  		ID:        n,
    91  		Container: c,
    92  		Process:   mp,
    93  		Started:   make(chan struct{}),
    94  	}
    95  	d.containers.Add(n, c)
    96  	d.registerExecCommand(c, ec)
    97  	err := d.ContainerExecResize(n, height, width)
    98  	assert.ErrorContains(t, err, "timeout waiting for exec session ready")
    99  }