TestIntegrationDocker tests agentcontainers functionality using a real Docker container. It starts a container with a known label, lists the containers, and verifies that the expected container is returned. It also executes a sample command inside the container. The container is deleted after the te
(t *testing.T)
| 33 | // |
| 34 | //nolint:paralleltest // This test tends to flake when lots of containers start and stop in parallel. |
| 35 | func TestIntegrationDocker(t *testing.T) { |
| 36 | if ctud, ok := os.LookupEnv("CODER_TEST_USE_DOCKER"); !ok || ctud != "1" { |
| 37 | t.Skip("Set CODER_TEST_USE_DOCKER=1 to run this test") |
| 38 | } |
| 39 | |
| 40 | pool, err := dockertest.NewPool("") |
| 41 | require.NoError(t, err, "Could not connect to docker") |
| 42 | testLabelValue := uuid.New().String() |
| 43 | // Create a temporary directory to validate that we surface mounts correctly. |
| 44 | testTempDir := t.TempDir() |
| 45 | // Pick a random port to expose for testing port bindings. |
| 46 | testRandPort := testutil.RandomPortNoListen(t) |
| 47 | ct, err := pool.RunWithOptions(&dockertest.RunOptions{ |
| 48 | Repository: "busybox", |
| 49 | Tag: "latest", |
| 50 | Cmd: []string{"sleep", "infnity"}, |
| 51 | Labels: map[string]string{ |
| 52 | "com.coder.test": testLabelValue, |
| 53 | "devcontainer.metadata": `[{"remoteEnv": {"FOO": "bar", "MULTILINE": "foo\nbar\nbaz"}}]`, |
| 54 | }, |
| 55 | Mounts: []string{testTempDir + ":" + testTempDir}, |
| 56 | ExposedPorts: []string{fmt.Sprintf("%d/tcp", testRandPort)}, |
| 57 | PortBindings: map[docker.Port][]docker.PortBinding{ |
| 58 | docker.Port(fmt.Sprintf("%d/tcp", testRandPort)): { |
| 59 | { |
| 60 | HostIP: "0.0.0.0", |
| 61 | HostPort: strconv.FormatInt(int64(testRandPort), 10), |
| 62 | }, |
| 63 | }, |
| 64 | }, |
| 65 | }, func(config *docker.HostConfig) { |
| 66 | config.AutoRemove = true |
| 67 | config.RestartPolicy = docker.RestartPolicy{Name: "no"} |
| 68 | }) |
| 69 | require.NoError(t, err, "Could not start test docker container") |
| 70 | t.Logf("Created container %q", ct.Container.Name) |
| 71 | t.Cleanup(func() { |
| 72 | assert.NoError(t, pool.Purge(ct), "Could not purge resource %q", ct.Container.Name) |
| 73 | t.Logf("Purged container %q", ct.Container.Name) |
| 74 | }) |
| 75 | // Wait for container to start |
| 76 | require.Eventually(t, func() bool { |
| 77 | ct, ok := pool.ContainerByName(ct.Container.Name) |
| 78 | return ok && ct.Container.State.Running |
| 79 | }, testutil.WaitShort, testutil.IntervalSlow, "Container did not start in time") |
| 80 | |
| 81 | dcl := agentcontainers.NewDockerCLI(agentexec.DefaultExecer) |
| 82 | ctx := testutil.Context(t, testutil.WaitShort) |
| 83 | actual, err := dcl.List(ctx) |
| 84 | require.NoError(t, err, "Could not list containers") |
| 85 | require.Empty(t, actual.Warnings, "Expected no warnings") |
| 86 | var found bool |
| 87 | for _, foundContainer := range actual.Containers { |
| 88 | if foundContainer.ID == ct.Container.ID { |
| 89 | found = true |
| 90 | assert.Equal(t, ct.Container.Created, foundContainer.CreatedAt) |
| 91 | // ory/dockertest pre-pends a forward slash to the container name. |
| 92 | assert.Equal(t, strings.TrimPrefix(ct.Container.Name, "/"), foundContainer.FriendlyName) |
nothing calls this directly
no test coverage detected