2013-05-04 16:47:11 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cgl.tideland.biz/asserts"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"net/rpc"
|
|
|
|
"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
|
|
|
|
prepareConfig interface{}
|
2013-05-10 20:01:24 -04:00
|
|
|
runCalled bool
|
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
|
|
|
|
|
|
|
nilRunResult bool
|
2013-05-04 16:47:11 -04:00
|
|
|
}
|
|
|
|
|
2013-05-09 13:54:42 -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-05-22 01:10:21 -04:00
|
|
|
func (b *testBuilder) Run(ui packer.Ui, hook packer.Hook) packer.Artifact {
|
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
|
|
|
|
|
|
|
if !b.nilRunResult {
|
|
|
|
return testBuilderArtifact
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
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) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
// 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-05-04 16:47:11 -04:00
|
|
|
assert.Nil(err, "should be able to connect")
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
assert.True(b.prepareCalled, "prepare should be called")
|
|
|
|
assert.Equal(b.prepareConfig, 42, "prepare should be called with right arg")
|
2013-05-04 16:50:02 -04:00
|
|
|
|
|
|
|
// Test Run
|
2013-05-11 13:31:30 -04:00
|
|
|
hook := &testHook{}
|
2013-05-04 16:50:02 -04:00
|
|
|
ui := &testUi{}
|
2013-05-22 01:13:29 -04:00
|
|
|
artifact := bClient.Run(ui, hook)
|
2013-05-04 16:50:02 -04:00
|
|
|
assert.True(b.runCalled, "runs hould be called")
|
|
|
|
|
|
|
|
if b.runCalled {
|
2013-05-12 20:30:30 -04:00
|
|
|
b.runHook.Run("foo", nil, nil, nil)
|
2013-05-11 13:31:30 -04:00
|
|
|
assert.True(hook.runCalled, "run should be called")
|
2013-05-04 16:50:02 -04:00
|
|
|
|
|
|
|
b.runUi.Say("format")
|
|
|
|
assert.True(ui.sayCalled, "say should be called")
|
2013-05-27 18:12:48 -04:00
|
|
|
assert.Equal(ui.sayMessage, "format", "message should be correct")
|
2013-05-22 01:13:29 -04:00
|
|
|
|
|
|
|
assert.Equal(artifact.Id(), testBuilderArtifact.Id(), "should have 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
|
|
|
|
artifact = bClient.Run(ui, hook)
|
|
|
|
assert.Nil(artifact, "should be nil")
|
|
|
|
|
2013-06-03 18:30:09 -04:00
|
|
|
// Test Cancel
|
|
|
|
bClient.Cancel()
|
|
|
|
assert.True(b.cancelCalled, "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-05-04 16:47:11 -04:00
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
var realBuilder packer.Builder
|
2013-05-09 01:25:47 -04:00
|
|
|
b := Builder(nil)
|
2013-05-04 16:47:11 -04:00
|
|
|
|
|
|
|
assert.Implementor(b, &realBuilder, "should be a Builder")
|
|
|
|
}
|