Skip to content

Commit 0270735

Browse files
committed
fixing unit tests
1 parent 271775d commit 0270735

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

provisioner/powershell/provisioner.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const wrapPowershellString string = `
4343
try {
4444
$results = . {
4545
{{.Payload}}
46-
}
46+
}
4747
} catch {
4848
$errorMessage = $_.Exception.Message
4949
throw "Script failed with error: $errorMessage"
@@ -287,6 +287,7 @@ func extractScript(p *Provisioner) (string, error) {
287287
payloadBuilder.WriteString(command + "\n")
288288
}
289289
ctxData := p.generatedData
290+
290291
ctxData["Payload"] = payloadBuilder.String()
291292
p.config.ctx.Data = ctxData
292293
log.Printf("Wrapping powershell script block")

provisioner/powershell/provisioner_test.go

+35-14
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func TestProvisionerPrepare_extractScript(t *testing.T) {
2323
config := testConfig()
2424
p := new(Provisioner)
2525
_ = p.Prepare(config)
26+
p.generatedData = generatedData()
2627
file, err := extractScript(p)
2728
defer os.Remove(file)
2829
if err != nil {
@@ -35,13 +36,15 @@ func TestProvisionerPrepare_extractScript(t *testing.T) {
3536

3637
// File contents should contain 2 lines concatenated by newlines: foo\nbar
3738
readFile, err := os.ReadFile(file)
38-
expectedContents := "foo\nbar\n"
39+
expectedContents := "\ntry {\n $results = . {\n\tfoo\nbar\n\n\t}\n} catch {\n $errorMessage = $_.Exception.Message\n\tthrow \"Script failed with error: $errorMessage\"\n\t\n}\n\nWrite-Host $results\nif ($global:LastExitCode -ne 0) {\n\tWrite-Host \"Script failed with exit code: $global:LastExitCode\"\n\tthrow \"Script failed with exit code: $global:LastExitCode\"\n}\nexit 0\n"
40+
normalizedExpectedContent := normalizeWhiteSpace(expectedContents)
3941
if err != nil {
4042
t.Fatalf("Should not be error: %s", err)
4143
}
4244
s := string(readFile[:])
43-
if s != expectedContents {
44-
t.Fatalf("Expected generated inlineScript to equal '%s', got '%s'", expectedContents, s)
45+
normalizedString := normalizeWhiteSpace(s)
46+
if normalizedString != normalizedExpectedContent {
47+
t.Fatalf("Expected generated inlineScript to equal '%s', got '%s'", normalizedExpectedContent, normalizedString)
4548
}
4649
}
4750

@@ -74,11 +77,11 @@ func TestProvisionerPrepare_Defaults(t *testing.T) {
7477
t.Error("expected elevated_password to be empty")
7578
}
7679

77-
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 }"` {
78-
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)
80+
if p.config.ExecuteCommand != `powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. {{.Vars}}; Set-Variable -Name LastExitCode -Value 0 -Scope Global; . {{.Path}}; exit $global:LastExitCode; };"` {
81+
t.Fatalf(`Default command should be 'powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. {{.Vars}}; Set-Variable -Name LastExitCode -Value 0 -Scope Global; . {{.Path}}; exit $global:LastExitCode; };', but got '%s'`, p.config.ExecuteCommand)
7982
}
8083

81-
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 }"` {
84+
if p.config.ElevatedExecuteCommand != `powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. {{.Vars}}; Set-Variable -Name LastExitCode -Value 0 -Scope Global; . {{.Path}}; exit $global:LastExitCode; };"` {
8285
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)
8386
}
8487

@@ -119,8 +122,10 @@ func TestProvisionerPrepare_DebugMode(t *testing.T) {
119122
if err != nil {
120123
t.Fatalf("err: %s", err)
121124
}
125+
// powershell -executionpolicy bypass "& { if (" +
126+
// "Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};Set-PsDebug -Trace 1;. {{.Vars}}; &'{{.Path}}'; exit $LastExitCode }"
122127

123-
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 }"`
128+
command := `powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};Set-PsDebug -Trace 1;. {{.Vars}}; Set-Variable -Name LastExitCode -Value 0 -Scope Global; . {{.Path}}; exit $global:LastExitCode; };"`
124129
if p.config.ExecuteCommand != command {
125130
t.Fatalf(fmt.Sprintf(`Expected command should be '%s' but got '%s'`, command, p.config.ExecuteCommand))
126131
}
@@ -483,7 +488,8 @@ func TestProvisionerProvision_Inline(t *testing.T) {
483488
}
484489

485490
cmd := comm.StartCmd.Command
486-
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 }"`)
491+
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; Set-Variable -Name LastExitCode -Value 0 -Scope Global; \. c:/Windows/Temp/inlineScript.ps1; exit \$global:LastExitCode; };"`)
492+
487493
matched := re.MatchString(cmd)
488494
if !matched {
489495
t.Fatalf("Got unexpected command: %s", cmd)
@@ -503,7 +509,7 @@ func TestProvisionerProvision_Inline(t *testing.T) {
503509
}
504510

505511
cmd = comm.StartCmd.Command
506-
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 }"`)
512+
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; Set-Variable -Name LastExitCode -Value 0 -Scope Global; \. c:/Windows/Temp/inlineScript.ps1; exit \$global:LastExitCode; };"`)
507513
matched = re.MatchString(cmd)
508514
if !matched {
509515
t.Fatalf("Got unexpected command: %s", cmd)
@@ -533,7 +539,7 @@ func TestProvisionerProvision_Scripts(t *testing.T) {
533539
}
534540

535541
cmd := comm.StartCmd.Command
536-
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 }"`)
542+
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; Set-Variable -Name LastExitCode -Value 0 -Scope Global; \. c:/Windows/Temp/script.ps1; exit \$global:LastExitCode; };"`)
537543
matched := re.MatchString(cmd)
538544
if !matched {
539545
t.Fatalf("Got unexpected command: %s", cmd)
@@ -570,7 +576,7 @@ func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) {
570576
}
571577

572578
cmd := comm.StartCmd.Command
573-
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 }"`)
579+
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; Set-Variable -Name LastExitCode -Value 0 -Scope Global; \. c:/Windows/Temp/script.ps1; exit \$global:LastExitCode; };"`)
574580
matched := re.MatchString(cmd)
575581
if !matched {
576582
t.Fatalf("Got unexpected command: %s", cmd)
@@ -595,11 +601,11 @@ func TestProvisionerProvision_SkipClean(t *testing.T) {
595601
}{
596602
{
597603
SkipClean: true,
598-
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 }"`,
604+
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; Set-Variable -Name LastExitCode -Value 0 -Scope Global; \. c:/Windows/Temp/script.ps1; exit \$global:LastExitCode; };"`,
599605
},
600606
{
601607
SkipClean: false,
602-
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 }"`,
608+
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; Set-Variable -Name LastExitCode -Value 0 -Scope Global; \. c:/Windows/Temp/packer-cleanup-[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}\.ps1; exit \$global:LastExitCode; };"`,
603609
},
604610
}
605611

@@ -917,7 +923,7 @@ func TestProvision_createCommandText(t *testing.T) {
917923
p.generatedData = make(map[string]interface{})
918924
cmd, _ := p.createCommandText()
919925

920-
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 }"`)
926+
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; Set-Variable -Name LastExitCode -Value 0 -Scope Global; \. c:/Windows/Temp/script.ps1; exit \$global:LastExitCode; };"`)
921927
matched := re.MatchString(cmd)
922928
if !matched {
923929
t.Fatalf("Got unexpected command: %s", cmd)
@@ -976,3 +982,18 @@ func generatedData() map[string]interface{} {
976982
"PackerHTTPPort": commonsteps.HttpPortNotImplemented,
977983
}
978984
}
985+
986+
func normalizeWhiteSpace(s string) string {
987+
// Replace multiple spaces/tabs with a single space
988+
re := regexp.MustCompile(`[\t ]+`)
989+
s = re.ReplaceAllString(s, " ")
990+
991+
// Trim leading/trailing spaces and newlines
992+
s = strings.TrimSpace(s)
993+
994+
// Normalize line breaks (remove excessive empty lines)
995+
s = strings.ReplaceAll(s, "\r\n", "\n") // Convert Windows line endings to Unix
996+
s = strings.ReplaceAll(s, "\r", "\n") // Convert old Mac line endings to Unix
997+
998+
return s
999+
}

0 commit comments

Comments
 (0)