|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/docker/docker/client" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +// TestNewDockerClientVersionNegotiation verifies that newDockerClient allows |
| 12 | +// API version negotiation even when DOCKER_API_VERSION is set. |
| 13 | +// |
| 14 | +// This is a regression test for https://github.com/jesseduffield/lazydocker/issues/715 |
| 15 | +// where users got "client version 1.25 is too old" errors because FromEnv() |
| 16 | +// includes WithVersionFromEnv() which sets manualOverride=true, preventing |
| 17 | +// API version negotiation. |
| 18 | +func TestNewDockerClientVersionNegotiation(t *testing.T) { |
| 19 | + // Save original env var and restore after test |
| 20 | + originalAPIVersion := os.Getenv("DOCKER_API_VERSION") |
| 21 | + defer func() { |
| 22 | + if originalAPIVersion == "" { |
| 23 | + os.Unsetenv("DOCKER_API_VERSION") |
| 24 | + } else { |
| 25 | + os.Setenv("DOCKER_API_VERSION", originalAPIVersion) |
| 26 | + } |
| 27 | + }() |
| 28 | + |
| 29 | + // Set DOCKER_API_VERSION to an old version that would cause |
| 30 | + // "client version 1.25 is too old" errors if negotiation is disabled |
| 31 | + os.Setenv("DOCKER_API_VERSION", "1.25") |
| 32 | + |
| 33 | + t.Run("FromEnv locks version preventing negotiation", func(t *testing.T) { |
| 34 | + // This demonstrates the problematic behavior we're avoiding. |
| 35 | + // When using FromEnv with DOCKER_API_VERSION set, the client |
| 36 | + // version gets locked to 1.25 and negotiation is disabled. |
| 37 | + cli, err := client.NewClientWithOpts( |
| 38 | + client.FromEnv, |
| 39 | + client.WithAPIVersionNegotiation(), |
| 40 | + ) |
| 41 | + assert.NoError(t, err) |
| 42 | + defer cli.Close() |
| 43 | + |
| 44 | + // Version is locked to the env var value |
| 45 | + assert.Equal(t, "1.25", cli.ClientVersion()) |
| 46 | + }) |
| 47 | + |
| 48 | + t.Run("newDockerClient allows version negotiation", func(t *testing.T) { |
| 49 | + // Test the actual production function. |
| 50 | + // Use DefaultDockerHost for cross-platform compatibility |
| 51 | + // (unix socket on Linux/macOS, named pipe on Windows). |
| 52 | + cli, err := newDockerClient(client.DefaultDockerHost) |
| 53 | + assert.NoError(t, err) |
| 54 | + defer cli.Close() |
| 55 | + |
| 56 | + // Version is NOT locked to the env var value (1.25). |
| 57 | + // Instead, it uses the library's default version and will negotiate |
| 58 | + // with the server on first request. This is the key difference that |
| 59 | + // fixes the "version too old" error. |
| 60 | + assert.NotEqual(t, "1.25", cli.ClientVersion(), |
| 61 | + "client version should not be locked to DOCKER_API_VERSION env var") |
| 62 | + }) |
| 63 | +} |
0 commit comments