From 87ab914a3cf2dec6e679e6347099ace076bb9b94 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 24 Dec 2013 23:09:22 -0700 Subject: [PATCH] builder/vmware: StepCompactDisk --- .../{iso => common}/step_compact_disk.go | 17 +++--- .../vmware/common/step_compact_disk_test.go | 59 +++++++++++++++++++ builder/vmware/iso/builder.go | 4 +- 3 files changed, 70 insertions(+), 10 deletions(-) rename builder/vmware/{iso => common}/step_compact_disk.go (65%) create mode 100644 builder/vmware/common/step_compact_disk_test.go diff --git a/builder/vmware/iso/step_compact_disk.go b/builder/vmware/common/step_compact_disk.go similarity index 65% rename from builder/vmware/iso/step_compact_disk.go rename to builder/vmware/common/step_compact_disk.go index 8b4cf6593..777e8aaea 100644 --- a/builder/vmware/iso/step_compact_disk.go +++ b/builder/vmware/common/step_compact_disk.go @@ -1,9 +1,8 @@ -package iso +package common import ( "fmt" "github.com/mitchellh/multistep" - vmwcommon "github.com/mitchellh/packer/builder/vmware/common" "github.com/mitchellh/packer/packer" "log" ) @@ -12,22 +11,22 @@ import ( // boolean is true. // // Uses: -// config *config // driver Driver // full_disk_path string // ui packer.Ui // // Produces: // -type stepCompactDisk struct{} +type StepCompactDisk struct { + Skip bool +} -func (stepCompactDisk) Run(state multistep.StateBag) multistep.StepAction { - config := state.Get("config").(*config) - driver := state.Get("driver").(vmwcommon.Driver) +func (s StepCompactDisk) Run(state multistep.StateBag) multistep.StepAction { + driver := state.Get("driver").(Driver) ui := state.Get("ui").(packer.Ui) full_disk_path := state.Get("full_disk_path").(string) - if config.SkipCompaction == true { + if s.Skip { log.Println("Skipping disk compaction step...") return multistep.ActionContinue } @@ -41,4 +40,4 @@ func (stepCompactDisk) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionContinue } -func (stepCompactDisk) Cleanup(multistep.StateBag) {} +func (StepCompactDisk) Cleanup(multistep.StateBag) {} diff --git a/builder/vmware/common/step_compact_disk_test.go b/builder/vmware/common/step_compact_disk_test.go new file mode 100644 index 000000000..e59a99a90 --- /dev/null +++ b/builder/vmware/common/step_compact_disk_test.go @@ -0,0 +1,59 @@ +package common + +import ( + "testing" + + "github.com/mitchellh/multistep" +) + +func TestStepCompactDisk_impl(t *testing.T) { + var _ multistep.Step = new(StepCompactDisk) +} + +func TestStepCompactDisk(t *testing.T) { + state := testState(t) + step := new(StepCompactDisk) + + state.Put("full_disk_path", "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 the driver + if !driver.CompactDiskCalled { + t.Fatal("should've called") + } + if driver.CompactDiskPath != "foo" { + t.Fatal("should call with right path") + } +} + +func TestStepCompactDisk_skip(t *testing.T) { + state := testState(t) + step := new(StepCompactDisk) + step.Skip = true + + state.Put("full_disk_path", "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 the driver + if driver.CompactDiskCalled { + t.Fatal("should not have called") + } +} diff --git a/builder/vmware/iso/builder.go b/builder/vmware/iso/builder.go index e65185afd..9bbbf851a 100644 --- a/builder/vmware/iso/builder.go +++ b/builder/vmware/iso/builder.go @@ -413,7 +413,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &stepShutdown{}, &vmwcommon.StepCleanFiles{}, &vmwcommon.StepCleanVMX{}, - &stepCompactDisk{}, + &vmwcommon.StepCompactDisk{ + Skip: b.config.SkipCompaction, + }, } // Run!