2020-02-04 18:38:41 -06:00
package runner
2019-01-31 01:53:39 -06:00
import (
"context"
2020-02-10 17:27:05 -06:00
"fmt"
2021-05-04 16:50:35 -05:00
"os"
2020-02-24 12:56:49 -06:00
"path/filepath"
2021-05-04 16:50:35 -05:00
"runtime"
"strings"
2019-01-31 01:53:39 -06:00
"testing"
2020-03-06 14:30:24 -06:00
"github.com/joho/godotenv"
2019-01-31 01:53:39 -06:00
log "github.com/sirupsen/logrus"
2021-05-18 01:14:49 -05:00
assert "github.com/stretchr/testify/assert"
2021-03-28 23:08:40 -05:00
"github.com/nektos/act/pkg/model"
2019-01-31 01:53:39 -06:00
)
2021-12-22 13:34:18 -06:00
var baseImage = "node:16-buster-slim"
2021-06-10 18:12:05 -05:00
func init ( ) {
if p := os . Getenv ( "ACT_TEST_IMAGE" ) ; p != "" {
baseImage = p
}
}
2019-04-08 06:01:49 -05:00
func TestGraphEvent ( t * testing . T ) {
2021-05-03 09:57:24 -05:00
planner , err := model . NewWorkflowPlanner ( "testdata/basic" , true )
2021-05-18 01:14:49 -05:00
assert . Nil ( t , err )
2019-04-08 06:01:49 -05:00
2020-02-10 17:27:05 -06:00
plan := planner . PlanEvent ( "push" )
2021-05-18 01:14:49 -05:00
assert . Nil ( t , err )
2020-02-19 21:20:47 -06:00
assert . Equal ( t , len ( plan . Stages ) , 3 , "stages" )
2020-02-10 17:27:05 -06:00
assert . Equal ( t , len ( plan . Stages [ 0 ] . Runs ) , 1 , "stage0.runs" )
assert . Equal ( t , len ( plan . Stages [ 1 ] . Runs ) , 1 , "stage1.runs" )
2020-02-19 21:20:47 -06:00
assert . Equal ( t , len ( plan . Stages [ 2 ] . Runs ) , 1 , "stage2.runs" )
assert . Equal ( t , plan . Stages [ 0 ] . Runs [ 0 ] . JobID , "check" , "jobid" )
assert . Equal ( t , plan . Stages [ 1 ] . Runs [ 0 ] . JobID , "build" , "jobid" )
assert . Equal ( t , plan . Stages [ 2 ] . Runs [ 0 ] . JobID , "test" , "jobid" )
2019-04-08 06:01:49 -05:00
2020-02-10 17:27:05 -06:00
plan = planner . PlanEvent ( "release" )
assert . Equal ( t , len ( plan . Stages ) , 0 , "stages" )
2019-04-08 06:01:49 -05:00
}
2020-11-29 23:45:11 -06:00
type TestJobFileInfo struct {
2021-03-28 23:08:40 -05:00
workdir string
workflowPath string
eventName string
errorMessage string
platforms map [ string ] string
containerArchitecture string
2020-11-29 23:45:11 -06:00
}
2021-05-10 10:12:57 -05:00
func runTestJobFile ( ctx context . Context , t * testing . T , tjfi TestJobFileInfo ) {
2020-11-29 23:45:11 -06:00
t . Run ( tjfi . workflowPath , func ( t * testing . T ) {
workdir , err := filepath . Abs ( tjfi . workdir )
2021-05-18 01:14:49 -05:00
assert . Nil ( t , err , workdir )
2020-11-29 23:45:11 -06:00
fullWorkflowPath := filepath . Join ( workdir , tjfi . workflowPath )
runnerConfig := & Config {
2021-03-28 23:08:40 -05:00
Workdir : workdir ,
2021-05-04 16:50:35 -05:00
BindWorkdir : false ,
2021-03-28 23:08:40 -05:00
EventName : tjfi . eventName ,
Platforms : tjfi . platforms ,
ReuseContainers : false ,
ContainerArchitecture : tjfi . containerArchitecture ,
2021-05-05 11:42:34 -05:00
GitHubInstance : "github.com" ,
2020-11-29 23:45:11 -06:00
}
2021-05-04 16:50:35 -05:00
2020-11-29 23:45:11 -06:00
runner , err := New ( runnerConfig )
2021-05-18 01:14:49 -05:00
assert . Nil ( t , err , tjfi . workflowPath )
2020-11-29 23:45:11 -06:00
2021-05-03 09:57:24 -05:00
planner , err := model . NewWorkflowPlanner ( fullWorkflowPath , true )
2021-05-18 01:14:49 -05:00
assert . Nil ( t , err , fullWorkflowPath )
2020-11-29 23:45:11 -06:00
plan := planner . PlanEvent ( tjfi . eventName )
err = runner . NewPlanExecutor ( plan ) ( ctx )
if tjfi . errorMessage == "" {
2021-05-18 01:14:49 -05:00
assert . Nil ( t , err , fullWorkflowPath )
2020-11-29 23:45:11 -06:00
} else {
2021-05-18 01:14:49 -05:00
assert . Error ( t , err , tjfi . errorMessage )
2020-11-29 23:45:11 -06:00
}
} )
}
2019-01-31 01:53:39 -06:00
func TestRunEvent ( t * testing . T ) {
2019-02-07 11:09:19 -06:00
if testing . Short ( ) {
t . Skip ( "skipping integration test" )
}
2020-11-29 23:45:11 -06:00
platforms := map [ string ] string {
2021-06-10 18:12:05 -05:00
"ubuntu-latest" : baseImage ,
2020-11-29 23:45:11 -06:00
}
2021-05-05 00:57:33 -05:00
2020-11-29 23:45:11 -06:00
tables := [ ] TestJobFileInfo {
2021-05-02 10:15:13 -05:00
{ "testdata" , "basic" , "push" , "" , platforms , "" } ,
{ "testdata" , "fail" , "push" , "exit with `FAILURE`: 1" , platforms , "" } ,
{ "testdata" , "runs-on" , "push" , "" , platforms , "" } ,
2021-05-10 10:12:57 -05:00
{ "testdata" , "checkout" , "push" , "" , platforms , "" } ,
2022-01-27 10:20:44 -06:00
{ "testdata" , "non-existent-action" , "push" , "Job 'nopanic' failed" , platforms , "" } ,
2021-11-03 13:47:47 -05:00
{ "testdata" , "shells/defaults" , "push" , "" , platforms , "" } ,
2021-12-22 00:37:16 -06:00
// TODO: figure out why it fails
// {"testdata", "shells/custom", "push", "", map[string]string{"ubuntu-latest": "ghcr.io/justingrote/act-pwsh:latest"}, ""}, // custom image with pwsh
2021-06-10 18:12:05 -05:00
{ "testdata" , "shells/pwsh" , "push" , "" , map [ string ] string { "ubuntu-latest" : "ghcr.io/justingrote/act-pwsh:latest" } , "" } , // custom image with pwsh
{ "testdata" , "shells/bash" , "push" , "" , platforms , "" } ,
2021-12-22 13:34:18 -06:00
{ "testdata" , "shells/python" , "push" , "" , map [ string ] string { "ubuntu-latest" : "node:16-buster" } , "" } , // slim doesn't have python
2021-06-10 18:12:05 -05:00
{ "testdata" , "shells/sh" , "push" , "" , platforms , "" } ,
2021-05-02 10:15:13 -05:00
{ "testdata" , "job-container" , "push" , "" , platforms , "" } ,
{ "testdata" , "job-container-non-root" , "push" , "" , platforms , "" } ,
2021-09-10 00:03:40 -05:00
{ "testdata" , "container-hostname" , "push" , "" , platforms , "" } ,
2021-05-02 10:15:13 -05:00
{ "testdata" , "uses-docker-url" , "push" , "" , platforms , "" } ,
{ "testdata" , "remote-action-docker" , "push" , "" , platforms , "" } ,
{ "testdata" , "remote-action-js" , "push" , "" , platforms , "" } ,
{ "testdata" , "local-action-docker-url" , "push" , "" , platforms , "" } ,
{ "testdata" , "local-action-dockerfile" , "push" , "" , platforms , "" } ,
2021-12-22 13:19:50 -06:00
{ "testdata" , "local-action-via-composite-dockerfile" , "push" , "" , platforms , "" } ,
2021-05-02 10:15:13 -05:00
{ "testdata" , "local-action-js" , "push" , "" , platforms , "" } ,
{ "testdata" , "matrix" , "push" , "" , platforms , "" } ,
{ "testdata" , "matrix-include-exclude" , "push" , "" , platforms , "" } ,
{ "testdata" , "commands" , "push" , "" , platforms , "" } ,
{ "testdata" , "workdir" , "push" , "" , platforms , "" } ,
{ "testdata" , "defaults-run" , "push" , "" , platforms , "" } ,
{ "testdata" , "uses-composite" , "push" , "" , platforms , "" } ,
2021-12-22 13:19:50 -06:00
{ "testdata" , "uses-composite-with-error" , "push" , "Job 'failing-composite-action' failed" , platforms , "" } ,
{ "testdata" , "uses-nested-composite" , "push" , "" , platforms , "" } ,
2022-03-30 12:20:45 -05:00
{ "testdata" , "uses-workflow" , "push" , "reusable workflows are currently not supported (see https://github.com/nektos/act/issues/826 for updates)" , platforms , "" } ,
2022-01-21 10:08:30 -06:00
{ "testdata" , "composite-fail-with-output" , "push" , "" , platforms , "" } ,
2021-05-03 16:57:46 -05:00
{ "testdata" , "issue-597" , "push" , "" , platforms , "" } ,
2021-05-05 15:04:03 -05:00
{ "testdata" , "issue-598" , "push" , "" , platforms , "" } ,
2022-01-25 11:27:27 -06:00
{ "testdata" , "if-env-act" , "push" , "" , platforms , "" } ,
2021-05-06 08:30:12 -05:00
{ "testdata" , "env-and-path" , "push" , "" , platforms , "" } ,
2021-11-24 09:49:08 -06:00
{ "testdata" , "outputs" , "push" , "" , platforms , "" } ,
2021-11-27 11:55:26 -06:00
{ "testdata" , "steps-context/conclusion" , "push" , "" , platforms , "" } ,
{ "testdata" , "steps-context/outcome" , "push" , "" , platforms , "" } ,
2021-12-08 14:57:42 -06:00
{ "testdata" , "job-status-check" , "push" , "job 'fail' failed" , platforms , "" } ,
{ "testdata" , "if-expressions" , "push" , "Job 'mytest' failed" , platforms , "" } ,
2022-02-15 10:35:02 -06:00
{ "testdata" , "evalmatrix" , "push" , "" , platforms , "" } ,
{ "testdata" , "evalmatrixneeds" , "push" , "" , platforms , "" } ,
{ "testdata" , "evalmatrixneeds2" , "push" , "" , platforms , "" } ,
{ "testdata" , "evalmatrix-merge-map" , "push" , "" , platforms , "" } ,
{ "testdata" , "evalmatrix-merge-array" , "push" , "" , platforms , "" } ,
2021-08-09 10:35:05 -05:00
{ "../model/testdata" , "strategy" , "push" , "" , platforms , "" } , // TODO: move all testdata into pkg so we can validate it with planner and runner
2021-05-02 10:15:13 -05:00
// {"testdata", "issue-228", "push", "", platforms, ""}, // TODO [igni]: Remove this once everything passes
2022-04-04 13:01:13 -05:00
{ "../model/testdata" , "container-volumes" , "push" , "" , platforms , "" } ,
2021-03-28 23:08:40 -05:00
2021-05-02 10:15:13 -05:00
// single test for different architecture: linux/arm64
2021-03-28 23:08:40 -05:00
{ "testdata" , "basic" , "push" , "" , platforms , "linux/arm64" } ,
2019-01-31 01:53:39 -06:00
}
log . SetLevel ( log . DebugLevel )
2020-02-10 17:27:05 -06:00
ctx := context . Background ( )
2019-01-31 01:53:39 -06:00
for _ , table := range tables {
2021-05-10 10:12:57 -05:00
runTestJobFile ( ctx , t , table )
2019-01-31 01:53:39 -06:00
}
}
2020-03-06 14:30:24 -06:00
func TestRunEventSecrets ( t * testing . T ) {
if testing . Short ( ) {
t . Skip ( "skipping integration test" )
}
log . SetLevel ( log . DebugLevel )
ctx := context . Background ( )
platforms := map [ string ] string {
2021-06-10 18:12:05 -05:00
"ubuntu-latest" : baseImage ,
2020-03-06 14:30:24 -06:00
}
workflowPath := "secrets"
eventName := "push"
workdir , err := filepath . Abs ( "testdata" )
2021-05-18 01:14:49 -05:00
assert . Nil ( t , err , workflowPath )
2020-03-06 14:30:24 -06:00
2020-04-17 12:04:40 -05:00
env , _ := godotenv . Read ( filepath . Join ( workdir , workflowPath , ".env" ) )
secrets , _ := godotenv . Read ( filepath . Join ( workdir , workflowPath , ".secrets" ) )
2020-03-06 14:30:24 -06:00
runnerConfig := & Config {
Workdir : workdir ,
EventName : eventName ,
Platforms : platforms ,
ReuseContainers : false ,
Secrets : secrets ,
2020-04-17 12:04:40 -05:00
Env : env ,
2020-03-06 14:30:24 -06:00
}
runner , err := New ( runnerConfig )
2021-05-18 01:14:49 -05:00
assert . Nil ( t , err , workflowPath )
2020-03-06 14:30:24 -06:00
2021-05-03 09:57:24 -05:00
planner , err := model . NewWorkflowPlanner ( fmt . Sprintf ( "testdata/%s" , workflowPath ) , true )
2021-05-18 01:14:49 -05:00
assert . Nil ( t , err , workflowPath )
2020-03-06 14:30:24 -06:00
plan := planner . PlanEvent ( eventName )
err = runner . NewPlanExecutor ( plan ) ( ctx )
2021-05-18 01:14:49 -05:00
assert . Nil ( t , err , workflowPath )
2020-03-06 14:30:24 -06:00
}
2020-03-06 16:17:57 -06:00
func TestRunEventPullRequest ( t * testing . T ) {
if testing . Short ( ) {
t . Skip ( "skipping integration test" )
}
log . SetLevel ( log . DebugLevel )
ctx := context . Background ( )
platforms := map [ string ] string {
2021-06-10 18:12:05 -05:00
"ubuntu-latest" : baseImage ,
2020-03-06 16:17:57 -06:00
}
workflowPath := "pull-request"
eventName := "pull_request"
workdir , err := filepath . Abs ( "testdata" )
2021-05-18 01:14:49 -05:00
assert . Nil ( t , err , workflowPath )
2020-03-06 16:17:57 -06:00
runnerConfig := & Config {
Workdir : workdir ,
EventName : eventName ,
EventPath : filepath . Join ( workdir , workflowPath , "event.json" ) ,
Platforms : platforms ,
ReuseContainers : false ,
}
runner , err := New ( runnerConfig )
2021-05-18 01:14:49 -05:00
assert . Nil ( t , err , workflowPath )
2020-03-06 16:17:57 -06:00
2021-05-03 09:57:24 -05:00
planner , err := model . NewWorkflowPlanner ( fmt . Sprintf ( "testdata/%s" , workflowPath ) , true )
2021-05-18 01:14:49 -05:00
assert . Nil ( t , err , workflowPath )
2020-03-06 16:17:57 -06:00
plan := planner . PlanEvent ( eventName )
err = runner . NewPlanExecutor ( plan ) ( ctx )
2021-05-18 01:14:49 -05:00
assert . Nil ( t , err , workflowPath )
2020-03-06 16:17:57 -06:00
}
2021-05-04 16:50:35 -05:00
func TestContainerPath ( t * testing . T ) {
type containerPathJob struct {
destinationPath string
sourcePath string
workDir string
}
if runtime . GOOS == "windows" {
cwd , err := os . Getwd ( )
if err != nil {
log . Error ( err )
}
rootDrive := os . Getenv ( "SystemDrive" )
rootDriveLetter := strings . ReplaceAll ( strings . ToLower ( rootDrive ) , ` : ` , "" )
for _ , v := range [ ] containerPathJob {
{ "/mnt/c/Users/act/go/src/github.com/nektos/act" , "C:\\Users\\act\\go\\src\\github.com\\nektos\\act\\" , "" } ,
{ "/mnt/f/work/dir" , ` F:\work\dir ` , "" } ,
2021-05-10 10:12:57 -05:00
{ "/mnt/c/windows/to/unix" , "windows\\to\\unix" , fmt . Sprintf ( "%s\\" , rootDrive ) } ,
2021-05-04 16:50:35 -05:00
{ fmt . Sprintf ( "/mnt/%v/act" , rootDriveLetter ) , "act" , fmt . Sprintf ( "%s\\" , rootDrive ) } ,
} {
if v . workDir != "" {
if err := os . Chdir ( v . workDir ) ; err != nil {
log . Error ( err )
t . Fail ( )
}
}
runnerConfig := & Config {
Workdir : v . sourcePath ,
}
assert . Equal ( t , v . destinationPath , runnerConfig . containerPath ( runnerConfig . Workdir ) )
}
if err := os . Chdir ( cwd ) ; err != nil {
log . Error ( err )
}
} else {
cwd , err := os . Getwd ( )
if err != nil {
log . Error ( err )
}
for _ , v := range [ ] containerPathJob {
{ "/home/act/go/src/github.com/nektos/act" , "/home/act/go/src/github.com/nektos/act" , "" } ,
{ "/home/act" , ` /home/act/ ` , "" } ,
{ cwd , "." , "" } ,
} {
runnerConfig := & Config {
Workdir : v . sourcePath ,
}
assert . Equal ( t , v . destinationPath , runnerConfig . containerPath ( runnerConfig . Workdir ) )
}
}
}