github.com/adityamillind98/moby@v23.0.0-rc.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/daemon/exec"
    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: exec.NewStore(),
    20  	}
    21  	c := &container.Container{
    22  		ExecCommands: exec.NewStore(),
    23  	}
    24  	ec := &exec.Config{
    25  		ID: n,
    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 execResizeMockContainerdClient struct {
    33  	MockContainerdClient
    34  	ProcessID   string
    35  	ContainerID string
    36  	Width       int
    37  	Height      int
    38  }
    39  
    40  func (c *execResizeMockContainerdClient) ResizeTerminal(ctx context.Context, containerID, processID string, width, height int) error {
    41  	c.ProcessID = processID
    42  	c.ContainerID = containerID
    43  	c.Width = width
    44  	c.Height = height
    45  	return nil
    46  }
    47  
    48  // This test is to make sure that when exec context is ready, resize should call ResizeTerminal via containerd client.
    49  func TestExecResize(t *testing.T) {
    50  	n := "TestExecResize"
    51  	width := 24
    52  	height := 8
    53  	ec := &exec.Config{
    54  		ID:          n,
    55  		ContainerID: n,
    56  		Started:     make(chan struct{}),
    57  	}
    58  	close(ec.Started)
    59  	mc := &execResizeMockContainerdClient{}
    60  	d := &Daemon{
    61  		execCommands: exec.NewStore(),
    62  		containerd:   mc,
    63  		containers:   container.NewMemoryStore(),
    64  	}
    65  	c := &container.Container{
    66  		ExecCommands: exec.NewStore(),
    67  		State:        &container.State{Running: true},
    68  	}
    69  	d.containers.Add(n, c)
    70  	d.registerExecCommand(c, ec)
    71  	err := d.ContainerExecResize(n, height, width)
    72  	assert.NilError(t, err)
    73  	assert.Equal(t, mc.Width, width)
    74  	assert.Equal(t, mc.Height, height)
    75  	assert.Equal(t, mc.ProcessID, n)
    76  	assert.Equal(t, mc.ContainerID, n)
    77  }
    78  
    79  // This test is to make sure that when exec context is not ready, a timeout error should happen.
    80  // TODO: the expect running time for this test is 10s, which would be too long for unit test.
    81  func TestExecResizeTimeout(t *testing.T) {
    82  	n := "TestExecResize"
    83  	width := 24
    84  	height := 8
    85  	ec := &exec.Config{
    86  		ID:          n,
    87  		ContainerID: n,
    88  		Started:     make(chan struct{}),
    89  	}
    90  	mc := &execResizeMockContainerdClient{}
    91  	d := &Daemon{
    92  		execCommands: exec.NewStore(),
    93  		containerd:   mc,
    94  		containers:   container.NewMemoryStore(),
    95  	}
    96  	c := &container.Container{
    97  		ExecCommands: exec.NewStore(),
    98  		State:        &container.State{Running: true},
    99  	}
   100  	d.containers.Add(n, c)
   101  	d.registerExecCommand(c, ec)
   102  	err := d.ContainerExecResize(n, height, width)
   103  	assert.ErrorContains(t, err, "timeout waiting for exec session ready")
   104  }