|
| 1 | +package agent |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/google/uuid" |
| 9 | + "github.com/ory/dockertest/v3" |
| 10 | + "github.com/ory/dockertest/v3/docker" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + "go.uber.org/mock/gomock" |
| 14 | + |
| 15 | + "github.com/coder/coder/v2/codersdk" |
| 16 | + "github.com/coder/coder/v2/testutil" |
| 17 | + "github.com/coder/quartz" |
| 18 | +) |
| 19 | + |
| 20 | +func TestDockerCLIContainerLister(t *testing.T) { |
| 21 | + t.Parallel() |
| 22 | + |
| 23 | + pool, err := dockertest.NewPool("") |
| 24 | + require.NoError(t, err, "Could not connect to docker") |
| 25 | + testLabelValue := uuid.New().String() |
| 26 | + res, err := pool.RunWithOptions(&dockertest.RunOptions{ |
| 27 | + Repository: "busybox", |
| 28 | + Tag: "latest", |
| 29 | + Cmd: []string{"sleep", "infnity"}, |
| 30 | + Labels: map[string]string{"com.coder.test": testLabelValue}, |
| 31 | + }, func(config *docker.HostConfig) { |
| 32 | + config.AutoRemove = true |
| 33 | + config.RestartPolicy = docker.RestartPolicy{Name: "no"} |
| 34 | + }) |
| 35 | + require.NoError(t, err, "Could not start test docker container") |
| 36 | + t.Cleanup(func() { |
| 37 | + assert.NoError(t, pool.Purge(res), "Could not purge resource") |
| 38 | + }) |
| 39 | + |
| 40 | + expectedCt := codersdk.WorkspaceAgentContainer{ |
| 41 | + CreatedAt: res.Container.Created.Local().Truncate(time.Second), |
| 42 | + ID: res.Container.ID, |
| 43 | + // For some reason, ory/dockertest pre-pends a forward slash to the container name. |
| 44 | + FriendlyName: strings.TrimPrefix(res.Container.Name, "/"), |
| 45 | + Image: res.Container.Image, |
| 46 | + Labels: res.Container.Config.Labels, |
| 47 | + } |
| 48 | + dcl := dockerCLIContainerLister{} |
| 49 | + ctx := testutil.Context(t, testutil.WaitShort) |
| 50 | + actual, err := dcl.List(ctx) |
| 51 | + require.NoError(t, err, "Could not list containers") |
| 52 | + var found bool |
| 53 | + for _, ct := range actual { |
| 54 | + if ct.Labels != nil && ct.Labels["com.coder.test"] == testLabelValue { |
| 55 | + found = true |
| 56 | + assert.Equal(t, expectedCt.CreatedAt, ct.CreatedAt) |
| 57 | + assert.Equal(t, expectedCt.FriendlyName, ct.FriendlyName) |
| 58 | + assert.Equal(t, expectedCt.ID, ct.ID) |
| 59 | + // Docker returns the sha256 digest of the image. |
| 60 | + // assert.Equal(t, expectedCt.Image, ct.Image) |
| 61 | + break |
| 62 | + } |
| 63 | + } |
| 64 | + assert.True(t, found, "Expected to find container with label 'com.coder.test=%s'", testLabelValue) |
| 65 | +} |
| 66 | + |
| 67 | +func TestContainersHandler(t *testing.T) { |
| 68 | + t.Parallel() |
| 69 | + |
| 70 | + t.Run("list", func(t *testing.T) { |
| 71 | + t.Parallel() |
| 72 | + |
| 73 | + // Given: a containersHandler backed by a mock |
| 74 | + var ( |
| 75 | + ctx = testutil.Context(t, testutil.WaitShort) |
| 76 | + clk = quartz.NewMock(t) |
| 77 | + ctrl = gomock.NewController(t) |
| 78 | + mockLister = NewMockContainerLister(ctrl) |
| 79 | + now = time.Now().UTC() |
| 80 | + ch = containersHandler{ |
| 81 | + cacheDuration: time.Second, |
| 82 | + cl: mockLister, |
| 83 | + clock: clk, |
| 84 | + } |
| 85 | + expected = []codersdk.WorkspaceAgentContainer{fakeContainer(t)} |
| 86 | + ) |
| 87 | + |
| 88 | + clk.Set(now).MustWait(ctx) |
| 89 | + |
| 90 | + // When: getContainers is called for the first time |
| 91 | + ch.mtime = time.Time{} |
| 92 | + mockLister.EXPECT().List(gomock.Any()).Return(expected, nil) |
| 93 | + actual, err := ch.getContainers(ctx) |
| 94 | + |
| 95 | + // Then: the underlying lister is called and the result is returned |
| 96 | + require.NoError(t, err, "expected no error on first call") |
| 97 | + require.Equal(t, expected, actual, "expected containers to be equal on first call") |
| 98 | + // Then: the result is cached |
| 99 | + require.Equal(t, now, ch.mtime, "expected container mtime to be set on first call") |
| 100 | + require.NotEmpty(t, ch.containers, "expected cached data to not be empty on first call") |
| 101 | + |
| 102 | + // When: getContainers is called again |
| 103 | + actual, err = ch.getContainers(ctx) |
| 104 | + |
| 105 | + // Then: the underlying lister is not called and the cached result is |
| 106 | + // returned |
| 107 | + require.NoError(t, err, "expected no error on second call") |
| 108 | + require.Equal(t, expected, actual, "expected containers to be equal on second call") |
| 109 | + // Then: the result is cached |
| 110 | + require.Equal(t, now, ch.mtime, "expected container mtime to not have changed on second call") |
| 111 | + require.Equal(t, expected, ch.containers, "expected cached data to not have changed on second call") |
| 112 | + |
| 113 | + // When: getContainers is called after the cache duration has expired |
| 114 | + expected = append(expected, fakeContainer(t)) |
| 115 | + later := now.Add(defaultGetContainersCacheDuration).Add(time.Second) |
| 116 | + clk.Set(later).MustWait(ctx) |
| 117 | + mockLister.EXPECT().List(gomock.Any()).Return(expected, nil) |
| 118 | + actual, err = ch.getContainers(ctx) |
| 119 | + |
| 120 | + // Then: the underlying lister is called and the result is returned |
| 121 | + require.NoError(t, err, "expected no error on third call") |
| 122 | + require.Equal(t, expected, actual, "expected containers to be equal on third call") |
| 123 | + // Then: the result is cached |
| 124 | + require.Equal(t, later, ch.mtime, "expected container mtime to later on third call") |
| 125 | + require.Equal(t, expected, ch.containers, "expected cached data to not have changed on third call") |
| 126 | + |
| 127 | + // When: getContainers is called again but the underlying lister returns an error |
| 128 | + actual, err = ch.getContainers(ctx) |
| 129 | + require.NoError(t, err) |
| 130 | + |
| 131 | + // Then: the cached data is not updated |
| 132 | + require.Equal(t, expected, actual, "expected containers to be equal on fourth call") |
| 133 | + require.Equal(t, later, ch.mtime, "expected container mtime to not have changed on fourth call") |
| 134 | + require.Equal(t, expected, ch.containers, "expected cached data to not have changed on fourth call") |
| 135 | + |
| 136 | + // When: time advances past mtime |
| 137 | + laterlater := later.Add(defaultGetContainersCacheDuration).Add(time.Second) |
| 138 | + clk.Set(laterlater).MustWait(ctx) |
| 139 | + mockLister.EXPECT().List(gomock.Any()).Return(nil, assert.AnError) |
| 140 | + actual, err = ch.getContainers(ctx) |
| 141 | + // Then: the underlying error is returned |
| 142 | + require.ErrorContains(t, err, assert.AnError.Error(), "expected error on fifth call") |
| 143 | + require.Nil(t, actual, "expected no data to be returned on fifth call") |
| 144 | + // Then: the underlying cached data remains the same |
| 145 | + require.Equal(t, later, ch.mtime, "expected container mtime to not have changed on fifth call") |
| 146 | + require.Equal(t, expected, ch.containers, "expected cached data to not have changed on fifth call") |
| 147 | + }) |
| 148 | +} |
| 149 | + |
| 150 | +func fakeContainer(t testing.TB, mut ...func(*codersdk.WorkspaceAgentContainer)) codersdk.WorkspaceAgentContainer { |
| 151 | + t.Helper() |
| 152 | + ct := codersdk.WorkspaceAgentContainer{ |
| 153 | + ID: uuid.New().String(), |
| 154 | + FriendlyName: testutil.GetRandomName(t), |
| 155 | + CreatedAt: time.Now().UTC(), |
| 156 | + Image: testutil.GetRandomName(t) + ":" + strings.Split(uuid.New().String(), "-")[0], |
| 157 | + Labels: map[string]string{}, |
| 158 | + } |
| 159 | + for _, m := range mut { |
| 160 | + m(&ct) |
| 161 | + } |
| 162 | + return ct |
| 163 | +} |
0 commit comments