2013-05-04 18:58:42 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"net/rpc"
|
2013-10-16 23:04:57 -04:00
|
|
|
"reflect"
|
2013-05-04 18:58:42 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-11-02 23:47:23 -04:00
|
|
|
var testEnvBuilder = &packer.MockBuilder{}
|
2013-06-10 00:09:09 -04:00
|
|
|
var testEnvCache = &testCache{}
|
2013-05-04 18:58:42 -04:00
|
|
|
var testEnvUi = &testUi{}
|
|
|
|
|
|
|
|
type testEnvironment struct {
|
2013-05-05 18:12:55 -04:00
|
|
|
builderCalled bool
|
2013-05-10 20:01:24 -04:00
|
|
|
builderName string
|
|
|
|
cliCalled bool
|
|
|
|
cliArgs []string
|
2013-05-11 12:51:49 -04:00
|
|
|
hookCalled bool
|
|
|
|
hookName string
|
2013-06-18 13:05:45 -04:00
|
|
|
ppCalled bool
|
|
|
|
ppName string
|
2013-05-22 18:35:52 -04:00
|
|
|
provCalled bool
|
|
|
|
provName string
|
2013-05-10 20:01:24 -04:00
|
|
|
uiCalled bool
|
2013-05-04 18:58:42 -04:00
|
|
|
}
|
|
|
|
|
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-06-10 00:09:09 -04:00
|
|
|
func (e *testEnvironment) Cache() packer.Cache {
|
|
|
|
return testEnvCache
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-05-11 12:51:49 -04:00
|
|
|
func (e *testEnvironment) Hook(name string) (packer.Hook, error) {
|
|
|
|
e.hookCalled = true
|
|
|
|
e.hookName = name
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2013-06-18 13:05:45 -04:00
|
|
|
func (e *testEnvironment) PostProcessor(name string) (packer.PostProcessor, error) {
|
|
|
|
e.ppCalled = true
|
|
|
|
e.ppName = name
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2013-05-22 18:35:52 -04:00
|
|
|
func (e *testEnvironment) Provisioner(name string) (packer.Provisioner, error) {
|
|
|
|
e.provCalled = true
|
|
|
|
e.provName = name
|
|
|
|
return nil, 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) {
|
|
|
|
// 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-10-16 23:04:57 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2013-05-04 18:58:42 -04:00
|
|
|
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-10-16 23:04:57 -04:00
|
|
|
if !e.builderCalled {
|
|
|
|
t.Fatal("builder should be called")
|
|
|
|
}
|
|
|
|
if e.builderName != "foo" {
|
|
|
|
t.Fatalf("bad: %#v", e.builderName)
|
|
|
|
}
|
2013-05-05 18:12:55 -04:00
|
|
|
|
|
|
|
builder.Prepare(nil)
|
2013-11-02 23:47:23 -04:00
|
|
|
if !testEnvBuilder.PrepareCalled {
|
2013-10-16 23:04:57 -04:00
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
2013-05-05 18:12:55 -04:00
|
|
|
|
2013-06-10 00:09:09 -04:00
|
|
|
// Test Cache
|
|
|
|
cache := eClient.Cache()
|
|
|
|
cache.Lock("foo")
|
2013-10-16 23:04:57 -04:00
|
|
|
if !testEnvCache.lockCalled {
|
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
2013-06-10 00:09:09 -04:00
|
|
|
|
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-10-16 23:04:57 -04:00
|
|
|
if !e.cliCalled {
|
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(e.cliArgs, cliArgs) {
|
|
|
|
t.Fatalf("bad: %#v", e.cliArgs)
|
|
|
|
}
|
|
|
|
if result != 42 {
|
|
|
|
t.Fatalf("bad: %#v", result)
|
|
|
|
}
|
2013-05-04 18:58:42 -04:00
|
|
|
|
2013-05-22 18:35:52 -04:00
|
|
|
// Test Provisioner
|
|
|
|
_, _ = eClient.Provisioner("foo")
|
2013-10-16 23:04:57 -04:00
|
|
|
if !e.provCalled {
|
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
|
|
|
if e.provName != "foo" {
|
|
|
|
t.Fatalf("bad: %s", e.provName)
|
|
|
|
}
|
2013-05-22 18:35:52 -04:00
|
|
|
|
2013-05-04 18:58:42 -04:00
|
|
|
// Test Ui
|
|
|
|
ui := eClient.Ui()
|
2013-10-16 23:04:57 -04:00
|
|
|
if !e.uiCalled {
|
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
2013-05-04 18:58:42 -04:00
|
|
|
|
|
|
|
// Test calls on the Ui
|
|
|
|
ui.Say("format")
|
2013-10-16 23:04:57 -04:00
|
|
|
if !testEnvUi.sayCalled {
|
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
|
|
|
if testEnvUi.sayMessage != "format" {
|
|
|
|
t.Fatalf("bad: %#v", testEnvUi.sayMessage)
|
|
|
|
}
|
2013-05-04 18:58:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironment_ImplementsEnvironment(t *testing.T) {
|
2013-10-16 23:04:57 -04:00
|
|
|
var _ packer.Environment = new(Environment)
|
2013-05-04 18:58:42 -04:00
|
|
|
}
|