mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
- relates to96b29f5a1f- similar to08e4e88482The daemon currently provides support for API versions all the way back to v1.24, which is the version of the API that shipped with docker 1.12.0 (released in 2016). Such old versions of the client are rare, and supporting older API versions has accumulated significant amounts of code to remain backward-compatible (which is largely untested, and a "best-effort" at most). This patch updates the minimum API version to v1.44, matching the minimum version of the client, and matching the API version of docker v25.0, which is the oldest supported version (through Mirantis MCR). The intent is to start deprecating older API versions when daemons implementing them reach EOL. This patch does not yet remove backward-compatibility code for older API versions, and the DOCKER_MIN_API_VERSION environment variable allows overriding the minimum version (to allow restoring the behavior from before this patch), however, API versions below v1.44 should be considered "best effort", and we may remove compatibility code to provide "degraded" support. With this patch the daemon defaults to API v1.44 as minimum: docker version Client: Version: 28.5.0 API version: 1.51 Go version: go1.24.7 Git commit: 887030f Built: Thu Oct 2 14:54:39 2025 OS/Arch: linux/arm64 Context: default Server: Engine: Version: dev API version: 1.52 (minimum version 1.44) .... Trying to use an older version of the API produces an error: DOCKER_API_VERSION=1.43 docker version Client: Version: 28.5.0 API version: 1.43 (downgraded from 1.51) Go version: go1.24.7 Git commit: 887030f Built: Thu Oct 2 14:54:39 2025 OS/Arch: linux/arm64 Context: default Error response from daemon: client version 1.43 is too old. Minimum supported API version is 1.44, please upgrade your client to a newer version To restore the previous minimum, users can start the daemon with the DOCKER_MIN_API_VERSION environment variable set: DOCKER_MIN_API_VERSION=1.24 dockerd API 1.24 is the oldest supported API version; docker version Client: Version: 28.5.0 API version: 1.24 (downgraded from 1.51) Go version: go1.24.7 Git commit: 887030f Built: Thu Oct 2 14:54:39 2025 OS/Arch: linux/arm64 Context: default Server: Engine: Version: dev API version: 1.52 (minimum version 1.24) .... When using the `DOCKER_MIN_API_VERSION` with a version of the API that is not supported, an error is produced when starting the daemon; DOCKER_MIN_API_VERSION=1.23 dockerd --validate invalid DOCKER_MIN_API_VERSION: minimum supported API version is 1.24: 1.23 DOCKER_MIN_API_VERSION=1.99 dockerd --validate invalid DOCKER_MIN_API_VERSION: maximum supported API version is 1.52: 1.99 Specifying a malformed API version also produces the same error; DOCKER_MIN_API_VERSION=hello dockerd --validate invalid DOCKER_MIN_API_VERSION: minimum supported API version is 1.24: hello Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
145 lines
4.4 KiB
Go
145 lines
4.4 KiB
Go
package daemon
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/moby/moby/client"
|
|
"github.com/moby/moby/client/pkg/stringid"
|
|
"github.com/moby/moby/v2/internal/testutil/daemon"
|
|
"github.com/pkg/errors"
|
|
"gotest.tools/v3/assert"
|
|
"gotest.tools/v3/icmd"
|
|
)
|
|
|
|
// Daemon represents a Docker daemon for the testing framework.
|
|
type Daemon struct {
|
|
*daemon.Daemon
|
|
dockerBinary string
|
|
}
|
|
|
|
// New returns a Daemon instance to be used for testing.
|
|
// This will create a directory such as d123456789 in the folder specified by $DOCKER_INTEGRATION_DAEMON_DEST or $DEST.
|
|
// The daemon will not automatically start.
|
|
func New(t testing.TB, dockerBinary string, dockerdBinary string, ops ...daemon.Option) *Daemon {
|
|
t.Helper()
|
|
ops = append(ops, daemon.WithDockerdBinary(dockerdBinary), daemon.WithEnvVars("DOCKER_MIN_API_VERSION=1.24"))
|
|
d := daemon.New(t, ops...)
|
|
return &Daemon{
|
|
Daemon: d,
|
|
dockerBinary: dockerBinary,
|
|
}
|
|
}
|
|
|
|
// Cmd executes a docker CLI command against this daemon.
|
|
// Example: d.Cmd("version") will run docker -H unix://path/to/unix.sock version
|
|
func (d *Daemon) Cmd(args ...string) (string, error) {
|
|
result := icmd.RunCmd(d.Command(args...))
|
|
return result.Combined(), result.Error
|
|
}
|
|
|
|
// Command creates a docker CLI command against this daemon, to be executed later.
|
|
// Example: d.Command("version") creates a command to run "docker -H unix://path/to/unix.sock version"
|
|
func (d *Daemon) Command(args ...string) icmd.Cmd {
|
|
return icmd.Command(d.dockerBinary, d.PrependHostArg(args)...)
|
|
}
|
|
|
|
// PrependHostArg prepend the specified arguments by the daemon host flags
|
|
func (d *Daemon) PrependHostArg(args []string) []string {
|
|
for _, arg := range args {
|
|
if arg == "--host" || arg == "-H" {
|
|
return args
|
|
}
|
|
}
|
|
return append([]string{"--host", d.Sock()}, args...)
|
|
}
|
|
|
|
// GetIDByName returns the ID of an object (container, volume, …) given its name
|
|
func (d *Daemon) GetIDByName(name string) (string, error) {
|
|
return d.inspectFieldWithError(name, "Id")
|
|
}
|
|
|
|
// InspectField returns the field filter by 'filter'
|
|
func (d *Daemon) InspectField(name, filter string) (string, error) {
|
|
return d.inspectFilter(name, filter)
|
|
}
|
|
|
|
func (d *Daemon) inspectFilter(name, filter string) (string, error) {
|
|
format := fmt.Sprintf("{{%s}}", filter)
|
|
out, err := d.Cmd("inspect", "-f", format, name)
|
|
if err != nil {
|
|
return "", errors.Errorf("failed to inspect %s: %s", name, out)
|
|
}
|
|
return strings.TrimSpace(out), nil
|
|
}
|
|
|
|
func (d *Daemon) inspectFieldWithError(name, field string) (string, error) {
|
|
return d.inspectFilter(name, "."+field)
|
|
}
|
|
|
|
// CheckActiveContainerCount returns the number of active containers
|
|
// FIXME(vdemeester) should re-use ActivateContainers in some way
|
|
func (d *Daemon) CheckActiveContainerCount(ctx context.Context) func(t *testing.T) (any, string) {
|
|
return func(t *testing.T) (any, string) {
|
|
t.Helper()
|
|
apiClient, err := client.NewClientWithOpts(client.FromEnv, client.WithHost(d.Sock()))
|
|
assert.NilError(t, err)
|
|
|
|
ctrs, err := apiClient.ContainerList(ctx, client.ContainerListOptions{})
|
|
_ = apiClient.Close()
|
|
assert.NilError(t, err)
|
|
var out strings.Builder
|
|
for _, ctr := range ctrs {
|
|
out.WriteString(stringid.TruncateID(ctr.ID) + "\n")
|
|
}
|
|
return len(ctrs), out.String()
|
|
}
|
|
}
|
|
|
|
// WaitRun waits for a container to be running for 10s
|
|
func (d *Daemon) WaitRun(contID string) error {
|
|
args := []string{"--host", d.Sock()}
|
|
return WaitInspectWithArgs(d.dockerBinary, contID, "{{.State.Running}}", "true", 10*time.Second, args...)
|
|
}
|
|
|
|
// WaitInspectWithArgs waits for the specified expression to be equals to the specified expected string in the given time.
|
|
// Deprecated: use cli.WaitCmd instead
|
|
func WaitInspectWithArgs(dockerBinary, name, expr, expected string, timeout time.Duration, arg ...string) error {
|
|
after := time.After(timeout)
|
|
|
|
args := append(arg, "inspect", "-f", expr, name)
|
|
for {
|
|
result := icmd.RunCommand(dockerBinary, args...)
|
|
if result.Error != nil {
|
|
if !strings.Contains(strings.ToLower(result.Stderr()), "no such") {
|
|
return errors.Errorf("error executing docker inspect: %v\n%s",
|
|
result.Stderr(), result.Stdout())
|
|
}
|
|
select {
|
|
case <-after:
|
|
return result.Error
|
|
default:
|
|
time.Sleep(10 * time.Millisecond)
|
|
continue
|
|
}
|
|
}
|
|
|
|
out := strings.TrimSpace(result.Stdout())
|
|
if out == expected {
|
|
break
|
|
}
|
|
|
|
select {
|
|
case <-after:
|
|
return errors.Errorf("condition \"%q == %q\" not true in time (%v)", out, expected, timeout)
|
|
default:
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
}
|
|
return nil
|
|
}
|