github.com/mitchellh/packer@v1.3.2/builder/virtualbox/common/step_attach_floppy_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 TestStepAttachFloppy_impl(t *testing.T) {
    13  	var _ multistep.Step = new(StepAttachFloppy)
    14  }
    15  
    16  func TestStepAttachFloppy(t *testing.T) {
    17  	state := testState(t)
    18  	step := new(StepAttachFloppy)
    19  
    20  	// Create a temporary file for our floppy file
    21  	tf, err := ioutil.TempFile("", "packer")
    22  	if err != nil {
    23  		t.Fatalf("err: %s", err)
    24  	}
    25  	tf.Close()
    26  	defer os.Remove(tf.Name())
    27  
    28  	state.Put("floppy_path", tf.Name())
    29  	state.Put("vmName", "foo")
    30  
    31  	driver := state.Get("driver").(*DriverMock)
    32  
    33  	// Test the run
    34  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
    35  		t.Fatalf("bad action: %#v", action)
    36  	}
    37  	if _, ok := state.GetOk("error"); ok {
    38  		t.Fatal("should NOT have error")
    39  	}
    40  
    41  	if len(driver.VBoxManageCalls) != 2 {
    42  		t.Fatal("not enough calls to VBoxManage")
    43  	}
    44  	if driver.VBoxManageCalls[0][0] != "storagectl" {
    45  		t.Fatal("bad call")
    46  	}
    47  	if driver.VBoxManageCalls[1][0] != "storageattach" {
    48  		t.Fatal("bad call")
    49  	}
    50  
    51  	// Test the cleanup
    52  	step.Cleanup(state)
    53  	if driver.VBoxManageCalls[2][0] != "storageattach" {
    54  		t.Fatal("bad call")
    55  	}
    56  }
    57  
    58  func TestStepAttachFloppy_noFloppy(t *testing.T) {
    59  	state := testState(t)
    60  	step := new(StepAttachFloppy)
    61  
    62  	driver := state.Get("driver").(*DriverMock)
    63  
    64  	// Test the run
    65  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
    66  		t.Fatalf("bad action: %#v", action)
    67  	}
    68  	if _, ok := state.GetOk("error"); ok {
    69  		t.Fatal("should NOT have error")
    70  	}
    71  
    72  	if len(driver.VBoxManageCalls) > 0 {
    73  		t.Fatal("should not call vboxmanage")
    74  	}
    75  }