Repository: moby/moby
File: client/client_test.go (Go)

Code before:
	}
}

func TestNewClientWithOpsFromEnvSetsDefaultVersion(t *testing.T) {
	t.Setenv("DOCKER_HOST", "")
	t.Setenv("DOCKER_API_VERSION", "")
	t.Setenv("DOCKER_TLS_VERIFY", "")
	t.Setenv("DOCKER_CERT_PATH", "")

	client, err := NewClientWithOpts(FromEnv)
	assert.NilError(t, err)
	assert.Check(t, is.Equal(client.ClientVersion(), MaxAPIVersion))

	const expected = "1.50"
	t.Setenv("DOCKER_API_VERSION", expected)
	client, err = NewClientWithOpts(FromEnv)
	assert.NilError(t, err)
	assert.Check(t, is.Equal(client.ClientVersion(), expected))
}

// TestNegotiateAPIVersionEmpty asserts that client.Client version negotiation
// downgrades to the correct API version if the API's ping response does not
// return an API version.
func TestNegotiateAPIVersionEmpty(t *testing.T) {
	t.Setenv("DOCKER_API_VERSION", "")

	client, err := NewClientWithOpts(FromEnv)
	assert.NilError(t, err)

	// set our version to something new
	client.version = "1.51"

	// if no version from server, expect the earliest
	// version before APIVersion was implemented
	const expected = fallbackAPIVersion

	// test downgrade
	client.NegotiateAPIVersionPing(PingResult{})
	assert.Check(t, is.Equal(client.ClientVersion(), expected))
}

// TestNegotiateAPIVersion asserts that client.Client can
// negotiate a compatible APIVersion with the server
func TestNegotiateAPIVersion(t *testing.T) {
	tests := []struct {
		doc             string
		clientVersion   string
		pingVersion     string
		expectedVersion string
	}{
		{

Review comment (suggestion):
Various of these tests have some overlap as well (and some should likely be moved to `client/ping_test.go`, but also keeping that for a later exercise. 😅 

Code after:
	}
}

func TestNewClientWithOpsFromEnvSetsDefaultVersion(t *testing.T) {
	t.Setenv("DOCKER_HOST", "")
	t.Setenv("DOCKER_API_VERSION", "")
	t.Setenv("DOCKER_TLS_VERIFY", "")
	t.Setenv("DOCKER_CERT_PATH", "")

	client, err := NewClientWithOpts(FromEnv)
	assert.NilError(t, err)
	assert.Check(t, is.Equal(client.ClientVersion(), MaxAPIVersion))

	const expected = "1.50"
	t.Setenv("DOCKER_API_VERSION", expected)
	client, err = NewClientWithOpts(FromEnv)
	assert.NilError(t, err)
	assert.Check(t, is.Equal(client.ClientVersion(), expected))
}

// TestNegotiateAPIVersionEmpty asserts that client.Client version negotiation
// downgrades to the correct API version if the API's ping response does not
// return an API version.
func TestNegotiateAPIVersionEmpty(t *testing.T) {
	t.Setenv("DOCKER_API_VERSION", "")

	// if no version from server, expect the earliest
	// version before APIVersion was implemented
	const expected = fallbackAPIVersion

	client, err := NewClientWithOpts(FromEnv,
		WithAPIVersionNegotiation(),
		WithMockClient(mockResponse(http.StatusOK, http.Header{"Api-Version": []string{expected}}, "OK")),
	)
	assert.NilError(t, err)

	// set our version to something new.
	// we're not using [WithVersion] here, as that marks the version
	// as manually overridden.
	client.version = "1.51"

	// test downgrade
	ping, err := client.Ping(t.Context(), PingOptions{
		NegotiateAPIVersion: true,
	})
	assert.NilError(t, err)
	assert.Check(t, is.Equal(ping.APIVersion, expected))
	assert.Check(t, is.Equal(client.ClientVersion(), expected))
}

// TestNegotiateAPIVersion asserts that client.Client can