TestIntegrationDockerCLI tests the DetectArchitecture, Copy, and ExecAs methods using a real Docker container. All tests share a single container to avoid setup overhead. Run manually with: CODER_TEST_USE_DOCKER=1 go test ./agent/agentcontainers -run TestIntegrationDockerCLI nolint:tparallel,paral
(t *testing.T)
| 25 | // |
| 26 | //nolint:tparallel,paralleltest // Docker integration tests don't run in parallel to avoid flakiness. |
| 27 | func TestIntegrationDockerCLI(t *testing.T) { |
| 28 | if ctud, ok := os.LookupEnv("CODER_TEST_USE_DOCKER"); !ok || ctud != "1" { |
| 29 | t.Skip("Set CODER_TEST_USE_DOCKER=1 to run this test") |
| 30 | } |
| 31 | |
| 32 | pool, err := dockertest.NewPool("") |
| 33 | require.NoError(t, err, "Could not connect to docker") |
| 34 | |
| 35 | // Start a simple busybox container for all subtests to share. |
| 36 | ct, err := pool.RunWithOptions(&dockertest.RunOptions{ |
| 37 | Repository: "busybox", |
| 38 | Tag: "latest", |
| 39 | Cmd: []string{"sleep", "infinity"}, |
| 40 | }, func(config *docker.HostConfig) { |
| 41 | config.AutoRemove = true |
| 42 | config.RestartPolicy = docker.RestartPolicy{Name: "no"} |
| 43 | }) |
| 44 | require.NoError(t, err, "Could not start test docker container") |
| 45 | t.Logf("Created container %q", ct.Container.Name) |
| 46 | t.Cleanup(func() { |
| 47 | assert.NoError(t, pool.Purge(ct), "Could not purge resource %q", ct.Container.Name) |
| 48 | t.Logf("Purged container %q", ct.Container.Name) |
| 49 | }) |
| 50 | |
| 51 | // Wait for container to start. |
| 52 | require.Eventually(t, func() bool { |
| 53 | ct, ok := pool.ContainerByName(ct.Container.Name) |
| 54 | return ok && ct.Container.State.Running |
| 55 | }, testutil.WaitShort, testutil.IntervalSlow, "Container did not start in time") |
| 56 | |
| 57 | dcli := agentcontainers.NewDockerCLI(agentexec.DefaultExecer) |
| 58 | containerName := strings.TrimPrefix(ct.Container.Name, "/") |
| 59 | |
| 60 | t.Run("DetectArchitecture", func(t *testing.T) { |
| 61 | t.Parallel() |
| 62 | ctx := testutil.Context(t, testutil.WaitShort) |
| 63 | |
| 64 | arch, err := dcli.DetectArchitecture(ctx, containerName) |
| 65 | require.NoError(t, err, "DetectArchitecture failed") |
| 66 | require.NotEmpty(t, arch, "arch has no content") |
| 67 | require.Equal(t, runtime.GOARCH, arch, "architecture does not match runtime, did you run this test with a remote Docker socket?") |
| 68 | |
| 69 | t.Logf("Detected architecture: %s", arch) |
| 70 | }) |
| 71 | |
| 72 | t.Run("Copy", func(t *testing.T) { |
| 73 | t.Parallel() |
| 74 | ctx := testutil.Context(t, testutil.WaitShort) |
| 75 | |
| 76 | want := "Help, I'm trapped!" |
| 77 | tempFile := filepath.Join(t.TempDir(), "test-file.txt") |
| 78 | err := os.WriteFile(tempFile, []byte(want), 0o600) |
| 79 | require.NoError(t, err, "create test file failed") |
| 80 | |
| 81 | destPath := "/tmp/copied-file.txt" |
| 82 | err = dcli.Copy(ctx, containerName, tempFile, destPath) |
| 83 | require.NoError(t, err, "Copy failed") |
| 84 |
nothing calls this directly
no test coverage detected