|
| 1 | +$Module = "$PSScriptRoot\..\DSCResources\MSFT_xComputer\MSFT_xComputer.psm1" |
| 2 | +Remove-Module -Name MSFT_xComputer -Force -ErrorAction SilentlyContinue |
| 3 | +Import-Module -Name $Module -Force -ErrorAction Stop |
| 4 | + |
| 5 | +InModuleScope MSFT_xComputer { |
| 6 | + |
| 7 | + Describe 'MSFT_xComputer' { |
| 8 | + |
| 9 | + $SecPassword = ConvertTo-SecureString -String 'password' -AsPlainText -Force |
| 10 | + $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'USER',$SecPassword |
| 11 | + $NotComputerName = if($env:COMPUTERNAME -ne 'othername'){'othername'}else{'name'} |
| 12 | + |
| 13 | + Context Test-TargetResource { |
| 14 | + Mock Get-WMIObject {[PSCustomObject]@{DomainName = 'ContosoLtd'}} -ParameterFilter {$Class -eq 'Win32_NTDomain'} |
| 15 | + It 'Throws if both DomainName and WorkGroupName are specified' { |
| 16 | + {Test-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com' -WorkGroupName 'workgroup'} | Should Throw |
| 17 | + } |
| 18 | + It 'Throws if Domain is specified without Credentials' { |
| 19 | + {Test-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com'} | Should Throw |
| 20 | + } |
| 21 | + It 'Should return True if Domain name is same as specified' { |
| 22 | + Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} |
| 23 | + Mock GetComputerDomain {'contoso.com'} |
| 24 | + Test-TargetResource -Name $Env:ComputerName -DomainName 'Contoso.com' -Credential $Credential | Should Be $true |
| 25 | + } |
| 26 | + It 'Should return True if Workgroup name is same as specified' { |
| 27 | + Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}} |
| 28 | + Mock GetComputerDomain {''} |
| 29 | + Test-TargetResource -Name $Env:ComputerName -WorkGroupName 'workgroup' | Should Be $true |
| 30 | + } |
| 31 | + It 'Should return True if ComputerName and Domain name is same as specified' { |
| 32 | + Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} |
| 33 | + Mock GetComputerDomain {'contoso.com'} |
| 34 | + Test-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com' -Credential $Credential | Should Be $true |
| 35 | + } |
| 36 | + It 'Should return True if ComputerName and Workgroup is same as specified' { |
| 37 | + Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}} |
| 38 | + Mock GetComputerDomain {''} |
| 39 | + Test-TargetResource -Name $Env:ComputerName -WorkGroupName 'workgroup' | Should Be $true |
| 40 | + } |
| 41 | + It 'Should return True if ComputerName is same and no Domain or Workgroup specified' { |
| 42 | + Mock Get-WmiObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}} |
| 43 | + Mock GetComputerDomain {''} |
| 44 | + Test-TargetResource -Name $Env:ComputerName | Should Be $true |
| 45 | + Mock Get-WmiObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} |
| 46 | + Mock GetComputerDomain {'contoso.com'} |
| 47 | + Test-TargetResource -Name $Env:ComputerName | Should Be $true |
| 48 | + } |
| 49 | + It 'Should return False if ComputerName is not same and no Domain or Workgroup specified' { |
| 50 | + Mock Get-WmiObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}} |
| 51 | + Mock GetComputerDomain {''} |
| 52 | + Test-TargetResource -Name $NotComputerName | Should Be $false |
| 53 | + Mock Get-WmiObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} |
| 54 | + Mock GetComputerDomain {'contoso.com'} |
| 55 | + Test-TargetResource -Name $NotComputerName | Should Be $false |
| 56 | + } |
| 57 | + It 'Should return False if Domain name is not same as specified' { |
| 58 | + Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} |
| 59 | + Mock GetComputerDomain {'contoso.com'} |
| 60 | + Test-TargetResource -Name $Env:ComputerName -DomainName 'adventure-works.com' -Credential $Credential | Should Be $false |
| 61 | + } |
| 62 | + It 'Should return False if Workgroup name is not same as specified' { |
| 63 | + Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}} |
| 64 | + Mock GetComputerDomain {''} |
| 65 | + Test-TargetResource -Name $Env:ComputerName -WorkGroupName 'NOTworkgroup' | Should Be $false |
| 66 | + } |
| 67 | + It 'Should return False if ComputerName is not same as specified' { |
| 68 | + Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Workgroup';Workgroup='Workgroup';PartOfDomain=$false}} |
| 69 | + Mock GetComputerDomain {''} |
| 70 | + Test-TargetResource -Name $NotComputerName -WorkGroupName 'workgroup' | Should Be $false |
| 71 | + Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} |
| 72 | + Mock GetComputerDomain {'contoso.com'} |
| 73 | + Test-TargetResource -Name $NotComputerName -DomainName 'contoso.com' -Credential $Credential | Should Be $false |
| 74 | + } |
| 75 | + It 'Should return False if Computer is in Workgroup and Domain is specified' { |
| 76 | + Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$false}} |
| 77 | + Mock GetComputerDomain {''} |
| 78 | + Test-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com' -Credential $Credential | Should Be $false |
| 79 | + } |
| 80 | + It 'Should return False if ComputerName is in Domain and Workgroup is specified' { |
| 81 | + Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} |
| 82 | + Mock GetComputerDomain {'contoso.com'} |
| 83 | + Test-TargetResource -Name $Env:ComputerName -WorkGroupName 'Contoso' -Credential $Credential -UnjoinCredential $Credential | Should Be $false |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + Context Get-TargetResource { |
| 88 | + It 'should not throw' { |
| 89 | + {Get-TargetResource -Name $env:COMPUTERNAME} | Should Not Throw |
| 90 | + } |
| 91 | + It 'Should return a hashtable containing Name,DomainName, Credential, UnjoinCredential and WorkGroupName' { |
| 92 | + $Result = Get-TargetResource -Name $env:COMPUTERNAME |
| 93 | + $Result.GetType().Fullname | Should Be 'System.Collections.Hashtable' |
| 94 | + $Result.Keys | Should Be @('Name','DomainName','Credential','UnjoinCredential','WorkGroupName') |
| 95 | + } |
| 96 | + } |
| 97 | + Context Set-TargetResource { |
| 98 | + Mock Rename-Computer {} |
| 99 | + Mock Add-Computer {} |
| 100 | + It 'Throws if both DomainName and WorkGroupName are specified' { |
| 101 | + {Set-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com' -WorkGroupName 'workgroup'} | Should Throw |
| 102 | + Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It |
| 103 | + Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It |
| 104 | + } |
| 105 | + It 'Throws if Domain is specified without Credentials' { |
| 106 | + {Set-TargetResource -Name $Env:ComputerName -DomainName 'contoso.com'} | Should Throw |
| 107 | + Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It |
| 108 | + Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It |
| 109 | + } |
| 110 | + It 'Changes ComputerName and changes Domain to new Domain' { |
| 111 | + Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} |
| 112 | + Mock GetComputerDomain {'contoso.com'} |
| 113 | + Set-TargetResource -Name $NotComputerName -DomainName 'adventure-works.com' -Credential $Credential -UnjoinCredential $Credential | Should BeNullOrEmpty |
| 114 | + Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It |
| 115 | + Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName} |
| 116 | + Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName} |
| 117 | + } |
| 118 | + It 'Changes ComputerName and changes Domain to Workgroup' { |
| 119 | + Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} |
| 120 | + Mock GetComputerDomain {'contoso.com'} |
| 121 | + Set-TargetResource -Name $NotComputerName -WorkGroupName 'contoso' -Credential $Credential | Should BeNullOrEmpty |
| 122 | + Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It |
| 123 | + Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$WorkGroupName -and $NewName -and $Credential} |
| 124 | + Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$DomainName -or $UnjoinCredential} |
| 125 | + } |
| 126 | + It 'Changes ComputerName and changes Workgroup to Domain' { |
| 127 | + Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso';Workgroup='Contoso';PartOfDomain=$false}} |
| 128 | + Mock GetComputerDomain {''} |
| 129 | + Set-TargetResource -Name $NotComputerName -DomainName 'Contoso.com' -Credential $Credential | Should BeNullOrEmpty |
| 130 | + Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It |
| 131 | + Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName -and $NewName} |
| 132 | + Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName} |
| 133 | + } |
| 134 | + It 'Changes ComputerName and changes Workgroup to new Workgroup' { |
| 135 | + Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso';Workgroup='Contoso';PartOfDomain=$false}} |
| 136 | + Mock GetComputerDomain {''} |
| 137 | + Set-TargetResource -Name $NotComputerName -WorkGroupName 'adventure-works' | Should BeNullOrEmpty |
| 138 | + Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It |
| 139 | + Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$WorkGroupName -and $NewName} |
| 140 | + Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$DomainName} |
| 141 | + } |
| 142 | + It 'Changes only the Domain to new Domain' { |
| 143 | + Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} |
| 144 | + Mock GetComputerDomain {'contoso.com'} |
| 145 | + Set-TargetResource -Name $Env:ComputerName -DomainName 'adventure-works.com' -Credential $Credential -UnjoinCredential $Credential | Should BeNullOrEmpty |
| 146 | + Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It |
| 147 | + Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$DomainName} |
| 148 | + Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$NewName} |
| 149 | + Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$WorkGroupName} |
| 150 | + } |
| 151 | + It 'Changes only Domain to Workgroup' { |
| 152 | + Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} |
| 153 | + Mock GetComputerDomain {''} |
| 154 | + Set-TargetResource -Name $Env:ComputerName -WorkGroupName 'Contoso' -UnjoinCredential $Credential | Should BeNullOrEmpty |
| 155 | + Assert-MockCalled -CommandName Rename-Computer -Exactly 0 -Scope It |
| 156 | + Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$NewName} |
| 157 | + Assert-MockCalled -CommandName Add-Computer -Exactly 1 -Scope It -ParameterFilter {$WorkGroupName} |
| 158 | + Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It -ParameterFilter {$DomainName} |
| 159 | + } |
| 160 | + It 'Changes only ComputerName in Domain' { |
| 161 | + Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso.com';Workgroup='Contoso.com';PartOfDomain=$true}} |
| 162 | + Mock GetComputerDomain {'contoso.com'} |
| 163 | + Set-TargetResource -Name $NotComputerName -Credential $Credential | Should BeNullOrEmpty |
| 164 | + Assert-MockCalled -CommandName Rename-Computer -Exactly 1 -Scope It |
| 165 | + Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It |
| 166 | + } |
| 167 | + It 'Changes only ComputerName in Workgroup' { |
| 168 | + Mock GetComputerDomain {''} |
| 169 | + Mock Get-WMIObject {[PSCustomObject]@{Domain = 'Contoso';Workgroup='Contoso';PartOfDomain=$false}} |
| 170 | + Set-TargetResource -Name $NotComputerName | Should BeNullOrEmpty |
| 171 | + Assert-MockCalled -CommandName Rename-Computer -Exactly 1 -Scope It |
| 172 | + Assert-MockCalled -CommandName Add-Computer -Exactly 0 -Scope It |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments