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

     1  // +build !windows
     2  
     3  package common
     4  
     5  import (
     6  	"io/ioutil"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/hashicorp/packer/common"
    11  )
    12  
    13  func TestOutputConfigPrepare(t *testing.T) {
    14  	c := new(OutputConfig)
    15  	if c.OutputDir != "" {
    16  		t.Fatalf("what: %s", c.OutputDir)
    17  	}
    18  
    19  	pc := &common.PackerConfig{PackerBuildName: "foo"}
    20  	errs := c.Prepare(testConfigTemplate(t), pc)
    21  	if len(errs) > 0 {
    22  		t.Fatalf("err: %#v", errs)
    23  	}
    24  
    25  	if c.OutputDir == "" {
    26  		t.Fatal("should have output dir")
    27  	}
    28  }
    29  
    30  func TestOutputConfigPrepare_exists(t *testing.T) {
    31  	td, err := ioutil.TempDir("", "packer")
    32  	if err != nil {
    33  		t.Fatalf("err: %s", err)
    34  	}
    35  	defer os.RemoveAll(td)
    36  
    37  	c := new(OutputConfig)
    38  	c.OutputDir = td
    39  
    40  	pc := &common.PackerConfig{
    41  		PackerBuildName: "foo",
    42  		PackerForce:     false,
    43  	}
    44  	errs := c.Prepare(testConfigTemplate(t), pc)
    45  	if len(errs) == 0 {
    46  		t.Fatal("should have errors")
    47  	}
    48  }
    49  
    50  func TestOutputConfigPrepare_forceExists(t *testing.T) {
    51  	td, err := ioutil.TempDir("", "packer")
    52  	if err != nil {
    53  		t.Fatalf("err: %s", err)
    54  	}
    55  	defer os.RemoveAll(td)
    56  
    57  	c := new(OutputConfig)
    58  	c.OutputDir = td
    59  
    60  	pc := &common.PackerConfig{
    61  		PackerBuildName: "foo",
    62  		PackerForce:     true,
    63  	}
    64  	errs := c.Prepare(testConfigTemplate(t), pc)
    65  	if len(errs) > 0 {
    66  		t.Fatal("should not have errors")
    67  	}
    68  }