act/pkg/runner/step_factory_test.go

82 lines
1.3 KiB
Go
Raw Normal View History

feat: split job steps into its own files/structs (#1004) * refactor: split step_context into separate files This commit moves functions from the step_context.go file into different files, but does otherwise not change anything. This is done to make it easier to review the changes made to these functions in the next commit, where we introduce a step factory to facilitate better unit testing of steps. Co-authored-by: Marcus Noll <marcus.noll@new-work.se> Co-authored-by: Björn Brauer <bjoern.brauer@new-work.se> Co-authored-by: Robert Kowalski <robert.kowalski@new-work.se> Co-authored-by: Philipp Hinrichsen <philipp.hinrichsen@new-work.se> Co-authored-by: Jonas Holland <jonas.holland@new-work.se> * refactor: introduce step factory and make steps testable With this commit we're introducing the `stepFactory` and interfaces and implementations for each different kind of step (run, docker, local and remote actions). Separating each step kind into its own interface and implementation makes it easier to reason about and to change behaviour of the step. By introducing interfaces we enable better unit testability as now each step implementation, the step factory and the job executor can be tested on their own by mocking out parts that are irrelevant. This commits prepares us for implementing pre/post actions in a later PR. Co-authored-by: Marcus Noll <marcus.noll@new-work.se> Co-authored-by: Björn Brauer <bjoern.brauer@new-work.se> Co-authored-by: Robert Kowalski <robert.kowalski@new-work.se> Co-authored-by: Philipp Hinrichsen <philipp.hinrichsen@new-work.se> Co-authored-by: Jonas Holland <jonas.holland@new-work.se> * fix: run post steps in reverse order * test: add missing asserts for mocks * refactor: use local reference instead of function This may make code more easy to follow. * refactor: correct typo in function name * test: use named structs * test: only expected valid calls There are mocks which are only called on certain conditions. * refactor: use step-model to get step name Using the step-logger we have to get the logger name from the step model. * test: only mock stopContainer if required Co-authored-by: Björn Brauer <bjoern.brauer@new-work.se> Co-authored-by: Marcus Noll <marcus.noll@new-work.se> Co-authored-by: Robert Kowalski <robert.kowalski@new-work.se> Co-authored-by: Philipp Hinrichsen <philipp.hinrichsen@new-work.se> Co-authored-by: Jonas Holland <jonas.holland@new-work.se> Co-authored-by: Casey Lee <cplee@nektos.com> Co-authored-by: Christopher Homberger <christopher.homberger@web.de>
2022-03-22 16:13:00 -05:00
package runner
import (
"testing"
"github.com/nektos/act/pkg/model"
"github.com/stretchr/testify/assert"
)
func TestStepFactoryNewStep(t *testing.T) {
table := []struct {
name string
model *model.Step
check func(s step) bool
}{
{
name: "StepRemoteAction",
model: &model.Step{
Uses: "remote/action@v1",
},
check: func(s step) bool {
_, ok := s.(*stepActionRemote)
return ok
},
},
{
name: "StepLocalAction",
model: &model.Step{
Uses: "./action@v1",
},
check: func(s step) bool {
_, ok := s.(*stepActionLocal)
return ok
},
},
{
name: "StepDocker",
model: &model.Step{
Uses: "docker://image:tag",
},
check: func(s step) bool {
_, ok := s.(*stepDocker)
return ok
},
},
{
name: "StepRun",
model: &model.Step{
Run: "cmd",
},
check: func(s step) bool {
_, ok := s.(*stepRun)
return ok
},
},
}
for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
sf := &stepFactoryImpl{}
step, err := sf.newStep(tt.model, &RunContext{})
assert.True(t, tt.check((step)))
assert.Nil(t, err)
})
}
}
func TestStepFactoryInvalidStep(t *testing.T) {
model := &model.Step{
Uses: "remote/action@v1",
Run: "cmd",
}
sf := &stepFactoryImpl{}
_, err := sf.newStep(model, &RunContext{})
assert.Error(t, err)
}