builder/virtualbox/common: StepSuppressMessages

This commit is contained in:
Mitchell Hashimoto 2013-12-21 16:04:41 -08:00
parent db167c5a3d
commit 886c0d3ad5
6 changed files with 128 additions and 8 deletions

View File

@ -0,0 +1,65 @@
package common
type DriverMock struct {
CreateSATAControllerVM string
CreateSATAControllerController string
CreateSATAControllerErr error
IsRunningName string
IsRunningReturn bool
IsRunningErr error
StopName string
StopErr error
SuppressMessagesCalled bool
SuppressMessagesErr error
VBoxManageCalled bool
VBoxManageArgs []string
VBoxManageErr error
VerifyCalled bool
VerifyErr error
VersionCalled bool
VersionResult string
VersionErr error
}
func (d *DriverMock) CreateSATAController(vm string, controller string) error {
d.CreateSATAControllerVM = vm
d.CreateSATAControllerController = vm
return d.CreateSATAControllerErr
}
func (d *DriverMock) IsRunning(name string) (bool, error) {
d.IsRunningName = name
return d.IsRunningReturn, d.IsRunningErr
}
func (d *DriverMock) Stop(name string) error {
d.StopName = name
return d.StopErr
}
func (d *DriverMock) SuppressMessages() error {
d.SuppressMessagesCalled = true
return d.SuppressMessagesErr
}
func (d *DriverMock) VBoxManage(args ...string) error {
d.VBoxManageCalled = true
d.VBoxManageArgs = args
return d.VBoxManageErr
}
func (d *DriverMock) Verify() error {
d.VerifyCalled = true
return d.VerifyErr
}
func (d *DriverMock) Version() (string, error) {
d.VersionCalled = true
return d.VersionResult, d.VersionErr
}

View File

@ -1,19 +1,18 @@
package iso
package common
import (
"fmt"
"github.com/mitchellh/multistep"
vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common"
"github.com/mitchellh/packer/packer"
"log"
)
// This step sets some variables in VirtualBox so that annoying
// pop-up messages don't exist.
type stepSuppressMessages struct{}
type StepSuppressMessages struct{}
func (stepSuppressMessages) Run(state multistep.StateBag) multistep.StepAction {
driver := state.Get("driver").(vboxcommon.Driver)
func (StepSuppressMessages) Run(state multistep.StateBag) multistep.StepAction {
driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui)
log.Println("Suppressing annoying messages in VirtualBox")
@ -27,4 +26,4 @@ func (stepSuppressMessages) Run(state multistep.StateBag) multistep.StepAction {
return multistep.ActionContinue
}
func (stepSuppressMessages) Cleanup(multistep.StateBag) {}
func (StepSuppressMessages) Cleanup(multistep.StateBag) {}

View File

@ -0,0 +1,50 @@
package common
import (
"errors"
"github.com/mitchellh/multistep"
"testing"
)
func TestStepSuppressMessages_impl(t *testing.T) {
var _ multistep.Step = new(StepSuppressMessages)
}
func TestStepSuppressMessages(t *testing.T) {
state := testState(t)
step := new(StepSuppressMessages)
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")
}
if !driver.SuppressMessagesCalled {
t.Fatal("should call suppressmessages")
}
}
func TestStepSuppressMessages_error(t *testing.T) {
state := testState(t)
step := new(StepSuppressMessages)
driver := state.Get("driver").(*DriverMock)
driver.SuppressMessagesErr = errors.New("foo")
// Test the run
if action := step.Run(state); action != multistep.ActionHalt {
t.Fatalf("bad action: %#v", action)
}
if _, ok := state.GetOk("error"); !ok {
t.Fatal("should have error")
}
if !driver.SuppressMessagesCalled {
t.Fatal("should call suppressmessages")
}
}

View File

@ -9,6 +9,7 @@ import (
func testState(t *testing.T) multistep.StateBag {
state := new(multistep.BasicStateBag)
state.Put("driver", new(DriverMock))
state.Put("ui", &packer.BasicUi{
Reader: new(bytes.Buffer),
Writer: new(bytes.Buffer),

View File

@ -401,7 +401,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
Files: b.config.FloppyFiles,
},
new(stepHTTPServer),
new(stepSuppressMessages),
new(vboxcommon.StepSuppressMessages),
new(stepCreateVM),
new(stepCreateDisk),
new(stepAttachISO),

View File

@ -37,7 +37,12 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
new(stepDownloadGuestAdditions),
*/
/*
new(stepPrepareOutputDir),
&vboxcommon.StepOutputDir{
Force: b.config.PackerForce,
Path: b.config.OutputDir,
},
*/
/*
&common.StepCreateFloppy{
Files: b.config.FloppyFiles,
},