Skip to content

Commit

Permalink
Merge pull request #112 from danhutchings/main
Browse files Browse the repository at this point in the history
Add install/uninstall failure testable examples
  • Loading branch information
1dustindavis authored May 3, 2021
2 parents c86cd54 + 06cee25 commit 968e8ac
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 17 deletions.
3 changes: 2 additions & 1 deletion pkg/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ var (
// These abstractions allows us to override when testing
execCommand = exec.Command
statusCheckStatus = status.CheckStatus
runCommand = runCMD

// Stores url where we will download an item
installerURL string
uninstallerURL string
)

// runCommand executes a command and it's argurments in the CMD environment
func runCommand(command string, arguments []string) (string, error) {
func runCMD(command string, arguments []string) (string, error) {
cmd := execCommand(command, arguments...)
var cmdOutput string
cmdReader, err := cmd.StdoutPipe()
Expand Down
104 changes: 88 additions & 16 deletions pkg/installer/installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var (
origCheckStatus = statusCheckStatus
origReportInstalled = report.InstalledItems
origInstallItemFunc = installItemFunc
origRunCommand = runCommand

// These tore the URL that `Install` generates during testing
installItemURL string
Expand Down Expand Up @@ -113,6 +114,19 @@ func fakeExecCommand(command string, args ...string) *exec.Cmd {
return cmd
}

// fakeRunCommand just returns a string and error interface
func fakeRunCommand(command string, arguments []string) (string, error) {
cmdOutput := "This is a fake test command return"
var err error
if msiItem.DisplayName == statusActionNoError {
err = nil
} else if msiItem.DisplayName == statusActionError {
err = fmt.Errorf("Deliberate test error has occurred!!")
}

return cmdOutput, err
}

// TestHelperProcess processes the commands passed to fakeExecCommand
func TestHelperProcess(t *testing.T) {
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
Expand Down Expand Up @@ -587,10 +601,12 @@ func Example_installItemSuccess() {
// Override execCommand and checkStatus with our fake versions
execCommand = fakeExecCommand
statusCheckStatus = fakeCheckStatus
runCommand = fakeRunCommand
download.SetConfig(downloadCfg)
defer func() {
execCommand = origExec
statusCheckStatus = origCheckStatus
runCommand = origRunCommand
}()

// Set shared testing variables
Expand All @@ -600,30 +616,61 @@ func Example_installItemSuccess() {
//
// Msi
//
msiItem.DisplayName = statusNoActionNoError
msiItem.DisplayName = statusActionNoError

//

// Run Install
installItem(msiItem, urlPackages, cachePath)

// Output:
// Installing msi for _gorilla_dev_noaction_noerror_
// command: C:\Windows\system32\msiexec.exe [/i testdata\packages\chef-client\chef-client-14.3.37-1-x64.msi /qn /norestart /L=1033 /S]
// Command Output:
// --------------------
// [C:\Windows\system32\msiexec.exe /i testdata\packages\chef-client\chef-client-14.3.37-1-x64.msi /qn /norestart /L=1033 /S]
// --------------------
// _gorilla_dev_noaction_noerror_ 1.2.3 Installation SUCCESSFUL
// Installing msi for _gorilla_dev_action_noerror_
// _gorilla_dev_action_noerror_ 1.2.3 Installation SUCCESSFUL

}

func Example_installItemFailure() {
// Override execCommand and checkStatus with our fake versions
execCommand = fakeExecCommand
statusCheckStatus = fakeCheckStatus
runCommand = fakeRunCommand
download.SetConfig(downloadCfg)
defer func() {
execCommand = origExec
statusCheckStatus = origCheckStatus
runCommand = origRunCommand
}()

// Set shared testing variables
cachePath := "testdata/"
urlPackages := "https://example.com/"

//
// Msi
//
msiItem.DisplayName = statusActionError

//

// Run Install
installItem(msiItem, urlPackages, cachePath)

// Output:
// Installing msi for _gorilla_dev_action_error_
// _gorilla_dev_action_error_ 1.2.3 Installation FAILED

}

func Example_uninstallItemSuccess() {
// Override execCommand and checkStatus with our fake versions
execCommand = fakeExecCommand
statusCheckStatus = fakeCheckStatus
runCommand = fakeRunCommand
download.SetConfig(downloadCfg)
defer func() {
execCommand = origExec
statusCheckStatus = origCheckStatus
runCommand = origRunCommand
}()

// Set shared testing variables
Expand All @@ -633,18 +680,43 @@ func Example_uninstallItemSuccess() {
//
// Msi
//
msiItem.DisplayName = statusNoActionNoError
msiItem.DisplayName = statusActionNoError

// Run Install
uninstallItem(msiItem, urlPackages, cachePath)

// Output:
// Uninstalling msi for _gorilla_dev_noaction_noerror_
// command: C:\Windows\system32\msiexec.exe [/x testdata\packages\chef-client\chef-client-14.3.37-1-x64uninst.msi /qn /norestart]
// Command Output:
// --------------------
// [C:\Windows\system32\msiexec.exe /x testdata\packages\chef-client\chef-client-14.3.37-1-x64uninst.msi /qn /norestart]
// --------------------
// _gorilla_dev_noaction_noerror_ 1.2.3 Uninstallation SUCCESSFUL
// Uninstalling msi for _gorilla_dev_action_noerror_
// _gorilla_dev_action_noerror_ 1.2.3 Uninstallation SUCCESSFUL

}

func Example_uninstallItemFailure() {
// Override execCommand and checkStatus with our fake versions
execCommand = fakeExecCommand
statusCheckStatus = fakeCheckStatus
runCommand = fakeRunCommand
download.SetConfig(downloadCfg)
defer func() {
execCommand = origExec
statusCheckStatus = origCheckStatus
runCommand = origRunCommand
}()

// Set shared testing variables
cachePath := "testdata/"
urlPackages := "https://example.com/"

//
// Msi
//
msiItem.DisplayName = statusActionError

// Run Install
uninstallItem(msiItem, urlPackages, cachePath)

// Output:
// Uninstalling msi for _gorilla_dev_action_error_
// _gorilla_dev_action_error_ 1.2.3 Uninstallation FAILED

}

0 comments on commit 968e8ac

Please sign in to comment.