@ -79,8 +79,8 @@ func TestProvisionerPrepare_Defaults(t *testing.T) {
t . Error ( "expected elevated_password to be empty" )
}
if p . config . ExecuteCommand != ` powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference) { $ProgressPreference='SilentlyContinue'}; . {{ .Vars }} ; &'{{ .Path }} ';exit $LastExitCode }" ` {
t . Fatalf ( ` Default command should be 'powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference) { $ProgressPreference='SilentlyContinue'}; . {{ .Vars }} ; &'{{ .Path }} ';exit $LastExitCode }"', but got '%s' ` , p . config . ExecuteCommand )
if p . config . ExecuteCommand != ` powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference) { $ProgressPreference='SilentlyContinue'}; {{ .Vars }} &'{{ .Path }} ';exit $LastExitCode }" ` {
t . Fatalf ( ` Default command should be 'powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference) { $ProgressPreference='SilentlyContinue'}; {{ .Vars }} &'{{ .Path }} ';exit $LastExitCode }"', but got '%s' ` , p . config . ExecuteCommand )
}
if p . config . ElevatedExecuteCommand != ` powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference) { $ProgressPreference='SilentlyContinue'};. {{ .Vars }} ; &' {{ .Path }} '; exit $LastExitCode }" ` {
@ -403,7 +403,7 @@ func TestProvisionerProvision_Inline(t *testing.T) {
ui := testUi ( )
p := new ( Provisioner )
// Defaults provided by Packer - env vars should not appear in cmd
// Defaults provided by Packer
p . config . PackerBuildName = "vmware"
p . config . PackerBuilderType = "iso"
comm := new ( packer . MockCommunicator )
@ -413,14 +413,11 @@ func TestProvisionerProvision_Inline(t *testing.T) {
t . Fatal ( "should not have error" )
}
cmd := comm . StartCmd . Command
re := regexp . MustCompile ( ` powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\) { \$ProgressPreference='SilentlyContinue'};\. \$ { env:SYSTEMROOT}\\Temp\\packer-env-vars-[[:alnum:]] { 8}-[[:alnum:]] { 4}-[[:alnum:]] { 4}-[[:alnum:]] { 4}-[[:alnum:]] { 12}\.ps1; &'c:/Windows/Temp/inlineScript.ps1';exit \$LastExitCode }" ` )
matched := re . MatchString ( cmd )
if ! matched {
t . Fatalf ( "Got unexpected command: %s" , cmd )
expectedCommand := ` powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference) { $ProgressPreference='SilentlyContinue'};$env:PACKER_BUILDER_TYPE=\"iso\"; $env:PACKER_BUILD_NAME=\"vmware\"; &'c:/Windows/Temp/inlineScript.ps1';exit $LastExitCode }" `
if comm . StartCmd . Command != expectedCommand {
t . Fatalf ( "Expect command to be: %s, got %s" , expectedCommand , comm . StartCmd . Command )
}
// User supplied env vars should not change things
envVars := make ( [ ] string , 2 )
envVars [ 0 ] = "FOO=BAR"
envVars [ 1 ] = "BAR=BAZ"
@ -433,11 +430,9 @@ func TestProvisionerProvision_Inline(t *testing.T) {
t . Fatal ( "should not have error" )
}
cmd = comm . StartCmd . Command
re = regexp . MustCompile ( ` powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\) { \$ProgressPreference='SilentlyContinue'};\. \$ { env:SYSTEMROOT}\\Temp\\packer-env-vars-[[:alnum:]] { 8}-[[:alnum:]] { 4}-[[:alnum:]] { 4}-[[:alnum:]] { 4}-[[:alnum:]] { 12}\.ps1; &'c:/Windows/Temp/inlineScript.ps1';exit \$LastExitCode }" ` )
matched = re . MatchString ( cmd )
if ! matched {
t . Fatalf ( "Got unexpected command: %s" , cmd )
expectedCommand = ` powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference) { $ProgressPreference='SilentlyContinue'};$env:BAR=\"BAZ\"; $env:FOO=\"BAR\"; $env:PACKER_BUILDER_TYPE=\"iso\"; $env:PACKER_BUILD_NAME=\"vmware\"; &'c:/Windows/Temp/inlineScript.ps1';exit $LastExitCode }" `
if comm . StartCmd . Command != expectedCommand {
t . Fatalf ( "Expect command to be: %s, got %s" , expectedCommand , comm . StartCmd . Command )
}
}
@ -460,12 +455,11 @@ func TestProvisionerProvision_Scripts(t *testing.T) {
t . Fatal ( "should not have error" )
}
cmd := comm . StartCmd . Command
re := regexp . MustCompile ( ` powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\) { \$ProgressPreference='SilentlyContinue'};\. \$ { env:SYSTEMROOT}\\Temp\\packer-env-vars-[[:alnum:]] { 8}-[[:alnum:]] { 4}-[[:alnum:]] { 4}-[[:alnum:]] { 4}-[[:alnum:]] { 12}\.ps1; &'c:/Windows/Temp/script.ps1';exit \$LastExitCode }" ` )
matched := re . MatchString ( cmd )
if ! matched {
t . Fatalf ( "Got unexpected command: %s" , cmd )
expectedCommand := ` powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference) { $ProgressPreference='SilentlyContinue'};$env:PACKER_BUILDER_TYPE=\"footype\"; $env:PACKER_BUILD_NAME=\"foobuild\"; &'c:/Windows/Temp/script.ps1';exit $LastExitCode }" `
if comm . StartCmd . Command != expectedCommand {
t . Fatalf ( "Expect command to be: %s, got %s" , expectedCommand , comm . StartCmd . Command )
}
}
func TestProvisionerProvision_ScriptsWithEnvVars ( t * testing . T ) {
@ -494,11 +488,9 @@ func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) {
t . Fatal ( "should not have error" )
}
cmd := comm . StartCmd . Command
re := regexp . MustCompile ( ` powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\) { \$ProgressPreference='SilentlyContinue'};\. \$ { env:SYSTEMROOT}\\Temp\\packer-env-vars-[[:alnum:]] { 8}-[[:alnum:]] { 4}-[[:alnum:]] { 4}-[[:alnum:]] { 4}-[[:alnum:]] { 12}\.ps1; &'c:/Windows/Temp/script.ps1';exit \$LastExitCode }" ` )
matched := re . MatchString ( cmd )
if ! matched {
t . Fatalf ( "Got unexpected command: %s" , cmd )
expectedCommand := ` powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference) { $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 }" `
if comm . StartCmd . Command != expectedCommand {
t . Fatalf ( "Expect command to be: %s, got %s" , expectedCommand , comm . StartCmd . Command )
}
}
@ -555,11 +547,11 @@ func TestProvisioner_createFlattenedEnvVars_windows(t *testing.T) {
{ "FOO==bar" } , // User env var with value starting with equals
}
expected := [ ] string {
` $env:PACKER_BUILDER_TYPE= "iso"; $env:PACKER_BUILD_NAME="vmware"; ` ,
` $env:FOO= "bar"; $env:PACKER_BUILDER_TYPE="iso"; $env:PACKER_BUILD_NAME="vmware"; ` ,
` $env:BAZ= "qux"; $env:FOO="bar"; $env:PACKER_BUILDER_TYPE="iso"; $env:PACKER_BUILD_NAME="vmware"; ` ,
` $env:FOO= "bar=baz"; $env:PACKER_BUILDER_TYPE="iso"; $env:PACKER_BUILD_NAME="vmware"; ` ,
` $env:FOO= "=bar"; $env:PACKER_BUILDER_TYPE="iso"; $env:PACKER_BUILD_NAME="vmware"; ` ,
` $env:PACKER_BUILDER_TYPE= \ "iso\ "; $env:PACKER_BUILD_NAME=\ "vmware\ "; ` ,
` $env:FOO= \ "bar\ "; $env:PACKER_BUILDER_TYPE=\ "iso\ "; $env:PACKER_BUILD_NAME=\ "vmware\ "; ` ,
` $env:BAZ= \ "qux\ "; $env:FOO=\ "bar\ "; $env:PACKER_BUILDER_TYPE=\ "iso\ "; $env:PACKER_BUILD_NAME=\ "vmware\ "; ` ,
` $env:FOO= \ "bar=baz\ "; $env:PACKER_BUILDER_TYPE=\ "iso\ "; $env:PACKER_BUILD_NAME=\ "vmware\ "; ` ,
` $env:FOO= \ "=bar\ "; $env:PACKER_BUILDER_TYPE=\ "iso\ "; $env:PACKER_BUILD_NAME=\ "vmware\ "; ` ,
}
p := new ( Provisioner )
@ -579,6 +571,7 @@ func TestProvisioner_createFlattenedEnvVars_windows(t *testing.T) {
}
func TestProvision_createCommandText ( t * testing . T ) {
config := testConfig ( )
config [ "remote_path" ] = "c:/Windows/Temp/script.ps1"
p := new ( Provisioner )
@ -593,46 +586,22 @@ func TestProvision_createCommandText(t *testing.T) {
// Non-elevated
cmd , _ := p . createCommandText ( )
re := regexp . MustCompile ( ` powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\) { \$ProgressPreference='SilentlyContinue'};\. \$ { env:SYSTEMROOT}\\Temp\\packer-env-vars-[[:alnum:]] { 8}-[[:alnum:]] { 4}-[[:alnum:]] { 4}-[[:alnum:]] { 4}-[[:alnum:]] { 12}\.ps1; &'c:/Windows/Temp/script.ps1';exit \$LastExitCode }" ` )
matched := re . MatchString ( cmd )
if ! matche d {
t . Fatalf ( " Got unexpected command: %s" , cmd )
expectedCommand := ` powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference) { $ProgressPreference='SilentlyContinue'};$env:PACKER_BUILDER_TYPE=\"iso\"; $env:PACKER_BUILD_NAME=\"vmware\"; &'c:/Windows/Temp/script.ps1';exit $LastExitCode }" `
if cmd != expectedComman d {
t . Fatalf ( " Expected Non-elevated command: %s, got %s", expectedCommand , cmd )
}
// Elevated
p . config . ElevatedUser = "vagrant"
p . config . ElevatedPassword = "vagrant"
cmd , _ = p . createCommandText ( )
re = regexp . MustCompile ( ` powershell -executionpolicy bypass -file "%TEMP%\\packer-elevated-shell-[[:alnum:]] { 8}-[[:alnum:]] { 4}-[[:alnum:]] { 4}-[[:alnum:]] { 4}-[[:alnum:]] { 12}\.ps1" ` )
matched = re . MatchString ( cmd )
matched , _ := regexp . MatchString ( "powershell -executionpolicy bypass -file \"%TEMP%(.{1})packer-elevated-shell.*" , cmd )
if ! matched {
t . Fatalf ( "Got unexpected elevated command: %s" , cmd )
}
}
func TestProvision_uploadEnvVars ( t * testing . T ) {
p := new ( Provisioner )
comm := new ( packer . MockCommunicator )
p . communicator = comm
flattenedEnvVars := ` $env:PACKER_BUILDER_TYPE="footype"; $env:PACKER_BUILD_NAME="foobuild"; `
envVarPath , err := p . uploadEnvVars ( flattenedEnvVars )
if err != nil {
t . Fatalf ( "Did not expect error: %s" , err . Error ( ) )
}
if comm . UploadCalled != true {
t . Fatalf ( "Failed to upload env var file" )
}
re := regexp . MustCompile ( ` \$ { env:SYSTEMROOT}\\Temp\\packer-env-vars-[[:alnum:]] { 8}-[[:alnum:]] { 4}-[[:alnum:]] { 4}-[[:alnum:]] { 4}-[[:alnum:]] { 12}\.ps1 ` )
matched := re . MatchString ( envVarPath )
if ! matched {
t . Fatalf ( "Got unexpected path for env var file: %s" , envVarPath )
}
}
func TestProvision_generateElevatedShellRunner ( t * testing . T ) {
// Non-elevated