package dockerclient_test import ( "path/filepath" "testing" "proxyctl/internal/dockerclient" "proxyctl/internal/profile" ) type identityEnvironment struct { current profile.UserIdentity users map[string]profile.UserIdentity env map[string]string } func (e identityEnvironment) CurrentUser() (profile.UserIdentity, error) { return e.current, nil } func (e identityEnvironment) LookupUser(name string) (profile.UserIdentity, error) { return e.users[name], nil } func (e identityEnvironment) LookupEnv(name string) (string, bool) { value, ok := e.env[name] return value, ok } func TestConfigurationUsesInvokingUserAndIgnoresSudoRootOverrides(t *testing.T) { owner := profile.UserIdentity{Username: "alice", HomeDir: "/home/alice", UID: 1000, GID: 1000} got, err := dockerclient.ResolveConfiguration(identityEnvironment{ current: profile.UserIdentity{Username: "root", HomeDir: "/root", UID: 0, GID: 0}, users: map[string]profile.UserIdentity{"alice": owner}, env: map[string]string{"SUDO_USER": "alice", "DOCKER_CONFIG": "/root/.docker", "XDG_STATE_HOME": "/root/state"}, }) if err != nil { t.Fatal(err) } if got.ConfigFile != "/home/alice/.docker/config.json" || got.RecoveryFile != "/home/alice/.local/state/proxyctl/docker-containers.json" || got.UID != 1000 || got.GID != 1000 || !got.OwnFiles { t.Fatalf("configuration = %#v", got) } } func TestConfigurationHonorsDirectOwnersAbsoluteDockerAndStateLocations(t *testing.T) { got, err := dockerclient.ResolveConfiguration(identityEnvironment{ current: profile.UserIdentity{Username: "root", HomeDir: "/root", UID: 0, GID: 0}, env: map[string]string{"DOCKER_CONFIG": "/var/lib/root-docker", "XDG_STATE_HOME": "/var/lib/root-state"}, }) if err != nil { t.Fatal(err) } if got.ConfigFile != filepath.Join("/var/lib/root-docker", "config.json") || got.RecoveryFile != filepath.Join("/var/lib/root-state", "proxyctl", "docker-containers.json") || got.UID != 0 { t.Fatalf("configuration = %#v", got) } }