@@ -252,18 +252,27 @@ func TestNewClientWithOpsFromEnvSetsDefaultVersion(t *testing.T) {
252252func TestNegotiateAPIVersionEmpty (t * testing.T ) {
253253 t .Setenv ("DOCKER_API_VERSION" , "" )
254254
255- client , err := NewClientWithOpts (FromEnv )
256- assert .NilError (t , err )
257-
258- // set our version to something new
259- client .version = "1.51"
260-
261255 // if no version from server, expect the earliest
262256 // version before APIVersion was implemented
263257 const expected = fallbackAPIVersion
264258
259+ client , err := NewClientWithOpts (FromEnv ,
260+ WithAPIVersionNegotiation (),
261+ WithMockClient (mockResponse (http .StatusOK , http.Header {"Api-Version" : []string {expected }}, "OK" )),
262+ )
263+ assert .NilError (t , err )
264+
265+ // set our version to something new.
266+ // we're not using [WithVersion] here, as that marks the version
267+ // as manually overridden.
268+ client .version = "1.51"
269+
265270 // test downgrade
266- client .NegotiateAPIVersionPing (PingResult {})
271+ ping , err := client .Ping (t .Context (), PingOptions {
272+ NegotiateAPIVersion : true ,
273+ })
274+ assert .NilError (t , err )
275+ assert .Check (t , is .Equal (ping .APIVersion , expected ))
267276 assert .Check (t , is .Equal (client .ClientVersion (), expected ))
268277}
269278
@@ -275,6 +284,7 @@ func TestNegotiateAPIVersion(t *testing.T) {
275284 clientVersion string
276285 pingVersion string
277286 expectedVersion string
287+ expectedErr string
278288 }{
279289 {
280290 // client should downgrade to the version reported by the daemon.
@@ -304,6 +314,7 @@ func TestNegotiateAPIVersion(t *testing.T) {
304314 doc : "no downgrade old" ,
305315 pingVersion : "1.19" ,
306316 expectedVersion : MaxAPIVersion ,
317+ expectedErr : "API version 1.19 is not supported by this client: the minimum supported API version is " + fallbackAPIVersion ,
307318 },
308319 {
309320 // client should not upgrade to a newer version if a version was set,
@@ -317,7 +328,12 @@ func TestNegotiateAPIVersion(t *testing.T) {
317328
318329 for _ , tc := range tests {
319330 t .Run (tc .doc , func (t * testing.T ) {
320- opts := make ([]Opt , 0 )
331+ opts := []Opt {
332+ FromEnv ,
333+ WithAPIVersionNegotiation (),
334+ WithMockClient (mockResponse (http .StatusOK , http.Header {"Api-Version" : []string {tc .pingVersion }}, "OK" )),
335+ }
336+
321337 if tc .clientVersion != "" {
322338 // Note that this check is redundant, as WithVersion() considers
323339 // an empty version equivalent to "not setting a version", but
@@ -326,7 +342,14 @@ func TestNegotiateAPIVersion(t *testing.T) {
326342 }
327343 client , err := NewClientWithOpts (opts ... )
328344 assert .NilError (t , err )
329- client .NegotiateAPIVersionPing (PingResult {APIVersion : tc .pingVersion })
345+ _ , err = client .Ping (t .Context (), PingOptions {
346+ NegotiateAPIVersion : true ,
347+ })
348+ if tc .expectedErr != "" {
349+ assert .Check (t , is .ErrorContains (err , tc .expectedErr ))
350+ } else {
351+ assert .NilError (t , err )
352+ }
330353 assert .Check (t , is .Equal (tc .expectedVersion , client .ClientVersion ()))
331354 })
332355 }
@@ -338,11 +361,16 @@ func TestNegotiateAPIVersionOverride(t *testing.T) {
338361 const expected = "9.99"
339362 t .Setenv ("DOCKER_API_VERSION" , expected )
340363
341- client , err := NewClientWithOpts (FromEnv )
364+ client , err := NewClientWithOpts (
365+ FromEnv ,
366+ WithMockClient (mockResponse (http .StatusOK , http.Header {"Api-Version" : []string {"1.45" }}, "OK" )),
367+ )
342368 assert .NilError (t , err )
343369
344370 // test that we honored the env var
345- client .NegotiateAPIVersionPing (PingResult {APIVersion : "1.24" })
371+ _ , err = client .Ping (t .Context (), PingOptions {
372+ NegotiateAPIVersion : true ,
373+ })
346374 assert .Check (t , is .Equal (client .ClientVersion (), expected ))
347375}
348376
@@ -353,9 +381,10 @@ func TestNegotiateAPIVersionConnectionFailure(t *testing.T) {
353381
354382 client , err := NewClientWithOpts (WithHost ("tcp://no-such-host.invalid" ))
355383 assert .NilError (t , err )
356-
357384 client .version = expected
358- client .NegotiateAPIVersion (context .Background ())
385+ _ , err = client .Ping (t .Context (), PingOptions {
386+ NegotiateAPIVersion : true ,
387+ })
359388 assert .Check (t , is .Equal (client .ClientVersion (), expected ))
360389}
361390
@@ -392,22 +421,34 @@ func TestNegotiateAPIVersionAutomatic(t *testing.T) {
392421// TestNegotiateAPIVersionWithEmptyVersion asserts that initializing a client
393422// with an empty version string does still allow API-version negotiation
394423func TestNegotiateAPIVersionWithEmptyVersion (t * testing.T ) {
395- client , err := NewClientWithOpts (WithVersion ("" ))
424+ client , err := NewClientWithOpts (
425+ WithVersion ("" ),
426+ WithMockClient (mockResponse (http .StatusOK , http.Header {"Api-Version" : []string {"1.50" }}, "OK" )),
427+ )
396428 assert .NilError (t , err )
397429
398430 const expected = "1.50"
399- client .NegotiateAPIVersionPing (PingResult {APIVersion : expected })
431+ _ , err = client .Ping (t .Context (), PingOptions {
432+ NegotiateAPIVersion : true ,
433+ })
400434 assert .Check (t , is .Equal (client .ClientVersion (), expected ))
401435}
402436
403437// TestNegotiateAPIVersionWithFixedVersion asserts that initializing a client
404438// with a fixed version disables API-version negotiation
405439func TestNegotiateAPIVersionWithFixedVersion (t * testing.T ) {
406440 const customVersion = "1.50"
407- client , err := NewClientWithOpts (WithVersion (customVersion ))
441+ client , err := NewClientWithOpts (
442+ WithVersion (customVersion ),
443+ WithMockClient (mockResponse (http .StatusOK , http.Header {"Api-Version" : []string {"1.49" }}, "OK" )),
444+ )
408445 assert .NilError (t , err )
409446
410- client .NegotiateAPIVersionPing (PingResult {APIVersion : "1.49" })
447+ _ , err = client .Ping (t .Context (), PingOptions {
448+ NegotiateAPIVersion : true ,
449+ ForceNegotiate : true ,
450+ })
451+ assert .NilError (t , err )
411452 assert .Check (t , is .Equal (client .ClientVersion (), customVersion ))
412453}
413454
0 commit comments