Skip to content

Commit

Permalink
actually sudo and sudo_user are mutually exclusive
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Apr 15, 2024
1 parent b97ba7d commit 4c39b9e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
6 changes: 4 additions & 2 deletions provisioner/testinfra.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,16 @@ func (provisioner *Provisioner) Prepare(raws ...interface{}) error {
if provisioner.config.Sudo {
log.Print("testinfra will execute with sudo")

// warn if sudo_user also specified
if len(provisioner.config.SudoUser) > 0 {
log.Printf("testinfra will execute as user: %s", provisioner.config.SudoUser)
log.Print("the 'sudo_user' parameter is ignored when sudo is enabled")
}
} else {
log.Print("testinfra will not execute with sudo")

// sudo_user mutually exclusive with sudo
if len(provisioner.config.SudoUser) > 0 {
log.Print("the 'sudo_user' parameter is ignored when sudo is not enabled")
log.Printf("testinfra will execute as user: %s", provisioner.config.SudoUser)
}
}

Expand Down
3 changes: 2 additions & 1 deletion provisioner/testinfra_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,13 @@ func (provisioner *Provisioner) determineExecCmd() (*exec.Cmd, *packer.RemoteCmd
// sudo
if provisioner.config.Sudo {
args = append(args, "--sudo")

} else {
// sudo_user
if len(provisioner.config.SudoUser) > 0 {
args = append(args, fmt.Sprintf("\"--sudo-user=%s\"", provisioner.config.SudoUser))
}
}

// verbose
if provisioner.config.Verbose {
args = append(args, "-v")
Expand Down
2 changes: 1 addition & 1 deletion provisioner/testinfra_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestProvisionerDetermineExecCmd(test *testing.T) {
test.Errorf("actual: %s, expected: %s", execCmd.Dir, basicConfig.Chdir)
}
// 1.22 slices.Concat( , provisioner.config.TestFiles)
if !slices.Equal(execCmd.Args, append([]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", fmt.Sprint(provisioner.config.Processes), "--sudo", fmt.Sprintf("\"--sudo-user=%s\"", provisioner.config.SudoUser), "-v"}, provisioner.config.TestFiles...)) {
if !slices.Equal(execCmd.Args, append([]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", fmt.Sprint(provisioner.config.Processes), "--sudo", "-v"}, 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 {
Expand Down

0 comments on commit 4c39b9e

Please sign in to comment.