Skip to content

Commit 702a5fd

Browse files
author
CI (Automated)
committed
Merge remote-tracking branch 'local-from/develop' into HEAD
2 parents 1231e04 + 5e13a37 commit 702a5fd

File tree

12 files changed

+110
-25
lines changed

12 files changed

+110
-25
lines changed

bosh-psmodules/modules/BOSH.CFCell/BOSH.CFCell.Tests.ps1

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,43 @@ Describe "Protect-CFCell" {
5858
}
5959
}
6060

61+
Describe "Install-CFFeatures" {
62+
It "restarts computer on Microsoft server 2016 and later" {
63+
Mock Install-CFFeatures2012 { } -ModuleName BOSH.CFCell
64+
Mock Install-CFFeatures2016 { } -ModuleName BOSH.CFCell
65+
Mock Write-Error { } -ModuleName BOSH.CFCell
66+
Mock Get-WmiObject { New-Object PSObject -Property @{Version = "10.0.1803"} } -ModuleName BOSH.CFCell
67+
68+
{ Install-CFFeatures } | Should -Not -Throw
69+
70+
Assert-MockCalled Install-CFFeatures2016 -Times 1 -Scope It -ModuleName BOSH.CFCell -ParameterFilter { $ForceReboot }
71+
Assert-MockCalled Install-CFFeatures2012 -Times 0 -Scope It -ModuleName BOSH.CFCell
72+
}
73+
}
74+
75+
Describe "Install-CFFeatures2016" {
76+
BeforeEach {
77+
Mock Write-Log { } -ModuleName BOSH.CFCell
78+
Mock Get-WinRMConfig { "Some config" } -ModuleName BOSH.CFCell
79+
Mock WindowsFeatureInstall { } -ModuleName BOSH.CFCell
80+
Mock Remove-WindowsFeature { } -ModuleName BOSH.CFCell
81+
Mock Set-Service { } -ModuleName BOSH.CFCell
82+
Mock Restart-Computer { } -ModuleName BOSH.CFCell
83+
}
84+
85+
It "triggers a machine restart when the -ForceReboot flag is set" {
86+
{ Install-CFFeatures2016 -ForceReboot } | Should -Not -Throw
87+
88+
Assert-MockCalled Restart-Computer -Times 1 -Scope It -ModuleName BOSH.CFCell
89+
}
90+
91+
It "doesn't trigger a machine restart if -ForceReboot flag not set" {
92+
{ Install-CFFeatures2016 } | Should -Not -Throw
93+
94+
Assert-MockCalled Restart-Computer -Times 0 -Scope It -ModuleName BOSH.CFCell
95+
}
96+
}
97+
6198
Describe "Remove-DockerPackage" {
6299
It "Is impossible to test this" {
63100
# Pest has issues mocking functions that use validateSet See: https://github.com/pester/Pester/issues/734

bosh-psmodules/modules/BOSH.CFCell/BOSH.CFCell.psm1

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function Install-CFFeatures {
1111
Install-CFFeatures2012
1212
}
1313
"10.0.*" {
14-
Install-CFFeatures2016
14+
Install-CFFeatures2016 -ForceReboot
1515
}
1616
default {
1717
Write-Error "Unsupported Windows version $($OS.Version)"
@@ -27,7 +27,7 @@ function Install-CFFeatures {
2727
#>
2828
function Install-CFFeatures2012 {
2929
Write-Log "Getting WinRM config"
30-
$winrm_config = & cmd.exe /c 'winrm get winrm/config'
30+
$winrm_config = Get-WinRMConfig
3131
Write-Log "$winrm_config"
3232

3333
Write-Log "Installing CloudFoundry Cell Windows 2012 Features"
@@ -51,8 +51,10 @@ function Install-CFFeatures2012 {
5151
This cmdlet installs the minimum set of features for a CloudFoundry Cell on Windows 2016
5252
#>
5353
function Install-CFFeatures2016 {
54+
param([switch]$ForceReboot)
55+
5456
Write-Log "Getting WinRM config"
55-
$winrm_config = & cmd.exe /c 'winrm get winrm/config'
57+
$winrm_config = Get-WinRMConfig
5658
Write-Log "$winrm_config"
5759

5860
Write-Log "Installing CloudFoundry Cell Windows 2016 Features"
@@ -67,6 +69,10 @@ function Install-CFFeatures2016 {
6769

6870
Write-Log "Setting WinRM startup type to automatic"
6971
Get-Service | Where-Object {$_.Name -eq "WinRM" } | Set-Service -StartupType Automatic
72+
73+
if ($ForceReboot -eq $true) {
74+
Restart-Computer
75+
}
7076
}
7177

7278
function Wait-ForNewIfaces() {
@@ -94,17 +100,17 @@ function Wait-ForNewIfaces() {
94100

95101
function Protect-CFCell {
96102
Write-Log "Getting WinRM config"
97-
$winrm_config = & cmd.exe /c 'winrm get winrm/config'
103+
$winrm_config = Get-WinRMConfig
98104
Write-Log "$winrm_config"
99105
Write-Log "Getting WinRM config"
100-
$winrm_config = & cmd.exe /c 'winrm get winrm/config'
106+
$winrm_config = Get-WinRMConfig
101107
Write-Log "$winrm_config"
102108
disable-service("WinRM")
103109
disable-service("W3Svc")
104110
disable-rdp
105111
set-firewall
106112
Write-Log "Getting WinRM config"
107-
$winrm_config = & cmd.exe /c 'winrm get winrm/config'
113+
$winrm_config = Get-WinRMConfig
108114
Write-Log "$winrm_config"
109115

110116
Write-Log "Disabling NetBIOS over TCP"

bosh-psmodules/modules/BOSH.Utils/BOSH.Utils.Tests.ps1

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
#We remove WinRM as it imports BOSH.Utils
1+
#We remove multiple module that import BOSH.Utils
22
Remove-Module -Name BOSH.WinRM -ErrorAction Ignore
3+
Remove-Module -Name BOSH.CFCell -ErrorAction Ignore
4+
Remove-Module -Name BOSH.AutoLogon -ErrorAction Ignore
35

46
Remove-Module -Name BOSH.Utils -ErrorAction Ignore
57
Import-Module ./BOSH.Utils.psm1
@@ -333,4 +335,32 @@ Describe "Get-OSVersion" {
333335
}
334336
}
335337

338+
Describe "Get-WinRMConfig" {
339+
It "makes a request for winrm config, returns stdout" {
340+
Mock Invoke-Expression {
341+
"Lots of winrm config"
342+
} -ModuleName BOSH.Utils
343+
344+
$output = ""
345+
{ Get-WinRMConfig | Set-Variable -Name "output" -Scope 1 } | Should -Not -Throw
346+
347+
$output | Should -eq "Lots of winrm config"
348+
349+
Assert-MockCalled Invoke-Expression -Times 1 -Scope It `
350+
-ParameterFilter { $Command -and $Command -eq "winrm get winrm/config" } -ModuleName BOSH.Utils
351+
}
352+
353+
It "throws a descriptive failure when winrm config is unavailable" {
354+
Mock Invoke-Expression {
355+
Write-Error "Some error output"
356+
} -ModuleName BOSH.Utils
357+
358+
$output = ""
359+
{ Get-WinRMConfig | Set-Variable -Name "output" -Scope 1 } | `
360+
Should -Throw "Failed to get WinRM config: Some error output"
361+
362+
$output | Should -eq ""
363+
}
364+
}
365+
336366
Remove-Module -Name BOSH.Utils -ErrorAction Ignore

bosh-psmodules/modules/BOSH.Utils/BOSH.Utils.psd1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ FunctionsToExport = @(
2222
'Enable-TLS12',
2323
'Disable-3DES',
2424
'Disable-DCOM',
25-
'Get-OSVersion')
25+
'Get-OSVersion',
26+
'Get-WinRMConfig'
27+
)
2628
CmdletsToExport = @()
2729
VariablesToExport = '*'
2830
AliasesToExport = @()

bosh-psmodules/modules/BOSH.Utils/BOSH.Utils.psm1

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,14 @@ function Get-OSVersion {
260260
Write-Log $_.Exception.Message
261261
throw $_.Exception
262262
}
263-
}
263+
}
264+
265+
function Get-WinRMConfig {
266+
Invoke-Expression "winrm get winrm/config" -OutVariable result -ErrorVariable err 2>&1 | Out-Null
267+
268+
if ($err -ne "") {
269+
throw "Failed to get WinRM config: $err"
270+
}
271+
272+
return $result
273+
}

bosh-psmodules/modules/BOSH.WindowsUpdates/BOSH.WindowsUpdates.psm1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ function Wait-WindowsUpdates {
3737
Enable-Autologon -Password $Password -User $User
3838

3939
Write-Log "Getting WinRM config"
40-
$winrm_config = & cmd.exe /c 'winrm get winrm/config'
40+
$winrm_config = Get-WinRMConfig
4141
Write-Log "$winrm_config"
4242

4343
disable-service("WinRM")
4444

4545
Write-Log "Getting WinRM config"
46-
$winrm_config = & cmd.exe /c 'winrm get winrm/config'
46+
$winrm_config = Get-WinRMConfig
4747
Write-Log "$winrm_config"
4848
}
4949

@@ -110,7 +110,7 @@ function Invoke-RebootOrComplete() {
110110
}
111111

112112
Write-Log "Getting WinRM config"
113-
$winrm_config = & cmd.exe /c 'winrm get winrm/config'
113+
$winrm_config = Get-WinRMConfig
114114
Write-Log "$winrm_config"
115115
}
116116
1 {
@@ -209,7 +209,7 @@ function Install-UpdateBatch() {
209209
Enable-WinRM
210210

211211
Write-Log "Getting WinRM config"
212-
$winrm_config = & cmd.exe /c 'winrm get winrm/config'
212+
$winrm_config = Get-WinRMConfig
213213
Write-Log "$winrm_config"
214214
break
215215
}

lib/packer/config/templates/provision_windows1803.json.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"inline": [
5050
"$ErrorActionPreference = \"Stop\";",
5151
"trap { $host.SetShouldExit(1) }",
52-
"Install-CFFeatures"
52+
"Install-CFFeatures2016"
5353
]
5454
},
5555
{

lib/packer/config/templates/provision_windows2016.json.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"inline": [
5050
"$ErrorActionPreference = \"Stop\";",
5151
"trap { $host.SetShouldExit(1) }",
52-
"Install-CFFeatures"
52+
"Install-CFFeatures2016"
5353
]
5454
},
5555
{

spec/packer/config/aws_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@
203203
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "New-Provisioner"]},
204204
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Remove-DockerPackage"]},
205205
{"type" => "windows-restart", "restart_timeout" => "1h" },
206-
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Install-CFFeatures"]},
206+
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Install-CFFeatures2016"]},
207207
{"type" => "windows-restart", "restart_timeout" => "1h" },
208208
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Add-Account -User Provisioner -Password some-password!"]},
209209
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Register-WindowsUpdatesTask"]},
@@ -286,7 +286,7 @@
286286
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "New-Provisioner"]},
287287
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Remove-DockerPackage"]},
288288
{"type" => "windows-restart", "restart_timeout" => "1h" },
289-
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Install-CFFeatures"]},
289+
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Install-CFFeatures2016"]},
290290
{"type" => "windows-restart", "restart_timeout" => "1h"},
291291
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Add-Account -User Provisioner -Password some-password!"]},
292292
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Register-WindowsUpdatesTask"]},

spec/packer/config/azure_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@
196196
{"type"=>"powershell", "inline"=>["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "New-Provisioner"]},
197197
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Remove-DockerPackage"]},
198198
{"type" => "windows-restart", "restart_timeout" => "1h"},
199-
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Install-CFFeatures"]},
199+
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Install-CFFeatures2016"]},
200200
{"type" => "windows-restart", "restart_timeout" => "1h"},
201201
{"type"=>"powershell", "inline"=>["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Add-Account -User Provisioner -Password some-password!"]},
202202
{"type"=>"powershell", "inline"=>["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Register-WindowsUpdatesTask"]},
@@ -284,7 +284,7 @@
284284
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "New-Provisioner"]},
285285
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Remove-DockerPackage"]},
286286
{"type" => "windows-restart", "restart_timeout" => "1h" },
287-
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Install-CFFeatures"]},
287+
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Install-CFFeatures2016"]},
288288
{"type" => "windows-restart", "restart_timeout" => "1h" },
289289
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Add-Account -User Provisioner -Password some-password!"]},
290290
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Register-WindowsUpdatesTask"]},

0 commit comments

Comments
 (0)