packer-cn/packer/rpc/build_test.go

80 lines
1.6 KiB
Go
Raw Normal View History

package rpc
import (
"cgl.tideland.biz/asserts"
"github.com/mitchellh/packer/packer"
"net/rpc"
"testing"
)
2013-05-22 01:38:41 -04:00
var testBuildArtifact = &testArtifact{}
type testBuild struct {
2013-05-10 20:01:24 -04:00
nameCalled bool
prepareCalled bool
2013-05-10 20:01:24 -04:00
runCalled bool
runUi packer.Ui
}
2013-05-09 14:32:03 -04:00
func (b *testBuild) Name() string {
b.nameCalled = true
return "name"
}
func (b *testBuild) Prepare() error {
b.prepareCalled = true
return nil
}
2013-05-22 01:38:41 -04:00
func (b *testBuild) Run(ui packer.Ui) packer.Artifact {
b.runCalled = true
b.runUi = ui
2013-05-22 01:38:41 -04:00
return testBuildArtifact
}
func TestBuildRPC(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
2013-05-22 01:38:41 -04:00
// Create the interface to test
b := new(testBuild)
2013-05-22 01:38:41 -04:00
// Start the server
server := rpc.NewServer()
RegisterBuild(server, b)
address := serveSingleConn(server)
// 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
bClient.Prepare()
assert.True(b.prepareCalled, "prepare should be called")
// Test Run
ui := new(testUi)
bClient.Run(ui)
assert.True(b.runCalled, "run should be called")
// Test the UI given to run, which should be fully functional
if b.runCalled {
b.runUi.Say("format")
assert.True(ui.sayCalled, "say should be called")
assert.Equal(ui.sayFormat, "format", "format should be correct")
}
}
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)
assert.Implementor(b, &realBuild, "should be a Build")
}