Skip to content

Commit

Permalink
convert go logging to packer logging where useful
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Sep 6, 2024
1 parent 828aa23 commit 87e2839
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion provisioner/testinfra.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions provisioner/testinfra_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -154,15 +154,15 @@ 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
}

// assign optional populated values
// 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 {
Expand All @@ -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 {
Expand Down
9 changes: 7 additions & 2 deletions provisioner/testinfra_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
11 changes: 6 additions & 5 deletions provisioner/testinfra_communication.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"

"github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/tmp"
)

Expand All @@ -18,15 +19,15 @@ 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

// parse generated data for required values
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 {
Expand Down Expand Up @@ -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")
}
}
Expand All @@ -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")
}

Expand Down
23 changes: 14 additions & 9 deletions provisioner/testinfra_communication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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")
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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")
}
Expand Down

0 comments on commit 87e2839

Please sign in to comment.