github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/parallels/common/step_shutdown_test.go (about)

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