Repository: moby/moby
File: internal/testutil/environment/environment.go (Go)

Code before:
// New creates a new Execution struct
// This is configured using the env client (see client.FromEnv)
func New(ctx context.Context) (*Execution, error) {
	c, err := client.NewClientWithOpts(client.FromEnv)
	if err != nil {
		return nil, errors.Wrapf(err, "failed to create client")
	}
	return FromClient(ctx, c)
}

// FromClient creates a new Execution environment from the passed in client
func FromClient(ctx context.Context, c *client.Client) (*Execution, error) {
	version, err := c.ServerVersion(ctx)
	if err != nil {
		return nil, errors.Wrapf(err, "failed to get version info from daemon")
	}

	var daemonComponent types.ComponentVersion
	for _, component := range version.Components {
		if component.Name == "Engine" {
			daemonComponent = component
			break
		}
	}

	if daemonComponent.Name == "" {
		return nil, errors.New("failed to find Engine component in version info from daemon")
	}

	result, err := c.Info(ctx, client.InfoOptions{})
	if err != nil {
		return nil, errors.Wrapf(err, "failed to get info from daemon")
	}

	return &Execution{
		client:            c,
		DaemonInfo:        result.Info,
		DaemonVersion:     daemonComponent,
		PlatformDefaults:  getPlatformDefaults(result.Info),
		protectedElements: newProtectedElements(),
	}, nil
}

func getPlatformDefaults(info system.Info) PlatformDefaults {
	volumesPath := filepath.Join(info.DockerRootDir, "volumes")
	containersPath := filepath.Join(info.DockerRootDir, "containers")

	switch info.OSType {
	case "linux":
		return PlatformDefaults{
			BaseImage:            "scratch",

Review comment (performance):
Possibly this part could be using the `apiClient.Ping()` 🤔 


```
curl -I -fsSL --unix-socket /var/run/docker.sock 'http://localhost/_ping'
HTTP/1.1 200 OK
Api-Version: 1.51
Builder-Version: 2
Cache-Control: no-cache, no-store, must-revalidate
Content-Type: text/plain; charset=utf-8
Date: Wed, 29 Oct 2025 13:16:01 GMT
Docker-Experimental: false
Ostype: linux
Pragma: no-cache
Server: Docker/28.5.1 (linux)
Swarm: active/manager
```

Code after:
// This is configured using the env client (see client.FromEnv)
func New(ctx context.Context) (*Execution, error) {
	c, err := client.NewClientWithOpts(client.FromEnv)
	if err != nil {
		return nil, errors.Wrapf(err, "failed to create client")
	}
	return FromClient(ctx, c)
}

// FromClient creates a new Execution environment from the passed in client
func FromClient(ctx context.Context, c *client.Client) (*Execution, error) {
	_, err := c.Ping(ctx, client.PingOptions{NegotiateAPIVersion: true})
	if err != nil {
		return nil, errors.Wrapf(err, "failed to ping daemon to negotiate api version")
	}

	result, err := c.Info(ctx, client.InfoOptions{})
	if err != nil {
		return nil, errors.Wrapf(err, "failed to get info from daemon")
	}

	version, err := c.ServerVersion(ctx, client.ServerVersionOptions{})
	if err != nil {
		return nil, errors.Wrapf(err, "failed to get version from daemon")
	}

	return &Execution{
		client:              c,
		DaemonInfo:          result.Info,
		DaemonMinAPIVersion: version.MinAPIVersion,
		PlatformDefaults:    getPlatformDefaults(result.Info),
		protectedElements:   newProtectedElements(),
	}, nil
}

func getPlatformDefaults(info system.Info) PlatformDefaults {
	volumesPath := filepath.Join(info.DockerRootDir, "volumes")
	containersPath := filepath.Join(info.DockerRootDir, "containers")

	switch info.OSType {
	case "linux":
		return PlatformDefaults{
			BaseImage:            "scratch",
			VolumesConfigPath:    toSlash(volumesPath),
			ContainerStoragePath: toSlash(containersPath),
		}
	case "windows":
		baseImage := "mcr.microsoft.com/windows/servercore:ltsc2022"
		if overrideBaseImage := os.Getenv("WINDOWS_BASE_IMAGE"); overrideBaseImage != "" {
			baseImage = overrideBaseImage
			if overrideBaseImageTag := os.Getenv("WINDOWS_BASE_IMAGE_TAG"); overrideBaseImageTag != "" {