mirror of https://github.com/hashicorp/packer
parent
0de7bb33cd
commit
dd767c9d54
@ -0,0 +1,47 @@
|
||||
package ovf
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/mitchellh/multistep"
|
||||
vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
)
|
||||
|
||||
// This step imports an OVF VM into VirtualBox.
|
||||
type StepImport struct {
|
||||
Name string
|
||||
SourcePath string
|
||||
|
||||
vmName string
|
||||
}
|
||||
|
||||
func (s *StepImport) Run(state multistep.StateBag) multistep.StepAction {
|
||||
driver := state.Get("driver").(vboxcommon.Driver)
|
||||
ui := state.Get("ui").(packer.Ui)
|
||||
|
||||
ui.Say(fmt.Sprintf("Importing VM: %s", s.SourcePath))
|
||||
if err := driver.Import(s.Name, s.SourcePath); err != nil {
|
||||
err := fmt.Errorf("Error importing VM: %s", err)
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
s.vmName = s.Name
|
||||
state.Put("vmName", s.Name)
|
||||
return multistep.ActionContinue
|
||||
}
|
||||
|
||||
func (s *StepImport) Cleanup(state multistep.StateBag) {
|
||||
if s.vmName == "" {
|
||||
return
|
||||
}
|
||||
|
||||
driver := state.Get("driver").(vboxcommon.Driver)
|
||||
ui := state.Get("ui").(packer.Ui)
|
||||
|
||||
ui.Say("Unregistering and deleting imported VM...")
|
||||
if err := driver.Delete(s.vmName); err != nil {
|
||||
ui.Error(fmt.Sprintf("Error deleting VM: %s", err))
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,55 @@
|
||||
package ovf
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/multistep"
|
||||
vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStepImport_impl(t *testing.T) {
|
||||
var _ multistep.Step = new(StepImport)
|
||||
}
|
||||
|
||||
func TestStepImport(t *testing.T) {
|
||||
state := testState(t)
|
||||
step := new(StepImport)
|
||||
step.Name = "bar"
|
||||
step.SourcePath = "foo"
|
||||
|
||||
driver := state.Get("driver").(*vboxcommon.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 driver
|
||||
if !driver.ImportCalled {
|
||||
t.Fatal("import should be called")
|
||||
}
|
||||
if driver.ImportName != step.Name {
|
||||
t.Fatalf("bad: %#v", driver.ImportName)
|
||||
}
|
||||
if driver.ImportPath != step.SourcePath {
|
||||
t.Fatalf("bad: %#v", driver.ImportPath)
|
||||
}
|
||||
|
||||
// Test output state
|
||||
if name, ok := state.GetOk("vmName"); !ok {
|
||||
t.Fatal("vmName should be set")
|
||||
} else if name != "bar" {
|
||||
t.Fatalf("bad: %#v", name)
|
||||
}
|
||||
|
||||
// Test cleanup
|
||||
step.Cleanup(state)
|
||||
if !driver.DeleteCalled {
|
||||
t.Fatal("delete should be called")
|
||||
}
|
||||
if driver.DeleteName != "bar" {
|
||||
t.Fatalf("bad: %#v", driver.DeleteName)
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
package ovf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/mitchellh/multistep"
|
||||
vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func testState(t *testing.T) multistep.StateBag {
|
||||
state := new(multistep.BasicStateBag)
|
||||
state.Put("driver", new(vboxcommon.DriverMock))
|
||||
state.Put("ui", &packer.BasicUi{
|
||||
Reader: new(bytes.Buffer),
|
||||
Writer: new(bytes.Buffer),
|
||||
})
|
||||
return state
|
||||
}
|
||||
Loading…
Reference in new issue