| 2723 | } |
| 2724 | |
| 2725 | func startContainerFromRef(ctx context.Context, t *testing.T, cli *client.Client, ref name.Reference) container.CreateResponse { |
| 2726 | t.Helper() |
| 2727 | |
| 2728 | // Ensure that we can pull the image. |
| 2729 | rc, err := cli.ImagePull(ctx, ref.String(), image.PullOptions{}) |
| 2730 | require.NoError(t, err) |
| 2731 | t.Cleanup(func() { _ = rc.Close() }) |
| 2732 | _, err = io.Copy(io.Discard, rc) |
| 2733 | require.NoError(t, err) |
| 2734 | |
| 2735 | // Start the container. |
| 2736 | ctr, err := cli.ContainerCreate(ctx, &container.Config{ |
| 2737 | Image: ref.String(), |
| 2738 | Labels: map[string]string{ |
| 2739 | testContainerLabel: "true", |
| 2740 | }, |
| 2741 | }, nil, nil, nil, "") |
| 2742 | require.NoError(t, err) |
| 2743 | |
| 2744 | t.Cleanup(func() { |
| 2745 | // Start a new context to ensure that the container is removed. |
| 2746 | ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 2747 | defer cancel() |
| 2748 | |
| 2749 | _ = cli.ContainerRemove(ctx, ctr.ID, container.RemoveOptions{ |
| 2750 | RemoveVolumes: true, |
| 2751 | Force: true, |
| 2752 | }) |
| 2753 | }) |
| 2754 | |
| 2755 | err = cli.ContainerStart(ctx, ctr.ID, container.StartOptions{}) |
| 2756 | require.NoError(t, err) |
| 2757 | |
| 2758 | return ctr |
| 2759 | } |
| 2760 | |
| 2761 | type runOpts struct { |
| 2762 | image string |