Skip to content

Commit fb4d4c3

Browse files
committed
* Update Unit tests to use the standard folder structure and test templates.
* Added .gitignore to prevent commit of DSCResource.Tests.
1 parent 74f51bb commit fb4d4c3

4 files changed

Lines changed: 252 additions & 219 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DSCResource.Tests

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ xComputer resource has following properties:
4848
## Versions
4949

5050
### Unreleased
51+
* Update Unit tests to use the standard folder structure and test templates.
52+
* Added .gitignore to prevent commit of DSCResource.Tests.
5153

5254
### 1.4.0.0
5355
* Added validation to the Name parameter
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
$Global:DSCModuleName = 'xComputerManagement'
2+
$Global:DSCResourceName = 'MSFT_xComputer'
3+
4+
#region HEADER
5+
[String] $moduleRoot = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $Script:MyInvocation.MyCommand.Path))
6+
if ( (-not (Test-Path -Path (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
7+
(-not (Test-Path -Path (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
8+
{
9+
& git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $moduleRoot -ChildPath '\DSCResource.Tests\'))
10+
}
11+
else
12+
{
13+
& git @('-C',(Join-Path -Path $moduleRoot -ChildPath '\DSCResource.Tests\'),'pull')
14+
}
15+
Import-Module (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force
16+
$TestEnvironment = Initialize-TestEnvironment `
17+
-DSCModuleName $Global:DSCModuleName `
18+
-DSCResourceName $Global:DSCResourceName `
19+
-TestType Unit
20+
#endregion
21+
22+
# Begin Testing
23+
try
24+
{
25+
#region Pester Tests
26+
27+
InModuleScope $Global:DSCResourceName {
28+
29+
Describe $Global:DSCResourceName {
30+
31+
$SecPassword = ConvertTo-SecureString -String 'password' -AsPlainText -Force
32+
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'USER',$SecPassword
33+
$NotComputerName = if($env:COMPUTERNAME -ne 'othername'){'othername'}else{'name'}
34+
35+
Context "$($Global:DSCResourceName)\Test-TargetResource" {
36+
Mock Get-WMIObject {[PSCustomObject]@{DomainName = 'ContosoLtd'}} -ParameterFilter {$Class -eq 'Win32_NTDomain'}
37+
It 'Throws if both DomainName and WorkGroupName are specified' {
38+
{Test-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com' -WorkGroupName 'workgroup'} | Should Throw
39+
}
40+
It 'Throws if Domain is specified without Credentials' {
41+
{Test-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com'} | Should Throw
42+
}
43+
It 'Should return True if Domain name is same as specified' {
44+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
45+
Mock GetComputerDomain {'contoso.com'}
46+
Test-TargetResource -Name $Env:ComputerName -DomainName 'Contoso.com' -Credential $Credential | Should Be $true
47+
}
48+
It 'Should return True if Workgroup name is same as specified' {
49+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}}
50+
Mock GetComputerDomain {''}
51+
Test-TargetResource -Name $Env:ComputerName -WorkGroupName 'workgroup' | Should Be $true
52+
}
53+
It 'Should return True if ComputerName and Domain name is same as specified' {
54+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
55+
Mock GetComputerDomain {'contoso.com'}
56+
Test-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com' -Credential $Credential | Should Be $true
57+
}
58+
It 'Should return True if ComputerName and Workgroup is same as specified' {
59+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}}
60+
Mock GetComputerDomain {''}
61+
Test-TargetResource -Name $Env:ComputerName -WorkGroupName 'workgroup' | Should Be $true
62+
}
63+
It 'Should return True if ComputerName is same and no Domain or Workgroup specified' {
64+
Mock Get-WmiObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}}
65+
Mock GetComputerDomain {''}
66+
Test-TargetResource -Name $Env:ComputerName | Should Be $true
67+
Mock Get-WmiObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
68+
Mock GetComputerDomain {'contoso.com'}
69+
Test-TargetResource -Name $Env:ComputerName | Should Be $true
70+
}
71+
It 'Should return False if ComputerName is not same and no Domain or Workgroup specified' {
72+
Mock Get-WmiObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}}
73+
Mock GetComputerDomain {''}
74+
Test-TargetResource -Name $NotComputerName | Should Be $false
75+
Mock Get-WmiObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
76+
Mock GetComputerDomain {'contoso.com'}
77+
Test-TargetResource -Name $NotComputerName | Should Be $false
78+
}
79+
It 'Should return False if Domain name is not same as specified' {
80+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
81+
Mock GetComputerDomain {'contoso.com'}
82+
Test-TargetResource -Name $Env:ComputerName -DomainName 'adventure-works.com' -Credential $Credential | Should Be $false
83+
}
84+
It 'Should return False if Workgroup name is not same as specified' {
85+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}}
86+
Mock GetComputerDomain {''}
87+
Test-TargetResource -Name $Env:ComputerName -WorkGroupName 'NOTworkgroup' | Should Be $false
88+
}
89+
It 'Should return False if ComputerName is not same as specified' {
90+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}}
91+
Mock GetComputerDomain {''}
92+
Test-TargetResource -Name $NotComputerName -WorkGroupName 'workgroup' | Should Be $false
93+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
94+
Mock GetComputerDomain {'contoso.com'}
95+
Test-TargetResource -Name $NotComputerName -DomainName 'contoso.com' -Credential $Credential | Should Be $false
96+
}
97+
It 'Should return False if Computer is in Workgroup and Domain is specified' {
98+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$false}}
99+
Mock GetComputerDomain {''}
100+
Test-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com' -Credential $Credential | Should Be $false
101+
}
102+
It 'Should return False if ComputerName is in Domain and Workgroup is specified' {
103+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
104+
Mock GetComputerDomain {'contoso.com'}
105+
Test-TargetResource -Name $Env:ComputerName -WorkGroupName 'Contoso' -Credential $Credential -UnjoinCredential $Credential | Should Be $false
106+
}
107+
It 'Throws if name is to long' {
108+
{Test-TargetResource -Name "ThisNameIsTooLong"} | Should Throw
109+
}
110+
It 'Throws if name contains illigal characters' {
111+
{Test-TargetResource -Name "ThisIsBad<>"} | Should Throw
112+
}
113+
114+
}
115+
Context "$($Global:DSCResourceName)\Get-TargetResource" {
116+
It 'should not throw' {
117+
{Get-TargetResource -Name $env:COMPUTERNAME} | Should Not Throw
118+
}
119+
It 'Should return a hashtable containing Name, DomainName, JoinOU, CurrentOU, Credential, UnjoinCredential and WorkGroupName' {
120+
$Result = Get-TargetResource -Name $env:COMPUTERNAME
121+
$Result.GetType().Fullname | Should Be 'System.Collections.Hashtable'
122+
$Result.Keys | Should Be @('Name', 'DomainName', 'JoinOU', 'CurrentOU', 'Credential', 'UnjoinCredential', 'WorkGroupName')
123+
}
124+
It 'Throws if name is to long' {
125+
{Get-TargetResource -Name "ThisNameIsTooLong"} | Should Throw
126+
}
127+
It 'Throws if name contains illigal characters' {
128+
{Get-TargetResource -Name "ThisIsBad<>"} | Should Throw
129+
}
130+
}
131+
Context "$($Global:DSCResourceName)\Set-TargetResource" {
132+
Mock Rename-Computer {}
133+
Mock Add-Computer {}
134+
It 'Throws if both DomainName and WorkGroupName are specified' {
135+
{Set-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com' -WorkGroupName 'workgroup'} | Should Throw
136+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
137+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It
138+
}
139+
It 'Throws if Domain is specified without Credentials' {
140+
{Set-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com'} | Should Throw
141+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
142+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It
143+
}
144+
It 'Changes ComputerName and changes Domain to new Domain' {
145+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
146+
Mock GetComputerDomain {'contoso.com'}
147+
Set-TargetResource -Name $NotComputerName -DomainName 'adventure-works.com' -Credential $Credential -UnjoinCredential $Credential | Should BeNullOrEmpty
148+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
149+
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName}
150+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
151+
}
152+
It 'Changes ComputerName and changes Domain to new Domain with specified OU' {
153+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
154+
Mock GetComputerDomain {'contoso.com'}
155+
Set-TargetResource -Name $NotComputerName -DomainName 'adventure-works.com' -JoinOU 'OU=Computers,DC=contoso,DC=com' -Credential $Credential -UnjoinCredential $Credential | Should BeNullOrEmpty
156+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
157+
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName}
158+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
159+
}
160+
It 'Changes ComputerName and changes Domain to Workgroup' {
161+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
162+
Mock GetComputerDomain {'contoso.com'}
163+
Set-TargetResource -Name $NotComputerName -WorkGroupName 'contoso' -Credential $Credential | Should BeNullOrEmpty
164+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
165+
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$WorkGroupName -and $NewName -and $Credential}
166+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$DomainName -or $UnjoinCredential}
167+
}
168+
It 'Changes ComputerName and changes Workgroup to Domain' {
169+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso';Workgroup='Contoso';PartOfDomain=$false}}
170+
Mock GetComputerDomain {''}
171+
Set-TargetResource -Name $NotComputerName -DomainName 'Contoso.com' -Credential $Credential | Should BeNullOrEmpty
172+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
173+
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName}
174+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
175+
}
176+
It 'Changes ComputerName and changes Workgroup to Domain with specified OU' {
177+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso';Workgroup='Contoso';PartOfDomain=$false}}
178+
Mock GetComputerDomain {''}
179+
Set-TargetResource -Name $NotComputerName -DomainName 'Contoso.com' -JoinOU 'OU=Computers,DC=contoso,DC=com' -Credential $Credential | Should BeNullOrEmpty
180+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
181+
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName}
182+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
183+
}
184+
It 'Changes ComputerName and changes Workgroup to new Workgroup' {
185+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso';Workgroup='Contoso';PartOfDomain=$false}}
186+
Mock GetComputerDomain {''}
187+
Set-TargetResource -Name $NotComputerName -WorkGroupName 'adventure-works' | Should BeNullOrEmpty
188+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
189+
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$WorkGroupName -and $NewName}
190+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$DomainName}
191+
}
192+
It 'Changes only the Domain to new Domain' {
193+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
194+
Mock GetComputerDomain {'contoso.com'}
195+
Set-TargetResource -Name $Env:ComputerName -DomainName 'adventure-works.com' -Credential $Credential -UnjoinCredential $Credential | Should BeNullOrEmpty
196+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
197+
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName}
198+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$NewName}
199+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
200+
}
201+
It 'Changes only the Domain to new Domain with specified OU' {
202+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
203+
Mock GetComputerDomain {'contoso.com'}
204+
Set-TargetResource -Name $Env:ComputerName -DomainName 'adventure-works.com' -JoinOU 'OU=Computers,DC=contoso,DC=com' -Credential $Credential -UnjoinCredential $Credential | Should BeNullOrEmpty
205+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
206+
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName}
207+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$NewName}
208+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName}
209+
}
210+
It 'Changes only Domain to Workgroup' {
211+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
212+
Mock GetComputerDomain {''}
213+
Set-TargetResource -Name $Env:ComputerName -WorkGroupName 'Contoso' -UnjoinCredential $Credential | Should BeNullOrEmpty
214+
Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It
215+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$NewName}
216+
Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$WorkGroupName}
217+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$DomainName}
218+
}
219+
It 'Changes only ComputerName in Domain' {
220+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}}
221+
Mock GetComputerDomain {'contoso.com'}
222+
Set-TargetResource -Name $NotComputerName -Credential $Credential | Should BeNullOrEmpty
223+
Assert-MockCalled -CommandName Rename-Computer -Exactly 1 -Scope It
224+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It
225+
}
226+
It 'Changes only ComputerName in Workgroup' {
227+
Mock GetComputerDomain {''}
228+
Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso';Workgroup='Contoso';PartOfDomain=$false}}
229+
Set-TargetResource -Name $NotComputerName | Should BeNullOrEmpty
230+
Assert-MockCalled -CommandName Rename-Computer -Exactly 1 -Scope It
231+
Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It
232+
}
233+
It 'Throws if name is to long' {
234+
{Set-TargetResource -Name "ThisNameIsTooLong"} | Should Throw
235+
}
236+
It 'Throws if name contains illigal characters' {
237+
{Set-TargetResource -Name "ThisIsBad<>"} | Should Throw
238+
}
239+
}
240+
}
241+
} #end InModuleScope $DSCResourceName
242+
#endregion
243+
}
244+
finally
245+
{
246+
#region FOOTER
247+
Restore-TestEnvironment -TestEnvironment $TestEnvironment
248+
#endregion
249+
}

0 commit comments

Comments
 (0)