2013-05-04 16:47:11 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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-11-02 23:47:23 -04:00
|
|
|
func builderRPCClient(t *testing.T) (*packer.MockBuilder, packer.Builder) {
|
|
|
|
b := new(packer.MockBuilder)
|
2013-05-04 16:47:11 -04:00
|
|
|
|
|
|
|
// 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-11-02 23:47:23 -04:00
|
|
|
return b, Builder(client)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare(t *testing.T) {
|
|
|
|
b, bClient := builderRPCClient(t)
|
2013-05-04 16:47:11 -04:00
|
|
|
|
|
|
|
// Test Prepare
|
|
|
|
config := 42
|
2013-11-02 23:49:10 -04:00
|
|
|
warnings, err := bClient.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
|
2013-11-02 23:47:23 -04:00
|
|
|
if !b.PrepareCalled {
|
2013-10-16 23:04:57 -04:00
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
|
|
|
|
2013-11-02 23:47:23 -04:00
|
|
|
if !reflect.DeepEqual(b.PrepareConfig, []interface{}{42}) {
|
|
|
|
t.Fatalf("bad: %#v", b.PrepareConfig)
|
2013-10-16 23:04:57 -04:00
|
|
|
}
|
2013-11-02 23:47:23 -04:00
|
|
|
}
|
|
|
|
|
2013-11-02 23:49:10 -04:00
|
|
|
func TestBuilderPrepare_Warnings(t *testing.T) {
|
|
|
|
b, bClient := builderRPCClient(t)
|
|
|
|
|
|
|
|
expected := []string{"foo"}
|
|
|
|
b.PrepareWarnings = expected
|
|
|
|
|
|
|
|
// Test Prepare
|
|
|
|
warnings, err := bClient.Prepare(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(warnings, expected) {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-02 23:47:23 -04:00
|
|
|
func TestBuilderRun(t *testing.T) {
|
|
|
|
b, bClient := builderRPCClient(t)
|
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)
|
|
|
|
}
|
|
|
|
|
2013-11-02 23:47:23 -04:00
|
|
|
if !b.RunCalled {
|
2013-10-16 23:04:57 -04:00
|
|
|
t.Fatal("run should be called")
|
|
|
|
}
|
2013-05-04 16:50:02 -04:00
|
|
|
|
2013-11-02 23:47:23 -04:00
|
|
|
b.RunCache.Lock("foo")
|
|
|
|
if !cache.lockCalled {
|
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
b.RunHook.Run("foo", nil, nil, nil)
|
|
|
|
if !hook.RunCalled {
|
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
b.RunUi.Say("format")
|
|
|
|
if !ui.sayCalled {
|
|
|
|
t.Fatal("say should be called")
|
2013-05-04 16:50:02 -04:00
|
|
|
}
|
2013-06-03 18:30:09 -04:00
|
|
|
|
2013-11-02 23:47:23 -04:00
|
|
|
if ui.sayMessage != "format" {
|
|
|
|
t.Fatalf("bad: %s", ui.sayMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
if artifact.Id() != testBuilderArtifact.Id() {
|
|
|
|
t.Fatalf("bad: %s", artifact.Id())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderRun_nilResult(t *testing.T) {
|
|
|
|
b, bClient := builderRPCClient(t)
|
|
|
|
b.RunNilResult = true
|
|
|
|
|
|
|
|
cache := new(testCache)
|
|
|
|
hook := &packer.MockHook{}
|
|
|
|
ui := &testUi{}
|
|
|
|
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-11-02 23:47:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderRun_ErrResult(t *testing.T) {
|
|
|
|
b, bClient := builderRPCClient(t)
|
|
|
|
b.RunErrResult = true
|
2013-06-12 18:58:02 -04:00
|
|
|
|
2013-11-02 23:47:23 -04:00
|
|
|
cache := new(testCache)
|
|
|
|
hook := &packer.MockHook{}
|
|
|
|
ui := &testUi{}
|
|
|
|
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-11-02 23:47:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderCancel(t *testing.T) {
|
|
|
|
b, bClient := builderRPCClient(t)
|
2013-06-05 18:36:26 -04:00
|
|
|
|
2013-06-03 18:30:09 -04:00
|
|
|
bClient.Cancel()
|
2013-11-02 23:47:23 -04:00
|
|
|
if !b.CancelCalled {
|
2013-10-16 23:04:57 -04:00
|
|
|
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
|
|
|
}
|