From 0041e2419a504a32b340de0b8f65870cb8542090 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 10 Apr 2019 00:54:16 +0200 Subject: [PATCH] Fix empty WithVersion blocking version negotiation commit 3d72963ab8fd43aab4e4387867f1b9ae99e61262 fixed situations where a version negotiation could override the version, even though a client was initialized with a fixed version. In situations where the "fixed" version is empty, we should ignore the option, and treat the client as "not having a fixed version", so that API version negotiation can still be performed. Signed-off-by: Sebastiaan van Stijn --- client/client_test.go | 20 ++++++++++++++++++++ client/options.go | 9 ++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/client/client_test.go b/client/client_test.go index 23ba3d36b6..56f6d8631c 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -265,6 +265,26 @@ func TestNegotiateAPVersionOverride(t *testing.T) { assert.Check(t, is.Equal(expected, client.version)) } +// TestNegotiateAPIVersionWithEmptyVersion asserts that initializing a client +// with an empty version string does still allow API-version negotiation +func TestNegotiateAPIVersionWithEmptyVersion(t *testing.T) { + client, err := NewClientWithOpts(WithVersion("")) + assert.NilError(t, err) + + client.NegotiateAPIVersionPing(types.Ping{APIVersion: "1.35"}) + assert.Equal(t, client.version, "1.35") +} + +// TestNegotiateAPIVersionWithFixedVersion asserts that initializing a client +// with an fixed version disables API-version negotiation +func TestNegotiateAPIVersionWithFixedVersion(t *testing.T) { + client, err := NewClientWithOpts(WithVersion("1.35")) + assert.NilError(t, err) + + client.NegotiateAPIVersionPing(types.Ping{APIVersion: "1.31"}) + assert.Equal(t, client.version, "1.35") +} + type roundTripFunc func(*http.Request) (*http.Response, error) func (rtf roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { diff --git a/client/options.go b/client/options.go index 752b6d19ba..33921867bf 100644 --- a/client/options.go +++ b/client/options.go @@ -139,11 +139,14 @@ func WithTLSClientConfig(cacertPath, certPath, keyPath string) Opt { } } -// WithVersion overrides the client version with the specified one +// WithVersion overrides the client version with the specified one. If an empty +// version is specified, the value will be ignored to allow version negotiation. func WithVersion(version string) Opt { return func(c *Client) error { - c.version = version - c.manualOverride = true + if version != "" { + c.version = version + c.manualOverride = true + } return nil } }