github.com/sneal/packer@v0.5.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  	}
    14  }
    15  
    16  func testConfigErr(t *testing.T, warns []string, err error) {
    17  	if len(warns) > 0 {
    18  		t.Fatalf("bad: %#v", warns)
    19  	}
    20  	if err == nil {
    21  		t.Fatal("should error")
    22  	}
    23  }
    24  
    25  func testConfigOk(t *testing.T, warns []string, err error) {
    26  	if len(warns) > 0 {
    27  		t.Fatalf("bad: %#v", warns)
    28  	}
    29  	if err != nil {
    30  		t.Fatalf("bad: %s", err)
    31  	}
    32  }
    33  
    34  func TestNewConfig_sourcePath(t *testing.T) {
    35  	// Bad
    36  	c := testConfig(t)
    37  	delete(c, "source_path")
    38  	_, warns, errs := NewConfig(c)
    39  	testConfigErr(t, warns, errs)
    40  
    41  	// Bad
    42  	c = testConfig(t)
    43  	c["source_path"] = "/i/dont/exist"
    44  	_, warns, errs = NewConfig(c)
    45  	testConfigErr(t, warns, errs)
    46  
    47  	// Good
    48  	tf, err := ioutil.TempFile("", "packer")
    49  	if err != nil {
    50  		t.Fatalf("err: %s", err)
    51  	}
    52  	tf.Close()
    53  	defer os.Remove(tf.Name())
    54  
    55  	c = testConfig(t)
    56  	c["source_path"] = tf.Name()
    57  	_, warns, errs = NewConfig(c)
    58  	testConfigOk(t, warns, errs)
    59  }