|
| 1 | +package daemon |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/docker/cli/cli/config" |
| 10 | + "github.com/docker/cli/cli/context/docker" |
| 11 | + "github.com/docker/cli/cli/context/store" |
| 12 | + dockerclient "github.com/docker/docker/client" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +const testContextName = "test-context" |
| 18 | + |
| 19 | +// createTestContext creates a Docker context using docker/cli context store API |
| 20 | +func createTestContext(dockerConfigDir string) error { |
| 21 | + // Create context store with proper configuration |
| 22 | + cfg := store.NewConfig( |
| 23 | + func() any { return &store.Metadata{} }, |
| 24 | + store.EndpointTypeGetter(docker.DockerEndpoint, func() any { return &docker.EndpointMeta{} }), |
| 25 | + ) |
| 26 | + contextStore := store.New(dockerConfigDir, cfg) |
| 27 | + |
| 28 | + // Create context metadata |
| 29 | + contextMetadata := store.Metadata{ |
| 30 | + Name: testContextName, |
| 31 | + Endpoints: map[string]any{ |
| 32 | + docker.DockerEndpoint: docker.EndpointMeta{ |
| 33 | + Host: testContextHost, |
| 34 | + }, |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + // Create or update the context |
| 39 | + return contextStore.CreateOrUpdate(contextMetadata) |
| 40 | +} |
| 41 | + |
| 42 | +// TestResolveDockerHost tests Docker host resolution with various scenarios |
| 43 | +// It's challenging to test it through DockerImage due to the need for a Docker daemon, |
| 44 | +// so we test the resolveDockerHost function directly, although it's private. |
| 45 | +func TestResolveDockerHost(t *testing.T) { |
| 46 | + tests := []struct { |
| 47 | + name string |
| 48 | + hostFlag string |
| 49 | + hostEnv string |
| 50 | + contextEnv string |
| 51 | + currentContext string |
| 52 | + want string |
| 53 | + wantErr string |
| 54 | + }{ |
| 55 | + { |
| 56 | + name: "flag takes highest priority", |
| 57 | + hostFlag: testFlagHost, |
| 58 | + hostEnv: testEnvHost, |
| 59 | + contextEnv: "", |
| 60 | + currentContext: "", |
| 61 | + want: testFlagHost, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "DOCKER_HOST takes priority over context", |
| 65 | + hostFlag: "", |
| 66 | + hostEnv: testEnvHost, |
| 67 | + contextEnv: "", |
| 68 | + currentContext: "", |
| 69 | + want: testEnvHost, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "valid context is used", |
| 73 | + hostFlag: "", |
| 74 | + hostEnv: "", |
| 75 | + contextEnv: testContextName, |
| 76 | + currentContext: "", |
| 77 | + want: testContextHost, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "current context is used when no options", |
| 81 | + hostFlag: "", |
| 82 | + hostEnv: "", |
| 83 | + contextEnv: "", |
| 84 | + currentContext: testContextName, |
| 85 | + want: testContextHost, |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "default context uses default socket when no options", |
| 89 | + hostFlag: "", |
| 90 | + hostEnv: "", |
| 91 | + contextEnv: "", |
| 92 | + currentContext: "", |
| 93 | + want: dockerclient.DefaultDockerHost, |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "invalid context fails", |
| 97 | + hostFlag: "", |
| 98 | + hostEnv: "", |
| 99 | + contextEnv: "non-existent-context", |
| 100 | + currentContext: "", |
| 101 | + wantErr: "failed to create Docker API client", |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + for _, tt := range tests { |
| 106 | + t.Run(tt.name, func(t *testing.T) { |
| 107 | + // Create temporary Docker config directory |
| 108 | + testDir := t.TempDir() |
| 109 | + |
| 110 | + t.Setenv("DOCKER_CONFIG", testDir) |
| 111 | + t.Setenv("DOCKER_HOST", tt.hostEnv) |
| 112 | + t.Setenv("DOCKER_CONTEXT", tt.contextEnv) |
| 113 | + |
| 114 | + // Set the config directory for docker/cli to use |
| 115 | + // This is required to handle global state in docker/cli config. |
| 116 | + // Due to sync.Once in docker/cli, this cannot be fully cleaned up after tests. |
| 117 | + config.SetDir(testDir) |
| 118 | + |
| 119 | + // Always create a test context |
| 120 | + contextDir := filepath.Join(testDir, "contexts") |
| 121 | + |
| 122 | + err := createTestContext(contextDir) |
| 123 | + require.NoError(t, err) |
| 124 | + |
| 125 | + // Create config.json |
| 126 | + configData := map[string]any{ |
| 127 | + "currentContext": tt.currentContext, |
| 128 | + } |
| 129 | + |
| 130 | + configJSON, err := json.MarshalIndent(configData, "", " ") |
| 131 | + require.NoError(t, err) |
| 132 | + require.NoError(t, os.WriteFile(filepath.Join(testDir, "config.json"), configJSON, 0o644)) |
| 133 | + |
| 134 | + // Test resolveDockerHost |
| 135 | + got, err := resolveDockerHost(tt.hostFlag) |
| 136 | + if tt.wantErr != "" { |
| 137 | + assert.ErrorContains(t, err, tt.wantErr) |
| 138 | + return |
| 139 | + } |
| 140 | + |
| 141 | + require.NoError(t, err) |
| 142 | + assert.Equal(t, tt.want, got) |
| 143 | + }) |
| 144 | + } |
| 145 | +} |
0 commit comments