github.com/mitchellh/packer@v1.3.2/builder/virtualbox/ovf/step_import_test.go (about)

     1  package ovf
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common"
     8  	"github.com/hashicorp/packer/helper/multistep"
     9  )
    10  
    11  func TestStepImport_impl(t *testing.T) {
    12  	var _ multistep.Step = new(StepImport)
    13  }
    14  
    15  func TestStepImport(t *testing.T) {
    16  	state := testState(t)
    17  	c := testConfig(t)
    18  	config, _, _ := NewConfig(c)
    19  	state.Put("vm_path", "foo")
    20  	state.Put("config", config)
    21  	step := new(StepImport)
    22  	step.Name = "bar"
    23  
    24  	driver := state.Get("driver").(*vboxcommon.DriverMock)
    25  
    26  	// Test the run
    27  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
    28  		t.Fatalf("bad action: %#v", action)
    29  	}
    30  	if _, ok := state.GetOk("error"); ok {
    31  		t.Fatal("should NOT have error")
    32  	}
    33  
    34  	// Test driver
    35  	if !driver.ImportCalled {
    36  		t.Fatal("import should be called")
    37  	}
    38  	if driver.ImportName != step.Name {
    39  		t.Fatalf("bad: %#v", driver.ImportName)
    40  	}
    41  
    42  	// Test output state
    43  	if name, ok := state.GetOk("vmName"); !ok {
    44  		t.Fatal("vmName should be set")
    45  	} else if name != "bar" {
    46  		t.Fatalf("bad: %#v", name)
    47  	}
    48  
    49  	// Test cleanup
    50  	config.KeepRegistered = true
    51  	step.Cleanup(state)
    52  
    53  	if driver.DeleteCalled {
    54  		t.Fatal("delete should not be called")
    55  	}
    56  
    57  	config.KeepRegistered = false
    58  	step.Cleanup(state)
    59  	if !driver.DeleteCalled {
    60  		t.Fatal("delete should be called")
    61  	}
    62  	if driver.DeleteName != "bar" {
    63  		t.Fatalf("bad: %#v", driver.DeleteName)
    64  	}
    65  }