mirror of
https://gitea.com/gitea/act_runner.git
synced 2026-07-22 02:37:45 +00:00
fix: restore global docker config dir and socket env in tests (#1004)
`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 <xiaolunwen@gmail.com> Co-authored-by: silverwind <me@silverwind.io> Co-committed-by: silverwind <me@silverwind.io>
This commit is contained in:
@@ -40,6 +40,9 @@ func TestCleanImage(t *testing.T) {
|
|||||||
func TestGetImagePullOptions(t *testing.T) {
|
func TestGetImagePullOptions(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
|
orig := config.Dir()
|
||||||
|
t.Cleanup(func() { config.SetDir(orig) })
|
||||||
|
|
||||||
config.SetDir("/non-existent/docker")
|
config.SetDir("/non-existent/docker")
|
||||||
|
|
||||||
options, err := getImagePullOptions(ctx, NewDockerPullExecutorInput{})
|
options, err := getImagePullOptions(ctx, NewDockerPullExecutorInput{})
|
||||||
|
|||||||
@@ -18,9 +18,19 @@ func init() {
|
|||||||
|
|
||||||
var originalCommonSocketLocations = CommonSocketLocations
|
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) {
|
func TestGetSocketAndHostWithSocket(t *testing.T) {
|
||||||
// Arrange
|
// Arrange
|
||||||
CommonSocketLocations = originalCommonSocketLocations
|
isolateSocketEnv(t)
|
||||||
dockerHost := "unix:///my/docker/host.sock"
|
dockerHost := "unix:///my/docker/host.sock"
|
||||||
socketURI := "/path/to/my.socket"
|
socketURI := "/path/to/my.socket"
|
||||||
t.Setenv("DOCKER_HOST", dockerHost)
|
t.Setenv("DOCKER_HOST", dockerHost)
|
||||||
@@ -48,9 +58,9 @@ func TestGetSocketAndHostNoSocket(t *testing.T) {
|
|||||||
|
|
||||||
func TestGetSocketAndHostOnlySocket(t *testing.T) {
|
func TestGetSocketAndHostOnlySocket(t *testing.T) {
|
||||||
// Arrange
|
// Arrange
|
||||||
|
isolateSocketEnv(t)
|
||||||
socketURI := "/path/to/my.socket"
|
socketURI := "/path/to/my.socket"
|
||||||
os.Unsetenv("DOCKER_HOST")
|
os.Unsetenv("DOCKER_HOST")
|
||||||
CommonSocketLocations = originalCommonSocketLocations
|
|
||||||
defaultSocket, defaultSocketFound := socketLocation()
|
defaultSocket, defaultSocketFound := socketLocation()
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
@@ -65,7 +75,7 @@ func TestGetSocketAndHostOnlySocket(t *testing.T) {
|
|||||||
|
|
||||||
func TestGetSocketAndHostDontMount(t *testing.T) {
|
func TestGetSocketAndHostDontMount(t *testing.T) {
|
||||||
// Arrange
|
// Arrange
|
||||||
CommonSocketLocations = originalCommonSocketLocations
|
isolateSocketEnv(t)
|
||||||
dockerHost := "unix:///my/docker/host.sock"
|
dockerHost := "unix:///my/docker/host.sock"
|
||||||
t.Setenv("DOCKER_HOST", dockerHost)
|
t.Setenv("DOCKER_HOST", dockerHost)
|
||||||
|
|
||||||
@@ -79,7 +89,7 @@ func TestGetSocketAndHostDontMount(t *testing.T) {
|
|||||||
|
|
||||||
func TestGetSocketAndHostNoHostNoSocket(t *testing.T) {
|
func TestGetSocketAndHostNoHostNoSocket(t *testing.T) {
|
||||||
// Arrange
|
// Arrange
|
||||||
CommonSocketLocations = originalCommonSocketLocations
|
isolateSocketEnv(t)
|
||||||
os.Unsetenv("DOCKER_HOST")
|
os.Unsetenv("DOCKER_HOST")
|
||||||
defaultSocket, found := socketLocation()
|
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
|
// > This happens if neither DOCKER_HOST nor --container-daemon-socket has a value, but socketLocation() returns a URI
|
||||||
func TestGetSocketAndHostNoHostNoSocketDefaultLocation(t *testing.T) {
|
func TestGetSocketAndHostNoHostNoSocketDefaultLocation(t *testing.T) {
|
||||||
// Arrange
|
// Arrange
|
||||||
|
isolateSocketEnv(t)
|
||||||
mySocketFile, tmpErr := os.CreateTemp(t.TempDir(), "act-*.sock")
|
mySocketFile, tmpErr := os.CreateTemp(t.TempDir(), "act-*.sock")
|
||||||
mySocket := mySocketFile.Name()
|
mySocket := mySocketFile.Name()
|
||||||
unixSocket := "unix://" + mySocket
|
unixSocket := "unix://" + mySocket
|
||||||
@@ -119,6 +130,7 @@ func TestGetSocketAndHostNoHostNoSocketDefaultLocation(t *testing.T) {
|
|||||||
|
|
||||||
func TestGetSocketAndHostNoHostInvalidSocket(t *testing.T) {
|
func TestGetSocketAndHostNoHostInvalidSocket(t *testing.T) {
|
||||||
// Arrange
|
// Arrange
|
||||||
|
isolateSocketEnv(t)
|
||||||
os.Unsetenv("DOCKER_HOST")
|
os.Unsetenv("DOCKER_HOST")
|
||||||
mySocket := "/my/socket/path.sock"
|
mySocket := "/my/socket/path.sock"
|
||||||
CommonSocketLocations = []string{"/unusual", "/socket", "/location"}
|
CommonSocketLocations = []string{"/unusual", "/socket", "/location"}
|
||||||
@@ -136,6 +148,7 @@ func TestGetSocketAndHostNoHostInvalidSocket(t *testing.T) {
|
|||||||
|
|
||||||
func TestGetSocketAndHostOnlySocketValidButUnusualLocation(t *testing.T) {
|
func TestGetSocketAndHostOnlySocketValidButUnusualLocation(t *testing.T) {
|
||||||
// Arrange
|
// Arrange
|
||||||
|
isolateSocketEnv(t)
|
||||||
socketURI := "unix:///path/to/my.socket"
|
socketURI := "unix:///path/to/my.socket"
|
||||||
CommonSocketLocations = []string{"/unusual", "/location"}
|
CommonSocketLocations = []string{"/unusual", "/location"}
|
||||||
os.Unsetenv("DOCKER_HOST")
|
os.Unsetenv("DOCKER_HOST")
|
||||||
|
|||||||
Reference in New Issue
Block a user