From abec931d98ba969c671a71b38e0cc1b60c15671d Mon Sep 17 00:00:00 2001 From: silverwind Date: Fri, 29 May 2026 16:47:10 +0000 Subject: [PATCH] fix: restore global docker config dir and socket env in tests (#1004) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `TestGetImagePullOptions` left docker/cli's process-global config dir pointed at `testdata/docker-pull-options` (which ships dummy `username:password` creds) via `config.SetDir`, without restoring it. Because that override is process-global, every later docker-gated test in the package then pulled with those creds — `TestDockerCopyToSymlinkPath`'s `alpine:latest` pull failed with `incorrect username or password` and broke CI. The workflow's `DOCKER_CONFIG` override can't mask this, since `SetDir` wins in-process. Restore `config.Dir()` with `t.Cleanup`, and isolate the socket tests' leaks of the exported `CommonSocketLocations` and `DOCKER_HOST` behind an `isolateSocketEnv` helper. Refs https://gitea.com/gitea/gitea.com/issues/83 --- This PR was written with the help of Claude Opus 4.8 Reviewed-on: https://gitea.com/gitea/runner/pulls/1004 Reviewed-by: Zettat123 <39446+zettat123@noreply.gitea.com> Reviewed-by: Lunny Xiao Co-authored-by: silverwind Co-committed-by: silverwind --- act/container/docker_pull_test.go | 3 +++ act/container/docker_socket_test.go | 21 +++++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/act/container/docker_pull_test.go b/act/container/docker_pull_test.go index 4a28c7fb..ae2f171f 100644 --- a/act/container/docker_pull_test.go +++ b/act/container/docker_pull_test.go @@ -40,6 +40,9 @@ func TestCleanImage(t *testing.T) { func TestGetImagePullOptions(t *testing.T) { ctx := context.Background() + orig := config.Dir() + t.Cleanup(func() { config.SetDir(orig) }) + config.SetDir("/non-existent/docker") options, err := getImagePullOptions(ctx, NewDockerPullExecutorInput{}) diff --git a/act/container/docker_socket_test.go b/act/container/docker_socket_test.go index a378dd32..44252f47 100644 --- a/act/container/docker_socket_test.go +++ b/act/container/docker_socket_test.go @@ -18,9 +18,19 @@ func init() { var originalCommonSocketLocations = CommonSocketLocations +func isolateSocketEnv(t *testing.T) { + t.Helper() + t.Cleanup(func() { CommonSocketLocations = originalCommonSocketLocations }) + if host, ok := os.LookupEnv("DOCKER_HOST"); ok { + t.Setenv("DOCKER_HOST", host) + } else { + t.Cleanup(func() { os.Unsetenv("DOCKER_HOST") }) + } +} + func TestGetSocketAndHostWithSocket(t *testing.T) { // Arrange - CommonSocketLocations = originalCommonSocketLocations + isolateSocketEnv(t) dockerHost := "unix:///my/docker/host.sock" socketURI := "/path/to/my.socket" t.Setenv("DOCKER_HOST", dockerHost) @@ -48,9 +58,9 @@ func TestGetSocketAndHostNoSocket(t *testing.T) { func TestGetSocketAndHostOnlySocket(t *testing.T) { // Arrange + isolateSocketEnv(t) socketURI := "/path/to/my.socket" os.Unsetenv("DOCKER_HOST") - CommonSocketLocations = originalCommonSocketLocations defaultSocket, defaultSocketFound := socketLocation() // Act @@ -65,7 +75,7 @@ func TestGetSocketAndHostOnlySocket(t *testing.T) { func TestGetSocketAndHostDontMount(t *testing.T) { // Arrange - CommonSocketLocations = originalCommonSocketLocations + isolateSocketEnv(t) dockerHost := "unix:///my/docker/host.sock" t.Setenv("DOCKER_HOST", dockerHost) @@ -79,7 +89,7 @@ func TestGetSocketAndHostDontMount(t *testing.T) { func TestGetSocketAndHostNoHostNoSocket(t *testing.T) { // Arrange - CommonSocketLocations = originalCommonSocketLocations + isolateSocketEnv(t) os.Unsetenv("DOCKER_HOST") defaultSocket, found := socketLocation() @@ -97,6 +107,7 @@ func TestGetSocketAndHostNoHostNoSocket(t *testing.T) { // > This happens if neither DOCKER_HOST nor --container-daemon-socket has a value, but socketLocation() returns a URI func TestGetSocketAndHostNoHostNoSocketDefaultLocation(t *testing.T) { // Arrange + isolateSocketEnv(t) mySocketFile, tmpErr := os.CreateTemp(t.TempDir(), "act-*.sock") mySocket := mySocketFile.Name() unixSocket := "unix://" + mySocket @@ -119,6 +130,7 @@ func TestGetSocketAndHostNoHostNoSocketDefaultLocation(t *testing.T) { func TestGetSocketAndHostNoHostInvalidSocket(t *testing.T) { // Arrange + isolateSocketEnv(t) os.Unsetenv("DOCKER_HOST") mySocket := "/my/socket/path.sock" CommonSocketLocations = []string{"/unusual", "/socket", "/location"} @@ -136,6 +148,7 @@ func TestGetSocketAndHostNoHostInvalidSocket(t *testing.T) { func TestGetSocketAndHostOnlySocketValidButUnusualLocation(t *testing.T) { // Arrange + isolateSocketEnv(t) socketURI := "unix:///path/to/my.socket" CommonSocketLocations = []string{"/unusual", "/location"} os.Unsetenv("DOCKER_HOST")