MCPcopy Index your code
hub / github.com/coder/envbox / startEnvboxCmd

Function startEnvboxCmd

integration/gpu_test.go:212–289  ·  view source on GitHub ↗

startEnvboxCmd starts the envbox container with the given arguments. Ideally we would use ory/dockertest for this, but it doesn't support specifying the runtime. We have alternatively used the docker client library, but a nice property of using the docker cli is that if a test fails, you can just ru

(ctx context.Context, t *testing.T, innerImage, innerUser string, addlArgs ...string)

Source from the content-addressed store, hash-verified

210// but a nice property of using the docker cli is that if a test fails, you can
211// just run the command manually to debug!
212func startEnvboxCmd(ctx context.Context, t *testing.T, innerImage, innerUser string, addlArgs ...string) (containerID string) {
213 t.Helper()
214
215 var (
216 tmpDir = integrationtest.TmpDir(t)
217 binds = integrationtest.DefaultBinds(t, tmpDir)
218 cancelCtx, cancel = context.WithCancel(ctx)
219 )
220 t.Cleanup(cancel)
221
222 // Unfortunately ory/dockertest does not allow us to specify runtime.
223 // We're instead going to just run the container directly via the docker cli.
224 startEnvboxArgs := []string{
225 "run",
226 "--detach",
227 "--rm",
228 "--privileged",
229 "--env", "CODER_INNER_IMAGE=" + innerImage,
230 "--env", "CODER_INNER_USERNAME=" + innerUser,
231 }
232 for _, bind := range binds {
233 bindParts := []string{bind.Source, bind.Target}
234 if bind.ReadOnly {
235 bindParts = append(bindParts, "ro")
236 }
237 startEnvboxArgs = append(startEnvboxArgs, []string{"-v", strings.Join(bindParts, ":")}...)
238 }
239 startEnvboxArgs = append(startEnvboxArgs, addlArgs...)
240 startEnvboxArgs = append(startEnvboxArgs, "envbox:latest", "/envbox", "docker")
241 t.Logf("envbox docker cmd: docker %s", strings.Join(startEnvboxArgs, " "))
242
243 // Start the envbox container without attaching.
244 startEnvboxCmd := exec.CommandContext(cancelCtx, "docker", startEnvboxArgs...)
245 out, err := startEnvboxCmd.CombinedOutput()
246 require.NoError(t, err, "failed to start envbox container")
247 containerID = strings.TrimSpace(string(out))
248 t.Logf("envbox container ID: %s", containerID)
249 t.Cleanup(func() {
250 if t.Failed() {
251 // Dump the logs if the test failed.
252 logsCmd := exec.Command("docker", "logs", containerID)
253 out, err := logsCmd.CombinedOutput()
254 if err != nil {
255 t.Logf("failed to read logs: %s", err)
256 }
257 t.Logf("envbox logs:\n%s", string(out))
258 }
259 // Stop the envbox container.
260 stopEnvboxCmd := exec.Command("docker", "rm", "-f", containerID)
261 out, err := stopEnvboxCmd.CombinedOutput()
262 if err != nil {
263 t.Errorf("failed to stop envbox container: %s", out)
264 }
265 })
266
267 // Wait for the Docker CVM to come up.
268 waitCtx, waitCancel := context.WithTimeout(cancelCtx, 5*time.Minute)
269 defer waitCancel()

Callers 1

TestDocker_NvidiaFunction · 0.85

Calls 5

TmpDirFunction · 0.92
DefaultBindsFunction · 0.92
CombinedOutputMethod · 0.80
CommandContextMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected