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

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