Revert "Merge pull request #9040 from hashicorp/powershell-exit-code-fix-4916"
This reverts commit 334f399ee36e1ea1833298c1a72354af001d01aa, reversing changes made to 45a5d28bad92bef23832fdc88a4806c911c6fc52. When testing against Windows SSH the Powershell script fails to parse the newly added if statement.
This commit is contained in:
parent
6e610927da
commit
d72173ac1a
@ -98,13 +98,13 @@ type Provisioner struct {
|
|||||||
|
|
||||||
func (p *Provisioner) defaultExecuteCommand() string {
|
func (p *Provisioner) defaultExecuteCommand() string {
|
||||||
baseCmd := `& { if (Test-Path variable:global:ProgressPreference)` +
|
baseCmd := `& { if (Test-Path variable:global:ProgressPreference)` +
|
||||||
`{set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};` +
|
`{set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};`
|
||||||
`{set-variable -name variable:global:ErrorActionPreference -value 'Continue'};`
|
|
||||||
|
|
||||||
if p.config.DebugMode != 0 {
|
if p.config.DebugMode != 0 {
|
||||||
baseCmd += fmt.Sprintf(`Set-PsDebug -Trace %d;`, p.config.DebugMode)
|
baseCmd += fmt.Sprintf(`Set-PsDebug -Trace %d;`, p.config.DebugMode)
|
||||||
}
|
}
|
||||||
baseCmd += `. {{.Vars}};try { & '{{.Path}}' } catch { Write-Error $Error[0]; exit 1 }; if ($LastExitCode) { exit $LastExitCode }}`
|
|
||||||
|
baseCmd += `. {{.Vars}}; &'{{.Path}}'; exit $LastExitCode }`
|
||||||
|
|
||||||
if p.config.ExecutionPolicy == ExecutionPolicyNone {
|
if p.config.ExecutionPolicy == ExecutionPolicyNone {
|
||||||
return baseCmd
|
return baseCmd
|
||||||
|
@ -39,15 +39,6 @@ func TestAccPowershellProvisioner_Script(t *testing.T) {
|
|||||||
acc.TestProvisionersAgainstBuilders(&testProvisioner, t)
|
acc.TestProvisionersAgainstBuilders(&testProvisioner, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAccPowershellProvisioner_ExitCodes(t *testing.T) {
|
|
||||||
acc.TestProvisionersPreCheck(TestProvisionerName, t)
|
|
||||||
|
|
||||||
// This provisioner should fail with an exit code of 1. To assert the failure the fixture
|
|
||||||
// uses the valid_exit_codes option to confirm a non-zero exit code
|
|
||||||
testProvisioner := PowershellProvisionerAccTest{"powershell-exit_codes-provisioner.txt"}
|
|
||||||
acc.TestProvisionersAgainstBuilders(&testProvisioner, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
type PowershellProvisionerAccTest struct {
|
type PowershellProvisionerAccTest struct {
|
||||||
ConfigName string
|
ConfigName string
|
||||||
}
|
}
|
||||||
|
@ -71,13 +71,12 @@ func TestProvisionerPrepare_Defaults(t *testing.T) {
|
|||||||
t.Error("expected elevated_password to be empty")
|
t.Error("expected elevated_password to be empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.config.ExecuteCommand != executeCommand(`. {{.Vars}}`, `{{.Path}}`) {
|
if p.config.ExecuteCommand != `powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. {{.Vars}}; &'{{.Path}}'; exit $LastExitCode }"` {
|
||||||
t.Fatalf(`Default command should be %s, but got '%s'`, executeCommand(`. {{.Vars}}`, ` {{.Path}}`), p.config.ExecuteCommand)
|
t.Fatalf(`Default command should be 'powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. {{.Vars}}; &'{{.Path}}'; exit $LastExitCode }"', but got '%s'`, p.config.ExecuteCommand)
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.config.ElevatedExecuteCommand != executeCommand(`. {{.Vars}}`, `{{.Path}}`) {
|
if p.config.ElevatedExecuteCommand != `powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. {{.Vars}}; &'{{.Path}}'; exit $LastExitCode }"` {
|
||||||
t.Fatalf(`Default command should be %s, but got '%s'`, executeCommand(`. {{.Vars}}`, `{{.Path}}`), p.config.ElevatedExecuteCommand)
|
t.Fatalf(`Default command should be 'powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. {{.Vars}}; &'{{.Path}}'; exit $LastExitCode }"', but got '%s'`, p.config.ElevatedExecuteCommand)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.config.ElevatedEnvVarFormat != `$env:%s="%s"; ` {
|
if p.config.ElevatedEnvVarFormat != `$env:%s="%s"; ` {
|
||||||
@ -118,7 +117,7 @@ func TestProvisionerPrepare_DebugMode(t *testing.T) {
|
|||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
command := executeCommand(`Set-PsDebug -Trace 1;. {{.Vars}}`, `{{.Path}}`)
|
command := `powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};Set-PsDebug -Trace 1;. {{.Vars}}; &'{{.Path}}'; exit $LastExitCode }"`
|
||||||
if p.config.ExecuteCommand != command {
|
if p.config.ExecuteCommand != command {
|
||||||
t.Fatalf(fmt.Sprintf(`Expected command should be '%s' but got '%s'`, command, p.config.ExecuteCommand))
|
t.Fatalf(fmt.Sprintf(`Expected command should be '%s' but got '%s'`, command, p.config.ExecuteCommand))
|
||||||
}
|
}
|
||||||
@ -427,7 +426,7 @@ func TestProvisionerProvision_Inline(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmd := comm.StartCmd.Command
|
cmd := comm.StartCmd.Command
|
||||||
re := regexp.MustCompile(`\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1;try { & 'c:/Windows/Temp/inlineScript\.ps1' }`)
|
re := regexp.MustCompile(`powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1; &'c:/Windows/Temp/inlineScript.ps1'; exit \$LastExitCode }"`)
|
||||||
matched := re.MatchString(cmd)
|
matched := re.MatchString(cmd)
|
||||||
if !matched {
|
if !matched {
|
||||||
t.Fatalf("Got unexpected command: %s", cmd)
|
t.Fatalf("Got unexpected command: %s", cmd)
|
||||||
@ -447,7 +446,7 @@ func TestProvisionerProvision_Inline(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmd = comm.StartCmd.Command
|
cmd = comm.StartCmd.Command
|
||||||
re = regexp.MustCompile(`\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1;try { & 'c:/Windows/Temp/inlineScript\.ps1' }`)
|
re = regexp.MustCompile(`powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1; &'c:/Windows/Temp/inlineScript.ps1'; exit \$LastExitCode }"`)
|
||||||
matched = re.MatchString(cmd)
|
matched = re.MatchString(cmd)
|
||||||
if !matched {
|
if !matched {
|
||||||
t.Fatalf("Got unexpected command: %s", cmd)
|
t.Fatalf("Got unexpected command: %s", cmd)
|
||||||
@ -477,7 +476,7 @@ func TestProvisionerProvision_Scripts(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmd := comm.StartCmd.Command
|
cmd := comm.StartCmd.Command
|
||||||
re := regexp.MustCompile(`\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1;try { & 'c:/Windows/Temp/script\.ps1' }`)
|
re := regexp.MustCompile(`powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1; &'c:/Windows/Temp/script.ps1'; exit \$LastExitCode }"`)
|
||||||
matched := re.MatchString(cmd)
|
matched := re.MatchString(cmd)
|
||||||
if !matched {
|
if !matched {
|
||||||
t.Fatalf("Got unexpected command: %s", cmd)
|
t.Fatalf("Got unexpected command: %s", cmd)
|
||||||
@ -514,7 +513,7 @@ func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmd := comm.StartCmd.Command
|
cmd := comm.StartCmd.Command
|
||||||
re := regexp.MustCompile(`\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1;try { & 'c:/Windows/Temp/script\.ps1' }`)
|
re := regexp.MustCompile(`powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1; &'c:/Windows/Temp/script.ps1'; exit \$LastExitCode }"`)
|
||||||
matched := re.MatchString(cmd)
|
matched := re.MatchString(cmd)
|
||||||
if !matched {
|
if !matched {
|
||||||
t.Fatalf("Got unexpected command: %s", cmd)
|
t.Fatalf("Got unexpected command: %s", cmd)
|
||||||
@ -539,11 +538,11 @@ func TestProvisionerProvision_SkipClean(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
SkipClean: true,
|
SkipClean: true,
|
||||||
LastExecutedCommandRegex: `\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1;try { & 'c:/Windows/Temp/script.ps1' }`,
|
LastExecutedCommandRegex: `powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1; &'c:/Windows/Temp/script.ps1'; exit \$LastExitCode }"`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
SkipClean: false,
|
SkipClean: false,
|
||||||
LastExecutedCommandRegex: `\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1;try { & 'c:/Windows/Temp/packer-cleanup-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1' }`,
|
LastExecutedCommandRegex: `powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1; &'c:/Windows/Temp/packer-cleanup-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1'; exit \$LastExitCode }"`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -803,7 +802,7 @@ func TestProvision_createCommandText(t *testing.T) {
|
|||||||
p.generatedData = make(map[string]interface{})
|
p.generatedData = make(map[string]interface{})
|
||||||
cmd, _ := p.createCommandText()
|
cmd, _ := p.createCommandText()
|
||||||
|
|
||||||
re := regexp.MustCompile(`\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1;try { & 'c:/Windows/Temp/script\.ps1' }`)
|
re := regexp.MustCompile(`powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1; &'c:/Windows/Temp/script.ps1'; exit \$LastExitCode }"`)
|
||||||
matched := re.MatchString(cmd)
|
matched := re.MatchString(cmd)
|
||||||
if !matched {
|
if !matched {
|
||||||
t.Fatalf("Got unexpected command: %s", cmd)
|
t.Fatalf("Got unexpected command: %s", cmd)
|
||||||
@ -862,7 +861,3 @@ func generatedData() map[string]interface{} {
|
|||||||
"PackerHTTPPort": common.HttpPortNotImplemented,
|
"PackerHTTPPort": common.HttpPortNotImplemented,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func executeCommand(varsStr, pathStr string) string {
|
|
||||||
return fmt.Sprintf(`powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};{set-variable -name variable:global:ErrorActionPreference -value 'Continue'};%s;try { & '%s' } catch { Write-Error $Error[0]; exit 1 }; if ($LastExitCode) { exit $LastExitCode }}"`, varsStr, pathStr)
|
|
||||||
}
|
|
||||||
|
@ -1,27 +0,0 @@
|
|||||||
{
|
|
||||||
"type": "powershell",
|
|
||||||
"inline": ["invalid-cmdlet"],
|
|
||||||
"valid_exit_codes": ["1"]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "powershell",
|
|
||||||
"inline": ["#Requires -Version 10.0"],
|
|
||||||
"valid_exit_codes": ["1"]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "powershell",
|
|
||||||
"script": "../../provisioner/powershell/test-fixtures/scripts/set_version_latest.ps1",
|
|
||||||
"valid_exit_codes": ["0"]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "powershell",
|
|
||||||
"elevated_user": "Administrator",
|
|
||||||
"elevated_password": "{{.WinRMPassword}}",
|
|
||||||
"inline": "Get-ItemProperty -Path HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion",
|
|
||||||
"valid_exit_codes": ["0"]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "powershell",
|
|
||||||
"inline": "sc.exe start Life",
|
|
||||||
"valid_exit_codes": ["1060"]
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
# Test fixture is a modified version of the example found at
|
|
||||||
# https://www.powershellmagazine.com/2012/10/23/pstip-set-strictmode-why-should-you-care/
|
|
||||||
|
|
||||||
Set-StrictMode -Version Latest
|
|
||||||
$ErrorActionPreference = "Stop"
|
|
||||||
|
|
||||||
$myNumbersCollection = 1..5
|
|
||||||
if($myNumbersCollection -contains 3) {
|
|
||||||
"collection contains 3"
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
"collection doesn't contain 3"
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user