github.com/mitchellh/packer@v1.3.2/builder/virtualbox/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  }
   108  
   109  func TestStepShutdown_shutdownDelay(t *testing.T) {
   110  	state := testState(t)
   111  	step := new(StepShutdown)
   112  	step.Command = "poweroff"
   113  	step.Timeout = 5 * time.Second
   114  	step.Delay = 2 * time.Second
   115  
   116  	comm := new(packer.MockCommunicator)
   117  	state.Put("communicator", comm)
   118  	state.Put("vmName", "foo")
   119  
   120  	driver := state.Get("driver").(*DriverMock)
   121  	driver.IsRunningReturn = true
   122  	start := time.Now()
   123  
   124  	go func() {
   125  		time.Sleep(10 * time.Millisecond)
   126  		driver.Lock()
   127  		defer driver.Unlock()
   128  		driver.IsRunningReturn = false
   129  	}()
   130  
   131  	// Test the run
   132  
   133  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
   134  		t.Fatalf("bad action: %#v", action)
   135  	}
   136  	testDuration := time.Since(start)
   137  	if testDuration < 2500*time.Millisecond || testDuration > 2700*time.Millisecond {
   138  		t.Fatalf("incorrect duration %s", testDuration)
   139  	}
   140  
   141  	if _, ok := state.GetOk("error"); ok {
   142  		t.Fatal("should NOT have error")
   143  	}
   144  
   145  	step.Delay = 0
   146  
   147  	driver.IsRunningReturn = true
   148  	start = time.Now()
   149  
   150  	go func() {
   151  		time.Sleep(10 * time.Millisecond)
   152  		driver.Lock()
   153  		defer driver.Unlock()
   154  		driver.IsRunningReturn = false
   155  	}()
   156  
   157  	// Test the run
   158  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
   159  		t.Fatalf("bad action: %#v", action)
   160  	}
   161  	testDuration = time.Since(start)
   162  	if testDuration > 600*time.Millisecond {
   163  		t.Fatalf("incorrect duration %s", testDuration)
   164  	}
   165  
   166  	if _, ok := state.GetOk("error"); ok {
   167  		t.Fatal("should NOT have error")
   168  	}
   169  
   170  }