packer/rpc: tests green from new BuilderFactory changes
This commit is contained in:
parent
14c568a9d2
commit
a6aafde00e
|
@ -1,46 +0,0 @@
|
|||
package rpc
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"net/rpc"
|
||||
)
|
||||
|
||||
// An implementation of packer.BuilderFactory where the factory is actually
|
||||
// executed over an RPC connection.
|
||||
type BuilderFactory struct {
|
||||
client *rpc.Client
|
||||
}
|
||||
|
||||
// BuilderFactoryServer wraps a packer.BuilderFactory and makes it exportable
|
||||
// as part of a Golang RPC server.
|
||||
type BuilderFactoryServer struct {
|
||||
bf packer.BuilderFactory
|
||||
}
|
||||
|
||||
type BuilderFactoryCreateArgs struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func (b *BuilderFactory) CreateBuilder(name string) packer.Builder {
|
||||
var reply string
|
||||
|
||||
b.client.Call("BuilderFactory.CreateBuilder", &BuilderFactoryCreateArgs{name}, &reply)
|
||||
|
||||
// TODO: error handling
|
||||
client, _ := rpc.Dial("tcp", reply)
|
||||
return &Builder{client}
|
||||
}
|
||||
|
||||
func (b *BuilderFactoryServer) CreateBuilder(args *BuilderFactoryCreateArgs, reply *string) error {
|
||||
// Get the actual builder response
|
||||
builder := b.bf.CreateBuilder(args.Name)
|
||||
|
||||
// Now we wrap that back up into a server, and send it on backwards.
|
||||
server := NewServer()
|
||||
server.RegisterBuilder(builder)
|
||||
server.StartSingle()
|
||||
|
||||
// Set the reply to the address of the sever
|
||||
*reply = server.Address()
|
||||
return nil
|
||||
}
|
|
@ -1,58 +0,0 @@
|
|||
package rpc
|
||||
|
||||
import (
|
||||
"cgl.tideland.biz/asserts"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"net/rpc"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var createResult = &testBuilder{}
|
||||
|
||||
type testBuilderFactory struct {
|
||||
createCalled bool
|
||||
createName string
|
||||
}
|
||||
|
||||
func (b *testBuilderFactory) CreateBuilder(name string) packer.Builder {
|
||||
b.createCalled = true
|
||||
b.createName = name
|
||||
return createResult
|
||||
}
|
||||
|
||||
func TestBuilderFactoryRPC(t *testing.T) {
|
||||
assert := asserts.NewTestingAsserts(t, true)
|
||||
|
||||
// Create the interface to test
|
||||
b := new(testBuilderFactory)
|
||||
|
||||
// Start the server
|
||||
server := NewServer()
|
||||
server.RegisterBuilderFactory(b)
|
||||
server.Start()
|
||||
defer server.Stop()
|
||||
|
||||
// Create the client over RPC and run some methods to verify it works
|
||||
client, err := rpc.Dial("tcp", server.Address())
|
||||
assert.Nil(err, "should be able to connect")
|
||||
|
||||
// Test Create
|
||||
name := "foo"
|
||||
bClient := &BuilderFactory{client}
|
||||
builder := bClient.CreateBuilder(name)
|
||||
assert.True(b.createCalled, "create should be called")
|
||||
assert.Equal(b.createName, "foo", "name should be foo")
|
||||
|
||||
builder.Prepare(42)
|
||||
assert.True(createResult.prepareCalled, "prepare should be called")
|
||||
assert.Equal(createResult.prepareConfig, 42, "42 should be config")
|
||||
}
|
||||
|
||||
func TestBuilderFactory_ImplementsBuilderFactory(t *testing.T) {
|
||||
assert := asserts.NewTestingAsserts(t, true)
|
||||
|
||||
var realVar packer.BuilderFactory
|
||||
b := &BuilderFactory{nil}
|
||||
|
||||
assert.Implementor(b, &realVar, "should be a BuilderFactory")
|
||||
}
|
|
@ -21,15 +21,6 @@ type EnvironmentCliArgs struct {
|
|||
Args []string
|
||||
}
|
||||
|
||||
func (e *Environment) BuilderFactory() packer.BuilderFactory {
|
||||
var reply string
|
||||
e.client.Call("Environment.BuilderFactory", new(interface{}), &reply)
|
||||
|
||||
// TODO: error handling
|
||||
client, _ := rpc.Dial("tcp", reply)
|
||||
return &BuilderFactory{client}
|
||||
}
|
||||
|
||||
func (e *Environment) Cli(args []string) (result int) {
|
||||
rpcArgs := &EnvironmentCliArgs{args}
|
||||
e.client.Call("Environment.Cli", rpcArgs, &result)
|
||||
|
@ -45,18 +36,6 @@ func (e *Environment) Ui() packer.Ui {
|
|||
return &Ui{client}
|
||||
}
|
||||
|
||||
func (e *EnvironmentServer) BuilderFactory(args *interface{}, reply *string) error {
|
||||
bf := e.env.BuilderFactory()
|
||||
|
||||
// Wrap that up into a server, and server a single connection back
|
||||
server := NewServer()
|
||||
server.RegisterBuilderFactory(bf)
|
||||
server.StartSingle()
|
||||
|
||||
*reply = server.Address()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *EnvironmentServer) Cli(args *EnvironmentCliArgs, reply *int) error {
|
||||
*reply = e.env.Cli(args.Args)
|
||||
return nil
|
||||
|
|
|
@ -7,7 +7,6 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
var testEnvBF = &testBuilderFactory{}
|
||||
var testEnvUi = &testUi{}
|
||||
|
||||
type testEnvironment struct {
|
||||
|
@ -17,11 +16,6 @@ type testEnvironment struct {
|
|||
uiCalled bool
|
||||
}
|
||||
|
||||
func (e *testEnvironment) BuilderFactory() packer.BuilderFactory {
|
||||
e.bfCalled = true
|
||||
return testEnvBF
|
||||
}
|
||||
|
||||
func (e *testEnvironment) Cli(args []string) int {
|
||||
e.cliCalled = true
|
||||
e.cliArgs = args
|
||||
|
@ -48,15 +42,7 @@ func TestEnvironmentRPC(t *testing.T) {
|
|||
// Create the client over RPC and run some methods to verify it works
|
||||
client, err := rpc.Dial("tcp", server.Address())
|
||||
assert.Nil(err, "should be able to connect")
|
||||
|
||||
// Test BuilderFactory
|
||||
eClient := &Environment{client}
|
||||
bf := eClient.BuilderFactory()
|
||||
assert.True(e.bfCalled, "BuilderFactory should be called")
|
||||
|
||||
// Test calls on the factory
|
||||
_ = bf.CreateBuilder("foo")
|
||||
assert.True(testEnvBF.createCalled, "create should be called on BF")
|
||||
|
||||
// Test Cli
|
||||
cliArgs := []string{"foo", "bar"}
|
||||
|
|
|
@ -37,10 +37,6 @@ func (s *Server) RegisterBuilder(b packer.Builder) {
|
|||
s.server.RegisterName("Builder", &BuilderServer{b})
|
||||
}
|
||||
|
||||
func (s *Server) RegisterBuilderFactory(b packer.BuilderFactory) {
|
||||
s.server.RegisterName("BuilderFactory", &BuilderFactoryServer{b})
|
||||
}
|
||||
|
||||
func (s *Server) RegisterCommand(c packer.Command) {
|
||||
s.server.RegisterName("Command", &ServerCommand{c})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue