runEnvbuilder starts the envbuilder container with the given environment variables and returns the container ID.
(t *testing.T, opts runOpts)
| 2770 | // runEnvbuilder starts the envbuilder container with the given environment |
| 2771 | // variables and returns the container ID. |
| 2772 | func runEnvbuilder(t *testing.T, opts runOpts) (string, error) { |
| 2773 | t.Helper() |
| 2774 | ctx := context.Background() |
| 2775 | cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) |
| 2776 | require.NoError(t, err) |
| 2777 | t.Cleanup(func() { |
| 2778 | cli.Close() |
| 2779 | }) |
| 2780 | mounts := make([]mount.Mount, 0) |
| 2781 | for volName, volPath := range opts.volumes { |
| 2782 | mounts = append(mounts, mount.Mount{ |
| 2783 | Type: mount.TypeVolume, |
| 2784 | Source: volName, |
| 2785 | Target: volPath, |
| 2786 | }) |
| 2787 | _, err = cli.VolumeCreate(ctx, volume.CreateOptions{ |
| 2788 | Name: volName, |
| 2789 | }) |
| 2790 | require.NoError(t, err) |
| 2791 | t.Cleanup(func() { |
| 2792 | _ = cli.VolumeRemove(ctx, volName, true) |
| 2793 | }) |
| 2794 | } |
| 2795 | img := "envbuilder:latest" |
| 2796 | if opts.image != "" { |
| 2797 | // Pull the image first so we can start it afterwards. |
| 2798 | rc, err := cli.ImagePull(ctx, opts.image, image.PullOptions{}) |
| 2799 | require.NoError(t, err, "failed to pull image") |
| 2800 | t.Cleanup(func() { _ = rc.Close() }) |
| 2801 | _, err = io.Copy(io.Discard, rc) |
| 2802 | require.NoError(t, err, "failed to read image pull response") |
| 2803 | img = opts.image |
| 2804 | } |
| 2805 | hostConfig := &container.HostConfig{ |
| 2806 | NetworkMode: container.NetworkMode("host"), |
| 2807 | Binds: opts.binds, |
| 2808 | Mounts: mounts, |
| 2809 | } |
| 2810 | if opts.privileged { |
| 2811 | hostConfig.CapAdd = append(hostConfig.CapAdd, "SYS_ADMIN") |
| 2812 | hostConfig.Privileged = true |
| 2813 | } |
| 2814 | ctr, err := cli.ContainerCreate(ctx, &container.Config{ |
| 2815 | Image: img, |
| 2816 | Env: opts.env, |
| 2817 | Labels: map[string]string{ |
| 2818 | testContainerLabel: "true", |
| 2819 | }, |
| 2820 | }, hostConfig, nil, nil, "") |
| 2821 | require.NoError(t, err) |
| 2822 | t.Cleanup(func() { |
| 2823 | _ = cli.ContainerRemove(ctx, ctr.ID, container.RemoveOptions{ |
| 2824 | RemoveVolumes: true, |
| 2825 | Force: true, |
| 2826 | }) |
| 2827 | }) |
| 2828 | err = cli.ContainerStart(ctx, ctr.ID, container.StartOptions{}) |
| 2829 | require.NoError(t, err) |
no test coverage detected