mirror of
https://gitea.com/gitea/act_runner
synced 2026-11-03 10:57:22 +01:00
The test context is cancelled when the test ends, so anything still holding it stops instead of leaking into the next test. Uses inside `t.Cleanup`, `defer` and `go func` blocks are left alone, as the context is already cancelled there. Stacked on https://gitea.com/gitea/runner/pulls/1305. Assisted-by: Codet Co-authored-by: bircni <bircni@icloud.com> Reviewed-on: https://gitea.com/gitea/runner/pulls/1306 Reviewed-by: bircni <bircni@icloud.com>
63 lines
1.9 KiB
Go
63 lines
1.9 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package runner
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"runtime"
|
|
"testing"
|
|
|
|
"gitea.com/gitea/runner/act/container"
|
|
|
|
mobyclient "github.com/moby/moby/client"
|
|
)
|
|
|
|
// requireLinuxDocker skips on non-Linux hosts. Some integration workflows need Docker features
|
|
// that only a Linux daemon provides (host networking, host /proc bind mounts); Docker Desktop
|
|
// on macOS/Windows does not, so those tests can only run on Linux.
|
|
func requireLinuxDocker(t *testing.T) {
|
|
t.Helper()
|
|
if runtime.GOOS != "linux" {
|
|
t.Skip("skipping: requires a Linux Docker host")
|
|
}
|
|
}
|
|
|
|
// requireDocker skips the test unless a reachable docker daemon is available.
|
|
// GetDockerClient succeeds even without a running daemon (its ping is best-effort),
|
|
// so the daemon has to be pinged explicitly here to decide whether to skip.
|
|
func requireDocker(t *testing.T) {
|
|
t.Helper()
|
|
ctx := t.Context()
|
|
cli, err := container.GetDockerClient(ctx)
|
|
if err != nil {
|
|
t.Skipf("skipping: docker client unavailable: %v", err)
|
|
}
|
|
defer cli.Close()
|
|
if _, err := cli.Ping(ctx, mobyclient.PingOptions{}); err != nil {
|
|
t.Skipf("skipping: docker daemon unreachable: %v", err)
|
|
}
|
|
}
|
|
|
|
func requireDockerSharingFiles(t *testing.T) {
|
|
t.Helper()
|
|
requireDocker(t)
|
|
for _, marker := range []string{"/.dockerenv", "/run/.containerenv"} {
|
|
if _, err := os.Stat(marker); err == nil {
|
|
t.Skip("skipping: inside a container, the docker daemon cannot bind-mount this process's files")
|
|
}
|
|
}
|
|
}
|
|
|
|
// requireHostTools skips the test unless every named executable is on PATH. Used by the
|
|
// self-hosted (host environment) suite, which runs steps directly on the host.
|
|
func requireHostTools(t *testing.T, tools ...string) {
|
|
t.Helper()
|
|
for _, tool := range tools {
|
|
if _, err := exec.LookPath(tool); err != nil {
|
|
t.Skipf("skipping: required host tool %q not found: %v", tool, err)
|
|
}
|
|
}
|
|
}
|