github.com/sneal/packer@v0.5.2/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_cancelled(t *testing.T) {
    49  	state := testState(t)
    50  	step := testStepOutputDir(t)
    51  
    52  	// Test the run
    53  	if action := step.Run(state); action != multistep.ActionContinue {
    54  		t.Fatalf("bad action: %#v", action)
    55  	}
    56  	if _, ok := state.GetOk("error"); ok {
    57  		t.Fatal("should NOT have error")
    58  	}
    59  	if _, err := os.Stat(step.Path); err != nil {
    60  		t.Fatalf("err: %s", err)
    61  	}
    62  
    63  	// Mark
    64  	state.Put(multistep.StateCancelled, true)
    65  
    66  	// Test the cleanup
    67  	step.Cleanup(state)
    68  	if _, err := os.Stat(step.Path); err == nil {
    69  		t.Fatal("should not exist")
    70  	}
    71  }
    72  
    73  func TestStepOutputDir_halted(t *testing.T) {
    74  	state := testState(t)
    75  	step := testStepOutputDir(t)
    76  
    77  	// Test the run
    78  	if action := step.Run(state); action != multistep.ActionContinue {
    79  		t.Fatalf("bad action: %#v", action)
    80  	}
    81  	if _, ok := state.GetOk("error"); ok {
    82  		t.Fatal("should NOT have error")
    83  	}
    84  	if _, err := os.Stat(step.Path); err != nil {
    85  		t.Fatalf("err: %s", err)
    86  	}
    87  
    88  	// Mark
    89  	state.Put(multistep.StateHalted, true)
    90  
    91  	// Test the cleanup
    92  	step.Cleanup(state)
    93  	if _, err := os.Stat(step.Path); err == nil {
    94  		t.Fatal("should not exist")
    95  	}
    96  }