packer-cn/packer-plugin-sdk/multistep/multistep_test.go

76 lines
1.6 KiB
Go
Raw Normal View History

2018-01-18 01:49:03 -05:00
package multistep
2018-01-22 19:55:12 -05:00
import "context"
2018-01-18 01:49:03 -05:00
2018-03-13 03:03:51 -04:00
// A step for testing that accumulates data into a string slice in the
2018-01-18 01:49:03 -05:00
// the state bag. It always uses the "data" key in the state bag, and will
// initialize it.
type TestStepAcc struct {
// The data inserted into the state bag.
Data string
// If true, it will halt at the step when it is run
Halt bool
}
// A step that syncs by sending a channel and expecting a response.
type TestStepSync struct {
Ch chan chan bool
}
// A step that sleeps forever
type TestStepWaitForever struct {
}
// A step that manually flips state to cancelling in run
type TestStepInjectCancel struct {
}
func (s TestStepAcc) Run(ctx context.Context, state StateBag) StepAction {
2018-01-18 01:49:03 -05:00
s.insertData(state, "data")
if s.Halt {
return ActionHalt
}
return ActionContinue
}
func (s TestStepAcc) Cleanup(state StateBag) {
s.insertData(state, "cleanup")
}
func (s TestStepAcc) insertData(state StateBag, key string) {
if _, ok := state.GetOk(key); !ok {
state.Put(key, make([]string, 0, 5))
}
data := state.Get(key).([]string)
data = append(data, s.Data)
state.Put(key, data)
}
func (s TestStepSync) Run(context.Context, StateBag) StepAction {
ch := make(chan bool)
s.Ch <- ch
<-ch
return ActionContinue
}
func (s TestStepSync) Cleanup(StateBag) { close(s.Ch) }
2018-01-18 01:49:03 -05:00
func (s TestStepWaitForever) Run(context.Context, StateBag) StepAction {
select {}
}
func (s TestStepWaitForever) Cleanup(StateBag) {}
func (s TestStepInjectCancel) Run(ctx context.Context, state StateBag) StepAction {
2018-01-18 01:49:03 -05:00
r := state.Get("runner").(*BasicRunner)
r.state = stateCancelling
return ActionContinue
}
func (s TestStepInjectCancel) Cleanup(StateBag) {}