diff --git a/packer/core.go b/packer/core.go index de6fe4169..caa18a196 100644 --- a/packer/core.go +++ b/packer/core.go @@ -47,6 +47,35 @@ func NewCore(c *CoreConfig) (*Core, error) { }, nil } +// Build returns the Build object for the given name. +func (c *Core) Build(n string) (Build, error) { + // Setup the builder + configBuilder, ok := c.template.Builders[n] + if !ok { + return nil, fmt.Errorf("no such build found: %s", n) + } + builder, err := c.components.Builder(configBuilder.Type) + if err != nil { + return nil, fmt.Errorf( + "error initializing builder '%s': %s", + configBuilder.Type, err) + } + if builder == nil { + return nil, fmt.Errorf( + "builder type not found: %s", configBuilder.Type) + } + + // TODO: template process name + + return &coreBuild{ + name: n, + builder: builder, + builderConfig: configBuilder.Config, + builderType: configBuilder.Type, + variables: c.variables, + }, nil +} + // Validate does a full validation of the template. // // This will automatically call template.Validate() in addition to doing diff --git a/packer/testing.go b/packer/testing.go new file mode 100644 index 000000000..099119180 --- /dev/null +++ b/packer/testing.go @@ -0,0 +1,44 @@ +package packer + +import ( + "bytes" + "io/ioutil" + "os" + "testing" +) + +func TestCoreConfig(t *testing.T) *CoreConfig { + // Create a UI that is effectively /dev/null everywhere + var buf bytes.Buffer + ui := &BasicUi{ + Reader: &buf, + Writer: ioutil.Discard, + ErrorWriter: ioutil.Discard, + } + + // Create some test components + components := ComponentFinder{ + Builder: func(n string) (Builder, error) { + if n != "test" { + return nil, nil + } + + return &MockBuilder{}, nil + }, + } + + return &CoreConfig{ + Cache: &FileCache{CacheDir: os.TempDir()}, + Components: components, + Ui: ui, + } +} + +func TestCore(t *testing.T, c *CoreConfig) *Core { + core, err := NewCore(c) + if err != nil { + t.Fatalf("err: %s", err) + } + + return core +}