diff --git a/CHANGELOG.md b/CHANGELOG.md index f3ed536..9fcfdfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ### 1.3.0 (Next) - Support changing into other directory for test execution. - Support optional Pytest verbose output instead of former mandatory. +- Support becoming different user after elevated permissions. ### 1.2.3 - Only allow `processes` input parameter if `pytest-xdist` installed. diff --git a/provisioner/testinfra_command.go b/provisioner/testinfra_command.go index 460e1e6..25a6d0a 100644 --- a/provisioner/testinfra_command.go +++ b/provisioner/testinfra_command.go @@ -182,6 +182,11 @@ func (provisioner *Provisioner) determineExecCmd() (*exec.Cmd, *packer.RemoteCmd // sudo if provisioner.config.Sudo { args = append(args, "--sudo") + + // sudo_user + if len(provisioner.config.SudoUser) > 0 { + args = append(args, fmt.Sprintf("\"--sudo-user=%s\"", provisioner.config.SudoUser)) + } } // verbose if provisioner.config.Verbose { diff --git a/provisioner/testinfra_command_test.go b/provisioner/testinfra_command_test.go index 9d65c11..ac69ec3 100644 --- a/provisioner/testinfra_command_test.go +++ b/provisioner/testinfra_command_test.go @@ -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", "-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", fmt.Sprintf("\"--sudo-user=%s\"", provisioner.config.SudoUser), "-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 { diff --git a/provisioner/testinfra_test.go b/provisioner/testinfra_test.go index fbf628f..e02dc2e 100644 --- a/provisioner/testinfra_test.go +++ b/provisioner/testinfra_test.go @@ -21,6 +21,7 @@ var basicConfig = &Config{ Processes: 4, PytestPath: "/usr/local/bin/py.test", Sudo: true, + SudoUser: "fooman", TestFiles: []string{"fixtures/test.py"}, Verbose: true, } @@ -35,7 +36,7 @@ func TestProvisionerConfig(test *testing.T) { config: *basicConfig, } - if provisioner.config.PytestPath != basicConfig.PytestPath || !slices.Equal(provisioner.config.TestFiles, basicConfig.TestFiles) || provisioner.config.Chdir != basicConfig.Chdir || provisioner.config.Keyword != basicConfig.Keyword || provisioner.config.Marker != basicConfig.Marker || provisioner.config.Processes != basicConfig.Processes || provisioner.config.Sudo != basicConfig.Sudo || provisioner.config.Verbose != basicConfig.Verbose { + if provisioner.config.PytestPath != basicConfig.PytestPath || !slices.Equal(provisioner.config.TestFiles, basicConfig.TestFiles) || provisioner.config.Chdir != basicConfig.Chdir || provisioner.config.Keyword != basicConfig.Keyword || provisioner.config.Marker != basicConfig.Marker || provisioner.config.Processes != basicConfig.Processes || provisioner.config.Sudo != basicConfig.Sudo || provisioner.config.SudoUser != basicConfig.SudoUser || provisioner.config.Verbose != basicConfig.Verbose { test.Errorf("provisioner config struct not initialized correctly") } } @@ -97,6 +98,10 @@ func TestProvisionerPrepareMinimal(test *testing.T) { test.Errorf("default false setting for Sudo is incorrect: %t", provisioner.config.Sudo) } + if len(provisioner.config.SudoUser) > 0 { + test.Errorf("default empty setting for SudoUser is incorrect: %s", provisioner.config.SudoUser) + } + if provisioner.config.Verbose == true { test.Errorf("default false setting for Verbose is incorrect: %t", provisioner.config.Verbose) }