mirror of https://github.com/hashicorp/packer
parent
026bcd33fe
commit
5feb7bce18
@ -0,0 +1,37 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
)
|
||||
|
||||
type ExportConfig struct {
|
||||
Format string `mapstruture:"format"`
|
||||
}
|
||||
|
||||
func (c *ExportConfig) Prepare(t *packer.ConfigTemplate) []error {
|
||||
if c.Format == "" {
|
||||
c.Format = "ovf"
|
||||
}
|
||||
|
||||
templates := map[string]*string{
|
||||
"format": &c.Format,
|
||||
}
|
||||
|
||||
errs := make([]error, 0)
|
||||
for n, ptr := range templates {
|
||||
var err error
|
||||
*ptr, err = t.Process(*ptr, nil)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("Error processing %s: %s", n, err))
|
||||
}
|
||||
}
|
||||
|
||||
if c.Format != "ovf" && c.Format != "ova" {
|
||||
errs = append(errs,
|
||||
errors.New("invalid format, only 'ovf' or 'ova' are allowed"))
|
||||
}
|
||||
|
||||
return errs
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestExportConfigPrepare_BootWait(t *testing.T) {
|
||||
var c *ExportConfig
|
||||
var errs []error
|
||||
|
||||
// Bad
|
||||
c = new(ExportConfig)
|
||||
c.Format = "illega"
|
||||
errs = c.Prepare(testConfigTemplate(t))
|
||||
if len(errs) == 0 {
|
||||
t.Fatalf("bad: %#v", errs)
|
||||
}
|
||||
|
||||
// Good
|
||||
c = new(ExportConfig)
|
||||
c.Format = "ova"
|
||||
errs = c.Prepare(testConfigTemplate(t))
|
||||
if len(errs) > 0 {
|
||||
t.Fatalf("should not have error: %s", errs)
|
||||
}
|
||||
|
||||
// Good
|
||||
c = new(ExportConfig)
|
||||
c.Format = "ovf"
|
||||
errs = c.Prepare(testConfigTemplate(t))
|
||||
if len(errs) > 0 {
|
||||
t.Fatalf("should not have error: %s", errs)
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,43 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/multistep"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStepExport_impl(t *testing.T) {
|
||||
var _ multistep.Step = new(StepExport)
|
||||
}
|
||||
|
||||
func TestStepExport(t *testing.T) {
|
||||
state := testState(t)
|
||||
step := new(StepExport)
|
||||
|
||||
state.Put("vmName", "foo")
|
||||
|
||||
driver := state.Get("driver").(*DriverMock)
|
||||
|
||||
// Test the run
|
||||
if action := step.Run(state); action != multistep.ActionContinue {
|
||||
t.Fatalf("bad action: %#v", action)
|
||||
}
|
||||
if _, ok := state.GetOk("error"); ok {
|
||||
t.Fatal("should NOT have error")
|
||||
}
|
||||
|
||||
// Test output state
|
||||
if _, ok := state.GetOk("exportPath"); !ok {
|
||||
t.Fatal("should set exportPath")
|
||||
}
|
||||
|
||||
// Test driver
|
||||
if len(driver.VBoxManageCalls) != 2 {
|
||||
t.Fatal("should call vboxmanage")
|
||||
}
|
||||
if driver.VBoxManageCalls[0][0] != "modifyvm" {
|
||||
t.Fatal("bad")
|
||||
}
|
||||
if driver.VBoxManageCalls[1][0] != "export" {
|
||||
t.Fatal("bad")
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue