From 9831fea4db5d08c7e296ca356e6093e6de28dd48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Gronowski?= Date: Mon, 12 Feb 2024 16:27:25 +0100 Subject: [PATCH] testenv: Add DaemonAPIVersion helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- internal/test/environment/testenv.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/internal/test/environment/testenv.go b/internal/test/environment/testenv.go index 8b035fca1b..7fe6880244 100644 --- a/internal/test/environment/testenv.go +++ b/internal/test/environment/testenv.go @@ -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()) +}