packer-cn/packer/rpc/builder_test.go

121 lines
2.8 KiB
Go
Raw Normal View History

2013-05-04 16:47:11 -04:00
package rpc
import (
"cgl.tideland.biz/asserts"
"errors"
2013-05-04 16:47:11 -04:00
"github.com/mitchellh/packer/packer"
"net/rpc"
"testing"
)
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-06-10 01:00:47 -04:00
runCache packer.Cache
runHook packer.Hook
2013-05-10 20:01:24 -04:00
runUi packer.Ui
cancelCalled bool
errRunResult bool
nilRunResult bool
2013-05-04 16:47:11 -04:00
}
func (b *testBuilder) Prepare(config ...interface{}) error {
2013-05-04 16:47:11 -04:00
b.prepareCalled = true
b.prepareConfig = config
return nil
2013-05-04 16:47:11 -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
b.runHook = hook
2013-05-04 16:47:11 -04:00
b.runUi = ui
if b.errRunResult {
return nil, errors.New("foo")
} else if b.nilRunResult {
return nil, nil
} else {
return testBuilderArtifact, nil
}
2013-05-04 16:47:11 -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
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
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
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, []interface{}{42}, "prepare should be called with right arg")
2013-05-04 16:50:02 -04:00
// Test Run
2013-06-10 01:00:47 -04:00
cache := new(testCache)
hook := &testHook{}
2013-05-04 16:50:02 -04:00
ui := &testUi{}
artifact, err := bClient.Run(ui, hook, cache)
assert.Nil(err, "should have no error")
2013-05-04 16:50:02 -04:00
assert.True(b.runCalled, "runs hould be called")
if b.runCalled {
2013-06-10 01:00:47 -04:00
b.runCache.Lock("foo")
assert.True(cache.lockCalled, "lock should be called")
b.runHook.Run("foo", nil, nil, nil)
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")
assert.Equal(ui.sayMessage, "format", "message should be correct")
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
// Test run with nil result
b.nilRunResult = true
artifact, err = bClient.Run(ui, hook, cache)
assert.Nil(artifact, "should be nil")
assert.Nil(err, "should have no error")
// Test with an error
b.errRunResult = true
b.nilRunResult = false
artifact, err = bClient.Run(ui, hook, cache)
assert.Nil(artifact, "should be nil")
assert.NotNil(err, "should have error")
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
b := Builder(nil)
2013-05-04 16:47:11 -04:00
assert.Implementor(b, &realBuilder, "should be a Builder")
}