Files
Packer-Cn/packer/hook_test.go
T

70 lines
1.4 KiB
Go
Raw Normal View History

2013-05-10 23:15:13 -07:00
package packer
import (
"context"
"testing"
)
func TestDispatchHook_Implements(t *testing.T) {
2013-10-16 21:09:27 -10:00
var _ Hook = new(DispatchHook)
}
func TestDispatchHook_Run_NoHooks(t *testing.T) {
// Just make sure nothing blows up
2013-08-30 17:26:51 -07:00
dh := &DispatchHook{}
dh.Run(context.Background(), "foo", nil, nil, nil)
}
func TestDispatchHook_Run(t *testing.T) {
2013-08-30 17:03:55 -07:00
hook := &MockHook{}
mapping := make(map[string][]Hook)
mapping["foo"] = []Hook{hook}
2013-08-30 17:26:51 -07:00
dh := &DispatchHook{Mapping: mapping}
dh.Run(context.Background(), "foo", nil, nil, 42)
2013-10-16 21:09:27 -10:00
if !hook.RunCalled {
t.Fatal("should be called")
}
if hook.RunName != "foo" {
t.Fatalf("bad: %s", hook.RunName)
}
if hook.RunData != 42 {
t.Fatalf("bad: %#v", hook.RunData)
}
2013-05-10 23:15:13 -07:00
}
2013-08-30 17:26:51 -07:00
// A helper Hook implementation for testing cancels.
// Run will wait indetinitelly until ctx is cancelled.
type CancelHook struct {
cancel func()
}
func (h *CancelHook) Run(ctx context.Context, _ string, _ Ui, _ Communicator, _ interface{}) error {
h.cancel()
<-ctx.Done()
return ctx.Err()
}
2013-08-30 17:26:51 -07:00
func TestDispatchHook_cancel(t *testing.T) {
cancelHook := new(CancelHook)
2013-08-30 17:26:51 -07:00
dh := &DispatchHook{
Mapping: map[string][]Hook{
"foo": {cancelHook},
2013-08-30 17:26:51 -07:00
},
}
ctx, cancel := context.WithCancel(context.Background())
cancelHook.cancel = cancel
2013-08-30 17:26:51 -07:00
errchan := make(chan error)
go func() {
errchan <- dh.Run(ctx, "foo", nil, nil, 42)
}()
if err := <-errchan; err == nil {
t.Fatal("hook should've errored")
2013-08-30 17:26:51 -07:00
}
}