mirror of https://github.com/hashicorp/packer
parent
ba359394b1
commit
dc74ec5612
@ -1,183 +0,0 @@
|
||||
// The packer package contains the core components of Packer.
|
||||
package packer
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// The function type used to lookup Builder implementations.
|
||||
type BuilderFunc func(name string) (Builder, error)
|
||||
|
||||
// The function type used to lookup Hook implementations.
|
||||
type HookFunc func(name string) (Hook, error)
|
||||
|
||||
// The function type used to lookup PostProcessor implementations.
|
||||
type PostProcessorFunc func(name string) (PostProcessor, error)
|
||||
|
||||
// The function type used to lookup Provisioner implementations.
|
||||
type ProvisionerFunc func(name string) (Provisioner, error)
|
||||
|
||||
// ComponentFinder is a struct that contains the various function
|
||||
// pointers necessary to look up components of Packer such as builders,
|
||||
// commands, etc.
|
||||
type ComponentFinder struct {
|
||||
Builder BuilderFunc
|
||||
Hook HookFunc
|
||||
PostProcessor PostProcessorFunc
|
||||
Provisioner ProvisionerFunc
|
||||
}
|
||||
|
||||
// The environment interface provides access to the configuration and
|
||||
// state of a single Packer run.
|
||||
//
|
||||
// It allows for things such as executing CLI commands, getting the
|
||||
// list of available builders, and more.
|
||||
type Environment interface {
|
||||
Builder(string) (Builder, error)
|
||||
Cache() Cache
|
||||
Hook(string) (Hook, error)
|
||||
PostProcessor(string) (PostProcessor, error)
|
||||
Provisioner(string) (Provisioner, error)
|
||||
Ui() Ui
|
||||
}
|
||||
|
||||
// An implementation of an Environment that represents the Packer core
|
||||
// environment.
|
||||
type coreEnvironment struct {
|
||||
cache Cache
|
||||
components ComponentFinder
|
||||
ui Ui
|
||||
}
|
||||
|
||||
// This struct configures new environments.
|
||||
type EnvironmentConfig struct {
|
||||
Cache Cache
|
||||
Components ComponentFinder
|
||||
Ui Ui
|
||||
}
|
||||
|
||||
// DefaultEnvironmentConfig returns a default EnvironmentConfig that can
|
||||
// be used to create a new enviroment with NewEnvironment with sane defaults.
|
||||
func DefaultEnvironmentConfig() *EnvironmentConfig {
|
||||
config := &EnvironmentConfig{}
|
||||
config.Ui = &BasicUi{
|
||||
Reader: os.Stdin,
|
||||
Writer: os.Stdout,
|
||||
ErrorWriter: os.Stdout,
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
// This creates a new environment
|
||||
func NewEnvironment(config *EnvironmentConfig) (resultEnv Environment, err error) {
|
||||
if config == nil {
|
||||
err = errors.New("config must be given to initialize environment")
|
||||
return
|
||||
}
|
||||
|
||||
env := &coreEnvironment{}
|
||||
env.cache = config.Cache
|
||||
env.components = config.Components
|
||||
env.ui = config.Ui
|
||||
|
||||
// We want to make sure the components have valid function pointers.
|
||||
// If a function pointer was not given, we assume that the function
|
||||
// will just return a nil component.
|
||||
if env.components.Builder == nil {
|
||||
env.components.Builder = func(string) (Builder, error) { return nil, nil }
|
||||
}
|
||||
|
||||
if env.components.Hook == nil {
|
||||
env.components.Hook = func(string) (Hook, error) { return nil, nil }
|
||||
}
|
||||
|
||||
if env.components.PostProcessor == nil {
|
||||
env.components.PostProcessor = func(string) (PostProcessor, error) { return nil, nil }
|
||||
}
|
||||
|
||||
if env.components.Provisioner == nil {
|
||||
env.components.Provisioner = func(string) (Provisioner, error) { return nil, nil }
|
||||
}
|
||||
|
||||
// The default cache is just the system temporary directory
|
||||
if env.cache == nil {
|
||||
env.cache = &FileCache{CacheDir: os.TempDir()}
|
||||
}
|
||||
|
||||
resultEnv = env
|
||||
return
|
||||
}
|
||||
|
||||
// Returns a builder of the given name that is registered with this
|
||||
// environment.
|
||||
func (e *coreEnvironment) Builder(name string) (b Builder, err error) {
|
||||
b, err = e.components.Builder(name)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if b == nil {
|
||||
err = fmt.Errorf("No builder returned for name: %s", name)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Returns the cache for this environment
|
||||
func (e *coreEnvironment) Cache() Cache {
|
||||
return e.cache
|
||||
}
|
||||
|
||||
// Returns a hook of the given name that is registered with this
|
||||
// environment.
|
||||
func (e *coreEnvironment) Hook(name string) (h Hook, err error) {
|
||||
h, err = e.components.Hook(name)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if h == nil {
|
||||
err = fmt.Errorf("No hook returned for name: %s", name)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Returns a PostProcessor for the given name that is registered with this
|
||||
// environment.
|
||||
func (e *coreEnvironment) PostProcessor(name string) (p PostProcessor, err error) {
|
||||
p, err = e.components.PostProcessor(name)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if p == nil {
|
||||
err = fmt.Errorf("No post processor found for name: %s", name)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Returns a provisioner for the given name that is registered with this
|
||||
// environment.
|
||||
func (e *coreEnvironment) Provisioner(name string) (p Provisioner, err error) {
|
||||
p, err = e.components.Provisioner(name)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if p == nil {
|
||||
err = fmt.Errorf("No provisioner returned for name: %s", name)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Returns the UI for the environment. The UI is the interface that should
|
||||
// be used for all communication with the outside world.
|
||||
func (e *coreEnvironment) Ui() Ui {
|
||||
return e.ui
|
||||
}
|
||||
@ -1,310 +0,0 @@
|
||||
package packer
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// Disable log output for tests
|
||||
log.SetOutput(ioutil.Discard)
|
||||
}
|
||||
|
||||
func testComponentFinder() *ComponentFinder {
|
||||
builderFactory := func(n string) (Builder, error) { return new(MockBuilder), nil }
|
||||
ppFactory := func(n string) (PostProcessor, error) { return new(TestPostProcessor), nil }
|
||||
provFactory := func(n string) (Provisioner, error) { return new(MockProvisioner), nil }
|
||||
return &ComponentFinder{
|
||||
Builder: builderFactory,
|
||||
PostProcessor: ppFactory,
|
||||
Provisioner: provFactory,
|
||||
}
|
||||
}
|
||||
|
||||
func testEnvironment() Environment {
|
||||
config := DefaultEnvironmentConfig()
|
||||
config.Ui = &BasicUi{
|
||||
Reader: new(bytes.Buffer),
|
||||
Writer: new(bytes.Buffer),
|
||||
ErrorWriter: new(bytes.Buffer),
|
||||
}
|
||||
|
||||
env, err := NewEnvironment(config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return env
|
||||
}
|
||||
|
||||
func TestEnvironment_DefaultConfig_Ui(t *testing.T) {
|
||||
config := DefaultEnvironmentConfig()
|
||||
if config.Ui == nil {
|
||||
t.Fatal("config.Ui should not be nil")
|
||||
}
|
||||
|
||||
rwUi, ok := config.Ui.(*BasicUi)
|
||||
if !ok {
|
||||
t.Fatal("default UI should be BasicUi")
|
||||
}
|
||||
if rwUi.Writer != os.Stdout {
|
||||
t.Fatal("default UI should go to stdout")
|
||||
}
|
||||
if rwUi.Reader != os.Stdin {
|
||||
t.Fatal("default UI reader should go to stdin")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewEnvironment_NoConfig(t *testing.T) {
|
||||
env, err := NewEnvironment(nil)
|
||||
if env != nil {
|
||||
t.Fatal("env should be nil")
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatal("should have error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvironment_NilComponents(t *testing.T) {
|
||||
config := DefaultEnvironmentConfig()
|
||||
config.Components = *new(ComponentFinder)
|
||||
|
||||
env, err := NewEnvironment(config)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
// All of these should not cause panics... so we don't assert
|
||||
// anything but if there is a panic in the test then yeah, something
|
||||
// went wrong.
|
||||
env.Builder("foo")
|
||||
env.Hook("foo")
|
||||
env.PostProcessor("foo")
|
||||
env.Provisioner("foo")
|
||||
}
|
||||
|
||||
func TestEnvironment_Builder(t *testing.T) {
|
||||
builder := &MockBuilder{}
|
||||
builders := make(map[string]Builder)
|
||||
builders["foo"] = builder
|
||||
|
||||
config := DefaultEnvironmentConfig()
|
||||
config.Components.Builder = func(n string) (Builder, error) { return builders[n], nil }
|
||||
|
||||
env, _ := NewEnvironment(config)
|
||||
returnedBuilder, err := env.Builder("foo")
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if returnedBuilder != builder {
|
||||
t.Fatalf("bad: %#v", returnedBuilder)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvironment_Builder_NilError(t *testing.T) {
|
||||
config := DefaultEnvironmentConfig()
|
||||
config.Components.Builder = func(n string) (Builder, error) { return nil, nil }
|
||||
|
||||
env, _ := NewEnvironment(config)
|
||||
returnedBuilder, err := env.Builder("foo")
|
||||
if err == nil {
|
||||
t.Fatal("should have error")
|
||||
}
|
||||
if returnedBuilder != nil {
|
||||
t.Fatalf("bad: %#v", returnedBuilder)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvironment_Builder_Error(t *testing.T) {
|
||||
config := DefaultEnvironmentConfig()
|
||||
config.Components.Builder = func(n string) (Builder, error) { return nil, errors.New("foo") }
|
||||
|
||||
env, _ := NewEnvironment(config)
|
||||
returnedBuilder, err := env.Builder("foo")
|
||||
if err == nil {
|
||||
t.Fatal("should have error")
|
||||
}
|
||||
if err.Error() != "foo" {
|
||||
t.Fatalf("bad err: %s", err)
|
||||
}
|
||||
if returnedBuilder != nil {
|
||||
t.Fatalf("should be nil: %#v", returnedBuilder)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvironment_Cache(t *testing.T) {
|
||||
config := DefaultEnvironmentConfig()
|
||||
env, _ := NewEnvironment(config)
|
||||
if env.Cache() == nil {
|
||||
t.Fatal("cache should not be nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvironment_Hook(t *testing.T) {
|
||||
hook := &MockHook{}
|
||||
hooks := make(map[string]Hook)
|
||||
hooks["foo"] = hook
|
||||
|
||||
config := DefaultEnvironmentConfig()
|
||||
config.Components.Hook = func(n string) (Hook, error) { return hooks[n], nil }
|
||||
|
||||
env, _ := NewEnvironment(config)
|
||||
returned, err := env.Hook("foo")
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if returned != hook {
|
||||
t.Fatalf("bad: %#v", returned)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvironment_Hook_NilError(t *testing.T) {
|
||||
config := DefaultEnvironmentConfig()
|
||||
config.Components.Hook = func(n string) (Hook, error) { return nil, nil }
|
||||
|
||||
env, _ := NewEnvironment(config)
|
||||
returned, err := env.Hook("foo")
|
||||
if err == nil {
|
||||
t.Fatal("should have error")
|
||||
}
|
||||
if returned != nil {
|
||||
t.Fatalf("bad: %#v", returned)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvironment_Hook_Error(t *testing.T) {
|
||||
config := DefaultEnvironmentConfig()
|
||||
config.Components.Hook = func(n string) (Hook, error) { return nil, errors.New("foo") }
|
||||
|
||||
env, _ := NewEnvironment(config)
|
||||
returned, err := env.Hook("foo")
|
||||
if err == nil {
|
||||
t.Fatal("should have error")
|
||||
}
|
||||
if err.Error() != "foo" {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if returned != nil {
|
||||
t.Fatalf("bad: %#v", returned)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvironment_PostProcessor(t *testing.T) {
|
||||
pp := &TestPostProcessor{}
|
||||
pps := make(map[string]PostProcessor)
|
||||
pps["foo"] = pp
|
||||
|
||||
config := DefaultEnvironmentConfig()
|
||||
config.Components.PostProcessor = func(n string) (PostProcessor, error) { return pps[n], nil }
|
||||
|
||||
env, _ := NewEnvironment(config)
|
||||
returned, err := env.PostProcessor("foo")
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if returned != pp {
|
||||
t.Fatalf("bad: %#v", returned)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvironment_PostProcessor_NilError(t *testing.T) {
|
||||
config := DefaultEnvironmentConfig()
|
||||
config.Components.PostProcessor = func(n string) (PostProcessor, error) { return nil, nil }
|
||||
|
||||
env, _ := NewEnvironment(config)
|
||||
returned, err := env.PostProcessor("foo")
|
||||
if err == nil {
|
||||
t.Fatal("should have error")
|
||||
}
|
||||
if returned != nil {
|
||||
t.Fatalf("bad: %#v", returned)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvironment_PostProcessor_Error(t *testing.T) {
|
||||
config := DefaultEnvironmentConfig()
|
||||
config.Components.PostProcessor = func(n string) (PostProcessor, error) { return nil, errors.New("foo") }
|
||||
|
||||
env, _ := NewEnvironment(config)
|
||||
returned, err := env.PostProcessor("foo")
|
||||
if err == nil {
|
||||
t.Fatal("should be an error")
|
||||
}
|
||||
if err.Error() != "foo" {
|
||||
t.Fatalf("bad err: %s", err)
|
||||
}
|
||||
if returned != nil {
|
||||
t.Fatalf("bad: %#v", returned)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvironmentProvisioner(t *testing.T) {
|
||||
p := &MockProvisioner{}
|
||||
ps := make(map[string]Provisioner)
|
||||
ps["foo"] = p
|
||||
|
||||
config := DefaultEnvironmentConfig()
|
||||
config.Components.Provisioner = func(n string) (Provisioner, error) { return ps[n], nil }
|
||||
|
||||
env, _ := NewEnvironment(config)
|
||||
returned, err := env.Provisioner("foo")
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if returned != p {
|
||||
t.Fatalf("bad: %#v", returned)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvironmentProvisioner_NilError(t *testing.T) {
|
||||
config := DefaultEnvironmentConfig()
|
||||
config.Components.Provisioner = func(n string) (Provisioner, error) { return nil, nil }
|
||||
|
||||
env, _ := NewEnvironment(config)
|
||||
returned, err := env.Provisioner("foo")
|
||||
if err == nil {
|
||||
t.Fatal("should have error")
|
||||
}
|
||||
if returned != nil {
|
||||
t.Fatalf("bad: %#v", returned)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvironmentProvisioner_Error(t *testing.T) {
|
||||
config := DefaultEnvironmentConfig()
|
||||
config.Components.Provisioner = func(n string) (Provisioner, error) {
|
||||
return nil, errors.New("foo")
|
||||
}
|
||||
|
||||
env, _ := NewEnvironment(config)
|
||||
returned, err := env.Provisioner("foo")
|
||||
if err == nil {
|
||||
t.Fatal("should have error")
|
||||
}
|
||||
if err.Error() != "foo" {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if returned != nil {
|
||||
t.Fatalf("bad: %#v", returned)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvironment_SettingUi(t *testing.T) {
|
||||
ui := &BasicUi{
|
||||
Reader: new(bytes.Buffer),
|
||||
Writer: new(bytes.Buffer),
|
||||
}
|
||||
|
||||
config := &EnvironmentConfig{}
|
||||
config.Ui = ui
|
||||
|
||||
env, _ := NewEnvironment(config)
|
||||
|
||||
if env.Ui() != ui {
|
||||
t.Fatalf("UI should be equal: %#v", env.Ui())
|
||||
}
|
||||
}
|
||||
@ -1,178 +0,0 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"log"
|
||||
"net/rpc"
|
||||
)
|
||||
|
||||
// A Environment is an implementation of the packer.Environment interface
|
||||
// where the actual environment is executed over an RPC connection.
|
||||
type Environment struct {
|
||||
client *rpc.Client
|
||||
mux *muxBroker
|
||||
}
|
||||
|
||||
// A EnvironmentServer wraps a packer.Environment and makes it exportable
|
||||
// as part of a Golang RPC server.
|
||||
type EnvironmentServer struct {
|
||||
env packer.Environment
|
||||
mux *muxBroker
|
||||
}
|
||||
|
||||
func (e *Environment) Builder(name string) (b packer.Builder, err error) {
|
||||
var streamId uint32
|
||||
err = e.client.Call("Environment.Builder", name, &streamId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
client, err := newClientWithMux(e.mux, streamId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b = client.Builder()
|
||||
return
|
||||
}
|
||||
|
||||
func (e *Environment) Cache() packer.Cache {
|
||||
var streamId uint32
|
||||
if err := e.client.Call("Environment.Cache", new(interface{}), &streamId); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
client, err := newClientWithMux(e.mux, streamId)
|
||||
if err != nil {
|
||||
log.Printf("[ERR] Error getting cache client: %s", err)
|
||||
return nil
|
||||
}
|
||||
return client.Cache()
|
||||
}
|
||||
|
||||
func (e *Environment) Hook(name string) (h packer.Hook, err error) {
|
||||
var streamId uint32
|
||||
err = e.client.Call("Environment.Hook", name, &streamId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
client, err := newClientWithMux(e.mux, streamId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return client.Hook(), nil
|
||||
}
|
||||
|
||||
func (e *Environment) PostProcessor(name string) (p packer.PostProcessor, err error) {
|
||||
var streamId uint32
|
||||
err = e.client.Call("Environment.PostProcessor", name, &streamId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
client, err := newClientWithMux(e.mux, streamId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p = client.PostProcessor()
|
||||
return
|
||||
}
|
||||
|
||||
func (e *Environment) Provisioner(name string) (p packer.Provisioner, err error) {
|
||||
var streamId uint32
|
||||
err = e.client.Call("Environment.Provisioner", name, &streamId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
client, err := newClientWithMux(e.mux, streamId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p = client.Provisioner()
|
||||
return
|
||||
}
|
||||
|
||||
func (e *Environment) Ui() packer.Ui {
|
||||
var streamId uint32
|
||||
e.client.Call("Environment.Ui", new(interface{}), &streamId)
|
||||
|
||||
client, err := newClientWithMux(e.mux, streamId)
|
||||
if err != nil {
|
||||
log.Printf("[ERR] Error connecting to Ui: %s", err)
|
||||
return nil
|
||||
}
|
||||
return client.Ui()
|
||||
}
|
||||
|
||||
func (e *EnvironmentServer) Builder(name string, reply *uint32) error {
|
||||
builder, err := e.env.Builder(name)
|
||||
if err != nil {
|
||||
return NewBasicError(err)
|
||||
}
|
||||
|
||||
*reply = e.mux.NextId()
|
||||
server := newServerWithMux(e.mux, *reply)
|
||||
server.RegisterBuilder(builder)
|
||||
go server.Serve()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *EnvironmentServer) Cache(args *interface{}, reply *uint32) error {
|
||||
cache := e.env.Cache()
|
||||
|
||||
*reply = e.mux.NextId()
|
||||
server := newServerWithMux(e.mux, *reply)
|
||||
server.RegisterCache(cache)
|
||||
go server.Serve()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *EnvironmentServer) Hook(name string, reply *uint32) error {
|
||||
hook, err := e.env.Hook(name)
|
||||
if err != nil {
|
||||
return NewBasicError(err)
|
||||
}
|
||||
|
||||
*reply = e.mux.NextId()
|
||||
server := newServerWithMux(e.mux, *reply)
|
||||
server.RegisterHook(hook)
|
||||
go server.Serve()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *EnvironmentServer) PostProcessor(name string, reply *uint32) error {
|
||||
pp, err := e.env.PostProcessor(name)
|
||||
if err != nil {
|
||||
return NewBasicError(err)
|
||||
}
|
||||
|
||||
*reply = e.mux.NextId()
|
||||
server := newServerWithMux(e.mux, *reply)
|
||||
server.RegisterPostProcessor(pp)
|
||||
go server.Serve()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *EnvironmentServer) Provisioner(name string, reply *uint32) error {
|
||||
prov, err := e.env.Provisioner(name)
|
||||
if err != nil {
|
||||
return NewBasicError(err)
|
||||
}
|
||||
|
||||
*reply = e.mux.NextId()
|
||||
server := newServerWithMux(e.mux, *reply)
|
||||
server.RegisterProvisioner(prov)
|
||||
go server.Serve()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *EnvironmentServer) Ui(args *interface{}, reply *uint32) error {
|
||||
ui := e.env.Ui()
|
||||
|
||||
*reply = e.mux.NextId()
|
||||
server := newServerWithMux(e.mux, *reply)
|
||||
server.RegisterUi(ui)
|
||||
go server.Serve()
|
||||
return nil
|
||||
}
|
||||
@ -1,124 +0,0 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var testEnvBuilder = &packer.MockBuilder{}
|
||||
var testEnvCache = &testCache{}
|
||||
var testEnvUi = &testUi{}
|
||||
|
||||
type testEnvironment struct {
|
||||
builderCalled bool
|
||||
builderName string
|
||||
cliCalled bool
|
||||
cliArgs []string
|
||||
hookCalled bool
|
||||
hookName string
|
||||
ppCalled bool
|
||||
ppName string
|
||||
provCalled bool
|
||||
provName string
|
||||
uiCalled bool
|
||||
}
|
||||
|
||||
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) {
|
||||
e.cliCalled = true
|
||||
e.cliArgs = args
|
||||
return 42, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (e *testEnvironment) Provisioner(name string) (packer.Provisioner, error) {
|
||||
e.provCalled = true
|
||||
e.provName = name
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
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
|
||||
client, server := testClientServer(t)
|
||||
defer client.Close()
|
||||
defer server.Close()
|
||||
server.RegisterEnvironment(e)
|
||||
eClient := client.Environment()
|
||||
|
||||
// Test Builder
|
||||
builder, _ := eClient.Builder("foo")
|
||||
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 {
|
||||
t.Fatal("should be called")
|
||||
}
|
||||
|
||||
// Test Cache
|
||||
cache := eClient.Cache()
|
||||
cache.Lock("foo")
|
||||
if !testEnvCache.lockCalled {
|
||||
t.Fatal("should be called")
|
||||
}
|
||||
|
||||
// Test Provisioner
|
||||
_, _ = eClient.Provisioner("foo")
|
||||
if !e.provCalled {
|
||||
t.Fatal("should be called")
|
||||
}
|
||||
if e.provName != "foo" {
|
||||
t.Fatalf("bad: %s", e.provName)
|
||||
}
|
||||
|
||||
// Test Ui
|
||||
ui := eClient.Ui()
|
||||
if !e.uiCalled {
|
||||
t.Fatal("should be called")
|
||||
}
|
||||
|
||||
// Test calls on the Ui
|
||||
ui.Say("format")
|
||||
if !testEnvUi.sayCalled {
|
||||
t.Fatal("should be called")
|
||||
}
|
||||
if testEnvUi.sayMessage != "format" {
|
||||
t.Fatalf("bad: %#v", testEnvUi.sayMessage)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvironment_ImplementsEnvironment(t *testing.T) {
|
||||
var _ packer.Environment = new(Environment)
|
||||
}
|
||||
Loading…
Reference in new issue