2013-05-04 16:47:11 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2013-06-12 18:58:02 -04:00
|
|
|
"errors"
|
2013-05-04 16:47:11 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"net/rpc"
|
2013-10-16 23:04:57 -04:00
|
|
|
"reflect"
|
2013-05-04 16:47:11 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-05-22 01:13:29 -04:00
|
|
|
var testBuilderArtifact = &testArtifact{}
|
|
|
|
|
2013-05-04 16:47:11 -04:00
|
|
|
type testBuilder struct {
|
|
|
|
prepareCalled bool
|
2013-06-14 15:27:50 -04:00
|
|
|
prepareConfig []interface{}
|
2013-05-10 20:01:24 -04:00
|
|
|
runCalled bool
|
2013-06-10 01:00:47 -04:00
|
|
|
runCache packer.Cache
|
2013-05-11 13:31:30 -04:00
|
|
|
runHook packer.Hook
|
2013-05-10 20:01:24 -04:00
|
|
|
runUi packer.Ui
|
2013-06-03 17:44:34 -04:00
|
|
|
cancelCalled bool
|
2013-06-05 18:36:26 -04:00
|
|
|
|
2013-06-12 18:58:02 -04:00
|
|
|
errRunResult bool
|
2013-06-05 18:36:26 -04:00
|
|
|
nilRunResult bool
|
2013-05-04 16:47:11 -04:00
|
|
|
}
|
|
|
|
|
2013-06-14 15:27:50 -04:00
|
|
|
func (b *testBuilder) Prepare(config ...interface{}) error {
|
2013-05-04 16:47:11 -04:00
|
|
|
b.prepareCalled = true
|
|
|
|
b.prepareConfig = config
|
2013-05-09 13:54:42 -04:00
|
|
|
return nil
|
2013-05-04 16:47:11 -04:00
|
|
|
}
|
|
|
|
|
2013-06-12 18:58:02 -04:00
|
|
|
func (b *testBuilder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
|
2013-06-10 01:00:47 -04:00
|
|
|
b.runCache = cache
|
2013-05-04 16:47:11 -04:00
|
|
|
b.runCalled = true
|
2013-05-11 13:31:30 -04:00
|
|
|
b.runHook = hook
|
2013-05-04 16:47:11 -04:00
|
|
|
b.runUi = ui
|
2013-06-05 18:36:26 -04:00
|
|
|
|
2013-06-12 18:58:02 -04:00
|
|
|
if b.errRunResult {
|
|
|
|
return nil, errors.New("foo")
|
|
|
|
} else if b.nilRunResult {
|
|
|
|
return nil, nil
|
2013-06-05 18:36:26 -04:00
|
|
|
} else {
|
2013-06-12 18:58:02 -04:00
|
|
|
return testBuilderArtifact, nil
|
2013-06-05 18:36:26 -04:00
|
|
|
}
|
2013-05-04 16:47:11 -04:00
|
|
|
}
|
|
|
|
|
2013-06-03 17:44:34 -04:00
|
|
|
func (b *testBuilder) Cancel() {
|
|
|
|
b.cancelCalled = true
|
|
|
|
}
|
|
|
|
|
2013-05-04 16:47:11 -04:00
|
|
|
func TestBuilderRPC(t *testing.T) {
|
|
|
|
// Create the interface to test
|
|
|
|
b := new(testBuilder)
|
|
|
|
|
|
|
|
// Start the server
|
2013-05-05 20:38:50 -04:00
|
|
|
server := rpc.NewServer()
|
|
|
|
RegisterBuilder(server, b)
|
|
|
|
address := serveSingleConn(server)
|
2013-05-04 16:47:11 -04:00
|
|
|
|
|
|
|
// Create the client over RPC and run some methods to verify it works
|
2013-05-05 20:38:50 -04:00
|
|
|
client, err := rpc.Dial("tcp", address)
|
2013-10-16 23:04:57 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2013-05-04 16:47:11 -04:00
|
|
|
|
|
|
|
// Test Prepare
|
|
|
|
config := 42
|
2013-05-09 01:25:47 -04:00
|
|
|
bClient := Builder(client)
|
2013-05-04 16:47:11 -04:00
|
|
|
bClient.Prepare(config)
|
2013-10-16 23:04:57 -04:00
|
|
|
if !b.prepareCalled {
|
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(b.prepareConfig, []interface{}{42}) {
|
|
|
|
t.Fatalf("bad: %#v", b.prepareConfig)
|
|
|
|
}
|
2013-05-04 16:50:02 -04:00
|
|
|
|
|
|
|
// Test Run
|
2013-06-10 01:00:47 -04:00
|
|
|
cache := new(testCache)
|
2013-08-30 20:03:55 -04:00
|
|
|
hook := &packer.MockHook{}
|
2013-05-04 16:50:02 -04:00
|
|
|
ui := &testUi{}
|
2013-06-12 18:58:02 -04:00
|
|
|
artifact, err := bClient.Run(ui, hook, cache)
|
2013-10-16 23:04:57 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !b.runCalled {
|
|
|
|
t.Fatal("run should be called")
|
|
|
|
}
|
2013-05-04 16:50:02 -04:00
|
|
|
|
|
|
|
if b.runCalled {
|
2013-06-10 01:00:47 -04:00
|
|
|
b.runCache.Lock("foo")
|
2013-10-16 23:04:57 -04:00
|
|
|
if !cache.lockCalled {
|
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
2013-06-10 01:00:47 -04:00
|
|
|
|
2013-05-12 20:30:30 -04:00
|
|
|
b.runHook.Run("foo", nil, nil, nil)
|
2013-10-16 23:04:57 -04:00
|
|
|
if !hook.RunCalled {
|
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
2013-05-04 16:50:02 -04:00
|
|
|
|
|
|
|
b.runUi.Say("format")
|
2013-10-16 23:04:57 -04:00
|
|
|
if !ui.sayCalled {
|
|
|
|
t.Fatal("say should be called")
|
|
|
|
}
|
2013-05-22 01:13:29 -04:00
|
|
|
|
2013-10-16 23:04:57 -04:00
|
|
|
if ui.sayMessage != "format" {
|
|
|
|
t.Fatalf("bad: %s", ui.sayMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
if artifact.Id() != testBuilderArtifact.Id() {
|
|
|
|
t.Fatalf("bad: %s", artifact.Id())
|
|
|
|
}
|
2013-05-04 16:50:02 -04:00
|
|
|
}
|
2013-06-03 18:30:09 -04:00
|
|
|
|
2013-06-05 18:36:26 -04:00
|
|
|
// Test run with nil result
|
|
|
|
b.nilRunResult = true
|
2013-06-12 18:58:02 -04:00
|
|
|
artifact, err = bClient.Run(ui, hook, cache)
|
2013-10-16 23:04:57 -04:00
|
|
|
if artifact != nil {
|
|
|
|
t.Fatalf("bad: %#v", artifact)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
2013-06-12 18:58:02 -04:00
|
|
|
|
|
|
|
// Test with an error
|
|
|
|
b.errRunResult = true
|
|
|
|
b.nilRunResult = false
|
|
|
|
artifact, err = bClient.Run(ui, hook, cache)
|
2013-10-16 23:04:57 -04:00
|
|
|
if artifact != nil {
|
|
|
|
t.Fatalf("bad: %#v", artifact)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
2013-06-05 18:36:26 -04:00
|
|
|
|
2013-06-03 18:30:09 -04:00
|
|
|
// Test Cancel
|
|
|
|
bClient.Cancel()
|
2013-10-16 23:04:57 -04:00
|
|
|
if !b.cancelCalled {
|
|
|
|
t.Fatal("cancel should be called")
|
|
|
|
}
|
2013-05-04 16:47:11 -04:00
|
|
|
}
|
|
|
|
|
2013-05-09 14:32:03 -04:00
|
|
|
func TestBuilder_ImplementsBuilder(t *testing.T) {
|
2013-10-16 23:04:57 -04:00
|
|
|
var _ packer.Builder = Builder(nil)
|
2013-05-04 16:47:11 -04:00
|
|
|
}
|