github.phpd.cn/hashicorp/packer@v1.3.2/builder/hyperv/common/step_output_dir_test.go (about)

     1  package common
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/packer/helper/multistep"
     9  )
    10  
    11  func TestStepOutputDir_imp(t *testing.T) {
    12  	var _ multistep.Step = new(StepOutputDir)
    13  }
    14  
    15  func TestStepOutputDir_Default(t *testing.T) {
    16  	state := testState(t)
    17  	step := new(StepOutputDir)
    18  
    19  	step.Path = genTestDirPath("packerHypervOutput")
    20  
    21  	// Test the run
    22  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
    23  		t.Fatalf("Bad action: %v", action)
    24  	}
    25  	if _, ok := state.GetOk("error"); ok {
    26  		t.Fatal("Should NOT have error")
    27  	}
    28  
    29  	// The directory should have been created
    30  	if _, err := os.Stat(step.Path); err != nil {
    31  		t.Fatal("Should have created output directory")
    32  	}
    33  
    34  	// Remove the directory created due to test
    35  	err := os.RemoveAll(step.Path)
    36  	if err != nil {
    37  		t.Fatalf("Error encountered removing directory created by test: %s", err)
    38  	}
    39  }
    40  
    41  func TestStepOutputDir_DirectoryAlreadyExistsNoForce(t *testing.T) {
    42  	state := testState(t)
    43  	step := new(StepOutputDir)
    44  
    45  	step.Path = genTestDirPath("packerHypervOutput")
    46  
    47  	// Create the directory so that we can test
    48  	err := os.Mkdir(step.Path, 0755)
    49  	if err != nil {
    50  		t.Fatal("Test failed to create directory for test of Cancel and Cleanup")
    51  	}
    52  	defer os.RemoveAll(step.Path) // Ensure we clean up if something goes wrong
    53  
    54  	step.Force = false // Default
    55  	// Test the run
    56  	if action := step.Run(context.Background(), state); action != multistep.ActionHalt {
    57  		t.Fatalf("Should halt when directory exists and 'Force' is false. Bad action: %v", action)
    58  	}
    59  	if _, ok := state.GetOk("error"); !ok {
    60  		t.Fatal("Should error when directory exists and 'Force' is false")
    61  	}
    62  }
    63  
    64  func TestStepOutputDir_DirectoryAlreadyExistsForce(t *testing.T) {
    65  	state := testState(t)
    66  	step := new(StepOutputDir)
    67  
    68  	step.Path = genTestDirPath("packerHypervOutput")
    69  
    70  	// Create the directory so that we can test
    71  	err := os.Mkdir(step.Path, 0755)
    72  	if err != nil {
    73  		t.Fatal("Test failed to create directory for test of Cancel and Cleanup")
    74  	}
    75  	defer os.RemoveAll(step.Path) // Ensure we clean up if something goes wrong
    76  
    77  	step.Force = true // User specified that existing directory and contents should be discarded
    78  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
    79  		t.Fatalf("Should complete when directory exists and 'Force' is true. Bad action: %v", action)
    80  	}
    81  	if _, ok := state.GetOk("error"); ok {
    82  		t.Fatalf("Should NOT error when directory exists and 'Force' is true: %s", err)
    83  	}
    84  }
    85  
    86  func TestStepOutputDir_CleanupBuildCancelled(t *testing.T) {
    87  	state := testState(t)
    88  	step := new(StepOutputDir)
    89  
    90  	step.Path = genTestDirPath("packerHypervOutput")
    91  
    92  	// Create the directory so that we can test the cleanup
    93  	err := os.Mkdir(step.Path, 0755)
    94  	if err != nil {
    95  		t.Fatal("Test failed to create directory for test of Cancel and Cleanup")
    96  	}
    97  	defer os.RemoveAll(step.Path) // Ensure we clean up if something goes wrong
    98  
    99  	// 'Cancel' the build
   100  	state.Put(multistep.StateCancelled, true)
   101  
   102  	// Ensure the directory isn't removed if the cleanup flag is false
   103  	step.cleanup = false
   104  	step.Cleanup(state)
   105  	if _, err := os.Stat(step.Path); err != nil {
   106  		t.Fatal("Output dir should NOT be removed if on 'Cancel' if cleanup flag is unset/false")
   107  	}
   108  
   109  	// Ensure the directory is removed if the cleanup flag is true
   110  	step.cleanup = true
   111  	step.Cleanup(state)
   112  	if _, err := os.Stat(step.Path); err == nil {
   113  		t.Fatalf("Output directory should NOT exist after 'Cancel' and Cleanup: %s", step.Path)
   114  	}
   115  }
   116  
   117  func TestStepOutputDir_CleanupBuildHalted(t *testing.T) {
   118  	state := testState(t)
   119  	step := new(StepOutputDir)
   120  
   121  	step.Path = genTestDirPath("packerHypervOutput")
   122  
   123  	// Create the directory so that we can test the cleanup
   124  	err := os.Mkdir(step.Path, 0755)
   125  	if err != nil {
   126  		t.Fatal("Test failed to create directory for test of Cancel and Cleanup")
   127  	}
   128  	defer os.RemoveAll(step.Path) // Ensure we clean up if something goes wrong
   129  
   130  	// 'Halt' the build and test the directory is removed
   131  	state.Put(multistep.StateHalted, true)
   132  
   133  	// Ensure the directory isn't removed if the cleanup flag is false
   134  	step.cleanup = false
   135  	step.Cleanup(state)
   136  	if _, err := os.Stat(step.Path); err != nil {
   137  		t.Fatal("Output dir should NOT be removed if on 'Halt' if cleanup flag is unset/false")
   138  	}
   139  
   140  	// Ensure the directory is removed if the cleanup flag is true
   141  	step.cleanup = true
   142  	step.Cleanup(state)
   143  	if _, err := os.Stat(step.Path); err == nil {
   144  		t.Fatalf("Output directory should NOT exist after 'Halt' and Cleanup: %s", step.Path)
   145  	}
   146  }