packer-cn/packer/rpc/environment_test.go

145 lines
2.9 KiB
Go
Raw Normal View History

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"
)
var testEnvBuilder = &packer.MockBuilder{}
var testEnvCache = &testCache{}
2013-05-04 18:58:42 -04:00
var testEnvUi = &testUi{}
type testEnvironment struct {
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
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
}
func (e *testEnvironment) Builder(name string) (packer.Builder, error) {
e.builderCalled = true
e.builderName = name
return testEnvBuilder, nil
}
func (e *testEnvironment) Cache() packer.Cache {
return testEnvCache
}
func (e *testEnvironment) Cli(args []string) (int, error) {
2013-05-04 18:58:42 -04:00
e.cliCalled = true
e.cliArgs = args
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
}
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
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
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}
// Test Builder
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)
}
builder.Prepare(nil)
if !testEnvBuilder.PrepareCalled {
2013-10-16 23:04:57 -04:00
t.Fatal("should be called")
}
// 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-05-04 18:58:42 -04:00
// Test Cli
cliArgs := []string{"foo", "bar"}
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
}