2013-05-04 02:55:08 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cgl.tideland.biz/asserts"
|
2013-06-12 19:01:42 -04:00
|
|
|
"errors"
|
2013-05-04 02:55:08 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"net/rpc"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-05-22 01:38:41 -04:00
|
|
|
var testBuildArtifact = &testArtifact{}
|
|
|
|
|
2013-05-04 02:55:08 -04:00
|
|
|
type testBuild struct {
|
2013-06-14 15:22:19 -04:00
|
|
|
nameCalled bool
|
|
|
|
prepareCalled bool
|
|
|
|
runCalled bool
|
|
|
|
runCache packer.Cache
|
|
|
|
runUi packer.Ui
|
|
|
|
setDebugCalled bool
|
|
|
|
cancelCalled bool
|
2013-06-12 19:01:42 -04:00
|
|
|
|
|
|
|
errRunResult bool
|
2013-05-04 02:55:08 -04:00
|
|
|
}
|
|
|
|
|
2013-05-09 14:32:03 -04:00
|
|
|
func (b *testBuild) Name() string {
|
|
|
|
b.nameCalled = true
|
|
|
|
return "name"
|
|
|
|
}
|
|
|
|
|
2013-06-13 13:08:31 -04:00
|
|
|
func (b *testBuild) Prepare() error {
|
2013-05-04 02:55:08 -04:00
|
|
|
b.prepareCalled = true
|
2013-05-09 13:54:42 -04:00
|
|
|
return nil
|
2013-05-04 02:55:08 -04:00
|
|
|
}
|
|
|
|
|
2013-06-18 13:24:23 -04:00
|
|
|
func (b *testBuild) Run(ui packer.Ui, cache packer.Cache) ([]packer.Artifact, error) {
|
2013-05-04 02:55:08 -04:00
|
|
|
b.runCalled = true
|
2013-06-10 01:00:47 -04:00
|
|
|
b.runCache = cache
|
2013-05-04 02:55:08 -04:00
|
|
|
b.runUi = ui
|
2013-06-12 19:01:42 -04:00
|
|
|
|
|
|
|
if b.errRunResult {
|
|
|
|
return nil, errors.New("foo")
|
|
|
|
} else {
|
2013-06-18 13:24:23 -04:00
|
|
|
return []packer.Artifact{testBuildArtifact}, nil
|
2013-06-12 19:01:42 -04:00
|
|
|
}
|
2013-05-04 02:55:08 -04:00
|
|
|
}
|
|
|
|
|
2013-06-14 15:22:19 -04:00
|
|
|
func (b *testBuild) SetDebug(bool) {
|
|
|
|
b.setDebugCalled = true
|
|
|
|
}
|
|
|
|
|
2013-06-03 19:03:08 -04:00
|
|
|
func (b *testBuild) Cancel() {
|
|
|
|
b.cancelCalled = true
|
|
|
|
}
|
|
|
|
|
2013-05-04 02:55:08 -04:00
|
|
|
func TestBuildRPC(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
2013-05-22 01:38:41 -04:00
|
|
|
// Create the interface to test
|
2013-05-04 02:55:08 -04:00
|
|
|
b := new(testBuild)
|
|
|
|
|
2013-05-22 01:38:41 -04:00
|
|
|
// Start the server
|
|
|
|
server := rpc.NewServer()
|
|
|
|
RegisterBuild(server, b)
|
|
|
|
address := serveSingleConn(server)
|
2013-05-04 02:55:08 -04:00
|
|
|
|
|
|
|
// Create the client over RPC and run some methods to verify it works
|
2013-05-22 01:38:41 -04:00
|
|
|
client, err := rpc.Dial("tcp", address)
|
|
|
|
assert.Nil(err, "should be able to connect")
|
|
|
|
bClient := Build(client)
|
2013-05-09 14:32:03 -04:00
|
|
|
|
|
|
|
// Test Name
|
|
|
|
bClient.Name()
|
|
|
|
assert.True(b.nameCalled, "name should be called")
|
|
|
|
|
|
|
|
// Test Prepare
|
2013-06-13 13:08:31 -04:00
|
|
|
bClient.Prepare()
|
2013-05-04 02:55:08 -04:00
|
|
|
assert.True(b.prepareCalled, "prepare should be called")
|
|
|
|
|
|
|
|
// Test Run
|
2013-06-10 01:00:47 -04:00
|
|
|
cache := new(testCache)
|
2013-06-13 13:08:31 -04:00
|
|
|
ui := new(testUi)
|
2013-06-18 13:24:23 -04:00
|
|
|
artifacts, err := bClient.Run(ui, cache)
|
2013-05-04 02:55:08 -04:00
|
|
|
assert.True(b.runCalled, "run should be called")
|
2013-06-12 19:01:42 -04:00
|
|
|
assert.Nil(err, "should not error")
|
2013-06-18 13:24:23 -04:00
|
|
|
assert.Equal(len(artifacts), 1, "should have one artifact")
|
|
|
|
assert.Equal(artifacts[0].BuilderId(), "bid", "should have proper builder id")
|
2013-05-04 02:55:08 -04:00
|
|
|
|
|
|
|
// Test the UI given to run, which should be fully functional
|
|
|
|
if b.runCalled {
|
2013-06-10 01:00:47 -04:00
|
|
|
b.runCache.Lock("foo")
|
|
|
|
assert.True(cache.lockCalled, "lock should be called")
|
|
|
|
|
2013-05-04 02:55:08 -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-04 02:55:08 -04:00
|
|
|
}
|
2013-06-03 19:03:08 -04:00
|
|
|
|
2013-06-12 19:01:42 -04:00
|
|
|
// Test run with an error
|
|
|
|
b.errRunResult = true
|
|
|
|
_, err = bClient.Run(ui, cache)
|
|
|
|
assert.NotNil(err, "should not nil")
|
|
|
|
|
2013-06-14 15:22:19 -04:00
|
|
|
// Test SetDebug
|
|
|
|
bClient.SetDebug(true)
|
|
|
|
assert.True(b.setDebugCalled, "should be called")
|
|
|
|
|
2013-06-03 19:03:08 -04:00
|
|
|
// Test Cancel
|
|
|
|
bClient.Cancel()
|
|
|
|
assert.True(b.cancelCalled, "cancel should be called")
|
2013-05-04 02:55:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuild_ImplementsBuild(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
var realBuild packer.Build
|
2013-05-22 01:38:41 -04:00
|
|
|
b := Build(nil)
|
2013-05-04 02:55:08 -04:00
|
|
|
|
|
|
|
assert.Implementor(b, &realBuild, "should be a Build")
|
|
|
|
}
|