Fix tests post changes. Add test for upload func.
This commit is contained in:
parent
478589abec
commit
a7b118ed94
|
@ -79,8 +79,8 @@ 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 != `powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){$ProgressPreference='SilentlyContinue'};{{.Vars}}&'{{.Path}}';exit $LastExitCode }"` {
|
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)
|
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 }"` {
|
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()
|
ui := testUi()
|
||||||
p := new(Provisioner)
|
p := new(Provisioner)
|
||||||
|
|
||||||
// Defaults provided by Packer
|
// Defaults provided by Packer - env vars should not appear in cmd
|
||||||
p.config.PackerBuildName = "vmware"
|
p.config.PackerBuildName = "vmware"
|
||||||
p.config.PackerBuilderType = "iso"
|
p.config.PackerBuilderType = "iso"
|
||||||
comm := new(packer.MockCommunicator)
|
comm := new(packer.MockCommunicator)
|
||||||
|
@ -413,11 +413,14 @@ func TestProvisionerProvision_Inline(t *testing.T) {
|
||||||
t.Fatal("should not have error")
|
t.Fatal("should not have error")
|
||||||
}
|
}
|
||||||
|
|
||||||
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 }"`
|
cmd := comm.StartCmd.Command
|
||||||
if comm.StartCmd.Command != expectedCommand {
|
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 }"`)
|
||||||
t.Fatalf("Expect command to be: %s, got %s", expectedCommand, comm.StartCmd.Command)
|
matched := re.MatchString(cmd)
|
||||||
|
if !matched {
|
||||||
|
t.Fatalf("Got unexpected command: %s", cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// User supplied env vars should not change things
|
||||||
envVars := make([]string, 2)
|
envVars := make([]string, 2)
|
||||||
envVars[0] = "FOO=BAR"
|
envVars[0] = "FOO=BAR"
|
||||||
envVars[1] = "BAR=BAZ"
|
envVars[1] = "BAR=BAZ"
|
||||||
|
@ -430,9 +433,11 @@ func TestProvisionerProvision_Inline(t *testing.T) {
|
||||||
t.Fatal("should not have error")
|
t.Fatal("should not have error")
|
||||||
}
|
}
|
||||||
|
|
||||||
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 }"`
|
cmd = comm.StartCmd.Command
|
||||||
if comm.StartCmd.Command != expectedCommand {
|
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 }"`)
|
||||||
t.Fatalf("Expect command to be: %s, got %s", expectedCommand, comm.StartCmd.Command)
|
matched = re.MatchString(cmd)
|
||||||
|
if !matched {
|
||||||
|
t.Fatalf("Got unexpected command: %s", cmd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -455,11 +460,12 @@ func TestProvisionerProvision_Scripts(t *testing.T) {
|
||||||
t.Fatal("should not have error")
|
t.Fatal("should not have error")
|
||||||
}
|
}
|
||||||
|
|
||||||
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 }"`
|
cmd := comm.StartCmd.Command
|
||||||
if comm.StartCmd.Command != expectedCommand {
|
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 }"`)
|
||||||
t.Fatalf("Expect command to be: %s, got %s", expectedCommand, comm.StartCmd.Command)
|
matched := re.MatchString(cmd)
|
||||||
|
if !matched {
|
||||||
|
t.Fatalf("Got unexpected command: %s", cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) {
|
func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) {
|
||||||
|
@ -488,9 +494,11 @@ func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) {
|
||||||
t.Fatal("should not have error")
|
t.Fatal("should not have error")
|
||||||
}
|
}
|
||||||
|
|
||||||
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 }"`
|
cmd := comm.StartCmd.Command
|
||||||
if comm.StartCmd.Command != expectedCommand {
|
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 }"`)
|
||||||
t.Fatalf("Expect command to be: %s, got %s", expectedCommand, comm.StartCmd.Command)
|
matched := re.MatchString(cmd)
|
||||||
|
if !matched {
|
||||||
|
t.Fatalf("Got unexpected command: %s", cmd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -547,11 +555,11 @@ func TestProvisioner_createFlattenedEnvVars_windows(t *testing.T) {
|
||||||
{"FOO==bar"}, // User env var with value starting with equals
|
{"FOO==bar"}, // User env var with value starting with equals
|
||||||
}
|
}
|
||||||
expected := []string{
|
expected := []string{
|
||||||
`$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: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: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=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:FOO="=bar"; $env:PACKER_BUILDER_TYPE="iso"; $env:PACKER_BUILD_NAME="vmware"; `,
|
||||||
}
|
}
|
||||||
|
|
||||||
p := new(Provisioner)
|
p := new(Provisioner)
|
||||||
|
@ -571,7 +579,6 @@ func TestProvisioner_createFlattenedEnvVars_windows(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProvision_createCommandText(t *testing.T) {
|
func TestProvision_createCommandText(t *testing.T) {
|
||||||
|
|
||||||
config := testConfig()
|
config := testConfig()
|
||||||
config["remote_path"] = "c:/Windows/Temp/script.ps1"
|
config["remote_path"] = "c:/Windows/Temp/script.ps1"
|
||||||
p := new(Provisioner)
|
p := new(Provisioner)
|
||||||
|
@ -586,22 +593,46 @@ func TestProvision_createCommandText(t *testing.T) {
|
||||||
// Non-elevated
|
// Non-elevated
|
||||||
cmd, _ := p.createCommandText()
|
cmd, _ := p.createCommandText()
|
||||||
|
|
||||||
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 }"`
|
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 cmd != expectedCommand {
|
if !matched {
|
||||||
t.Fatalf("Expected Non-elevated command: %s, got %s", expectedCommand, cmd)
|
t.Fatalf("Got unexpected command: %s", cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Elevated
|
// Elevated
|
||||||
p.config.ElevatedUser = "vagrant"
|
p.config.ElevatedUser = "vagrant"
|
||||||
p.config.ElevatedPassword = "vagrant"
|
p.config.ElevatedPassword = "vagrant"
|
||||||
cmd, _ = p.createCommandText()
|
cmd, _ = p.createCommandText()
|
||||||
matched, _ := regexp.MatchString("powershell -executionpolicy bypass -file \"%TEMP%(.{1})packer-elevated-shell.*", cmd)
|
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)
|
||||||
if !matched {
|
if !matched {
|
||||||
t.Fatalf("Got unexpected elevated command: %s", cmd)
|
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) {
|
func TestProvision_generateElevatedShellRunner(t *testing.T) {
|
||||||
|
|
||||||
// Non-elevated
|
// Non-elevated
|
||||||
|
|
Loading…
Reference in New Issue