-
Notifications
You must be signed in to change notification settings - Fork 80
Added tests for MSFT_xComputer resource #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| $Module = "$PSScriptRoot\..\DSCResources\MSFT_xComputer\MSFT_xComputer.psm1" | ||
| Remove-Module -Name MSFT_xComputer -Force -ErrorAction SilentlyContinue | ||
| Import-Module -Name $Module -Force -ErrorAction Stop | ||
|
|
||
| InModuleScope MSFT_xComputer { | ||
|
|
||
| Describe 'MSFT_xComputer' { | ||
|
|
||
| $SecPassword = ConvertTo-SecureString -String 'password' -AsPlainText -Force | ||
| $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'USER',$SecPassword | ||
| $NotComputerName = if($env:COMPUTERNAME -ne 'othername'){'othername'}else{'name'} | ||
|
|
||
| Context Test-TargetResource { | ||
| Mock Get-WMIObject {[PSCustomObject]@{DomainName = 'ContosoLtd'}} -ParameterFilter {$Class -eq 'Win32_NTDomain'} | ||
| It 'Throws if both DomainName and WorkGroupName are specified' { | ||
| {Test-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com' -WorkGroupName 'workgroup'} | Should Throw | ||
| } | ||
| It 'Throws if Domain is specified without Credentials' { | ||
| {Test-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com'} | Should Throw | ||
| } | ||
| It 'Should return True if Domain name is same as specified' { | ||
| Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} | ||
| Mock GetComputerDomain {'contoso.com'} | ||
| Test-TargetResource -Name $Env:ComputerName -DomainName 'Contoso.com' -Credential $Credential | Should Be $true | ||
| } | ||
| It 'Should return True if Workgroup name is same as specified' { | ||
| Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}} | ||
| Mock GetComputerDomain {''} | ||
| Test-TargetResource -Name $Env:ComputerName -WorkGroupName 'workgroup' | Should Be $true | ||
| } | ||
| It 'Should return True if ComputerName and Domain name is same as specified' { | ||
| Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} | ||
| Mock GetComputerDomain {'contoso.com'} | ||
| Test-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com' -Credential $Credential | Should Be $true | ||
| } | ||
| It 'Should return True if ComputerName and Workgroup is same as specified' { | ||
| Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}} | ||
| Mock GetComputerDomain {''} | ||
| Test-TargetResource -Name $Env:ComputerName -WorkGroupName 'workgroup' | Should Be $true | ||
| } | ||
| It 'Should return False if Domain name is not same as specified' { | ||
| Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} | ||
| Mock GetComputerDomain {'contoso.com'} | ||
| Test-TargetResource -Name $Env:ComputerName -DomainName 'adventure-works.com' -Credential $Credential | Should Be $false | ||
| } | ||
| It 'Should return False if Workgroup name is not same as specified' { | ||
| Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}} | ||
| Mock GetComputerDomain {''} | ||
| Test-TargetResource -Name $Env:ComputerName -WorkGroupName 'NOTworkgroup' | Should Be $false | ||
| } | ||
| It 'Should return False if ComputerName is not same as specified' { | ||
| Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}} | ||
| Mock GetComputerDomain {''} | ||
| Test-TargetResource -Name $NotComputerName -WorkGroupName 'workgroup' | Should Be $false | ||
| Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} | ||
| Mock GetComputerDomain {'contoso.com'} | ||
| Test-TargetResource -Name $NotComputerName -DomainName 'contoso.com' -Credential $Credential | Should Be $false | ||
| } | ||
| It 'Should return False if Computer is in Workgroup and Domain is specified' { | ||
| Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$false}} | ||
| Mock GetComputerDomain {''} | ||
| Test-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com' -Credential $Credential | Should Be $false | ||
| } | ||
| It 'Should return False if ComputerName is in Domain and Workgroup is specified' { | ||
| Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} | ||
| Mock GetComputerDomain {'contoso.com'} | ||
| Test-TargetResource -Name $Env:ComputerName -WorkGroupName 'Contoso' -Credential $Credential -UnjoinCredential $Credential | Should Be $false | ||
| } | ||
| } | ||
| Context Get-TargetResource { | ||
| It 'should not throw' { | ||
| {Get-TargetResource -Name $env:COMPUTERNAME} | Should Not Throw | ||
| } | ||
| It 'Should return a hashtable containing Name,DomainName, Credential, UnjoinCredential and WorkGroupName' { | ||
| $Result = Get-TargetResource -Name $env:COMPUTERNAME | ||
| $Result.GetType().Fullname | Should Be 'System.Collections.Hashtable' | ||
| $Result.Keys | Should Be @('Name','DomainName','Credential','UnjoinCredential','WorkGroupName') | ||
| } | ||
| } | ||
| Context Set-TargetResource { | ||
| Mock Rename-Computer {} | ||
| Mock Add-Computer {} | ||
| It 'Throws if both DomainName and WorkGroupName are specified' { | ||
| {Set-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com' -WorkGroupName 'workgroup'} | Should Throw | ||
| Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It | ||
| Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It | ||
| } | ||
| It 'Throws if Domain is specified without Credentials' { | ||
| {Set-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com'} | Should Throw | ||
| Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It | ||
| Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It | ||
| } | ||
| It 'Changes ComputerName and changes Domain to new Domain' { | ||
| Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} | ||
| Mock GetComputerDomain {'contoso.com'} | ||
| Set-TargetResource -Name $NotComputerName -DomainName 'adventure-works.com' -Credential $Credential -UnjoinCredential $Credential | Should BeNullOrEmpty | ||
| Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It | ||
| Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName} | ||
| Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName} | ||
| } | ||
| It 'Changes ComputerName and changes Domain to Workgroup' { | ||
| Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} | ||
| Mock GetComputerDomain {'contoso.com'} | ||
| Set-TargetResource -Name $NotComputerName -WorkGroupName 'contoso' -Credential $Credential | Should BeNullOrEmpty | ||
| Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It | ||
| Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$WorkGroupName -and $NewName -and $Credential} | ||
| Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$DomainName -or $UnjoinCredential} | ||
| } | ||
| It 'Changes ComputerName and changes Workgroup to Domain' { | ||
| Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso';Workgroup='Contoso';PartOfDomain=$false}} | ||
| Mock GetComputerDomain {''} | ||
| Set-TargetResource -Name $NotComputerName -DomainName 'Contoso.com' -Credential $Credential | Should BeNullOrEmpty | ||
| Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It | ||
| Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName} | ||
| Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName} | ||
| } | ||
| It 'Changes ComputerName and changes Workgroup to new Workgroup' { | ||
| Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso';Workgroup='Contoso';PartOfDomain=$false}} | ||
| Mock GetComputerDomain {''} | ||
| Set-TargetResource -Name $NotComputerName -WorkGroupName 'adventure-works' | Should BeNullOrEmpty | ||
| Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It | ||
| Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$WorkGroupName -and $NewName} | ||
| Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$DomainName} | ||
| } | ||
| It 'Changes only the Domain to new Domain' { | ||
| Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} | ||
| Mock GetComputerDomain {'contoso.com'} | ||
| Set-TargetResource -Name $Env:ComputerName -DomainName 'adventure-works.com' -Credential $Credential -UnjoinCredential $Credential | Should BeNullOrEmpty | ||
| Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It | ||
| Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName} | ||
| Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$NewName} | ||
| Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName} | ||
| } | ||
| It 'Changes only Domain to Workgroup' { | ||
| Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} | ||
| Mock GetComputerDomain {''} | ||
| Set-TargetResource -Name $Env:ComputerName -WorkGroupName 'Contoso' -UnjoinCredential $Credential | Should BeNullOrEmpty | ||
| Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It | ||
| Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$NewName} | ||
| Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$WorkGroupName} | ||
| Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$DomainName} | ||
| } | ||
| It 'Changes only ComputerName in Domain' { | ||
| Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} | ||
| Mock GetComputerDomain {'contoso.com'} | ||
| Set-TargetResource -Name $NotComputerName -Credential $Credential | Should BeNullOrEmpty | ||
| Assert-MockCalled -CommandName Rename-Computer -Exactly 1 -Scope It | ||
| Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It | ||
| } | ||
| It 'Changes only ComputerName in Workgroup' { | ||
| Mock GetComputerDomain {''} | ||
| Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso';Workgroup='Contoso';PartOfDomain=$false}} | ||
| Set-TargetResource -Name $NotComputerName | Should BeNullOrEmpty | ||
| Assert-MockCalled -CommandName Rename-Computer -Exactly 1 -Scope It | ||
| Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It | ||
| } | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest verifying the message in the future, not blocking the PR.