packer/builder/null/config_test.go
hashicorp-copywrite[bot] 19055df3ec
[COMPLIANCE] License changes (#12568)
* Updating the license from MPL to Business Source License

Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at https://hashi.co/license-faq, and details of the license at www.hashicorp.com/bsl.

* Update copyright file headers to BUSL-1.1

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-08-10 15:53:29 -07:00

106 lines
2.2 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package null
import (
"os"
"testing"
"github.com/hashicorp/packer-plugin-sdk/communicator"
)
func testConfig() map[string]interface{} {
return map[string]interface{}{
"ssh_host": "foo",
"ssh_username": "bar",
"ssh_password": "baz",
}
}
func testConfigStruct(t *testing.T) *Config {
var c Config
warns, errs := c.Prepare(testConfig())
if len(warns) > 0 {
t.Fatalf("bad: %#v", len(warns))
}
if errs != nil {
t.Fatalf("bad: %#v", errs)
}
return &c
}
func testConfigErr(t *testing.T, warns []string, err error) {
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err == nil {
t.Fatal("should error")
}
}
func testConfigOk(t *testing.T, warns []string, err error) {
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err != nil {
t.Fatalf("bad: %s", err)
}
}
func TestConfigPrepare_port(t *testing.T) {
raw := testConfig()
// default port should be 22
delete(raw, "port")
var c Config
warns, errs := c.Prepare(raw)
if c.CommConfig.SSHPort != 22 {
t.Fatalf("bad: port should default to 22, not %d", c.CommConfig.SSHPort)
}
testConfigOk(t, warns, errs)
}
func TestConfigPrepare_host(t *testing.T) {
raw := testConfig()
// No host
delete(raw, "ssh_host")
var c Config
warns, errs := c.Prepare(raw)
testConfigErr(t, warns, errs)
// Good host
raw["ssh_host"] = "good"
warns, errs = c.Prepare(raw)
testConfigOk(t, warns, errs)
}
func TestConfigPrepare_sshCredential(t *testing.T) {
raw := testConfig()
// no ssh_password and no ssh_private_key_file
delete(raw, "ssh_password")
delete(raw, "ssh_private_key_file")
warns, errs := (&Config{}).Prepare(raw)
testConfigErr(t, warns, errs)
// only ssh_password
raw["ssh_password"] = "good"
warns, errs = (&Config{}).Prepare(raw)
testConfigOk(t, warns, errs)
// only ssh_private_key_file
testFile := communicator.TestPEM(t)
defer os.Remove(testFile)
raw["ssh_private_key_file"] = testFile
delete(raw, "ssh_password")
warns, errs = (&Config{}).Prepare(raw)
testConfigOk(t, warns, errs)
// both ssh_password and ssh_private_key_file set
raw["ssh_password"] = "bad"
warns, errs = (&Config{}).Prepare(raw)
testConfigErr(t, warns, errs)
}