2022-03-22 16:13:00 -05:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
2022-12-09 05:16:15 -06:00
|
|
|
"bytes"
|
2022-03-22 16:13:00 -05:00
|
|
|
"context"
|
2022-12-09 05:16:15 -06:00
|
|
|
"io"
|
2022-11-16 15:42:57 -06:00
|
|
|
"path/filepath"
|
2022-05-24 08:36:06 -05:00
|
|
|
"strings"
|
2022-03-22 16:13:00 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nektos/act/pkg/common"
|
|
|
|
"github.com/nektos/act/pkg/model"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/mock"
|
2022-05-24 08:36:06 -05:00
|
|
|
"gopkg.in/yaml.v3"
|
2022-03-22 16:13:00 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepActionLocalMocks struct {
|
|
|
|
mock.Mock
|
|
|
|
}
|
|
|
|
|
2022-03-29 12:42:11 -05:00
|
|
|
func (salm *stepActionLocalMocks) runAction(step actionStep, actionDir string, remoteAction *remoteAction) common.Executor {
|
|
|
|
args := salm.Called(step, actionDir, remoteAction)
|
2022-03-22 16:13:00 -05:00
|
|
|
return args.Get(0).(func(context.Context) error)
|
|
|
|
}
|
|
|
|
|
2023-07-10 19:12:12 -05:00
|
|
|
func (salm *stepActionLocalMocks) readAction(_ context.Context, step *model.Step, actionDir string, actionPath string, readFile actionYamlReader, writeFile fileWriter) (*model.Action, error) {
|
2022-03-22 16:13:00 -05:00
|
|
|
args := salm.Called(step, actionDir, actionPath, readFile, writeFile)
|
|
|
|
return args.Get(0).(*model.Action), args.Error(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepActionLocalTest(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
cm := &containerMock{}
|
|
|
|
salm := &stepActionLocalMocks{}
|
|
|
|
|
|
|
|
sal := &stepActionLocal{
|
|
|
|
readAction: salm.readAction,
|
|
|
|
runAction: salm.runAction,
|
|
|
|
RunContext: &RunContext{
|
|
|
|
StepResults: map[string]*model.StepResult{},
|
|
|
|
ExprEval: &expressionEvaluator{},
|
|
|
|
Config: &Config{
|
|
|
|
Workdir: "/tmp",
|
|
|
|
},
|
|
|
|
Run: &model.Run{
|
|
|
|
JobID: "1",
|
|
|
|
Workflow: &model.Workflow{
|
|
|
|
Jobs: map[string]*model.Job{
|
|
|
|
"1": {
|
|
|
|
Defaults: model.Defaults{
|
|
|
|
Run: model.RunDefaults{
|
|
|
|
Shell: "bash",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
JobContainer: cm,
|
|
|
|
},
|
|
|
|
Step: &model.Step{
|
|
|
|
ID: "1",
|
|
|
|
Uses: "./path/to/action",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-11-16 15:42:57 -06:00
|
|
|
salm.On("readAction", sal.Step, filepath.Clean("/tmp/path/to/action"), "", mock.Anything, mock.Anything).
|
2022-03-22 16:13:00 -05:00
|
|
|
Return(&model.Action{}, nil)
|
|
|
|
|
2023-02-04 07:35:13 -06:00
|
|
|
cm.On("Copy", "/var/run/act", mock.AnythingOfType("[]*container.FileEntry")).Return(func(ctx context.Context) error {
|
2022-03-22 16:13:00 -05:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
cm.On("UpdateFromEnv", "/var/run/act/workflow/envs.txt", mock.AnythingOfType("*map[string]string")).Return(func(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-11-16 15:42:57 -06:00
|
|
|
cm.On("UpdateFromEnv", "/var/run/act/workflow/statecmd.txt", mock.AnythingOfType("*map[string]string")).Return(func(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
cm.On("UpdateFromEnv", "/var/run/act/workflow/outputcmd.txt", mock.AnythingOfType("*map[string]string")).Return(func(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-12-09 05:16:15 -06:00
|
|
|
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/pathcmd.txt").Return(io.NopCloser(&bytes.Buffer{}), nil)
|
|
|
|
|
2022-11-16 15:42:57 -06:00
|
|
|
salm.On("runAction", sal, filepath.Clean("/tmp/path/to/action"), (*remoteAction)(nil)).Return(func(ctx context.Context) error {
|
2022-03-22 16:13:00 -05:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-05-24 08:36:06 -05:00
|
|
|
err := sal.pre()(ctx)
|
|
|
|
assert.Nil(t, err)
|
2022-03-22 16:13:00 -05:00
|
|
|
|
2022-05-24 08:36:06 -05:00
|
|
|
err = sal.main()(ctx)
|
2022-03-22 16:13:00 -05:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
cm.AssertExpectations(t)
|
|
|
|
salm.AssertExpectations(t)
|
|
|
|
}
|
|
|
|
|
2022-05-24 08:36:06 -05:00
|
|
|
func TestStepActionLocalPost(t *testing.T) {
|
|
|
|
table := []struct {
|
2022-12-15 11:08:31 -06:00
|
|
|
name string
|
|
|
|
stepModel *model.Step
|
|
|
|
actionModel *model.Action
|
|
|
|
initialStepResults map[string]*model.StepResult
|
|
|
|
err error
|
|
|
|
mocks struct {
|
2022-05-24 08:36:06 -05:00
|
|
|
env bool
|
|
|
|
exec bool
|
|
|
|
}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "main-success",
|
|
|
|
stepModel: &model.Step{
|
|
|
|
ID: "step",
|
|
|
|
Uses: "./local/action",
|
|
|
|
},
|
|
|
|
actionModel: &model.Action{
|
|
|
|
Runs: model.ActionRuns{
|
|
|
|
Using: "node16",
|
|
|
|
Post: "post.js",
|
|
|
|
PostIf: "always()",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
initialStepResults: map[string]*model.StepResult{
|
|
|
|
"step": {
|
|
|
|
Conclusion: model.StepStatusSuccess,
|
|
|
|
Outcome: model.StepStatusSuccess,
|
|
|
|
Outputs: map[string]string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
mocks: struct {
|
|
|
|
env bool
|
|
|
|
exec bool
|
|
|
|
}{
|
|
|
|
env: true,
|
|
|
|
exec: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "main-failed",
|
|
|
|
stepModel: &model.Step{
|
|
|
|
ID: "step",
|
|
|
|
Uses: "./local/action",
|
|
|
|
},
|
|
|
|
actionModel: &model.Action{
|
|
|
|
Runs: model.ActionRuns{
|
|
|
|
Using: "node16",
|
|
|
|
Post: "post.js",
|
|
|
|
PostIf: "always()",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
initialStepResults: map[string]*model.StepResult{
|
|
|
|
"step": {
|
|
|
|
Conclusion: model.StepStatusFailure,
|
|
|
|
Outcome: model.StepStatusFailure,
|
|
|
|
Outputs: map[string]string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
mocks: struct {
|
|
|
|
env bool
|
|
|
|
exec bool
|
|
|
|
}{
|
|
|
|
env: true,
|
|
|
|
exec: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "skip-if-failed",
|
|
|
|
stepModel: &model.Step{
|
|
|
|
ID: "step",
|
|
|
|
Uses: "./local/action",
|
|
|
|
},
|
|
|
|
actionModel: &model.Action{
|
|
|
|
Runs: model.ActionRuns{
|
|
|
|
Using: "node16",
|
|
|
|
Post: "post.js",
|
|
|
|
PostIf: "success()",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
initialStepResults: map[string]*model.StepResult{
|
|
|
|
"step": {
|
|
|
|
Conclusion: model.StepStatusFailure,
|
|
|
|
Outcome: model.StepStatusFailure,
|
|
|
|
Outputs: map[string]string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
mocks: struct {
|
|
|
|
env bool
|
|
|
|
exec bool
|
|
|
|
}{
|
2023-02-04 07:35:13 -06:00
|
|
|
env: false,
|
2022-05-24 08:36:06 -05:00
|
|
|
exec: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "skip-if-main-skipped",
|
|
|
|
stepModel: &model.Step{
|
|
|
|
ID: "step",
|
|
|
|
If: yaml.Node{Value: "failure()"},
|
|
|
|
Uses: "./local/action",
|
|
|
|
},
|
|
|
|
actionModel: &model.Action{
|
|
|
|
Runs: model.ActionRuns{
|
|
|
|
Using: "node16",
|
|
|
|
Post: "post.js",
|
|
|
|
PostIf: "always()",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
initialStepResults: map[string]*model.StepResult{
|
|
|
|
"step": {
|
|
|
|
Conclusion: model.StepStatusSkipped,
|
|
|
|
Outcome: model.StepStatusSkipped,
|
|
|
|
Outputs: map[string]string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
mocks: struct {
|
|
|
|
env bool
|
|
|
|
exec bool
|
|
|
|
}{
|
|
|
|
env: false,
|
|
|
|
exec: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range table {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
cm := &containerMock{}
|
|
|
|
|
|
|
|
sal := &stepActionLocal{
|
|
|
|
env: map[string]string{},
|
|
|
|
RunContext: &RunContext{
|
|
|
|
Config: &Config{
|
|
|
|
GitHubInstance: "https://github.com",
|
|
|
|
},
|
|
|
|
JobContainer: cm,
|
|
|
|
Run: &model.Run{
|
|
|
|
JobID: "1",
|
|
|
|
Workflow: &model.Workflow{
|
|
|
|
Jobs: map[string]*model.Job{
|
|
|
|
"1": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
StepResults: tt.initialStepResults,
|
|
|
|
},
|
|
|
|
Step: tt.stepModel,
|
|
|
|
action: tt.actionModel,
|
|
|
|
}
|
2022-11-16 15:55:23 -06:00
|
|
|
sal.RunContext.ExprEval = sal.RunContext.NewExpressionEvaluator(ctx)
|
2022-05-24 08:36:06 -05:00
|
|
|
|
|
|
|
if tt.mocks.exec {
|
|
|
|
suffixMatcher := func(suffix string) interface{} {
|
|
|
|
return mock.MatchedBy(func(array []string) bool {
|
|
|
|
return strings.HasSuffix(array[1], suffix)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
cm.On("Exec", suffixMatcher("pkg/runner/local/action/post.js"), sal.env, "", "").Return(func(ctx context.Context) error { return tt.err })
|
2022-11-16 15:42:57 -06:00
|
|
|
|
|
|
|
cm.On("Copy", "/var/run/act", mock.AnythingOfType("[]*container.FileEntry")).Return(func(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2023-02-04 07:35:13 -06:00
|
|
|
cm.On("UpdateFromEnv", "/var/run/act/workflow/envs.txt", mock.AnythingOfType("*map[string]string")).Return(func(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-11-16 15:42:57 -06:00
|
|
|
cm.On("UpdateFromEnv", "/var/run/act/workflow/statecmd.txt", mock.AnythingOfType("*map[string]string")).Return(func(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
cm.On("UpdateFromEnv", "/var/run/act/workflow/outputcmd.txt", mock.AnythingOfType("*map[string]string")).Return(func(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
})
|
2022-12-09 05:16:15 -06:00
|
|
|
|
|
|
|
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/pathcmd.txt").Return(io.NopCloser(&bytes.Buffer{}), nil)
|
2022-05-24 08:36:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
err := sal.post()(ctx)
|
|
|
|
|
|
|
|
assert.Equal(t, tt.err, err)
|
2022-12-15 11:08:31 -06:00
|
|
|
assert.Equal(t, sal.RunContext.StepResults["post-step"], (*model.StepResult)(nil))
|
2022-05-24 08:36:06 -05:00
|
|
|
cm.AssertExpectations(t)
|
|
|
|
})
|
|
|
|
}
|
2022-03-22 16:13:00 -05:00
|
|
|
}
|