github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/containerupdate/docker_container_updater_test.go (about)

     1  package containerupdate
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  
    11  	"github.com/tilt-dev/tilt/internal/store/liveupdates"
    12  	"github.com/tilt-dev/tilt/internal/testutils"
    13  
    14  	"github.com/tilt-dev/tilt/internal/docker"
    15  	"github.com/tilt-dev/tilt/pkg/model"
    16  )
    17  
    18  var TestContainerInfo = liveupdates.Container{
    19  	PodID:         "somepod",
    20  	ContainerID:   docker.TestContainer,
    21  	ContainerName: "my-container",
    22  	Namespace:     "ns-foo",
    23  }
    24  
    25  func TestUpdateInContainerCopiesAndRmsFiles(t *testing.T) {
    26  	f := newDCUFixture(t)
    27  
    28  	archive := bytes.NewBuffer([]byte("hello world"))
    29  	toDelete := []string{"/src/does-not-exist"}
    30  	err := f.dcu.UpdateContainer(f.ctx, TestContainerInfo, archive, toDelete, nil, false)
    31  	if err != nil {
    32  		f.t.Fatal(err)
    33  	}
    34  
    35  	if assert.Equal(f.t, 1, len(f.dCli.ExecCalls), "calls to ExecInContainer") {
    36  		assert.Equal(f.t, docker.TestContainer, f.dCli.ExecCalls[0].Container)
    37  		expectedCmd := model.Cmd{Argv: []string{"rm", "-rf", "/src/does-not-exist"}}
    38  		assert.Equal(f.t, expectedCmd, f.dCli.ExecCalls[0].Cmd)
    39  	}
    40  
    41  	if assert.Equal(f.t, 1, f.dCli.CopyCount, "calls to CopyToContainer") {
    42  		assert.Equal(f.t, docker.TestContainer, f.dCli.CopyContainer)
    43  		// TODO(maia): assert that the right stuff made it into the archive (f.dCli.CopyContent)
    44  	}
    45  }
    46  
    47  func TestUpdateContainerExecsRuns(t *testing.T) {
    48  	f := newDCUFixture(t)
    49  
    50  	cmdA := model.Cmd{Argv: []string{"a"}}
    51  	cmdB := model.Cmd{Argv: []string{"cu", "and cu", "another cu"}}
    52  
    53  	err := f.dcu.UpdateContainer(f.ctx, TestContainerInfo, nil, nil, []model.Cmd{cmdA, cmdB}, false)
    54  	if err != nil {
    55  		f.t.Fatal(err)
    56  	}
    57  
    58  	expectedExecs := []docker.ExecCall{
    59  		docker.ExecCall{Container: docker.TestContainer, Cmd: cmdA},
    60  		docker.ExecCall{Container: docker.TestContainer, Cmd: cmdB},
    61  	}
    62  
    63  	assert.Equal(f.t, expectedExecs, f.dCli.ExecCalls)
    64  }
    65  
    66  func TestUpdateContainerRestartsContainer(t *testing.T) {
    67  	f := newDCUFixture(t)
    68  
    69  	err := f.dcu.UpdateContainer(f.ctx, TestContainerInfo, nil, nil, nil, false)
    70  	if err != nil {
    71  		f.t.Fatal(err)
    72  	}
    73  
    74  	assert.Equal(f.t, f.dCli.RestartsByContainer[docker.TestContainer], 1)
    75  }
    76  
    77  func TestUpdateContainerHotReloadDoesNotRestartContainer(t *testing.T) {
    78  	f := newDCUFixture(t)
    79  
    80  	err := f.dcu.UpdateContainer(f.ctx, TestContainerInfo, nil, nil, nil, true)
    81  	if err != nil {
    82  		f.t.Fatal(err)
    83  	}
    84  
    85  	assert.Equal(f.t, 0, len(f.dCli.RestartsByContainer))
    86  }
    87  
    88  func TestUpdateContainerKillTask(t *testing.T) {
    89  	f := newDCUFixture(t)
    90  
    91  	f.dCli.SetExecError(docker.ExitError{ExitCode: GenericExitCodeKilled})
    92  
    93  	cmdA := model.Cmd{Argv: []string{"cat"}}
    94  	err := f.dcu.UpdateContainer(f.ctx, TestContainerInfo, nil, nil, []model.Cmd{cmdA}, false)
    95  	msg := "killed by container runtime"
    96  	if err == nil || !strings.Contains(err.Error(), msg) {
    97  		f.t.Errorf("Expected error %q, actual: %v", msg, err)
    98  	}
    99  
   100  	expectedExecs := []docker.ExecCall{
   101  		docker.ExecCall{Container: docker.TestContainer, Cmd: cmdA},
   102  	}
   103  
   104  	assert.Equal(f.t, expectedExecs, f.dCli.ExecCalls)
   105  }
   106  
   107  type dockerContainerUpdaterFixture struct {
   108  	t    testing.TB
   109  	ctx  context.Context
   110  	dCli *docker.FakeClient
   111  	dcu  *DockerUpdater
   112  }
   113  
   114  func newDCUFixture(t testing.TB) *dockerContainerUpdaterFixture {
   115  	fakeCli := docker.NewFakeClient()
   116  	cu := &DockerUpdater{dCli: fakeCli}
   117  	ctx, _, _ := testutils.CtxAndAnalyticsForTest()
   118  
   119  	return &dockerContainerUpdaterFixture{
   120  		t:    t,
   121  		ctx:  ctx,
   122  		dCli: fakeCli,
   123  		dcu:  cu,
   124  	}
   125  }