Browse code

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 <github@gone.nl>

Sebastiaan van Stijn authored on 2019/04/10 07:54:16
Showing 2 changed files
... ...
@@ -265,6 +265,26 @@ func TestNegotiateAPVersionOverride(t *testing.T) {
265 265
 	assert.Check(t, is.Equal(expected, client.version))
266 266
 }
267 267
 
268
+// TestNegotiateAPIVersionWithEmptyVersion asserts that initializing a client
269
+// with an empty version string does still allow API-version negotiation
270
+func TestNegotiateAPIVersionWithEmptyVersion(t *testing.T) {
271
+	client, err := NewClientWithOpts(WithVersion(""))
272
+	assert.NilError(t, err)
273
+
274
+	client.NegotiateAPIVersionPing(types.Ping{APIVersion: "1.35"})
275
+	assert.Equal(t, client.version, "1.35")
276
+}
277
+
278
+// TestNegotiateAPIVersionWithFixedVersion asserts that initializing a client
279
+// with an fixed version disables API-version negotiation
280
+func TestNegotiateAPIVersionWithFixedVersion(t *testing.T) {
281
+	client, err := NewClientWithOpts(WithVersion("1.35"))
282
+	assert.NilError(t, err)
283
+
284
+	client.NegotiateAPIVersionPing(types.Ping{APIVersion: "1.31"})
285
+	assert.Equal(t, client.version, "1.35")
286
+}
287
+
268 288
 type roundTripFunc func(*http.Request) (*http.Response, error)
269 289
 
270 290
 func (rtf roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
... ...
@@ -139,11 +139,14 @@ func WithTLSClientConfig(cacertPath, certPath, keyPath string) Opt {
139 139
 	}
140 140
 }
141 141
 
142
-// WithVersion overrides the client version with the specified one
142
+// WithVersion overrides the client version with the specified one. If an empty
143
+// version is specified, the value will be ignored to allow version negotiation.
143 144
 func WithVersion(version string) Opt {
144 145
 	return func(c *Client) error {
145
-		c.version = version
146
-		c.manualOverride = true
146
+		if version != "" {
147
+			c.version = version
148
+			c.manualOverride = true
149
+		}
147 150
 		return nil
148 151
 	}
149 152
 }