fix tests

This commit is contained in:
Megan Marsh 2020-03-27 16:44:41 -07:00
parent 19d49f8b78
commit 6c756a7b31
2 changed files with 72 additions and 40 deletions

View File

@ -304,9 +304,9 @@ func (p *Provisioner) setupAdapter(ui packer.Ui, comm packer.Communicator) (stri
return k.privKeyFile, nil
}
const DefaultSSHInventoryFilev2 = `{{ .HostAlias }} ansible_host={{ .Host }} ansible_user={{ .User }} ansible_port={{ .Port }}`
const DefaultSSHInventoryFilev1 = `{{ .HostAlias }} ansible_ssh_host={{ .Host }} ansible_ssh_user={{ .User }} ansible_ssh_port={{ .Port }}`
const DefaultWinRMInventoryFilev2 = `{{ .HostAlias}} ansible_host={{ .Host }} ansible_connection=winrm ansible_password={{ .Password }} ansible_shell_type=powershell ansible_user={{ .User}} ansible_port={{ .Port }}`
const DefaultSSHInventoryFilev2 = "{{ .HostAlias }} ansible_host={{ .Host }} ansible_user={{ .User }} ansible_port={{ .Port }}\n"
const DefaultSSHInventoryFilev1 = "{{ .HostAlias }} ansible_ssh_host={{ .Host }} ansible_ssh_user={{ .User }} ansible_ssh_port={{ .Port }}\n"
const DefaultWinRMInventoryFilev2 = "{{ .HostAlias}} ansible_host={{ .Host }} ansible_connection=winrm ansible_password={{ .Password }} ansible_shell_type=powershell ansible_user={{ .User}} ansible_port={{ .Port }}\n"
func (p *Provisioner) createInventoryFile() error {
log.Printf("Creating inventory file for Ansible run...")
@ -437,13 +437,13 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C
privKeyFile = SSHPrivateKeyFile
} else {
// See if we can get a private key and write that to a tmpfile
SSHPrivateKey := generatedData["SSHPrivateKey"].([]byte)
SSHPrivateKey := generatedData["SSHPrivateKey"].(string)
tmpSSHPrivateKey, err := tmp.File("ansible-key")
if err != nil {
return fmt.Errorf("Error writing private key to temp file for"+
"ansible connection: %v", err)
}
_, err = tmpSSHPrivateKey.Write(SSHPrivateKey)
_, err = tmpSSHPrivateKey.WriteString(SSHPrivateKey)
if err != nil {
return errors.New("failed to write private key to temp file")
}

View File

@ -18,28 +18,6 @@ import (
"github.com/hashicorp/packer/packer"
)
type provisionLogicTracker struct {
setupAdapterCalled bool
executeAnsibleCalled bool
happyPath bool
}
func (l *provisionLogicTracker) setupAdapter(ui packer.Ui, comm packer.Communicator) (string, error) {
l.setupAdapterCalled = true
if l.happyPath {
return "fakeKeyString", nil
}
return "", fmt.Errorf("chose sadpath")
}
func (l *provisionLogicTracker) executeAnsible(ui packer.Ui, comm packer.Communicator, privKeyFile string) error {
l.executeAnsibleCalled = true
if l.happyPath {
return fmt.Errorf("Chose sadpath")
}
return nil
}
// Be sure to remove the Ansible stub file in each test with:
// defer os.Remove(config["command"].(string))
func testConfig(t *testing.T) map[string]interface{} {
@ -384,8 +362,13 @@ func TestCreateInventoryFile_vers1(t *testing.T) {
defer os.Remove(p.config.Command)
p.ansibleMajVersion = 1
p.config.User = "testuser"
p.config.UseProxy = confighelper.TriFalse
p.generatedData = map[string]interface{}{
"Host": "123.45.67.89",
"Port": "2222",
}
err := p.createInventoryFile("123.45.67.89", 2222)
err := p.createInventoryFile()
if err != nil {
t.Fatalf("error creating config using localhost and local port proxy")
}
@ -410,8 +393,13 @@ func TestCreateInventoryFile_vers2(t *testing.T) {
defer os.Remove(p.config.Command)
p.ansibleMajVersion = 2
p.config.User = "testuser"
p.config.UseProxy = confighelper.TriFalse
p.generatedData = map[string]interface{}{
"Host": "123.45.67.89",
"Port": "1234",
}
err := p.createInventoryFile("123.45.67.89", 1234)
err := p.createInventoryFile()
if err != nil {
t.Fatalf("error creating config using localhost and local port proxy")
}
@ -436,8 +424,13 @@ func TestCreateInventoryFile_Groups(t *testing.T) {
p.ansibleMajVersion = 1
p.config.User = "testuser"
p.config.Groups = []string{"Group1", "Group2"}
p.config.UseProxy = confighelper.TriFalse
p.generatedData = map[string]interface{}{
"Host": "123.45.67.89",
"Port": "1234",
}
err := p.createInventoryFile("123.45.67.89", 1234)
err := p.createInventoryFile()
if err != nil {
t.Fatalf("error creating config using localhost and local port proxy")
}
@ -467,8 +460,13 @@ func TestCreateInventoryFile_EmptyGroups(t *testing.T) {
p.ansibleMajVersion = 1
p.config.User = "testuser"
p.config.EmptyGroups = []string{"Group1", "Group2"}
p.config.UseProxy = confighelper.TriFalse
p.generatedData = map[string]interface{}{
"Host": "123.45.67.89",
"Port": "1234",
}
err := p.createInventoryFile("123.45.67.89", 1234)
err := p.createInventoryFile()
if err != nil {
t.Fatalf("error creating config using localhost and local port proxy")
}
@ -497,8 +495,13 @@ func TestCreateInventoryFile_GroupsAndEmptyGroups(t *testing.T) {
p.config.User = "testuser"
p.config.Groups = []string{"Group1", "Group2"}
p.config.EmptyGroups = []string{"Group3"}
p.config.UseProxy = confighelper.TriFalse
p.generatedData = map[string]interface{}{
"Host": "123.45.67.89",
"Port": "1234",
}
err := p.createInventoryFile("123.45.67.89", 1234)
err := p.createInventoryFile()
if err != nil {
t.Fatalf("error creating config using localhost and local port proxy")
}
@ -535,8 +538,12 @@ func TestUseProxy(t *testing.T) {
explanation: "use_proxy is true; we should set up adapter",
UseProxy: confighelper.TriTrue,
generatedData: map[string]interface{}{
"Host": "123.45.67.8",
"Port": int64(1234),
"Host": "123.45.67.8",
"Port": int64(1234),
"ConnType": "ssh",
"SSHPrivateKeyFile": "",
"SSHPrivateKey": "asdf",
"User": "PartyPacker",
},
expectedSetupAdapterCalled: true,
},
@ -544,8 +551,12 @@ func TestUseProxy(t *testing.T) {
explanation: "use_proxy is false but no IP addr is available; we should set up adapter anyway.",
UseProxy: confighelper.TriFalse,
generatedData: map[string]interface{}{
"Host": "",
"Port": nil,
"Host": "",
"Port": nil,
"ConnType": "ssh",
"SSHPrivateKeyFile": "",
"SSHPrivateKey": "asdf",
"User": "PartyPacker",
},
expectedSetupAdapterCalled: true,
},
@ -553,17 +564,38 @@ func TestUseProxy(t *testing.T) {
explanation: "use_proxy is false; we shouldn't set up adapter.",
UseProxy: confighelper.TriFalse,
generatedData: map[string]interface{}{
"Host": "123.45.67.8",
"Port": int64(1234),
"Host": "123.45.67.8",
"Port": int64(1234),
"ConnType": "ssh",
"SSHPrivateKeyFile": "",
"SSHPrivateKey": "asdf",
"User": "PartyPacker",
},
expectedSetupAdapterCalled: false,
},
{
explanation: "use_proxy is false but connType isn't ssh or winrm.",
UseProxy: confighelper.TriFalse,
generatedData: map[string]interface{}{
"Host": "123.45.67.8",
"Port": int64(1234),
"ConnType": "docker",
"SSHPrivateKeyFile": "",
"SSHPrivateKey": "asdf",
"User": "PartyPacker",
},
expectedSetupAdapterCalled: true,
},
{
explanation: "use_proxy is unset; we should default to setting up the adapter (for now).",
UseProxy: confighelper.TriUnset,
generatedData: map[string]interface{}{
"Host": "123.45.67.8",
"Port": int64(1234),
"Host": "123.45.67.8",
"Port": int64(1234),
"ConnType": "ssh",
"SSHPrivateKeyFile": "",
"SSHPrivateKey": "asdf",
"User": "PartyPacker",
},
expectedSetupAdapterCalled: true,
},