testenv: Add DaemonAPIVersion helper

Allow tests to check the negotiated API version used by the client.

Can be used to skip tests based on API versions, for example:
```go
    skip.If(t, versions.LessThan(environment.DaemonAPIVersion(t), "1.44"))
```

will skip the test if the API version is older than 1.44

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski 2024-02-12 16:27:25 +01:00
parent d1b88930c3
commit 9831fea4db
No known key found for this signature in database
GPG Key ID: B85EFCFE26DEF92A
1 changed files with 11 additions and 0 deletions

View File

@ -108,3 +108,14 @@ func SkipIfNotPlatform(t *testing.T, platform string) {
daemonPlatform := strings.TrimSpace(result.Stdout())
skip.If(t, daemonPlatform != platform, "running against a non %s daemon", platform)
}
// DaemonAPIVersion returns the negotiated daemon API version.
func DaemonAPIVersion(t *testing.T) string {
t.Helper()
// Use Client.APIVersion instead of Server.APIVersion.
// The latter is the maximum version that the server supports
// while the Client.APIVersion contains the negotiated version.
result := icmd.RunCmd(icmd.Command("docker", "version", "--format", "{{.Client.APIVersion}}"))
result.Assert(t, icmd.Expected{Err: icmd.None})
return strings.TrimSpace(result.Stdout())
}