github.com/sneal/packer@v0.5.2/builder/virtualbox/common/step_shutdown_test.go (about)

     1  package common
     2  
     3  import (
     4  	"github.com/mitchellh/multistep"
     5  	"github.com/mitchellh/packer/packer"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  func TestStepShutdown_impl(t *testing.T) {
    11  	var _ multistep.Step = new(StepShutdown)
    12  }
    13  
    14  func TestStepShutdown_noShutdownCommand(t *testing.T) {
    15  	state := testState(t)
    16  	step := new(StepShutdown)
    17  
    18  	comm := new(packer.MockCommunicator)
    19  	state.Put("communicator", comm)
    20  	state.Put("vmName", "foo")
    21  
    22  	driver := state.Get("driver").(*DriverMock)
    23  
    24  	// Test the run
    25  	if action := step.Run(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 that Stop was just called
    33  	if driver.StopName != "foo" {
    34  		t.Fatal("should call stop")
    35  	}
    36  	if comm.StartCalled {
    37  		t.Fatal("comm start should not be called")
    38  	}
    39  }
    40  
    41  func TestStepShutdown_shutdownCommand(t *testing.T) {
    42  	state := testState(t)
    43  	step := new(StepShutdown)
    44  	step.Command = "poweroff"
    45  	step.Timeout = 1 * time.Second
    46  
    47  	comm := new(packer.MockCommunicator)
    48  	state.Put("communicator", comm)
    49  	state.Put("vmName", "foo")
    50  
    51  	driver := state.Get("driver").(*DriverMock)
    52  	driver.IsRunningReturn = true
    53  
    54  	go func() {
    55  		time.Sleep(10 * time.Millisecond)
    56  		driver.Lock()
    57  		defer driver.Unlock()
    58  		driver.IsRunningReturn = false
    59  	}()
    60  
    61  	// Test the run
    62  	if action := step.Run(state); action != multistep.ActionContinue {
    63  		t.Fatalf("bad action: %#v", action)
    64  	}
    65  	if _, ok := state.GetOk("error"); ok {
    66  		t.Fatal("should NOT have error")
    67  	}
    68  
    69  	// Test that Stop was just called
    70  	if driver.StopName != "" {
    71  		t.Fatal("should not call stop")
    72  	}
    73  	if comm.StartCmd.Command != step.Command {
    74  		t.Fatal("comm start should be called")
    75  	}
    76  }
    77  
    78  func TestStepShutdown_shutdownTimeout(t *testing.T) {
    79  	state := testState(t)
    80  	step := new(StepShutdown)
    81  	step.Command = "poweroff"
    82  	step.Timeout = 1 * time.Second
    83  
    84  	comm := new(packer.MockCommunicator)
    85  	state.Put("communicator", comm)
    86  	state.Put("vmName", "foo")
    87  
    88  	driver := state.Get("driver").(*DriverMock)
    89  	driver.IsRunningReturn = true
    90  
    91  	go func() {
    92  		time.Sleep(2 * time.Second)
    93  		driver.Lock()
    94  		defer driver.Unlock()
    95  		driver.IsRunningReturn = false
    96  	}()
    97  
    98  	// Test the run
    99  	if action := step.Run(state); action != multistep.ActionHalt {
   100  		t.Fatalf("bad action: %#v", action)
   101  	}
   102  	if _, ok := state.GetOk("error"); !ok {
   103  		t.Fatal("should have error")
   104  	}
   105  }