github.com/mitchellh/packer@v1.3.2/builder/vmware/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 testOutputDir(t *testing.T) *LocalOutputDir {
    13  	td, err := ioutil.TempDir("", "packer")
    14  	if err != nil {
    15  		t.Fatalf("err: %s", err)
    16  	}
    17  	os.RemoveAll(td)
    18  
    19  	result := new(LocalOutputDir)
    20  	result.SetOutputDir(td)
    21  	return result
    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 := new(StepOutputDir)
    31  
    32  	dir := testOutputDir(t)
    33  	// Delete the test output directory when done
    34  	defer os.RemoveAll(dir.dir)
    35  	state.Put("dir", dir)
    36  
    37  	// Test the run
    38  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
    39  		t.Fatalf("bad action: %#v", action)
    40  	}
    41  	if _, ok := state.GetOk("error"); ok {
    42  		t.Fatal("should NOT have error")
    43  	}
    44  	if _, err := os.Stat(dir.dir); err != nil {
    45  		t.Fatalf("err: %s", err)
    46  	}
    47  
    48  	// Test the cleanup
    49  	step.Cleanup(state)
    50  	if _, err := os.Stat(dir.dir); err != nil {
    51  		t.Fatalf("err: %s", err)
    52  	}
    53  }
    54  
    55  func TestStepOutputDir_existsNoForce(t *testing.T) {
    56  	state := testState(t)
    57  	step := new(StepOutputDir)
    58  
    59  	dir := testOutputDir(t)
    60  	state.Put("dir", dir)
    61  
    62  	// Make sure the dir exists
    63  	if err := os.MkdirAll(dir.dir, 0755); err != nil {
    64  		t.Fatalf("err: %s", err)
    65  	}
    66  	defer os.RemoveAll(dir.dir)
    67  
    68  	// Test the run
    69  	if action := step.Run(context.Background(), state); action != multistep.ActionHalt {
    70  		t.Fatalf("bad action: %#v", action)
    71  	}
    72  	if _, ok := state.GetOk("error"); !ok {
    73  		t.Fatal("should have error")
    74  	}
    75  
    76  	// Test the cleanup
    77  	step.Cleanup(state)
    78  	if _, err := os.Stat(dir.dir); err != nil {
    79  		t.Fatal("should not delete dir")
    80  	}
    81  }
    82  
    83  func TestStepOutputDir_existsForce(t *testing.T) {
    84  	state := testState(t)
    85  	step := new(StepOutputDir)
    86  	step.Force = true
    87  
    88  	dir := testOutputDir(t)
    89  	state.Put("dir", dir)
    90  
    91  	// Make sure the dir exists
    92  	if err := os.MkdirAll(dir.dir, 0755); err != nil {
    93  		t.Fatalf("err: %s", err)
    94  	}
    95  	defer os.RemoveAll(dir.dir)
    96  
    97  	// Test the run
    98  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
    99  		t.Fatalf("bad action: %#v", action)
   100  	}
   101  	if _, ok := state.GetOk("error"); ok {
   102  		t.Fatal("should NOT have error")
   103  	}
   104  	if _, err := os.Stat(dir.dir); err != nil {
   105  		t.Fatalf("err: %s", err)
   106  	}
   107  }
   108  
   109  func TestStepOutputDir_cancel(t *testing.T) {
   110  	state := testState(t)
   111  	step := new(StepOutputDir)
   112  
   113  	dir := testOutputDir(t)
   114  	state.Put("dir", dir)
   115  
   116  	// Test the run
   117  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
   118  		t.Fatalf("bad action: %#v", action)
   119  	}
   120  	if _, ok := state.GetOk("error"); ok {
   121  		t.Fatal("should NOT have error")
   122  	}
   123  	if _, err := os.Stat(dir.dir); err != nil {
   124  		t.Fatalf("err: %s", err)
   125  	}
   126  
   127  	// Test cancel/halt
   128  	state.Put(multistep.StateCancelled, true)
   129  	step.Cleanup(state)
   130  	if _, err := os.Stat(dir.dir); err == nil {
   131  		t.Fatal("directory should not exist")
   132  	}
   133  }
   134  
   135  func TestStepOutputDir_halt(t *testing.T) {
   136  	state := testState(t)
   137  	step := new(StepOutputDir)
   138  
   139  	dir := testOutputDir(t)
   140  	state.Put("dir", dir)
   141  
   142  	// Test the run
   143  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
   144  		t.Fatalf("bad action: %#v", action)
   145  	}
   146  	if _, ok := state.GetOk("error"); ok {
   147  		t.Fatal("should NOT have error")
   148  	}
   149  	if _, err := os.Stat(dir.dir); err != nil {
   150  		t.Fatalf("err: %s", err)
   151  	}
   152  
   153  	// Test cancel/halt
   154  	state.Put(multistep.StateHalted, true)
   155  	step.Cleanup(state)
   156  	if _, err := os.Stat(dir.dir); err == nil {
   157  		t.Fatal("directory should not exist")
   158  	}
   159  }