diff --git a/provisioner/testinfra.go b/provisioner/testinfra.go index 3e081c0..3159bf3 100644 --- a/provisioner/testinfra.go +++ b/provisioner/testinfra.go @@ -209,7 +209,7 @@ func (provisioner *Provisioner) Provision(ctx context.Context, ui packer.Ui, com provisioner.config.ctx.Data = generatedData // prepare testinfra test command - cmd, localCmd, err := provisioner.determineExecCmd() + cmd, localCmd, err := provisioner.determineExecCmd(ui) if err != nil { ui.Error("the execution command could not be accurately determined") return err diff --git a/provisioner/testinfra_command.go b/provisioner/testinfra_command.go index 567e414..8b465d2 100644 --- a/provisioner/testinfra_command.go +++ b/provisioner/testinfra_command.go @@ -134,16 +134,16 @@ func packerRemoteCmd(localCmd *packer.RemoteCmd, installCmd []string, comm packe } // determine and return execution command for testinfra -func (provisioner *Provisioner) determineExecCmd() (*exec.Cmd, *packer.RemoteCmd, error) { +func (provisioner *Provisioner) determineExecCmd(ui packer.Ui) (*exec.Cmd, *packer.RemoteCmd, error) { // declare args var args []string // assign determined communication string localExec := provisioner.config.Local if !localExec { - communication, err := provisioner.determineCommunication() + communication, err := provisioner.determineCommunication(ui) if err != nil { - log.Print("could not accurately determine communication configuration") + ui.Say("could not accurately determine communication configuration") return nil, nil, err } @@ -154,7 +154,7 @@ func (provisioner *Provisioner) determineExecCmd() (*exec.Cmd, *packer.RemoteCmd // pytest path pytestPath, err := interpolate.Render(provisioner.config.PytestPath, &provisioner.config.ctx) if err != nil { - log.Printf("error parsing config for PytestPath: %v", err.Error()) + ui.Sayf("error parsing config for PytestPath: %v", err.Error()) return nil, nil, err } @@ -162,7 +162,7 @@ func (provisioner *Provisioner) determineExecCmd() (*exec.Cmd, *packer.RemoteCmd // keyword keyword, err := interpolate.Render(provisioner.config.Keyword, &provisioner.config.ctx) if err != nil { - log.Printf("error parsing config for Keyword: %v", err.Error()) + ui.Sayf("error parsing config for Keyword: %v", err.Error()) return nil, nil, err } if len(keyword) > 0 { @@ -171,7 +171,7 @@ func (provisioner *Provisioner) determineExecCmd() (*exec.Cmd, *packer.RemoteCmd // marker marker, err := interpolate.Render(provisioner.config.Marker, &provisioner.config.ctx) if err != nil { - log.Printf("error parsing config for Marker: %v", err.Error()) + ui.Sayf("error parsing config for Marker: %v", err.Error()) return nil, nil, err } if len(marker) > 0 { diff --git a/provisioner/testinfra_command_test.go b/provisioner/testinfra_command_test.go index 0c7f097..50c2a09 100644 --- a/provisioner/testinfra_command_test.go +++ b/provisioner/testinfra_command_test.go @@ -4,10 +4,15 @@ import ( "fmt" "slices" "testing" + + "github.com/hashicorp/packer-plugin-sdk/packer" ) // test provisioner determineExecCmd properly determines execution command func TestProvisionerDetermineExecCmd(test *testing.T) { + // initialize simple test ui + ui := packer.TestUi(test) + // test minimal config with local execution var provisioner = &Provisioner{ config: Config{ @@ -17,7 +22,7 @@ func TestProvisionerDetermineExecCmd(test *testing.T) { }, } - execCmd, localCmd, err := provisioner.determineExecCmd() + execCmd, localCmd, err := provisioner.determineExecCmd(ui) if err != nil { test.Errorf("determineExecCmd function failed to determine execution commands for local execution minimal config: %v", err) } @@ -47,7 +52,7 @@ func TestProvisionerDetermineExecCmd(test *testing.T) { provisioner.generatedData = generatedData - execCmd, localCmd, err = provisioner.determineExecCmd() + 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) } diff --git a/provisioner/testinfra_communication.go b/provisioner/testinfra_communication.go index 0b0e0c2..6572e0c 100644 --- a/provisioner/testinfra_communication.go +++ b/provisioner/testinfra_communication.go @@ -5,6 +5,7 @@ import ( "fmt" "log" + "github.com/hashicorp/packer-plugin-sdk/packer" "github.com/hashicorp/packer-plugin-sdk/tmp" ) @@ -18,7 +19,7 @@ const ( ) // determine and return appropriate communication string for pytest/testinfra -func (provisioner *Provisioner) determineCommunication() ([]string, error) { +func (provisioner *Provisioner) determineCommunication(ui packer.Ui) ([]string, error) { // declare communication args var args []string @@ -26,7 +27,7 @@ func (provisioner *Provisioner) determineCommunication() ([]string, error) { connectionType := provisioner.generatedData["ConnType"].(string) // determine communication string by packer connection type - log.Printf("testinfra communicating via %s connection type", connectionType) + ui.Sayf("testinfra communicating via %s connection type", connectionType) // determine communication based on connection type switch connectionType { @@ -79,7 +80,7 @@ func (provisioner *Provisioner) determineCommunication() ([]string, error) { // no winrm password available if !ok || len(winrmPassword) == 0 { - log.Print("winrm communicator password could not be determined from available Packer data") + ui.Say("winrm communicator password could not be determined from available Packer data") return nil, errors.New("unknown winrm password") } } @@ -90,14 +91,14 @@ func (provisioner *Provisioner) determineCommunication() ([]string, error) { // determine instanceid instanceID, ok := provisioner.generatedData["ID"].(string) if !ok || len(instanceID) == 0 { - log.Print("instance id could not be determined") + ui.Say("instance id could not be determined") return nil, errors.New("unknown instance id") } // append args with container connection backend information (instanceid) args = append(args, fmt.Sprintf("--hosts=%s://%s", connectionType, instanceID)) default: - log.Printf("communication backend with machine image is not supported, and was resolved to '%s'", connectionType) + ui.Sayf("communication backend with machine image is not supported, and was resolved to '%s'", connectionType) return nil, errors.New("unsupported communication type") } diff --git a/provisioner/testinfra_communication_test.go b/provisioner/testinfra_communication_test.go index c53e4a9..1c82e3d 100644 --- a/provisioner/testinfra_communication_test.go +++ b/provisioner/testinfra_communication_test.go @@ -6,10 +6,15 @@ import ( "regexp" "slices" "testing" + + "github.com/hashicorp/packer-plugin-sdk/packer" ) // test provisioner determineCommunication properly determines communication strings func TestProvisionerDetermineCommunication(test *testing.T) { + // initialize simple test ui + ui := packer.TestUi(test) + var provisioner Provisioner // test ssh with httpaddr and password @@ -27,7 +32,7 @@ func TestProvisionerDetermineCommunication(test *testing.T) { provisioner.generatedData = generatedData - communication, err := provisioner.determineCommunication() + communication, err := provisioner.determineCommunication(ui) if err != nil { test.Errorf("determineCommunication function failed to determine ssh: %s", err) } @@ -39,7 +44,7 @@ func TestProvisionerDetermineCommunication(test *testing.T) { delete(generatedData, "Password") provisioner.generatedData = generatedData - communication, err = provisioner.determineCommunication() + communication, err = provisioner.determineCommunication(ui) if err != nil { test.Errorf("determineCommunication function failed to determine ssh: %s", err) } @@ -52,7 +57,7 @@ func TestProvisionerDetermineCommunication(test *testing.T) { generatedData["SSHAgentAuth"] = true provisioner.generatedData = generatedData - communication, err = provisioner.determineCommunication() + communication, err = provisioner.determineCommunication(ui) if err != nil { test.Errorf("determineCommunication function failed to determine ssh: %s", err) } @@ -73,7 +78,7 @@ func TestProvisionerDetermineCommunication(test *testing.T) { provisioner.generatedData = generatedData - communication, err = provisioner.determineCommunication() + communication, err = provisioner.determineCommunication(ui) if err != nil { test.Errorf("determineCommunication function failed to determine winrm: %s", err) } @@ -85,7 +90,7 @@ func TestProvisionerDetermineCommunication(test *testing.T) { delete(generatedData, "Password") provisioner.generatedData = generatedData - _, err = provisioner.determineCommunication() + _, err = provisioner.determineCommunication(ui) if err == nil { test.Errorf("determineCommunication function did not fail on no available password") } @@ -102,7 +107,7 @@ func TestProvisionerDetermineCommunication(test *testing.T) { provisioner.generatedData = generatedData - communication, err = provisioner.determineCommunication() + communication, err = provisioner.determineCommunication(ui) if err != nil { test.Errorf("determineCommunication function failed to determine docker: %s", err) } @@ -122,7 +127,7 @@ func TestProvisionerDetermineCommunication(test *testing.T) { provisioner.generatedData = generatedData - communication, err = provisioner.determineCommunication() + communication, err = provisioner.determineCommunication(ui) if err != nil { test.Errorf("determineCommunication function failed to determine podman: %s", err) } @@ -142,7 +147,7 @@ func TestProvisionerDetermineCommunication(test *testing.T) { provisioner.generatedData = generatedData - communication, err = provisioner.determineCommunication() + communication, err = provisioner.determineCommunication(ui) if err != nil { test.Errorf("determineCommunication function failed to determine lxc: %s", err) } @@ -162,7 +167,7 @@ func TestProvisionerDetermineCommunication(test *testing.T) { provisioner.generatedData = generatedData - _, err = provisioner.determineCommunication() + _, err = provisioner.determineCommunication(ui) if err == nil { test.Errorf("determineCommunication function did not fail on unknown connection type") }