github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/parallels/common/step_output_dir_test.go (about)

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