github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/daemon/resize_test.go (about)

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