github.com/mitchellh/packer@v1.3.2/builder/vmware/vmx/config_test.go (about)

     1  package vmx
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  )
     8  
     9  func testConfig(t *testing.T) map[string]interface{} {
    10  	return map[string]interface{}{
    11  		"ssh_username":     "foo",
    12  		"shutdown_command": "foo",
    13  		"source_path":      "config_test.go",
    14  	}
    15  }
    16  
    17  func testConfigErr(t *testing.T, warns []string, err error) {
    18  	if len(warns) > 0 {
    19  		t.Fatalf("bad: %#v", warns)
    20  	}
    21  	if err == nil {
    22  		t.Fatal("should error")
    23  	}
    24  }
    25  
    26  func testConfigOk(t *testing.T, warns []string, err error) {
    27  	if len(warns) > 0 {
    28  		t.Fatalf("bad: %#v", warns)
    29  	}
    30  	if err != nil {
    31  		t.Fatalf("bad: %s", err)
    32  	}
    33  }
    34  
    35  func TestNewConfig_sourcePath(t *testing.T) {
    36  	// Bad
    37  	c := testConfig(t)
    38  	delete(c, "source_path")
    39  	_, warns, errs := NewConfig(c)
    40  	testConfigErr(t, warns, errs)
    41  
    42  	// Bad
    43  	c = testConfig(t)
    44  	c["source_path"] = "/i/dont/exist"
    45  	_, warns, errs = NewConfig(c)
    46  	testConfigErr(t, warns, errs)
    47  
    48  	// Good
    49  	tf, err := ioutil.TempFile("", "packer")
    50  	if err != nil {
    51  		t.Fatalf("err: %s", err)
    52  	}
    53  	tf.Close()
    54  	defer os.Remove(tf.Name())
    55  
    56  	c = testConfig(t)
    57  	c["source_path"] = tf.Name()
    58  	_, warns, errs = NewConfig(c)
    59  	testConfigOk(t, warns, errs)
    60  }