Files
act_runner/act/runner/step_docker_test.go
silverwind e71a5028b0 enhance: align job execution closer to GitHub Actions (#1421)
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>
2026-11-22 10:11:23 +00:00

146 lines
5.2 KiB
Go

// Copyright 2022 The Gitea Authors. All rights reserved.
// Copyright 2022 The nektos/act Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package runner
import (
"errors"
"slices"
"testing"
"gitea.com/gitea/runner/act/container"
"gitea.dev/actionslib/pkg/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yaml.in/yaml/v4"
)
func TestStepDockerMain(t *testing.T) {
cm := &containerMock{}
var input *container.NewContainerInput
origContainerNewContainer := ContainerNewContainer
ContainerNewContainer = func(containerInput *container.NewContainerInput) container.ExecutionsEnvironment {
input = containerInput
return cm
}
defer func() {
ContainerNewContainer = origContainerNewContainer
}()
ctx := t.Context()
sd := &stepDocker{
RunContext: &RunContext{
StepResults: map[string]*model.StepResult{},
Config: &Config{
PresetGitHubContext: &model.GithubContext{},
Secrets: map[string]string{
"DOCKER_USERNAME": "docker-user",
"DOCKER_PASSWORD": "docker-password",
},
},
Run: &model.Run{JobID: "1", Workflow: &model.Workflow{Jobs: map[string]*model.Job{"1": {}}}},
JobContainer: cm,
Env: map[string]string{"GITHUB_ACTION_REPOSITORY": "other/composite", "GITHUB_ACTION_REF": "v1"},
},
Step: &model.Step{
ID: "1",
Uses: "docker://node:14",
If: yaml.Node{Value: "github.action_repository == 'other/composite' && github.action_ref == 'v1'"},
},
}
require.NoError(t, sd.Step.RawWith.Encode(map[string]string{"args": "identity:${{ github.action_repository }}:${{ github.action_ref }}"}))
sd.RunContext.ExprEval = sd.RunContext.NewExpressionEvaluator(ctx)
cm.On("Pull", false).Return(noopExecutor)
cm.On("Remove").Return(noopExecutor)
cm.On("Create", []string(nil), []string(nil)).Return(noopExecutor)
cm.On("Start", true).Return(noopExecutor)
cm.On("Close").Return(noopExecutor)
expectStepFileCommands(t, cm)
require.NoError(t, sd.main()(ctx))
assert.Equal(t, "node:14", input.Image)
assert.Equal(t, []string{"identity::"}, input.Cmd)
assert.Contains(t, input.Env, "GITHUB_ACTION_REPOSITORY=")
assert.Contains(t, input.Env, "GITHUB_ACTION_REF=")
assert.Equal(t, "other/composite", sd.RunContext.Env["GITHUB_ACTION_REPOSITORY"])
assert.Equal(t, "v1", sd.RunContext.Env["GITHUB_ACTION_REF"])
assert.Empty(t, input.Username)
assert.Empty(t, input.Password)
assert.True(t, input.AutoRemove)
cm.AssertExpectations(t)
ContainerNewContainer = func(_input *container.NewContainerInput) container.ExecutionsEnvironment { return nil }
require.ErrorIs(t, sd.runUsesContainer(ctx), errors.ErrUnsupported)
}
func TestStepDockerNewStepContainer(t *testing.T) {
inheritedPath := (&container.LinuxContainerEnvironmentExtensions{}).DefaultPathVariable()
for _, test := range []struct {
name, platform, path, network string
allocPTY, keepsPath bool
}{
{name: "docker mode attaches to job container network", platform: "node:14", path: inheritedPath, network: "job-network", allocPTY: true, keepsPath: true},
{name: "host mode uses default network and the image's PATH", platform: "-self-hosted", path: inheritedPath, network: "default"},
{name: "host mode keeps a PATH that is not the runner's", platform: "-self-hosted", path: "/custom/bin", network: "default", keepsPath: true},
} {
t.Run(test.name, func(t *testing.T) {
job := &RunContext{
Config: &Config{AllocatePTY: test.allocPTY},
Run: &model.Run{Workflow: &model.Workflow{}},
jobNetworkName: "job-network",
}
step := &stepDocker{
RunContext: &RunContext{Parent: job, Config: job.Config, JobContainer: &containerMock{}, platformImage: test.platform},
Step: &model.Step{},
env: map[string]string{"PATH": test.path},
}
input := stepContainerInput(t.Context(), step, "alpine", nil, nil, "")
assert.Equal(t, test.network, input.NetworkMode)
assert.Equal(t, test.allocPTY, input.AllocatePTY)
assert.Equal(t, test.keepsPath, slices.Contains(input.Env, "PATH="+test.path))
})
}
}
func TestWindowsHostContainerActions(t *testing.T) {
for _, test := range []struct {
name, hostOS string
jobContainer container.ExecutionsEnvironment
wantError bool
}{
{name: "Windows host", hostOS: "windows", jobContainer: &container.HostEnvironment{}, wantError: true},
{name: "Linux host", hostOS: "linux", jobContainer: &container.HostEnvironment{}},
{name: "macOS host", hostOS: "darwin", jobContainer: &container.HostEnvironment{}},
{name: "Windows with Linux job container", hostOS: "windows", jobContainer: &containerMock{}},
} {
t.Run(test.name, func(t *testing.T) {
original := hostOS
hostOS = test.hostOS
t.Cleanup(func() { hostOS = original })
rc := &RunContext{JobContainer: test.jobContainer, kubernetes: true}
wantError := errKubernetesDocker.Error()
if test.wantError {
wantError = errWindowsHostContainer.Error()
}
t.Run("docker step", func(t *testing.T) {
require.EqualError(t, (&stepDocker{RunContext: rc}).runUsesContainer(t.Context()), wantError)
})
t.Run("container action", func(t *testing.T) {
require.EqualError(t, execAsDocker(t.Context(), &stepActionRemote{RunContext: rc}, stepStageMain, nil, "", ""), wantError)
})
})
}
}