2013-05-04 18:58:42 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cgl.tideland.biz/asserts"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"net/rpc"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-05-05 18:12:55 -04:00
|
|
|
var testEnvBuilder = &testBuilder{}
|
2013-05-04 18:58:42 -04:00
|
|
|
var testEnvUi = &testUi{}
|
|
|
|
|
|
|
|
type testEnvironment struct {
|
2013-05-05 18:12:55 -04:00
|
|
|
builderCalled bool
|
|
|
|
builderName string
|
2013-05-04 18:58:42 -04:00
|
|
|
cliCalled bool
|
|
|
|
cliArgs []string
|
|
|
|
uiCalled bool
|
|
|
|
}
|
|
|
|
|
2013-05-07 23:42:49 -04:00
|
|
|
func (e *testEnvironment) Builder(name string) (packer.Builder, error) {
|
2013-05-05 18:12:55 -04:00
|
|
|
e.builderCalled = true
|
|
|
|
e.builderName = name
|
2013-05-07 23:42:49 -04:00
|
|
|
return testEnvBuilder, nil
|
2013-05-05 18:12:55 -04:00
|
|
|
}
|
|
|
|
|
2013-05-07 23:42:49 -04:00
|
|
|
func (e *testEnvironment) Cli(args []string) (int, error) {
|
2013-05-04 18:58:42 -04:00
|
|
|
e.cliCalled = true
|
|
|
|
e.cliArgs = args
|
2013-05-07 23:42:49 -04:00
|
|
|
return 42, nil
|
2013-05-04 18:58:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *testEnvironment) Ui() packer.Ui {
|
|
|
|
e.uiCalled = true
|
|
|
|
return testEnvUi
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironmentRPC(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
// Create the interface to test
|
|
|
|
e := &testEnvironment{}
|
|
|
|
|
|
|
|
// Start the server
|
2013-05-05 20:38:50 -04:00
|
|
|
server := rpc.NewServer()
|
|
|
|
RegisterEnvironment(server, e)
|
|
|
|
address := serveSingleConn(server)
|
2013-05-04 18:58:42 -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 18:58:42 -04:00
|
|
|
assert.Nil(err, "should be able to connect")
|
|
|
|
eClient := &Environment{client}
|
|
|
|
|
2013-05-05 18:12:55 -04:00
|
|
|
// Test Builder
|
2013-05-07 23:42:49 -04:00
|
|
|
builder, _ := eClient.Builder("foo")
|
2013-05-05 18:12:55 -04:00
|
|
|
assert.True(e.builderCalled, "Builder should be called")
|
|
|
|
assert.Equal(e.builderName, "foo", "Correct name for Builder")
|
|
|
|
|
|
|
|
builder.Prepare(nil)
|
|
|
|
assert.True(testEnvBuilder.prepareCalled, "Prepare should be called")
|
|
|
|
|
2013-05-04 18:58:42 -04:00
|
|
|
// Test Cli
|
|
|
|
cliArgs := []string{"foo", "bar"}
|
2013-05-07 23:42:49 -04:00
|
|
|
result, _ := eClient.Cli(cliArgs)
|
2013-05-04 18:58:42 -04:00
|
|
|
assert.True(e.cliCalled, "CLI should be called")
|
|
|
|
assert.Equal(e.cliArgs, cliArgs, "args should match")
|
|
|
|
assert.Equal(result, 42, "result shuld be 42")
|
|
|
|
|
|
|
|
// Test Ui
|
|
|
|
ui := eClient.Ui()
|
|
|
|
assert.True(e.uiCalled, "Ui should've been called")
|
|
|
|
|
|
|
|
// Test calls on the Ui
|
|
|
|
ui.Say("format")
|
|
|
|
assert.True(testEnvUi.sayCalled, "Say should be called")
|
|
|
|
assert.Equal(testEnvUi.sayFormat, "format", "format should match")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironment_ImplementsEnvironment(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
var realVar packer.Environment
|
|
|
|
e := &Environment{nil}
|
|
|
|
|
|
|
|
assert.Implementor(e, &realVar, "should be an Environment")
|
|
|
|
}
|