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

     1  package common
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/mitchellh/multistep"
    11  	"github.com/mitchellh/packer/packer"
    12  )
    13  
    14  func testStepShutdownState(t *testing.T) multistep.StateBag {
    15  	dir := testOutputDir(t)
    16  	if err := dir.MkdirAll(); err != nil {
    17  		t.Fatalf("err: %s", err)
    18  	}
    19  
    20  	state := testState(t)
    21  	state.Put("communicator", new(packer.MockCommunicator))
    22  	state.Put("dir", dir)
    23  	state.Put("vmx_path", "foo")
    24  	return state
    25  }
    26  
    27  func TestStepShutdown_impl(t *testing.T) {
    28  	var _ multistep.Step = new(StepShutdown)
    29  }
    30  
    31  func TestStepShutdown_command(t *testing.T) {
    32  	state := testStepShutdownState(t)
    33  	step := new(StepShutdown)
    34  	step.Command = "foo"
    35  	step.Timeout = 10 * time.Second
    36  
    37  	comm := state.Get("communicator").(*packer.MockCommunicator)
    38  	driver := state.Get("driver").(*DriverMock)
    39  	driver.IsRunningResult = true
    40  
    41  	// Set not running after some time
    42  	go func() {
    43  		time.Sleep(100 * time.Millisecond)
    44  		driver.Lock()
    45  		defer driver.Unlock()
    46  		driver.IsRunningResult = false
    47  	}()
    48  
    49  	resultCh := make(chan multistep.StepAction, 1)
    50  	go func() {
    51  		resultCh <- step.Run(state)
    52  	}()
    53  
    54  	select {
    55  	case <-resultCh:
    56  		t.Fatal("should not have returned so quickly")
    57  	case <-time.After(50 * time.Millisecond):
    58  	}
    59  
    60  	var action multistep.StepAction
    61  	select {
    62  	case action = <-resultCh:
    63  	case <-time.After(300 * time.Millisecond):
    64  		t.Fatal("should've returned by now")
    65  	}
    66  
    67  	// Test the run
    68  	if action != multistep.ActionContinue {
    69  		t.Fatalf("bad action: %#v", action)
    70  	}
    71  	if _, ok := state.GetOk("error"); ok {
    72  		t.Fatal("should NOT have error")
    73  	}
    74  
    75  	// Test the driver
    76  	if driver.StopCalled {
    77  		t.Fatal("stop should not be called")
    78  	}
    79  
    80  	if !comm.StartCalled {
    81  		t.Fatal("start should be called")
    82  	}
    83  	if comm.StartCmd.Command != "foo" {
    84  		t.Fatalf("bad: %#v", comm.StartCmd.Command)
    85  	}
    86  }
    87  
    88  func TestStepShutdown_noCommand(t *testing.T) {
    89  	state := testStepShutdownState(t)
    90  	step := new(StepShutdown)
    91  
    92  	comm := state.Get("communicator").(*packer.MockCommunicator)
    93  	driver := state.Get("driver").(*DriverMock)
    94  
    95  	// Test the run
    96  	if action := step.Run(state); action != multistep.ActionContinue {
    97  		t.Fatalf("bad action: %#v", action)
    98  	}
    99  	if _, ok := state.GetOk("error"); ok {
   100  		t.Fatal("should NOT have error")
   101  	}
   102  
   103  	// Test the driver
   104  	if !driver.StopCalled {
   105  		t.Fatal("stop should be called")
   106  	}
   107  	if driver.StopPath != "foo" {
   108  		t.Fatal("should call with right path")
   109  	}
   110  
   111  	if comm.StartCalled {
   112  		t.Fatal("start should not be called")
   113  	}
   114  }
   115  
   116  func TestStepShutdown_locks(t *testing.T) {
   117  	state := testStepShutdownState(t)
   118  	step := new(StepShutdown)
   119  
   120  	dir := state.Get("dir").(*LocalOutputDir)
   121  	comm := state.Get("communicator").(*packer.MockCommunicator)
   122  	driver := state.Get("driver").(*DriverMock)
   123  
   124  	// Create some lock files
   125  	lockPath := filepath.Join(dir.dir, "nope.lck")
   126  	err := ioutil.WriteFile(lockPath, []byte("foo"), 0644)
   127  	if err != nil {
   128  		t.Fatalf("err: %s")
   129  	}
   130  
   131  	// Remove the lock file after a certain time
   132  	go func() {
   133  		time.Sleep(100 * time.Millisecond)
   134  		os.Remove(lockPath)
   135  	}()
   136  
   137  	resultCh := make(chan multistep.StepAction, 1)
   138  	go func() {
   139  		resultCh <- step.Run(state)
   140  	}()
   141  
   142  	select {
   143  	case <-resultCh:
   144  		t.Fatal("should not have returned so quickly")
   145  	case <-time.After(50 * time.Millisecond):
   146  	}
   147  
   148  	var action multistep.StepAction
   149  	select {
   150  	case action = <-resultCh:
   151  	case <-time.After(300 * time.Millisecond):
   152  		t.Fatal("should've returned by now")
   153  	}
   154  
   155  	// Test the run
   156  	if action != multistep.ActionContinue {
   157  		t.Fatalf("bad action: %#v", action)
   158  	}
   159  	if _, ok := state.GetOk("error"); ok {
   160  		t.Fatal("should NOT have error")
   161  	}
   162  
   163  	// Test the driver
   164  	if !driver.StopCalled {
   165  		t.Fatal("stop should be called")
   166  	}
   167  	if driver.StopPath != "foo" {
   168  		t.Fatal("should call with right path")
   169  	}
   170  
   171  	if comm.StartCalled {
   172  		t.Fatal("start should not be called")
   173  	}
   174  }