Switch back to call operator as opposed to dot sourcing

* Ensure child scope doesn't conflict with parent scope
* Add elevated user options to tests case.
This commit is contained in:
Wilken Rivera 2020-05-20 15:25:19 -04:00
parent 38f799df3e
commit 2e326ef334
5 changed files with 29 additions and 29 deletions

View File

@ -99,14 +99,12 @@ type Provisioner struct {
func (p *Provisioner) defaultExecuteCommand() string {
baseCmd := `& { if (Test-Path variable:global:ProgressPreference)` +
`{set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};` +
`{set-variable -name variable:global:ErrorActionPreference -value 'Continue'};` +
`%s. {{.Vars}};try { & '{{.Path}}' } catch { Write-Error $Error[0]; exit 1 }; if ($LastExitCode) { exit $LastExitCode }}`
`{set-variable -name variable:global:ErrorActionPreference -value 'Continue'};`
var debugLine string
if p.config.DebugMode != 0 {
debugLine = fmt.Sprintf(`Set-PsDebug -Trace %d;`, p.config.DebugMode)
baseCmd += fmt.Sprintf(`Set-PsDebug -Trace %d;`, p.config.DebugMode)
}
baseCmd = fmt.Sprintf(baseCmd, debugLine)
baseCmd += `. {{.Vars}};try { & '{{.Path}}' } catch { Write-Error $Error[0]; exit 1 }; if ($LastExitCode) { exit $LastExitCode }}`
if p.config.ExecutionPolicy == ExecutionPolicyNone {
return baseCmd

View File

@ -39,7 +39,7 @@ func TestAccPowershellProvisioner_Script(t *testing.T) {
acc.TestProvisionersAgainstBuilders(&testProvisioner, t)
}
func TestAccPowershellProvisioner_Invalid(t *testing.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

View File

@ -427,7 +427,7 @@ func TestProvisionerProvision_Inline(t *testing.T) {
}
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(`\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1;try { & 'c:/Windows/Temp/inlineScript\.ps1' }`)
matched := re.MatchString(cmd)
if !matched {
t.Fatalf("Got unexpected command: %s", cmd)
@ -447,7 +447,7 @@ func TestProvisionerProvision_Inline(t *testing.T) {
}
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(`\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1;try { & 'c:/Windows/Temp/inlineScript\.ps1' }`)
matched = re.MatchString(cmd)
if !matched {
t.Fatalf("Got unexpected command: %s", cmd)
@ -477,7 +477,7 @@ func TestProvisionerProvision_Scripts(t *testing.T) {
}
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(`\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1;try { & 'c:/Windows/Temp/script\.ps1' }`)
matched := re.MatchString(cmd)
if !matched {
t.Fatalf("Got unexpected command: %s", cmd)
@ -514,7 +514,7 @@ func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) {
}
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(`\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1;try { & 'c:/Windows/Temp/script\.ps1' }`)
matched := re.MatchString(cmd)
if !matched {
t.Fatalf("Got unexpected command: %s", cmd)
@ -539,11 +539,11 @@ func TestProvisionerProvision_SkipClean(t *testing.T) {
}{
{
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: `\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1;try { & 'c:/Windows/Temp/script.ps1' }`,
},
{
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: `\. 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' }`,
},
}
@ -803,7 +803,7 @@ func TestProvision_createCommandText(t *testing.T) {
p.generatedData = make(map[string]interface{})
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(`\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1;try { & 'c:/Windows/Temp/script\.ps1' }`)
matched := re.MatchString(cmd)
if !matched {
t.Fatalf("Got unexpected command: %s", cmd)
@ -864,5 +864,5 @@ func generatedData() map[string]interface{} {
}
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 }; exit $LastExitCode }"`, varsStr, pathStr)
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)
}

View File

@ -10,7 +10,15 @@
},
{
"type": "powershell",
"script": "../../provisioner/powershell/test-fixtures/scripts/set_version_latest.ps1"
"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",

View File

@ -1,19 +1,13 @@
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
function test
$myNumbersCollection = 1..5
if($myNumbersCollection -contains 3)
{
$myNumbersCollection = 1..5
if($myNumbersCollection -contains 3)
{
"collection contains 3"
}
else
{
"collection doesn't contain 3"
}
"collection contains 3"
}
else
{
"collection doesn't contain 3"
}
test