github.com/rawahars/moby@v24.0.4+incompatible/daemon/resize_test.go (about)

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