Skip to content

Commit

Permalink
add sudo_user functionality and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Apr 15, 2024
1 parent af142fa commit b97ba7d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
5 changes: 5 additions & 0 deletions provisioner/testinfra_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
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", "-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 {
Expand Down
7 changes: 6 additions & 1 deletion provisioner/testinfra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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")
}
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit b97ba7d

Please sign in to comment.