Skip to content

Commit b7ecddf

Browse files
committed
Powershell: pester tests have top-level Describe
- add top-level mock for Write-Log
1 parent 85fb505 commit b7ecddf

File tree

7 files changed

+1022
-935
lines changed

7 files changed

+1022
-935
lines changed

modules/BOSH.Account/BOSH.Account.Tests.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ BeforeAll {
66
Import-Module ../BOSH.Utils/BOSH.Utils.psm1
77
}
88

9-
Describe "Account" {
9+
Describe "BOSH.Account" {
10+
BeforeEach {
11+
Mock -ModuleName BOSH.Account Write-Log { }
12+
}
13+
1014
Context "when username is not provided" {
1115
It "throws" {
1216
{ Add-Account } | Should -Throw "Provide a user name"

modules/BOSH.Agent/BOSH.Agent.Tests.ps1

Lines changed: 184 additions & 176 deletions
Large diffs are not rendered by default.

modules/BOSH.CFCell/BOSH.CFCell.Tests.ps1

Lines changed: 98 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -6,120 +6,128 @@ BeforeAll {
66
Import-Module ../BOSH.Utils/BOSH.Utils.psm1
77
}
88

9-
Describe "Protect-CFCell" {
9+
Describe "BOSH.CFCell" {
1010
BeforeEach {
1111
Mock -ModuleName BOSH.CFCell Write-Log { }
12+
}
1213

13-
$oldWinRMStatus = (Get-Service winrm).Status
14-
$oldWinRMStartMode = ( Get-Service winrm ).StartType
14+
Describe "Protect-CFCell" {
15+
BeforeEach {
16+
Mock -ModuleName BOSH.CFCell Write-Log { }
1517

16-
{ Set-Service -Name "winrm" -StartupType "Manual" } | Should -Not -Throw
18+
$oldWinRMStatus = (Get-Service winrm).Status
19+
$oldWinRMStartMode = ( Get-Service winrm).StartType
1720

18-
Start-Service winrm
19-
}
21+
{ Set-Service -Name "winrm" -StartupType "Manual" } | Should -Not -Throw
2022

21-
AfterEach {
22-
if ($oldWinRMStatus -eq "Stopped") {
23-
{ Stop-Service winrm } | Should -Not -Throw
24-
} else {
25-
{ Set-Service -Name "winrm" -Status $oldWinRMStatus } | Should -Not -Throw
23+
Start-Service winrm
2624
}
27-
{ Set-Service -Name "winrm" -StartupType $oldWinRMStartMode } | Should -Not -Throw
28-
}
2925

30-
It "disables the RDP service and firewall rule" {
31-
Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Control\Terminal Server" -Name "fDenyTSConnections" -Value 0
32-
Get-NetFirewallRule -DisplayName "Remote Desktop*" | Set-NetFirewallRule -enabled true
33-
Get-Service "Termservice" | Set-Service -StartupType "Automatic"
34-
netstat /p tcp /a | findstr ":3389 " | Should -Not -BeNullOrEmpty
26+
AfterEach {
27+
if ($oldWinRMStatus -eq "Stopped")
28+
{
29+
{ Stop-Service winrm } | Should -Not -Throw
30+
}
31+
else
32+
{
33+
{ Set-Service -Name "winrm" -Status $oldWinRMStatus } | Should -Not -Throw
34+
}
35+
{ Set-Service -Name "winrm" -StartupType $oldWinRMStartMode } | Should -Not -Throw
36+
}
3537

36-
Protect-CFCell -IaaS "ignored"
38+
It "disables the RDP service and firewall rule" {
39+
Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Control\Terminal Server" -Name "fDenyTSConnections" -Value 0
40+
Get-NetFirewallRule -DisplayName "Remote Desktop*" | Set-NetFirewallRule -enabled true
41+
Get-Service "Termservice" | Set-Service -StartupType "Automatic"
42+
netstat /p tcp /a | findstr ":3389 " | Should -Not -BeNullOrEmpty
3743

38-
Get-ItemProperty -Path "HKLM:\System\CurrentControlSet\Control\Terminal Server" | select -exp fDenyTSConnections | Should -Be 1
39-
netstat /p tcp /a | findstr ":3389 " | Should -BeNullOrEmpty
40-
Get-NetFirewallRule -DisplayName "Remote Desktop*" | ForEach { $_.enabled | Should -Be "False" }
41-
Get-Service "Termservice" | Select -exp starttype | Should -Be "Disabled"
42-
}
44+
Protect-CFCell -IaaS "ignored"
4345

44-
It "disables the services" {
45-
Get-Service | Where-Object {$_.Name -eq "WinRM" } | Set-Service -StartupType Automatic
46-
Get-Service | Where-Object {$_.Name -eq "W3Svc" } | Set-Service -StartupType Automatic
47-
Protect-CFCell -IaaS "ignored"
48-
(Get-Service | Where-Object {$_.Name -eq "WinRM" } ).StartType| Should -Be "Disabled"
49-
$w3svcStartType = (Get-Service | Where-Object {$_.Name -eq "W3Svc" } ).StartType
50-
"Disabled", $null -contains $w3svcStartType | Should -Be $true
51-
}
46+
Get-ItemProperty -Path "HKLM:\System\CurrentControlSet\Control\Terminal Server" | select -exp fDenyTSConnections | Should -Be 1
47+
netstat /p tcp /a | findstr ":3389 " | Should -BeNullOrEmpty
48+
Get-NetFirewallRule -DisplayName "Remote Desktop*" | ForEach { $_.enabled | Should -Be "False" }
49+
Get-Service "Termservice" | Select -exp starttype | Should -Be "Disabled"
50+
}
5251

53-
It "sets firewall rules" {
54-
Set-NetFirewallProfile -all -DefaultInboundAction Allow -DefaultOutboundAction Allow -AllowUnicastResponseToMulticast False -Enabled True
55-
get-firewall "public" | Should -Be "public,Allow,Allow"
56-
get-firewall "private" | Should -Be "private,Allow,Allow"
57-
get-firewall "domain" | Should -Be "domain,Allow,Allow"
58-
Protect-CFCell -IaaS "ignored"
59-
get-firewall "public" | Should -Be "public,Block,Allow"
60-
get-firewall "private" | Should -Be "private,Block,Allow"
61-
get-firewall "domain" | Should -Be "domain,Block,Allow"
62-
}
63-
}
52+
It "disables the services" {
53+
Get-Service | Where-Object { $_.Name -eq "WinRM" } | Set-Service -StartupType Automatic
54+
Get-Service | Where-Object { $_.Name -eq "W3Svc" } | Set-Service -StartupType Automatic
55+
Protect-CFCell -IaaS "ignored"
56+
(Get-Service | Where-Object { $_.Name -eq "WinRM" }).StartType| Should -Be "Disabled"
57+
$w3svcStartType = (Get-Service | Where-Object { $_.Name -eq "W3Svc" }).StartType
58+
"Disabled", $null -contains $w3svcStartType | Should -Be $true
59+
}
6460

65-
Describe "Install-CFFeatures" {
66-
BeforeEach {
67-
Mock -ModuleName BOSH.CFCell Write-Log { }
68-
Mock -ModuleName BOSH.CFCell Get-WinRMConfig { "Some config" }
69-
Mock -ModuleName BOSH.CFCell WindowsFeatureInstall { }
70-
Mock -ModuleName BOSH.CFCell Uninstall-WindowsFeature { }
71-
Mock -ModuleName BOSH.CFCell Set-Service { }
72-
Mock -ModuleName BOSH.CFCell Restart-Computer { }
61+
It "sets firewall rules" {
62+
Set-NetFirewallProfile -all -DefaultInboundAction Allow -DefaultOutboundAction Allow -AllowUnicastResponseToMulticast False -Enabled True
63+
get-firewall "public" | Should -Be "public,Allow,Allow"
64+
get-firewall "private" | Should -Be "private,Allow,Allow"
65+
get-firewall "domain" | Should -Be "domain,Allow,Allow"
66+
Protect-CFCell -IaaS "ignored"
67+
get-firewall "public" | Should -Be "public,Block,Allow"
68+
get-firewall "private" | Should -Be "private,Block,Allow"
69+
get-firewall "domain" | Should -Be "domain,Block,Allow"
70+
}
7371
}
7472

75-
It "triggers a machine restart when the -ForceReboot flag is set" {
76-
{ Install-CFFeatures -IaaS "ignored" -ForceReboot } | Should -Not -Throw
73+
Describe "Install-CFFeatures" {
74+
BeforeEach {
75+
Mock -ModuleName BOSH.CFCell Write-Log { }
76+
Mock -ModuleName BOSH.CFCell Get-WinRMConfig { "Some config" }
77+
Mock -ModuleName BOSH.CFCell WindowsFeatureInstall { }
78+
Mock -ModuleName BOSH.CFCell Uninstall-WindowsFeature { }
79+
Mock -ModuleName BOSH.CFCell Set-Service { }
80+
Mock -ModuleName BOSH.CFCell Restart-Computer { }
81+
}
82+
83+
It "triggers a machine restart when the -ForceReboot flag is set" {
84+
{ Install-CFFeatures -IaaS "ignored" -ForceReboot } | Should -Not -Throw
7785

78-
Assert-MockCalled Restart-Computer -Times 1 -Scope It -ModuleName BOSH.CFCell
79-
}
86+
Assert-MockCalled Restart-Computer -Times 1 -Scope It -ModuleName BOSH.CFCell
87+
}
8088

81-
It "doesn't trigger a machine restart if -ForceReboot flag not set" {
82-
{ Install-CFFeatures -IaaS "ignored" } | Should -Not -Throw
89+
It "doesn't trigger a machine restart if -ForceReboot flag not set" {
90+
{ Install-CFFeatures -IaaS "ignored" } | Should -Not -Throw
8391

84-
Assert-MockCalled Restart-Computer -Times 0 -Scope It -ModuleName BOSH.CFCell
85-
}
92+
Assert-MockCalled Restart-Computer -Times 0 -Scope It -ModuleName BOSH.CFCell
93+
}
8694

87-
It "logs Installing CloudFoundry Cell Windows Features" {
88-
{ Install-CFFeatures -IaaS "ignored" } | Should -Not -Throw
95+
It "logs Installing CloudFoundry Cell Windows Features" {
96+
{ Install-CFFeatures -IaaS "ignored" } | Should -Not -Throw
8997

90-
Assert-MockCalled Write-Log -Times 1 -Scope It -ModuleName BOSH.CFCell -ParameterFilter { $Message -eq "Installing CloudFoundry Cell Windows Features" }
91-
}
98+
Assert-MockCalled Write-Log -Times 1 -Scope It -ModuleName BOSH.CFCell -ParameterFilter { $Message -eq "Installing CloudFoundry Cell Windows Features" }
99+
}
92100

93-
It "logs Installed CloudFoundry Cell Windows Features after installation" {
94-
{ Install-CFFeatures -IaaS "ignored" } | Should -Not -Throw
101+
It "logs Installed CloudFoundry Cell Windows Features after installation" {
102+
{ Install-CFFeatures -IaaS "ignored" } | Should -Not -Throw
95103

96-
Assert-MockCalled Write-Log -Times 1 -Scope It -ModuleName BOSH.CFCell -ParameterFilter { $Message -eq "Installed CloudFoundry Cell Windows Features" }
97-
}
104+
Assert-MockCalled Write-Log -Times 1 -Scope It -ModuleName BOSH.CFCell -ParameterFilter { $Message -eq "Installed CloudFoundry Cell Windows Features" }
105+
}
98106

99-
It "calls Uninstall-WindowsFeature (for '*Defender')" {
100-
{ Install-CFFeatures -IaaS "ignored" } | Should -Not -Throw
107+
It "calls Uninstall-WindowsFeature (for '*Defender')" {
108+
{ Install-CFFeatures -IaaS "ignored" } | Should -Not -Throw
101109

102-
Should -Invoke -ModuleName BOSH.CFCell -CommandName Uninstall-WindowsFeature
110+
Should -Invoke -ModuleName BOSH.CFCell -CommandName Uninstall-WindowsFeature
111+
}
103112
}
104-
}
105113

106-
Describe "Remove-DockerPackage" {
107-
It "Is impossible to test this" {
108-
# Pest has issues mocking functions that use validateSet See: https://github.com/pester/Pester/issues/734
109-
# Mock -ModuleName BOSH.CFCell Uninstall-Package { }
110-
# Mock -ModuleName BOSH.CFCell Write-Log { }
111-
# Mock -ModuleName BOSH.CFCell Uninstall-Module { } -ParameterFilter { $Name -eq "DockerMsftProvider" -and $ErrorAction -eq "Ignore" }
112-
# Mock -ModuleName BOSH.CFCell Get-HNSNetwork { "test-network" }
113-
# Mock -ModuleName BOSH.CFCell Remove-HNSNetwork { }
114-
# Mock -ModuleName BOSH.CFCell remove-DockerProgramData { }
115-
#
116-
# { Remove-DockerPackage } | Should -Not -Throw
117-
#
118-
# Assert-MockCalled Uninstall-Package -Times 1
119-
# Assert-MockCalled Uninstall-Module -Times 1 -Scope It -ParameterFilter { $Name -eq "DockerMsftProvider" -and $ErrorAction -eq "Ignore" } -ModuleName BOSH.CFCell
120-
# Assert-MockCalled Get-HNSNetwork -Times 1 -Scope It -ModuleName BOSH.CFCell
121-
# Assert-MockCalled Remove-HNSNetwork -Times 1 -Scope It -ParameterFilter { $ArgumentList -eq "test-network" } -ModuleName BOSH.CFCell
122-
# Assert-MockCalled remove-DockerProgramData-Times 1 -Scope It -ModuleName BOSH.CFCell
114+
Describe "Remove-DockerPackage" {
115+
It "Is impossible to test this" {
116+
# Pest has issues mocking functions that use validateSet See: https://github.com/pester/Pester/issues/734
117+
# Mock -ModuleName BOSH.CFCell Uninstall-Package { }
118+
# Mock -ModuleName BOSH.CFCell Write-Log { }
119+
# Mock -ModuleName BOSH.CFCell Uninstall-Module { } -ParameterFilter { $Name -eq "DockerMsftProvider" -and $ErrorAction -eq "Ignore" }
120+
# Mock -ModuleName BOSH.CFCell Get-HNSNetwork { "test-network" }
121+
# Mock -ModuleName BOSH.CFCell Remove-HNSNetwork { }
122+
# Mock -ModuleName BOSH.CFCell remove-DockerProgramData { }
123+
#
124+
# { Remove-DockerPackage } | Should -Not -Throw
125+
#
126+
# Assert-MockCalled Uninstall-Package -Times 1
127+
# Assert-MockCalled Uninstall-Module -Times 1 -Scope It -ParameterFilter { $Name -eq "DockerMsftProvider" -and $ErrorAction -eq "Ignore" } -ModuleName BOSH.CFCell
128+
# Assert-MockCalled Get-HNSNetwork -Times 1 -Scope It -ModuleName BOSH.CFCell
129+
# Assert-MockCalled Remove-HNSNetwork -Times 1 -Scope It -ParameterFilter { $ArgumentList -eq "test-network" } -ModuleName BOSH.CFCell
130+
# Assert-MockCalled remove-DockerProgramData-Times 1 -Scope It -ModuleName BOSH.CFCell
131+
}
123132
}
124-
}
125-
133+
}

modules/BOSH.Registry/BOSH.Registry.Tests.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ BeforeAll {
44

55
Describe "BOSH.Registry" {
66
BeforeEach {
7+
Mock -ModuleName BOSH.Registry Write-Log { }
8+
79
$newItemReturn = [pscustomobject]@{"NewPath" = "HKCU:/Path/created";}
810
Mock -ModuleName BOSH.Registry New-Item { $newItemReturn }
911
# reset for our -parameterfilter mock

0 commit comments

Comments
 (0)