Files
act_runner/pkg/runner/action_test.go
Christopher Homberger a77f10683d assert => require
2026-02-22 21:00:42 +01:00

227 lines
4.8 KiB
Go

package runner
import (
"context"
"io"
"io/fs"
"strings"
"testing"
"gitea.com/gitea/act_runner/pkg/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
type closerMock struct {
mock.Mock
}
func (m *closerMock) Close() error {
m.Called()
return nil
}
func TestActionReader(t *testing.T) {
yaml := strings.ReplaceAll(`
name: 'name'
runs:
using: 'node16'
main: 'main.js'
`, "\t", " ")
table := []struct {
name string
step *model.Step
filename string
fileContent string
expected *model.Action
}{
{
name: "readActionYml",
step: &model.Step{},
filename: "action.yml",
fileContent: yaml,
expected: &model.Action{
Name: "name",
Runs: model.ActionRuns{
Using: "node16",
Main: "main.js",
PreIf: "always()",
PostIf: "always()",
},
},
},
{
name: "readActionYaml",
step: &model.Step{},
filename: "action.yaml",
fileContent: yaml,
expected: &model.Action{
Name: "name",
Runs: model.ActionRuns{
Using: "node16",
Main: "main.js",
PreIf: "always()",
PostIf: "always()",
},
},
},
{
name: "readDockerfile",
step: &model.Step{},
filename: "Dockerfile",
fileContent: "FROM ubuntu:20.04",
expected: &model.Action{
Name: "(Synthetic)",
Runs: model.ActionRuns{
Using: "docker",
Image: "Dockerfile",
},
},
},
}
for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
closerMock := &closerMock{}
readFile := func(filename string) (io.Reader, io.Closer, error) {
if tt.filename != filename {
return nil, nil, fs.ErrNotExist
}
return strings.NewReader(tt.fileContent), closerMock, nil
}
if tt.filename != "" {
closerMock.On("Close")
}
action, err := readActionImpl(context.Background(), tt.step, readFile, model.ActionConfig{})
require.NoError(t, err)
assert.Equal(t, tt.expected, action)
closerMock.AssertExpectations(t)
})
}
}
func TestActionRunner(t *testing.T) {
remoteAction := newRemoteAction("org/repo/path@ref")
table := []struct {
name string
step actionStep
expectedEnv map[string]string
}{
{
name: "with-input",
step: &stepActionRemote{
Step: &model.Step{
Uses: "org/repo/path@ref",
},
RunContext: &RunContext{
Config: &Config{},
Run: &model.Run{
JobID: "job",
Workflow: &model.Workflow{
Jobs: map[string]*model.Job{
"job": {
Name: "job",
},
},
},
},
nodeToolFullPath: "node",
},
action: &model.Action{
Inputs: map[string]model.Input{
"key": {
Default: "default value",
},
},
Runs: model.ActionRuns{
Using: "node16",
},
},
remoteAction: remoteAction,
env: map[string]string{},
},
expectedEnv: map[string]string{"INPUT_KEY": "default value"},
},
{
name: "restore-saved-state",
step: &stepActionRemote{
Step: &model.Step{
ID: "step",
Uses: "org/repo/path@ref",
},
RunContext: &RunContext{
ActionPath: "path",
Config: &Config{},
Run: &model.Run{
JobID: "job",
Workflow: &model.Workflow{
Jobs: map[string]*model.Job{
"job": {
Name: "job",
},
},
},
},
CurrentStep: "post-step",
StepResults: map[string]*model.StepResult{
"step": {},
},
IntraActionState: map[string]map[string]string{
"step": {
"name": "state value",
},
},
nodeToolFullPath: "node",
},
action: &model.Action{
Runs: model.ActionRuns{
Using: "node16",
},
},
remoteAction: remoteAction,
env: map[string]string{},
},
expectedEnv: map[string]string{"STATE_name": "state value"},
},
}
for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
cm := &containerMock{}
cm.Mock.On("CopyTarStream", ctx, "/var/run/act/actions/org-repo-path@ref/", mock.Anything).Return(nil)
cacheMock := &TestRepositoryCache{}
cacheMock.Mock.On("GetTarArchive", ctx, "", "", "").Return(io.NopCloser(io.MultiReader()))
envMatcher := mock.MatchedBy(func(env map[string]string) bool {
for k, v := range tt.expectedEnv {
if env[k] != v {
return false
}
}
return true
})
cm.On("Exec", []string{"node", "/var/run/act/actions/org-repo-path@ref/path"}, envMatcher, "", "").Return(func(_ context.Context) error { return nil })
tt.step.getRunContext().JobContainer = cm
tt.step.getRunContext().Config.ActionCache = cacheMock
err := runActionImpl(tt.step)(ctx)
require.NoError(t, err)
cm.AssertExpectations(t)
})
}
}