github.phpd.cn/hashicorp/packer@v1.3.2/builder/parallels/common/step_compact_disk_test.go (about)

     1  package common
     2  
     3  import (
     4  	"context"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/packer/helper/multistep"
    10  )
    11  
    12  func TestStepCompactDisk_impl(t *testing.T) {
    13  	var _ multistep.Step = new(StepCompactDisk)
    14  }
    15  
    16  func TestStepCompactDisk(t *testing.T) {
    17  	tf, err := ioutil.TempFile("", "packer")
    18  	if err != nil {
    19  		t.Fatalf("err: %s", err)
    20  	}
    21  	tf.Close()
    22  	defer os.Remove(tf.Name())
    23  
    24  	state := testState(t)
    25  	step := new(StepCompactDisk)
    26  
    27  	state.Put("vmName", "foo")
    28  
    29  	driver := state.Get("driver").(*DriverMock)
    30  
    31  	// Mock results
    32  	driver.DiskPathResult = tf.Name()
    33  
    34  	// Test the run
    35  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
    36  		t.Fatalf("bad action: %#v", action)
    37  	}
    38  	if _, ok := state.GetOk("error"); ok {
    39  		t.Fatal("should NOT have error")
    40  	}
    41  
    42  	// Test the driver
    43  	if !driver.CompactDiskCalled {
    44  		t.Fatal("should've called")
    45  	}
    46  
    47  	path, _ := driver.DiskPath("foo")
    48  	if path != tf.Name() {
    49  		t.Fatal("should call with right path")
    50  	}
    51  }
    52  
    53  func TestStepCompactDisk_skip(t *testing.T) {
    54  	state := testState(t)
    55  	step := new(StepCompactDisk)
    56  	step.Skip = true
    57  
    58  	state.Put("vmName", "foo")
    59  
    60  	driver := state.Get("driver").(*DriverMock)
    61  
    62  	// Test the run
    63  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
    64  		t.Fatalf("bad action: %#v", action)
    65  	}
    66  	if _, ok := state.GetOk("error"); ok {
    67  		t.Fatal("should NOT have error")
    68  	}
    69  
    70  	// Test the driver
    71  	if driver.CompactDiskCalled {
    72  		t.Fatal("should not have called")
    73  	}
    74  }