From 6c756a7b31d82a03f49e1068f1bf2a75b9c9b4c8 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Fri, 27 Mar 2020 16:44:41 -0700 Subject: [PATCH] fix tests --- provisioner/ansible/provisioner.go | 10 +-- provisioner/ansible/provisioner_test.go | 102 ++++++++++++++++-------- 2 files changed, 72 insertions(+), 40 deletions(-) diff --git a/provisioner/ansible/provisioner.go b/provisioner/ansible/provisioner.go index a9d06dc6b..71d471f21 100644 --- a/provisioner/ansible/provisioner.go +++ b/provisioner/ansible/provisioner.go @@ -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") } diff --git a/provisioner/ansible/provisioner_test.go b/provisioner/ansible/provisioner_test.go index 295e38699..6c6da486e 100644 --- a/provisioner/ansible/provisioner_test.go +++ b/provisioner/ansible/provisioner_test.go @@ -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, },