From 4b394c8563dc66db875d12416d0575ee56ae8dd7 Mon Sep 17 00:00:00 2001 From: Taliesin Sisson Date: Mon, 2 Nov 2015 17:35:38 +0000 Subject: [PATCH] Write output will put ouput from function, so we don't want to be getting line from output of function Fix unit tests for not showing progress stream when using powershell Ensure that progress stream does not get leaked into stdout Using Write-Output instead of Write-Host since PS v5 now leaks the host stream to stderr --- powershell/powershell_test.go | 2 +- provisioner/powershell/elevated.go | 16 +++++++--------- provisioner/powershell/provisioner.go | 6 +++--- provisioner/powershell/provisioner_test.go | 14 +++++++------- 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/powershell/powershell_test.go b/powershell/powershell_test.go index 4aff20491..0925e3c74 100644 --- a/powershell/powershell_test.go +++ b/powershell/powershell_test.go @@ -37,7 +37,7 @@ func TestOutput(t *testing.T) { func TestRunFile(t *testing.T) { var blockBuffer bytes.Buffer - blockBuffer.WriteString("param([string]$a, [string]$b, [int]$x, [int]$y) $n = $x + $y; Write-Host $a, $b, $n") + blockBuffer.WriteString(`param([string]$a, [string]$b, [int]$x, [int]$y) $ProgressPreference='SilentlyContinue'; $n = $x + $y; Write-Output "$a $b $n";`) var ps PowerShellCmd cmdOut, err := ps.Output(blockBuffer.String(), "a", "b", "5", "10") diff --git a/provisioner/powershell/elevated.go b/provisioner/powershell/elevated.go index ec79afa36..6678b4b65 100644 --- a/provisioner/powershell/elevated.go +++ b/provisioner/powershell/elevated.go @@ -58,6 +58,7 @@ $t.XmlText = @' '@ +$ProgressPreference='SilentlyContinue'; $f = $s.GetFolder("\") $f.RegisterTaskDefinition($name, $t, 6, "{{.User}}", "{{.Password}}", 1, $null) | Out-Null $t = $f.GetTask("\$name") @@ -68,19 +69,16 @@ while ((!($t.state -eq 4)) -and ($sec -lt $timeout)) { Start-Sleep -s 1 $sec++ } -function SlurpOutput($l) { + +$line = 0 +do { + Start-Sleep -m 100 if (Test-Path $log) { - Get-Content $log | select -skip $l | ForEach { - $l += 1 + Get-Content $log | select -skip $line | ForEach { + $line += 1 Write-Output "$_" } } - return $l -} -$line = 0 -do { - Start-Sleep -m 100 - $line = SlurpOutput $line } while (!($t.state -eq 3)) $result = $t.LastTaskResult [System.Runtime.Interopservices.Marshal]::ReleaseComObject($s) | Out-Null diff --git a/provisioner/powershell/provisioner.go b/provisioner/powershell/provisioner.go index ad90e56a1..9d23dde9a 100644 --- a/provisioner/powershell/provisioner.go +++ b/provisioner/powershell/provisioner.go @@ -120,11 +120,11 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { } if p.config.ExecuteCommand == "" { - p.config.ExecuteCommand = `powershell '& { {{.Vars}}{{.Path}}; exit $LastExitCode}'` + p.config.ExecuteCommand = `powershell '& {$ProgressPreference='SilentlyContinue'; {{.Vars}}{{.Path}}; exit $LastExitCode}'` } if p.config.ElevatedExecuteCommand == "" { - p.config.ElevatedExecuteCommand = `powershell '& { {{.Vars}}{{.Path}}; exit $LastExitCode}'` + p.config.ElevatedExecuteCommand = `powershell '& {$ProgressPreference='SilentlyContinue'; {{.Vars}}{{.Path}}; exit $LastExitCode}'` } if p.config.Inline != nil && len(p.config.Inline) == 0 { @@ -425,7 +425,7 @@ func (p *Provisioner) generateElevatedRunner(command string) (uploadedPath strin Password: p.config.ElevatedPassword, TaskDescription: "Packer elevated task", TaskName: fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID()), - EncodedCommand: powershellEncode([]byte(command + "; exit $LASTEXITCODE")), + EncodedCommand: powershellEncode([]byte("$ProgressPreference='SilentlyContinue'; " + command + "; exit $LASTEXITCODE")), }) if err != nil { diff --git a/provisioner/powershell/provisioner_test.go b/provisioner/powershell/provisioner_test.go index e109eb6d5..e8b0728eb 100644 --- a/provisioner/powershell/provisioner_test.go +++ b/provisioner/powershell/provisioner_test.go @@ -75,11 +75,11 @@ func TestProvisionerPrepare_Defaults(t *testing.T) { t.Error("expected elevated_password to be empty") } - if p.config.ExecuteCommand != "powershell '& { {{.Vars}}{{.Path}}; exit $LastExitCode}'" { + if p.config.ExecuteCommand != "powershell '& {$ProgressPreference='SilentlyContinue'; {{.Vars}}{{.Path}}; exit $LastExitCode}'" { t.Fatalf("Default command should be powershell '& { {{.Vars}}{{.Path}}; exit $LastExitCode}', but got %s", p.config.ExecuteCommand) } - if p.config.ElevatedExecuteCommand != "powershell '& { {{.Vars}}{{.Path}}; exit $LastExitCode}'" { + if p.config.ElevatedExecuteCommand != "powershell '& {$ProgressPreference='SilentlyContinue'; {{.Vars}}{{.Path}}; exit $LastExitCode}'" { t.Fatalf("Default command should be powershell powershell '& { {{.Vars}}{{.Path}}; exit $LastExitCode}', but got %s", p.config.ElevatedExecuteCommand) } @@ -389,7 +389,7 @@ func TestProvisionerProvision_Inline(t *testing.T) { t.Fatal("should not have error") } - expectedCommand := `powershell '& { $env:PACKER_BUILDER_TYPE=\"iso\"; $env:PACKER_BUILD_NAME=\"vmware\"; c:/Windows/Temp/inlineScript.bat; exit $LastExitCode}'` + expectedCommand := `powershell '& {$ProgressPreference='SilentlyContinue'; $env:PACKER_BUILDER_TYPE=\"iso\"; $env:PACKER_BUILD_NAME=\"vmware\"; c:/Windows/Temp/inlineScript.bat; exit $LastExitCode}'` // Should run the command without alteration if comm.StartCmd.Command != expectedCommand { @@ -408,7 +408,7 @@ func TestProvisionerProvision_Inline(t *testing.T) { t.Fatal("should not have error") } - expectedCommand = `powershell '& { $env:BAR=\"BAZ\"; $env:FOO=\"BAR\"; $env:PACKER_BUILDER_TYPE=\"iso\"; $env:PACKER_BUILD_NAME=\"vmware\"; c:/Windows/Temp/inlineScript.bat; exit $LastExitCode}'` + expectedCommand = `powershell '& {$ProgressPreference='SilentlyContinue'; $env:BAR=\"BAZ\"; $env:FOO=\"BAR\"; $env:PACKER_BUILDER_TYPE=\"iso\"; $env:PACKER_BUILD_NAME=\"vmware\"; c:/Windows/Temp/inlineScript.bat; exit $LastExitCode}'` // Should run the command without alteration if comm.StartCmd.Command != expectedCommand { @@ -435,7 +435,7 @@ func TestProvisionerProvision_Scripts(t *testing.T) { } //powershell -Command "$env:PACKER_BUILDER_TYPE=''"; powershell -Command "$env:PACKER_BUILD_NAME='foobuild'"; powershell -Command c:/Windows/Temp/script.ps1 - expectedCommand := `powershell '& { $env:PACKER_BUILDER_TYPE=\"footype\"; $env:PACKER_BUILD_NAME=\"foobuild\"; c:/Windows/Temp/script.ps1; exit $LastExitCode}'` + expectedCommand := `powershell '& {$ProgressPreference='SilentlyContinue'; $env:PACKER_BUILDER_TYPE=\"footype\"; $env:PACKER_BUILD_NAME=\"foobuild\"; c:/Windows/Temp/script.ps1; exit $LastExitCode}'` // Should run the command without alteration if comm.StartCmd.Command != expectedCommand { @@ -468,7 +468,7 @@ func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) { t.Fatal("should not have error") } - expectedCommand := `powershell '& { $env:BAR=\"BAZ\"; $env:FOO=\"BAR\"; $env:PACKER_BUILDER_TYPE=\"footype\"; $env:PACKER_BUILD_NAME=\"foobuild\"; c:/Windows/Temp/script.ps1; exit $LastExitCode}'` + expectedCommand := `powershell '& {$ProgressPreference='SilentlyContinue'; $env:BAR=\"BAZ\"; $env:FOO=\"BAR\"; $env:PACKER_BUILDER_TYPE=\"footype\"; $env:PACKER_BUILD_NAME=\"foobuild\"; c:/Windows/Temp/script.ps1; exit $LastExitCode}'` // Should run the command without alteration if comm.StartCmd.Command != expectedCommand { @@ -582,7 +582,7 @@ func TestProvision_createCommandText(t *testing.T) { // Non-elevated cmd, _ := p.createCommandText() - if cmd != `powershell '& { $env:PACKER_BUILDER_TYPE=\"\"; $env:PACKER_BUILD_NAME=\"\"; c:/Windows/Temp/script.ps1; exit $LastExitCode}'` { + if cmd != `powershell '& {$ProgressPreference='SilentlyContinue'; $env:PACKER_BUILDER_TYPE=\"\"; $env:PACKER_BUILD_NAME=\"\"; c:/Windows/Temp/script.ps1; exit $LastExitCode}'` { t.Fatalf("Got unexpected non-elevated command: %s", cmd) }