github.com/rothwerx/packer@v0.9.0/builder/virtualbox/common/step_output_dir_test.go (about)

     1  package common
     2  
     3  import (
     4  	"github.com/mitchellh/multistep"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  )
     9  
    10  func testStepOutputDir(t *testing.T) *StepOutputDir {
    11  	td, err := ioutil.TempDir("", "packer")
    12  	if err != nil {
    13  		t.Fatalf("err: %s", err)
    14  	}
    15  	if err := os.RemoveAll(td); err != nil {
    16  		t.Fatalf("err: %s", err)
    17  	}
    18  
    19  	return &StepOutputDir{Force: false, Path: td}
    20  }
    21  
    22  func TestStepOutputDir_impl(t *testing.T) {
    23  	var _ multistep.Step = new(StepOutputDir)
    24  }
    25  
    26  func TestStepOutputDir(t *testing.T) {
    27  	state := testState(t)
    28  	step := testStepOutputDir(t)
    29  
    30  	// Test the run
    31  	if action := step.Run(state); action != multistep.ActionContinue {
    32  		t.Fatalf("bad action: %#v", action)
    33  	}
    34  	if _, ok := state.GetOk("error"); ok {
    35  		t.Fatal("should NOT have error")
    36  	}
    37  	if _, err := os.Stat(step.Path); err != nil {
    38  		t.Fatalf("err: %s", err)
    39  	}
    40  
    41  	// Test the cleanup
    42  	step.Cleanup(state)
    43  	if _, err := os.Stat(step.Path); err != nil {
    44  		t.Fatalf("err: %s", err)
    45  	}
    46  }
    47  
    48  func TestStepOutputDir_exists(t *testing.T) {
    49  	state := testState(t)
    50  	step := testStepOutputDir(t)
    51  
    52  	// Make the dir
    53  	if err := os.MkdirAll(step.Path, 0755); err != nil {
    54  		t.Fatalf("bad: %s", err)
    55  	}
    56  
    57  	// Test the run
    58  	if action := step.Run(state); action != multistep.ActionHalt {
    59  		t.Fatalf("bad action: %#v", action)
    60  	}
    61  	if _, ok := state.GetOk("error"); !ok {
    62  		t.Fatal("should have error")
    63  	}
    64  
    65  	// Test the cleanup
    66  	step.Cleanup(state)
    67  	if _, err := os.Stat(step.Path); err != nil {
    68  		t.Fatalf("err: %s", err)
    69  	}
    70  }
    71  
    72  func TestStepOutputDir_cancelled(t *testing.T) {
    73  	state := testState(t)
    74  	step := testStepOutputDir(t)
    75  
    76  	// Test the run
    77  	if action := step.Run(state); action != multistep.ActionContinue {
    78  		t.Fatalf("bad action: %#v", action)
    79  	}
    80  	if _, ok := state.GetOk("error"); ok {
    81  		t.Fatal("should NOT have error")
    82  	}
    83  	if _, err := os.Stat(step.Path); err != nil {
    84  		t.Fatalf("err: %s", err)
    85  	}
    86  
    87  	// Mark
    88  	state.Put(multistep.StateCancelled, true)
    89  
    90  	// Test the cleanup
    91  	step.Cleanup(state)
    92  	if _, err := os.Stat(step.Path); err == nil {
    93  		t.Fatal("should not exist")
    94  	}
    95  }
    96  
    97  func TestStepOutputDir_halted(t *testing.T) {
    98  	state := testState(t)
    99  	step := testStepOutputDir(t)
   100  
   101  	// Test the run
   102  	if action := step.Run(state); action != multistep.ActionContinue {
   103  		t.Fatalf("bad action: %#v", action)
   104  	}
   105  	if _, ok := state.GetOk("error"); ok {
   106  		t.Fatal("should NOT have error")
   107  	}
   108  	if _, err := os.Stat(step.Path); err != nil {
   109  		t.Fatalf("err: %s", err)
   110  	}
   111  
   112  	// Mark
   113  	state.Put(multistep.StateHalted, true)
   114  
   115  	// Test the cleanup
   116  	step.Cleanup(state)
   117  	if _, err := os.Stat(step.Path); err == nil {
   118  		t.Fatal("should not exist")
   119  	}
   120  }