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

     1  package null
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/packer/helper/communicator"
     8  )
     9  
    10  func testConfig() map[string]interface{} {
    11  	return map[string]interface{}{
    12  		"ssh_host":     "foo",
    13  		"ssh_username": "bar",
    14  		"ssh_password": "baz",
    15  	}
    16  }
    17  
    18  func testConfigStruct(t *testing.T) *Config {
    19  	c, warns, errs := NewConfig(testConfig())
    20  	if len(warns) > 0 {
    21  		t.Fatalf("bad: %#v", len(warns))
    22  	}
    23  	if errs != nil {
    24  		t.Fatalf("bad: %#v", errs)
    25  	}
    26  
    27  	return c
    28  }
    29  
    30  func testConfigErr(t *testing.T, warns []string, err error) {
    31  	if len(warns) > 0 {
    32  		t.Fatalf("bad: %#v", warns)
    33  	}
    34  	if err == nil {
    35  		t.Fatal("should error")
    36  	}
    37  }
    38  
    39  func testConfigOk(t *testing.T, warns []string, err error) {
    40  	if len(warns) > 0 {
    41  		t.Fatalf("bad: %#v", warns)
    42  	}
    43  	if err != nil {
    44  		t.Fatalf("bad: %s", err)
    45  	}
    46  }
    47  
    48  func TestConfigPrepare_port(t *testing.T) {
    49  	raw := testConfig()
    50  
    51  	// default port should be 22
    52  	delete(raw, "port")
    53  	c, warns, errs := NewConfig(raw)
    54  	if c.CommConfig.SSHPort != 22 {
    55  		t.Fatalf("bad: port should default to 22, not %d", c.CommConfig.SSHPort)
    56  	}
    57  	testConfigOk(t, warns, errs)
    58  }
    59  
    60  func TestConfigPrepare_host(t *testing.T) {
    61  	raw := testConfig()
    62  
    63  	// No host
    64  	delete(raw, "ssh_host")
    65  	_, warns, errs := NewConfig(raw)
    66  	testConfigErr(t, warns, errs)
    67  
    68  	// Good host
    69  	raw["ssh_host"] = "good"
    70  	_, warns, errs = NewConfig(raw)
    71  	testConfigOk(t, warns, errs)
    72  }
    73  
    74  func TestConfigPrepare_sshCredential(t *testing.T) {
    75  	raw := testConfig()
    76  
    77  	// no ssh_password and no ssh_private_key_file
    78  	delete(raw, "ssh_password")
    79  	delete(raw, "ssh_private_key_file")
    80  	_, warns, errs := NewConfig(raw)
    81  	testConfigErr(t, warns, errs)
    82  
    83  	// only ssh_password
    84  	raw["ssh_password"] = "good"
    85  	_, warns, errs = NewConfig(raw)
    86  	testConfigOk(t, warns, errs)
    87  
    88  	// only ssh_private_key_file
    89  	testFile := communicator.TestPEM(t)
    90  	defer os.Remove(testFile)
    91  	raw["ssh_private_key_file"] = testFile
    92  	delete(raw, "ssh_password")
    93  	_, warns, errs = NewConfig(raw)
    94  	testConfigOk(t, warns, errs)
    95  
    96  	// both ssh_password and ssh_private_key_file set
    97  	raw["ssh_password"] = "bad"
    98  	_, warns, errs = NewConfig(raw)
    99  	testConfigErr(t, warns, errs)
   100  }