mirror of
https://gitea.com/gitea/act_runner.git
synced 2026-11-23 20:33:23 +08:00
Closes many small gaps between how workflows run on GitHub and on the runner, each checked against GitHub's runner and live runs. - `action.yml` expressions are checked against the contexts GitHub allows per field, and input defaults are evaluated with those contexts only - Reusable workflows take inputs from `workflow_call` only, evaluate outputs once after all called jobs finish, stop at 10 nested workflows and resolve relative `./` paths in remote workflows against that workflow's repository - A called job failing under `continue-on-error` no longer fails the caller, and same-repository calls use their own clone cache - Job timeouts cancel the job with GitHub's message, step timeouts fail the step with GitHub's message, and invalid `timeout-minutes` values behave like on GitHub - `runner.timeout` expiry names the setting and server cancellation is logged - Matrix and strategy evaluation errors fail the job - `exec` records skipped jobs and sets `github.run_attempt`, `workflow_ref` and `workflow_sha` - `runs-on` expressions keep their types - Step summaries upload as each step ends - Docker actions join the job network, and the job container no longer gets a network alias from the job name - Container, network and volume names are unique per job invocation - Cached images are matched against the daemon's platform and Dockerfile actions build for the requested platform - `runner.arch` maps 32-bit ARM and x86 names like GitHub - Pinned action SHAs are verified and git replace objects are ignored - `hashFiles` uses GitHub's current bundle - Registration trims labels and writes `.runner` as `0600` - The container entrypoint quotes its arguments and reads the registration token from the environment or a file - The systemd example uses `KillMode=mixed` so running jobs can finish on stop - The cache server releases its database between eviction batches - Shared rules come from actionslib v1.7.0, added in https://gitea.com/gitea/actionslib/pulls/34 _Written by Claude (Opus 5.5)._ Reviewed-on: https://gitea.com/gitea/runner/pulls/1421 Reviewed-by: bircni <bircni@icloud.com> Co-authored-by: silverwind <me@silverwind.io>
151 lines
6.1 KiB
Go
151 lines
6.1 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// Copyright 2020 The nektos/act Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows || netbsd || freebsd))
|
|
|
|
package container
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"testing/iotest"
|
|
|
|
"gitea.com/gitea/runner/act/common/logging"
|
|
"gitea.com/gitea/runner/act/common/logging/logtest"
|
|
|
|
"github.com/moby/moby/api/pkg/authconfig"
|
|
"github.com/moby/moby/api/types/registry"
|
|
assert "github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func init() {
|
|
logging.SetLevel(logging.LevelDebug)
|
|
}
|
|
|
|
func TestCleanImage(t *testing.T) {
|
|
tables := []struct {
|
|
imageIn string
|
|
imageOut string
|
|
}{
|
|
{"myhost.com/foo/bar", "myhost.com/foo/bar"},
|
|
{"localhost:8000/canonical/ubuntu", "localhost:8000/canonical/ubuntu"},
|
|
{"localhost/canonical/ubuntu:latest", "localhost/canonical/ubuntu:latest"},
|
|
{"localhost:8000/canonical/ubuntu:latest", "localhost:8000/canonical/ubuntu:latest"},
|
|
{"ubuntu", "docker.io/library/ubuntu"},
|
|
{"ubuntu:18.04", "docker.io/library/ubuntu:18.04"},
|
|
{"cibuilds/hugo:0.53", "docker.io/cibuilds/hugo:0.53"},
|
|
}
|
|
|
|
for _, table := range tables {
|
|
imageOut := cleanImage(t.Context(), table.imageIn)
|
|
assert.Equal(t, table.imageOut, imageOut)
|
|
}
|
|
}
|
|
|
|
func TestGetImagePullOptions(t *testing.T) {
|
|
ctx := t.Context()
|
|
|
|
t.Setenv("DOCKER_CONFIG", "/non-existent/docker")
|
|
|
|
options, err := getImagePullOptions(ctx, NewDockerPullExecutorInput{})
|
|
assert.NoError(t, err, "Failed to create ImagePullOptions") //nolint:testifylint // pre-existing issue from nektos/act
|
|
assert.Equal(t, "", options.RegistryAuth, "RegistryAuth should be empty if no username or password is set") //nolint:testifylint // pre-existing issue from nektos/act
|
|
|
|
options, err = getImagePullOptions(ctx, NewDockerPullExecutorInput{
|
|
Image: "",
|
|
Username: "username",
|
|
Password: "password",
|
|
})
|
|
assert.NoError(t, err, "Failed to create ImagePullOptions") //nolint:testifylint // pre-existing issue from nektos/act
|
|
assert.Equal(t, "eyJ1c2VybmFtZSI6InVzZXJuYW1lIiwicGFzc3dvcmQiOiJwYXNzd29yZCJ9", options.RegistryAuth, "Username and Password should be provided")
|
|
|
|
t.Setenv("DOCKER_CONFIG", "testdata/docker-pull-options")
|
|
|
|
options, err = getImagePullOptions(ctx, NewDockerPullExecutorInput{
|
|
Image: "nektos/act",
|
|
})
|
|
assert.NoError(t, err, "Failed to create ImagePullOptions") //nolint:testifylint // pre-existing issue from nektos/act
|
|
assert.Equal(t, "eyJ1c2VybmFtZSI6InVzZXJuYW1lIiwicGFzc3dvcmQiOiJwYXNzd29yZFxuIiwic2VydmVyYWRkcmVzcyI6Imh0dHBzOi8vaW5kZXguZG9ja2VyLmlvL3YxLyJ9", options.RegistryAuth, "RegistryAuth should be taken from local docker config")
|
|
|
|
dir := t.TempDir()
|
|
require.NoError(t, os.WriteFile(filepath.Join(dir, "config.json"), []byte(`{"auths":{"identity.example.com":{"identitytoken":"identity-token"},"registry.example.com":{"registrytoken":"registry-token"}}}`), 0o600))
|
|
t.Setenv("DOCKER_CONFIG", dir)
|
|
for host, want := range map[string]registry.AuthConfig{
|
|
"identity.example.com": {IdentityToken: "identity-token", ServerAddress: "identity.example.com"},
|
|
"registry.example.com": {RegistryToken: "registry-token", ServerAddress: "registry.example.com"},
|
|
} {
|
|
options, err := getImagePullOptions(ctx, NewDockerPullExecutorInput{Image: host + "/app"})
|
|
require.NoError(t, err)
|
|
require.NotEmpty(t, options.RegistryAuth, host)
|
|
got, err := authconfig.Decode(options.RegistryAuth)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, want, *got)
|
|
}
|
|
}
|
|
|
|
// A digest-pinned image is immutable, so its local copy is always current.
|
|
func TestIsPinnedImage(t *testing.T) {
|
|
assert.True(t, isPinnedImage("alpine@sha256:28bd5fe8b56d1bd048e5babf5b10710ebe0bae67db86916198a6eec434943f8b"))
|
|
assert.False(t, isPinnedImage("alpine:latest"))
|
|
}
|
|
|
|
func TestDockerPullFallsBackToLocalPlatform(t *testing.T) {
|
|
t.Setenv("DOCKER_API_VERSION", "1.49")
|
|
t.Setenv("DOCKER_CONFIG", t.TempDir())
|
|
t.Setenv("DOCKER_AUTH_CONFIG", "")
|
|
for _, image := range []string{"multiarch", "foreign", "no-version"} {
|
|
pulled := false
|
|
daemon := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
|
switch path.Base(request.URL.Path) {
|
|
case "version":
|
|
if image == "no-version" {
|
|
writer.WriteHeader(http.StatusForbidden)
|
|
}
|
|
_, _ = io.WriteString(writer, `{"Os":"linux","Arch":"s390x"}`)
|
|
case "json":
|
|
if platform := request.URL.Query().Get("platform"); platform == "" {
|
|
_, _ = io.WriteString(writer, `{"Os":"linux","Architecture":"arm64"}`)
|
|
} else if image == "multiarch" {
|
|
assert.JSONEq(t, `{"os":"linux","architecture":"s390x"}`, platform)
|
|
_, _ = io.WriteString(writer, `{"Os":"linux","Architecture":"s390x"}`)
|
|
} else {
|
|
writer.WriteHeader(http.StatusNotFound)
|
|
}
|
|
case "create":
|
|
pulled = true
|
|
writer.WriteHeader(http.StatusServiceUnavailable)
|
|
}
|
|
}))
|
|
t.Setenv("DOCKER_HOST", daemon.URL)
|
|
err := NewDockerPullExecutor(NewDockerPullExecutorInput{Image: image})(t.Context())
|
|
daemon.Close()
|
|
require.NoError(t, err, image)
|
|
assert.Equal(t, image == "foreign", pulled, image)
|
|
}
|
|
}
|
|
|
|
// The pull path reports a failure the daemon sent mid-stream, so it must carry the reason
|
|
// whichever of the two shapes the daemon used.
|
|
func TestLogDockerResponseError(t *testing.T) {
|
|
logger, hook := logtest.NewLogger()
|
|
streamErr := func(line string) error {
|
|
return logDockerResponse(logger, io.NopCloser(strings.NewReader(line)), logger.Infof)
|
|
}
|
|
require.EqualError(t, streamErr(`{"error":"toomanyrequests: rate limit exceeded"}`), "toomanyrequests: rate limit exceeded")
|
|
require.EqualError(t, streamErr(`{"errorDetail":{"message":"unexpected EOF"}}`), "unexpected EOF")
|
|
require.NoError(t, streamErr(`{"status":"Downloading"}`))
|
|
require.NoError(t, streamErr(`{"stream":"`+strings.Repeat("x", 70000)+`"}`))
|
|
assert.Equal(t, logging.LevelInfo, hook.LastRecord().Level)
|
|
require.ErrorIs(t, logDockerResponse(logger, io.NopCloser(io.MultiReader(
|
|
strings.NewReader(`{"status":"Downloading"}`+"\n"), iotest.ErrReader(io.ErrUnexpectedEOF),
|
|
)), logger.Infof), io.ErrUnexpectedEOF)
|
|
}
|