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

     1  package common
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/packer/helper/multistep"
     8  )
     9  
    10  func TestStepCompactDisk_impl(t *testing.T) {
    11  	var _ multistep.Step = new(StepCompactDisk)
    12  }
    13  
    14  func TestStepCompactDisk(t *testing.T) {
    15  	state := testState(t)
    16  	step := new(StepCompactDisk)
    17  
    18  	// Set up the path to the build directory
    19  	buildDir := "foopath"
    20  	state.Put("build_dir", buildDir)
    21  
    22  	driver := state.Get("driver").(*DriverMock)
    23  
    24  	// Test the run
    25  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
    26  		t.Fatalf("Bad action: %v", action)
    27  	}
    28  	if _, ok := state.GetOk("error"); ok {
    29  		t.Fatal("Should NOT have error")
    30  	}
    31  
    32  	// Test the driver
    33  	if !driver.CompactDisks_Called {
    34  		t.Fatal("Should have called CompactDisks")
    35  	}
    36  	if driver.CompactDisks_Path != buildDir {
    37  		t.Fatalf("Should call with correct path. Got: %s Wanted: %s", driver.CompactDisks_Path, buildDir)
    38  	}
    39  }
    40  
    41  func TestStepCompactDisk_skip(t *testing.T) {
    42  	state := testState(t)
    43  	step := new(StepCompactDisk)
    44  	step.SkipCompaction = true
    45  
    46  	// Set up the path to the build directory
    47  	state.Put("build_dir", "foopath")
    48  
    49  	driver := state.Get("driver").(*DriverMock)
    50  
    51  	// Test the run
    52  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
    53  		t.Fatalf("Bad action: %v", action)
    54  	}
    55  	if _, ok := state.GetOk("error"); ok {
    56  		t.Fatalf("Should NOT have error")
    57  	}
    58  
    59  	// Test the driver
    60  	if driver.CompactDisks_Called {
    61  		t.Fatal("Should NOT have called CompactDisks")
    62  	}
    63  }