2015-06-21 06:35:52 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 19:03:49 -05:00
|
|
|
"context"
|
2015-06-21 06:35:52 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2015-06-21 06:35:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStepCompactDisk_impl(t *testing.T) {
|
|
|
|
var _ multistep.Step = new(StepCompactDisk)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepCompactDisk(t *testing.T) {
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
tf.Close()
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
|
|
|
state := testState(t)
|
|
|
|
step := new(StepCompactDisk)
|
|
|
|
|
|
|
|
state.Put("vmName", "foo")
|
|
|
|
|
|
|
|
driver := state.Get("driver").(*DriverMock)
|
|
|
|
|
|
|
|
// Mock results
|
|
|
|
driver.DiskPathResult = tf.Name()
|
|
|
|
|
|
|
|
// Test the run
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2015-06-21 06:35:52 -04:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
path, _ := driver.DiskPath("foo")
|
|
|
|
if path != tf.Name() {
|
|
|
|
t.Fatal("should call with right path")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepCompactDisk_skip(t *testing.T) {
|
|
|
|
state := testState(t)
|
|
|
|
step := new(StepCompactDisk)
|
|
|
|
step.Skip = true
|
|
|
|
|
|
|
|
state.Put("vmName", "foo")
|
|
|
|
|
|
|
|
driver := state.Get("driver").(*DriverMock)
|
|
|
|
|
|
|
|
// Test the run
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2015-06-21 06:35:52 -04:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|