From 81a4fdf97dac81aed05c59b640430065b5e294fd Mon Sep 17 00:00:00 2001 From: Matt Schuchard Date: Tue, 3 Dec 2024 11:48:39 -0500 Subject: [PATCH] direct assignment to gen data in unit tests --- provisioner/command_test.go | 6 +-- provisioner/communication_test.go | 80 +++++++++++-------------------- 2 files changed, 31 insertions(+), 55 deletions(-) diff --git a/provisioner/command_test.go b/provisioner/command_test.go index 50c2a09..986a535 100644 --- a/provisioner/command_test.go +++ b/provisioner/command_test.go @@ -39,7 +39,7 @@ func TestProvisionerDetermineExecCmd(test *testing.T) { config: *basicConfig, } - generatedData := map[string]interface{}{ + provisioner.generatedData = map[string]interface{}{ "ConnType": "ssh", "User": "me", "SSHPrivateKeyFile": "/path/to/sshprivatekeyfile", @@ -50,8 +50,6 @@ func TestProvisionerDetermineExecCmd(test *testing.T) { "ID": "1234567890", } - provisioner.generatedData = generatedData - execCmd, localCmd, err = provisioner.determineExecCmd(ui) if err != nil { test.Errorf("determineExecCmd function failed to determine execution command for basic config with SSH communicator: %s", err) @@ -60,7 +58,7 @@ func TestProvisionerDetermineExecCmd(test *testing.T) { test.Error("determineExecCmd function failed to determine execution directory for basic config") test.Errorf("actual: %s, expected: %s", execCmd.Dir, basicConfig.Chdir) } - if !slices.Equal(execCmd.Args, slices.Concat([]string{provisioner.config.PytestPath, fmt.Sprintf("--hosts=ssh://%s@%s:%d", generatedData["User"], generatedData["Host"], generatedData["Port"]), fmt.Sprintf("--ssh-identity-file=%s", generatedData["SSHPrivateKeyFile"]), "--ssh-extra-args=\"-o StrictHostKeyChecking=no\"", "-k", fmt.Sprintf("\"%s\"", provisioner.config.Keyword), "-m", fmt.Sprintf("\"%s\"", provisioner.config.Marker), "-n", "auto", "--sudo", "-vv"}, provisioner.config.TestFiles)) { + if !slices.Equal(execCmd.Args, slices.Concat([]string{provisioner.config.PytestPath, fmt.Sprintf("--hosts=ssh://%s@%s:%d", provisioner.generatedData["User"], provisioner.generatedData["Host"], provisioner.generatedData["Port"]), fmt.Sprintf("--ssh-identity-file=%s", provisioner.generatedData["SSHPrivateKeyFile"]), "--ssh-extra-args=\"-o StrictHostKeyChecking=no\"", "-k", fmt.Sprintf("\"%s\"", provisioner.config.Keyword), "-m", fmt.Sprintf("\"%s\"", provisioner.config.Marker), "-n", "auto", "--sudo", "-vv"}, provisioner.config.TestFiles)) { test.Errorf("determineExecCmd function failed to properly determine remote execution command for basic config with SSH communicator: %s", execCmd.String()) } if localCmd != nil { diff --git a/provisioner/communication_test.go b/provisioner/communication_test.go index 0240c4f..4377aa8 100644 --- a/provisioner/communication_test.go +++ b/provisioner/communication_test.go @@ -18,7 +18,7 @@ func TestProvisionerDetermineCommunication(test *testing.T) { var provisioner Provisioner // test ssh with httpaddr and password - generatedData := map[string]interface{}{ + provisioner.generatedData = map[string]interface{}{ "ConnType": "ssh", "SSHUsername": "me", "SSHPassword": "password", @@ -29,43 +29,39 @@ func TestProvisionerDetermineCommunication(test *testing.T) { "ID": "1234567890", } - provisioner.generatedData = generatedData - communication, err := provisioner.determineCommunication(ui) if err != nil { test.Errorf("determineCommunication function failed to determine ssh: %s", err) } - if !slices.Equal(communication, []string{fmt.Sprintf("--hosts=ssh://%s:%s@%s:%d", generatedData["SSHUsername"], generatedData["SSHPassword"], generatedData["SSHHost"], generatedData["SSHPort"]), "--ssh-extra-args=\"-o StrictHostKeyChecking=no\""}) { + if !slices.Equal(communication, []string{fmt.Sprintf("--hosts=ssh://%s:%s@%s:%d", provisioner.generatedData["SSHUsername"], provisioner.generatedData["SSHPassword"], provisioner.generatedData["SSHHost"], provisioner.generatedData["SSHPort"]), "--ssh-extra-args=\"-o StrictHostKeyChecking=no\""}) { test.Errorf("communication string slice for ssh password incorrectly determined: %v", communication) } // test ssh with private key file - delete(generatedData, "SSHPassword") - provisioner.generatedData = generatedData + delete(provisioner.generatedData, "SSHPassword") communication, err = provisioner.determineCommunication(ui) if err != nil { test.Errorf("determineCommunication function failed to determine ssh: %s", err) } - if !slices.Equal(communication, []string{fmt.Sprintf("--hosts=ssh://%s@%s:%d", generatedData["SSHUsername"], generatedData["SSHHost"], generatedData["SSHPort"]), fmt.Sprintf("--ssh-identity-file=%s", generatedData["SSHPrivateKeyFile"]), "--ssh-extra-args=\"-o StrictHostKeyChecking=no\""}) { + if !slices.Equal(communication, []string{fmt.Sprintf("--hosts=ssh://%s@%s:%d", provisioner.generatedData["SSHUsername"], provisioner.generatedData["SSHHost"], provisioner.generatedData["SSHPort"]), fmt.Sprintf("--ssh-identity-file=%s", provisioner.generatedData["SSHPrivateKeyFile"]), "--ssh-extra-args=\"-o StrictHostKeyChecking=no\""}) { test.Errorf("communication string slice for ssh private key incorrectly determined: %v", communication) } // test ssh with no private key but with agent auth - generatedData["SSHPrivateKeyFile"] = "" - generatedData["SSHAgentAuth"] = true - provisioner.generatedData = generatedData + provisioner.generatedData["SSHPrivateKeyFile"] = "" + provisioner.generatedData["SSHAgentAuth"] = true communication, err = provisioner.determineCommunication(ui) if err != nil { test.Errorf("determineCommunication function failed to determine ssh: %s", err) } - if !slices.Equal(communication, []string{fmt.Sprintf("--hosts=ssh://%s@%s:%d", generatedData["SSHUsername"], generatedData["SSHHost"], generatedData["SSHPort"]), "--ssh-extra-args=\"-o StrictHostKeyChecking=no\""}) { + if !slices.Equal(communication, []string{fmt.Sprintf("--hosts=ssh://%s@%s:%d", provisioner.generatedData["SSHUsername"], provisioner.generatedData["SSHHost"], provisioner.generatedData["SSHPort"]), "--ssh-extra-args=\"-o StrictHostKeyChecking=no\""}) { test.Errorf("communication string slice for ssh agent auth incorrectly determined: %v", communication) } // test winrm - generatedData = map[string]interface{}{ + provisioner.generatedData = map[string]interface{}{ "ConnType": "winrm", "WinRMUser": "me", "WinRMPassword": "password", @@ -75,19 +71,16 @@ func TestProvisionerDetermineCommunication(test *testing.T) { "WinRMInsecure": true, } - provisioner.generatedData = generatedData - communication, err = provisioner.determineCommunication(ui) if err != nil { test.Errorf("determineCommunication function failed to determine winrm: %s", err) } - if !slices.Equal(communication, []string{fmt.Sprintf("--hosts=winrm://%s:%s@%s:%d?no_ssl=true&no_verify_ssl=true", generatedData["WinRMUser"], generatedData["WinRMPassword"], generatedData["WinRMHost"], generatedData["WinRMPort"])}) { + if !slices.Equal(communication, []string{fmt.Sprintf("--hosts=winrm://%s:%s@%s:%d?no_ssl=true&no_verify_ssl=true", provisioner.generatedData["WinRMUser"], provisioner.generatedData["WinRMPassword"], provisioner.generatedData["WinRMHost"], provisioner.generatedData["WinRMPort"])}) { test.Errorf("communication string slice for winrm incorrectly determined: %v", communication) } // test fails on no winrmpassword or password - delete(generatedData, "WinRMPassword") - provisioner.generatedData = generatedData + delete(provisioner.generatedData, "WinRMPassword") _, err = provisioner.determineCommunication(ui) if err == nil { @@ -95,55 +88,49 @@ func TestProvisionerDetermineCommunication(test *testing.T) { } // test docker - generatedData = map[string]interface{}{ + provisioner.generatedData = map[string]interface{}{ "ConnType": "docker", "ID": "1234567890abcdefg", } - provisioner.generatedData = generatedData - communication, err = provisioner.determineCommunication(ui) if err != nil { test.Errorf("determineCommunication function failed to determine docker: %s", err) } - if !slices.Equal(communication, []string{fmt.Sprintf("--hosts=docker://%s", generatedData["ID"])}) { + if !slices.Equal(communication, []string{fmt.Sprintf("--hosts=docker://%s", provisioner.generatedData["ID"])}) { test.Errorf("communication string slice for docker incorrectly determined: %v", communication) } // test podman - generatedData = map[string]interface{}{ + provisioner.generatedData = map[string]interface{}{ "ConnType": "podman", "ID": "1234567890abcdefg", } - provisioner.generatedData = generatedData - communication, err = provisioner.determineCommunication(ui) if err != nil { test.Errorf("determineCommunication function failed to determine podman: %s", err) } - if !slices.Equal(communication, []string{fmt.Sprintf("--hosts=podman://%s", generatedData["ID"])}) { + if !slices.Equal(communication, []string{fmt.Sprintf("--hosts=podman://%s", provisioner.generatedData["ID"])}) { test.Errorf("communication string slice for podman incorrectly determined: %v", communication) } // test lxc - generatedData = map[string]interface{}{ + provisioner.generatedData = map[string]interface{}{ "ConnType": "lxc", "ID": "1234567890abcdefg", } - provisioner.generatedData = generatedData - communication, err = provisioner.determineCommunication(ui) if err != nil { test.Errorf("determineCommunication function failed to determine lxc: %s", err) } - if !slices.Equal(communication, []string{fmt.Sprintf("--hosts=lxc://%s", generatedData["ID"])}) { + if !slices.Equal(communication, []string{fmt.Sprintf("--hosts=lxc://%s", provisioner.generatedData["ID"])}) { test.Errorf("communication string slice for lxc incorrectly determined: %v", communication) } // test fails on no communication - generatedData = map[string]interface{}{ + provisioner.generatedData = map[string]interface{}{ "ConnType": "unknown", "User": "me", "Host": "192.168.0.1", @@ -151,8 +138,6 @@ func TestProvisionerDetermineCommunication(test *testing.T) { "ID": "1234567890abcdefg", } - provisioner.generatedData = generatedData - _, err = provisioner.determineCommunication(ui) if err == nil { test.Errorf("determineCommunication function did not fail on unknown connection type") @@ -164,24 +149,22 @@ func TestDetermineUserAddr(test *testing.T) { var provisioner Provisioner // dummy up fake user and address data - generatedData := map[string]interface{}{ + provisioner.generatedData = map[string]interface{}{ "User": "me", "Host": "192.168.0.1", "Port": int64(22), } - provisioner.generatedData = generatedData - user, httpAddr, err := provisioner.determineUserAddr("ssh") if err != nil { test.Error("determineUserAddr failed to determine user and address") test.Error(err) } - if user != generatedData["User"] { + if user != provisioner.generatedData["User"] { test.Error("user was incorrectly determined") - test.Errorf("expected: %s, actual: %s", generatedData["User"], user) + test.Errorf("expected: %s, actual: %s", provisioner.generatedData["User"], user) } - expectedHttpAddr := fmt.Sprintf("%s:%d", generatedData["Host"], generatedData["Port"]) + expectedHttpAddr := fmt.Sprintf("%s:%d", provisioner.generatedData["Host"], provisioner.generatedData["Port"]) if httpAddr != expectedHttpAddr { test.Error("address was incorrectly determined") test.Errorf("expected: %s, actual: %s", expectedHttpAddr, httpAddr) @@ -193,15 +176,13 @@ func TestProvisionerDetermineSSHAuth(test *testing.T) { var provisioner Provisioner // dummy up fake ssh data - generatedData := map[string]interface{}{ + provisioner.generatedData = map[string]interface{}{ "Password": "password", "SSHPrivateKey": "abcdefg12345", "SSHPrivateKeyFile": "/tmp/sshprivatekeyfile", "SSHAgentAuth": false, } - provisioner.generatedData = generatedData - // test successfully uses password for ssh auth sshAuthType, sshAuthString, err := provisioner.determineSSHAuth() if err != nil { @@ -210,13 +191,12 @@ func TestProvisionerDetermineSSHAuth(test *testing.T) { if sshAuthType != passwordSSHAuth { test.Errorf("ssh authentication type incorrectly determined: %s", sshAuthType) } - if sshAuthString != generatedData["Password"] { + if sshAuthString != provisioner.generatedData["Password"] { test.Errorf("password content incorrectly determined: %s", sshAuthString) } // remove password from data - delete(generatedData, "Password") - provisioner.generatedData = generatedData + delete(provisioner.generatedData, "Password") // test successfully returns ssh private key file location sshAuthType, sshAuthString, err = provisioner.determineSSHAuth() @@ -226,14 +206,13 @@ func TestProvisionerDetermineSSHAuth(test *testing.T) { if sshAuthType != privateKeySSHAuth { test.Errorf("ssh authentication type incorrectly determined: %s", sshAuthType) } - if sshAuthString != generatedData["SSHPrivateKeyFile"] { + if sshAuthString != provisioner.generatedData["SSHPrivateKeyFile"] { test.Errorf("ssh private key file location incorrectly determined: %s", sshAuthString) } // modify to empty ssh key and yes to ssh agent auth - generatedData["SSHPrivateKeyFile"] = "" - generatedData["SSHAgentAuth"] = true - provisioner.generatedData = generatedData + provisioner.generatedData["SSHPrivateKeyFile"] = "" + provisioner.generatedData["SSHAgentAuth"] = true // test successfully uses empty ssh private key file sshAuthType, sshAuthString, err = provisioner.determineSSHAuth() @@ -248,8 +227,7 @@ func TestProvisionerDetermineSSHAuth(test *testing.T) { } // modify to no ssh agent auth - generatedData["SSHAgentAuth"] = false - provisioner.generatedData = generatedData + provisioner.generatedData["SSHAgentAuth"] = false // test successfully creates tmpfile with expected content for private key sshAuthType, sshAuthString, err = provisioner.determineSSHAuth() @@ -262,7 +240,7 @@ func TestProvisionerDetermineSSHAuth(test *testing.T) { if matched, _ := regexp.Match(`/tmp/testinfra-key\d+`, []byte(sshAuthString)); !matched { test.Errorf("temporary ssh private key file was not created in the expected location: %s", sshAuthString) } - if sshPrivateKey, _ := os.ReadFile(sshAuthString); string(sshPrivateKey) != generatedData["SSHPrivateKey"] { + if sshPrivateKey, _ := os.ReadFile(sshAuthString); string(sshPrivateKey) != provisioner.generatedData["SSHPrivateKey"] { test.Errorf("temporary ssh key file content is not the ssh private key: %s", sshPrivateKey) } }