mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-09-15 18:57:01 +00:00
add unit tests for various ways an action can be defined
This commit is contained in:
parent
c29a448085
commit
6e74807de9
2 changed files with 159 additions and 13 deletions
|
@ -5,6 +5,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
@ -31,15 +32,33 @@ func ParseWorkflows(workingDir string, workflowPath string) (Workflows, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
workflows, err := parseWorkflowsFile(workflowReader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
workflows.WorkingDir = workingDir
|
||||||
|
workflows.WorkflowPath = workflowPath
|
||||||
|
workflows.TempDir, err = ioutil.TempDir("/tmp", "act-")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: add validation logic
|
||||||
|
// - check for circular dependencies
|
||||||
|
// - check for valid local path refs
|
||||||
|
// - check for valid dependencies
|
||||||
|
|
||||||
|
return workflows, nil
|
||||||
|
}
|
||||||
|
func parseWorkflowsFile(workflowReader io.Reader) (*workflowsFile, error) {
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
_, err = buf.ReadFrom(workflowReader)
|
_, err := buf.ReadFrom(workflowReader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
workflows := new(workflowsFile)
|
workflows := new(workflowsFile)
|
||||||
workflows.WorkingDir = workingDir
|
|
||||||
workflows.WorkflowPath = workflowPath
|
|
||||||
|
|
||||||
astFile, err := hcl.ParseBytes(buf.Bytes())
|
astFile, err := hcl.ParseBytes(buf.Bytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -51,16 +70,6 @@ func ParseWorkflows(workingDir string, workflowPath string) (Workflows, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
workflows.TempDir, err = ioutil.TempDir("/tmp", "act-")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: add validation logic
|
|
||||||
// - check for circular dependencies
|
|
||||||
// - check for valid local path refs
|
|
||||||
// - check for valid dependencies
|
|
||||||
|
|
||||||
return workflows, nil
|
return workflows, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
137
actions/parser_test.go
Normal file
137
actions/parser_test.go
Normal file
|
@ -0,0 +1,137 @@
|
||||||
|
package actions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseWorkflowsFile(t *testing.T) {
|
||||||
|
log.SetLevel(log.DebugLevel)
|
||||||
|
|
||||||
|
conf := `
|
||||||
|
workflow "build-and-deploy" {
|
||||||
|
on = "push"
|
||||||
|
resolves = ["deploy"]
|
||||||
|
}
|
||||||
|
|
||||||
|
action "build" {
|
||||||
|
uses = "./action1"
|
||||||
|
args = "echo 'build'"
|
||||||
|
}
|
||||||
|
|
||||||
|
action "test" {
|
||||||
|
uses = "docker://ubuntu:18.04"
|
||||||
|
runs = "echo 'test'"
|
||||||
|
needs = ["build"]
|
||||||
|
}
|
||||||
|
|
||||||
|
action "deploy" {
|
||||||
|
uses = "./action2"
|
||||||
|
args = ["echo","deploy"]
|
||||||
|
needs = ["test"]
|
||||||
|
}
|
||||||
|
|
||||||
|
action "docker-login" {
|
||||||
|
uses = "docker://docker"
|
||||||
|
runs = ["sh", "-c", "echo $DOCKER_AUTH | docker login --username $REGISTRY_USER --password-stdin"]
|
||||||
|
secrets = ["DOCKER_AUTH"]
|
||||||
|
env = {
|
||||||
|
REGISTRY_USER = "username"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
action "unit-tests" {
|
||||||
|
uses = "./scripts/github_actions"
|
||||||
|
runs = "yarn test:ci-unittest || echo \"Unit tests failed, but running danger to present the results!\" 2>&1"
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
workflows, err := parseWorkflowsFile(strings.NewReader(conf))
|
||||||
|
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, 1, len(workflows.Workflow))
|
||||||
|
|
||||||
|
w, wName, _ := workflows.getWorkflow("push")
|
||||||
|
assert.Equal(t, "build-and-deploy", wName)
|
||||||
|
assert.ElementsMatch(t, []string{"deploy"}, w.Resolves)
|
||||||
|
|
||||||
|
actions := []struct {
|
||||||
|
name string
|
||||||
|
uses string
|
||||||
|
needs []string
|
||||||
|
runs []string
|
||||||
|
args []string
|
||||||
|
secrets []string
|
||||||
|
}{
|
||||||
|
{"build",
|
||||||
|
"./action1",
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
[]string{"echo", "build"},
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
{"test",
|
||||||
|
"docker://ubuntu:18.04",
|
||||||
|
[]string{"build"},
|
||||||
|
[]string{"echo", "test"},
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
{"deploy",
|
||||||
|
"./action2",
|
||||||
|
[]string{"test"},
|
||||||
|
nil,
|
||||||
|
[]string{"echo", "deploy"},
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
{"docker-login",
|
||||||
|
"docker://docker",
|
||||||
|
nil,
|
||||||
|
[]string{"sh", "-c", "echo $DOCKER_AUTH | docker login --username $REGISTRY_USER --password-stdin"},
|
||||||
|
nil,
|
||||||
|
[]string{"DOCKER_AUTH"},
|
||||||
|
},
|
||||||
|
{"unit-tests",
|
||||||
|
"./scripts/github_actions",
|
||||||
|
nil,
|
||||||
|
[]string{"yarn", "test:ci-unittest", "||", "echo", "Unit tests failed, but running danger to present the results!", "2>&1"},
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, exp := range actions {
|
||||||
|
act, _ := workflows.getAction(exp.name)
|
||||||
|
assert.Equal(t, exp.uses, act.Uses, "[%s] Uses", exp.name)
|
||||||
|
if exp.needs == nil {
|
||||||
|
assert.Nil(t, act.Needs, "[%s] Needs", exp.name)
|
||||||
|
} else {
|
||||||
|
assert.ElementsMatch(t, exp.needs, act.Needs, "[%s] Needs", exp.name)
|
||||||
|
}
|
||||||
|
if exp.runs == nil {
|
||||||
|
assert.Nil(t, act.Runs, "[%s] Runs", exp.name)
|
||||||
|
} else {
|
||||||
|
assert.ElementsMatch(t, exp.runs, act.Runs, "[%s] Runs", exp.name)
|
||||||
|
}
|
||||||
|
if exp.args == nil {
|
||||||
|
assert.Nil(t, act.Args, "[%s] Args", exp.name)
|
||||||
|
} else {
|
||||||
|
assert.ElementsMatch(t, exp.args, act.Args, "[%s] Args", exp.name)
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
if exp.env == nil {
|
||||||
|
assert.Nil(t, act.Env, "[%s] Env", exp.name)
|
||||||
|
} else {
|
||||||
|
assert.ElementsMatch(t, exp.env, act.Env, "[%s] Env", exp.name)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
if exp.secrets == nil {
|
||||||
|
assert.Nil(t, act.Secrets, "[%s] Secrets", exp.name)
|
||||||
|
} else {
|
||||||
|
assert.ElementsMatch(t, exp.secrets, act.Secrets, "[%s] Secrets", exp.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue