github.com/mitchellh/packer@v1.3.2/builder/virtualbox/common/step_output_dir_test.go (about)

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