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