github.com/rothwerx/packer@v0.9.0/builder/null/config_test.go (about) 1 package null 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/mitchellh/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_sshUsername(t *testing.T) { 75 raw := testConfig() 76 77 // No ssh_username 78 delete(raw, "ssh_username") 79 _, warns, errs := NewConfig(raw) 80 testConfigErr(t, warns, errs) 81 82 // Good ssh_username 83 raw["ssh_username"] = "good" 84 _, warns, errs = NewConfig(raw) 85 testConfigOk(t, warns, errs) 86 } 87 88 func TestConfigPrepare_sshCredential(t *testing.T) { 89 raw := testConfig() 90 91 // no ssh_password and no ssh_private_key_file 92 delete(raw, "ssh_password") 93 delete(raw, "ssh_private_key_file") 94 _, warns, errs := NewConfig(raw) 95 testConfigErr(t, warns, errs) 96 97 // only ssh_password 98 raw["ssh_password"] = "good" 99 _, warns, errs = NewConfig(raw) 100 testConfigOk(t, warns, errs) 101 102 // only ssh_private_key_file 103 testFile := communicator.TestPEM(t) 104 defer os.Remove(testFile) 105 raw["ssh_private_key_file"] = testFile 106 delete(raw, "ssh_password") 107 _, warns, errs = NewConfig(raw) 108 testConfigOk(t, warns, errs) 109 110 // both ssh_password and ssh_private_key_file set 111 raw["ssh_password"] = "bad" 112 _, warns, errs = NewConfig(raw) 113 testConfigErr(t, warns, errs) 114 }