func()

in dev-tools/systemtests/container.go [210:258]


func (tr *DockerTestRunner) createTestContainer(ctx context.Context, apiClient *client.Client) container.CreateResponse {
	reader, err := apiClient.ImagePull(ctx, tr.Container, image.PullOptions{})
	require.NoError(tr.Runner, err, "error pulling image")
	defer reader.Close()

	_, err = io.Copy(os.Stdout, reader)
	require.NoError(tr.Runner, err, "error copying image")

	wdCmd := exec.Command("git", "rev-parse", "--show-toplevel")
	wdPath, err := wdCmd.CombinedOutput()
	require.NoError(tr.Runner, err, "error finding root path")

	cwd := strings.TrimSpace(string(wdPath))
	logp.L().Infof("using cwd: %s", cwd)

	testRunCmd := []string{"go", "test", "-v", tr.Basepath}
	if tr.Testname != "" {
		testRunCmd = append(testRunCmd, "-run", tr.Testname)
	}

	mountPath := "/hostfs"

	containerEnv := []string{fmt.Sprintf("HOSTFS=%s", mountPath)}
	// used by a few vendored libaries
	containerEnv = append(containerEnv, "HOST_PROC=%s", mountPath)
	if tr.Privileged {
		containerEnv = append(containerEnv, "PRIVILEGED=1")
	}

	if tr.MonitorPID != 0 {
		containerEnv = append(containerEnv, fmt.Sprintf("MONITOR_PID=%d", tr.MonitorPID))
	}

	resp, err := apiClient.ContainerCreate(ctx, &container.Config{
		Image:      tr.Container,
		Cmd:        testRunCmd,
		Tty:        false,
		WorkingDir: "/app",
		Env:        containerEnv,
		User:       tr.RunAsUser,
	}, &container.HostConfig{
		CgroupnsMode: tr.CgroupNSMode,
		Privileged:   tr.Privileged,
		Binds:        []string{fmt.Sprintf("/:%s", mountPath), fmt.Sprintf("%s:/app", cwd)},
	}, nil, nil, "")
	require.NoError(tr.Runner, err, "error creating container")

	return resp
}