Skip to content
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

ActiveDirectoryDsc: Add stubs generated by Indented.StubCommand #483

Merged
merged 5 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@
will no longer be an ActiveDirectory module installed. This is
to make sure that if the unit tests work locally they should also work
in the CI pipeline.
- Added new stubs for the cmdlets and classes to be used with unit tests.
The new stubs are based on the modules ActiveDirectory and ADDSDeployment
in Windows Server 2019. The stubs are generated using the PowerShell
module *Indented.StubCommand*. Instructions how to generate stubs
(for example for a new operating system) has been added to the README.md
in the `Tests/Unit/Stubs` folder ([issue #245](https://github.com/PowerShell/ActiveDirectoryDsc/issues/245)).
- Update all unit tests removing all local stub functions in favor of
the new stub modules.
- Changes to ActiveDirectoryDsc.Common
- Updated common helper function `Find-DomainController` with the
optional parameter `WaitForValidCredentials` which will ignore
authentication exceptions when the credentials cannot be authenticated.
- Updated the function `Test-ADReplicationSite` to make the parameter
`Credential` mandatory.
- Changes to WaitForADDomain
- Correct grammar issues in example descriptions.
- An optional parameter `WaitForValidCredentials` can be set to $true
Expand Down Expand Up @@ -89,7 +99,7 @@
descriptions in the schema.mof (so that Wiki will be updated)
([issue #426](https://github.com/PowerShell/ActiveDirectoryDsc/issues/426)).
- Removed unnecessary Script Analyzer rule overrides from tests.
- Added new helper functions in xActiveDirectory.Common.
- Added new helper functions in ActiveDirectoryDsc.Common.
- New-CimCredentialInstance
- Add-TypeAssembly
- New-ADDirectoryContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ function Test-ADReplicationSite
[System.String]
$DomainName,

[Parameter()]
[Parameter(Mandatory = $true)]
[System.Management.Automation.PSCredential]
$Credential
)
Expand Down
43 changes: 14 additions & 29 deletions Tests/Unit/ActiveDirectory.Common.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,9 @@ $script:modulesFolderPath = Join-Path -Path $script:resourceModulePath -ChildPat
Import-Module -Name (Join-Path -Path $script:modulesFolderPath -ChildPath 'ActiveDirectoryDsc.Common.psm1') -Force

InModuleScope 'ActiveDirectoryDsc.Common' {
#Load the AD Module Stub, so we can mock the cmdlets, then load the AD types
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\ActiveDirectoryStub.psm1') -Force

# If one type does not exist, it's assumed the other ones does not exist either.
if (-not ('Microsoft.DirectoryServices.Deployment.Types.ForestMode' -as [Type]))
{
Add-Type -Path (Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -ChildPath 'Unit\Stubs\Microsoft.DirectoryServices.Deployment.Types.cs')
}

# If one type does not exist, it's assumed the other ones does not exist either.
if (-not ('Microsoft.ActiveDirectory.Management.ADForestMode' -as [Type]))
{
Add-Type -Path (Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -ChildPath 'Unit\Stubs\Microsoft.ActiveDirectory.Management.cs')
}
# Load stub cmdlets and classes.
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\ActiveDirectory_2019.psm1') -Force
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\ADDSDeployment_2019.psm1') -Force

Describe 'ActiveDirectoryDsc.Common\Test-DscParameterState' -Tag TestDscParameterState {
Context 'When passing values' {
Expand Down Expand Up @@ -1258,10 +1247,6 @@ InModuleScope 'ActiveDirectoryDsc.Common' {
ObjectGUID = 'd3c8b8c1-c42b-4533-af7d-3aa73ecd2216'
}

function Restore-ADObject
{
}

$getAdCommonParameterReturnValue = @{Identity = 'something'}
$restoreIdentity = 'SomeObjectName'
$restoreObjectClass = 'user'
Expand Down Expand Up @@ -2173,21 +2158,21 @@ InModuleScope 'ActiveDirectoryDsc.Common' {
Assert-MockCalled -CommandName New-PSDrive -Exactly -Times 0 -Scope Context
}
}

}

Describe 'ActiveDirectoryDsc.Common\Test-ADReplicationSite' {
BeforeAll {
function Get-ADDomainController
{
}

function Get-ADReplicationSite
{
}
$mockAdministratorUser = '[email protected]'
$mockAdministratorPassword = 'P@ssw0rd-12P@ssw0rd-12'
$mockAdministratorCredential = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList @(
$mockAdministratorUser,
($mockAdministratorPassword | ConvertTo-SecureString -AsPlainText -Force)
)

Mock -CommandName Get-ADDomainController -MockWith {
return $env:COMPUTERNAME
return @{
HostName = $env:COMPUTERNAME
}
}
}

Expand All @@ -2199,7 +2184,7 @@ InModuleScope 'ActiveDirectoryDsc.Common' {
}

It 'Should return $false' {
$testADReplicationSiteResult = Test-ADReplicationSite -SiteName 'TestSite' -DomainName 'contoso.com'
$testADReplicationSiteResult = Test-ADReplicationSite -SiteName 'TestSite' -DomainName 'contoso.com' -Credential $mockAdministratorCredential
$testADReplicationSiteResult | Should -BeFalse

Assert-MockCalled -CommandName Get-ADDomainController -Exactly -Times 1 -Scope It
Expand All @@ -2215,7 +2200,7 @@ InModuleScope 'ActiveDirectoryDsc.Common' {
}

It 'Should return $true' {
$testADReplicationSiteResult = Test-ADReplicationSite -SiteName 'TestSite' -DomainName 'contoso.com'
$testADReplicationSiteResult = Test-ADReplicationSite -SiteName 'TestSite' -DomainName 'contoso.com' -Credential $mockAdministratorCredential
$testADReplicationSiteResult | Should -BeTrue

Assert-MockCalled -CommandName Get-ADDomainController -Exactly -Times 1 -Scope It
Expand Down
11 changes: 2 additions & 9 deletions Tests/Unit/MSFT_ADComputer.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,8 @@ try
Invoke-TestSetup

InModuleScope $script:dscResourceName {
#Load the AD Module Stub, so we can mock the cmdlets, then load the AD types
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\ActiveDirectoryStub.psm1') -Force

# If one type does not exist, it's assumed the other ones does not exist either.
if (-not ('Microsoft.ActiveDirectory.Management.ADComputer' -as [Type]))
{
$adModuleStub = (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\Microsoft.ActiveDirectory.Management.cs')
Add-Type -Path $adModuleStub
}
# Load stub cmdlets and classes.
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\ActiveDirectory_2019.psm1') -Force

$mockComputerNamePresent = 'TEST01'
$mockDomain = 'contoso.com'
Expand Down
62 changes: 3 additions & 59 deletions Tests/Unit/MSFT_ADDomain.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,6 @@ $TestEnvironment = Initialize-TestEnvironment `

function Invoke-TestSetup
{
# If one type does not exist, it's assumed the other ones does not exist either.
if (-not ('Microsoft.DirectoryServices.Deployment.Types.ForestMode' -as [Type]))
{
Add-Type -Path (Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -ChildPath 'Unit\Stubs\Microsoft.DirectoryServices.Deployment.Types.cs')
}

# If one type does not exist, it's assumed the other ones does not exist either.
if (-not ('Microsoft.ActiveDirectory.Management.ADForestMode' -as [Type]))
{
Add-Type -Path (Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -ChildPath 'Unit\Stubs\Microsoft.ActiveDirectory.Management.cs')
}
}

function Invoke-TestCleanup
Expand All @@ -54,14 +43,9 @@ try
Invoke-TestSetup

InModuleScope $script:dscResourceName {
#Load the AD Module Stub, so we can mock the cmdlets, then load the AD types
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\ActiveDirectoryStub.psm1') -Force

# If one type does not exist, it's assumed the other ones does not exist either.
if (-not ('Microsoft.ActiveDirectory.Management.ADForestMode' -as [Type]))
{
Add-Type -Path (Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -ChildPath 'Unit\Stubs\Microsoft.ActiveDirectory.Management.cs')
}
# Load stub cmdlets and classes.
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\ActiveDirectory_2019.psm1') -Force
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\ADDSDeployment_2019.psm1') -Force

$mockDomainName = 'contoso.com'
$mockNetBiosName = 'CONTOSO'
Expand Down Expand Up @@ -494,46 +478,6 @@ try

#region Function Set-TargetResource
Describe 'ADDomain\Set-TargetResource' {
function Install-ADDSForest
{
param
(
$DomainName,
$SafeModeAdministratorPassword,
$CreateDnsDelegation,
$DatabasePath,
$DnsDelegationCredential,
$InstallDns,
$LogPath,
$NoRebootOnCompletion,
$SysvolPath,
$DomainNetbiosName,
$ForestMode,
$DomainMode
)
}

function Install-ADDSDomain
{
param
(
$NewDomainName,
$ParentDomainName,
$SafeModeAdministratorPassword,
$CreateDnsDelegation,
$Credential,
$DatabasePath,
$DnsDelegationCredential,
$DomainType,
$InstallDns,
$LogPath,
$NewDomainNetbiosName,
$NoRebootOnCompletion,
$SysvolPath,
$DomainMode
)
}

$testDomainName = 'present.com'
$testParentDomainName = 'parent.com'
$testDomainNetBIOSNameName = 'PRESENT'
Expand Down
79 changes: 5 additions & 74 deletions Tests/Unit/MSFT_ADDomainController.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,9 @@ try
Invoke-TestSetup

InModuleScope $script:dscResourceName {
#Load the AD Module Stub, so we can mock the cmdlets, then load the AD types
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\ActiveDirectoryStub.psm1') -Force

# If one type does not exist, it's assumed the other ones does not exist either.
if (-not ('Microsoft.ActiveDirectory.Management.ADAuthType' -as [Type]))
{
$adModuleStub = (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\Microsoft.ActiveDirectory.Management.cs')
Add-Type -Path $adModuleStub
}

# If one type does not exist, it's assumed the other ones does not exist either.
if (-not ('Microsoft.ActiveDirectory.Management.ADForestMode' -as [Type]))
{
Add-Type -Path (Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -ChildPath 'Unit\Stubs\Microsoft.ActiveDirectory.Management.cs')
}
# Load stub cmdlets and classes.
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\ActiveDirectory_2019.psm1') -Force
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\ADDSDeployment_2019.psm1') -Force

#region Pester Test Variable Initialization
$correctDomainName = 'present.com'
Expand Down Expand Up @@ -87,64 +75,7 @@ try
ReadOnlyReplica = $true
SiteName = $correctSiteName
}

#Fake function because it is only available on Windows Server
function Install-ADDSDomainController
{
[CmdletBinding()]
param
(
[Parameter()]
$DomainName,

[Parameter()]
[System.Management.Automation.PSCredential]
$SafeModeAdministratorPassword,

[Parameter()]
[System.Management.Automation.PSCredential]
$Credential,

[Parameter()]
$NoRebootOnCompletion,

[Parameter()]
$Force,

[Parameter()]
$DatabasePath,

[Parameter()]
$LogPath,

[Parameter()]
$SysvolPath,

[Parameter()]
$SiteName,

[Parameter()]
$InstallationMediaPath,

[Parameter()]
$NoGlobalCatalog,

[Parameter()]
[System.Boolean]
$ReadOnlyReplica,

[Parameter()]
[System.String[]]
$AllowPasswordReplicationAccountName,

[Parameter()]
[System.String[]]
$DenyPasswordReplicationAccountName
)

throw [exception] 'Not Implemented'
}
#endregion Pester Test Initialization
#endregion Pester Test Variable Initialization

#region Function Get-TargetResource
Describe 'ADDomainController\Get-TargetResource' -Tag 'Get' {
Expand Down Expand Up @@ -967,7 +898,7 @@ try
Mock -CommandName Get-ADDomain
Mock -CommandName Get-ADForest -MockWith {
return @{
RIDMaster = 'dc.contoso.com'
SchemaMaster = 'dc.contoso.com'
}
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/Unit/MSFT_ADDomainDefaultPasswordPolicy.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ try
Invoke-TestSetup

InModuleScope $script:dscResourceName {
#Load the AD Module Stub, so we can mock the cmdlets, then load the AD types
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\ActiveDirectoryStub.psm1') -Force
# Load stub cmdlets and classes.
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\ActiveDirectory_2019.psm1') -Force

$testDomainName = 'contoso.com'
$testDefaultParams = @{
Expand Down
9 changes: 1 addition & 8 deletions Tests/Unit/MSFT_ADDomainFunctionalLevel.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,7 @@ try

InModuleScope $script:dscResourceName {
# Load the AD Module Stub, so we can mock the cmdlets, then load the AD types.
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\ActiveDirectoryStub.psm1') -Force

# If one type does not exist, it's assumed the other ones does not exist either.
if (-not ('Microsoft.ActiveDirectory.Management.ADComputer' -as [Type]))
{
$adModuleStub = (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\Microsoft.ActiveDirectory.Management.cs')
Add-Type -Path $adModuleStub
}
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\ActiveDirectory_2019.psm1') -Force

$mockDefaultParameters = @{
DomainIdentity = 'contoso.com'
Expand Down
9 changes: 1 addition & 8 deletions Tests/Unit/MSFT_ADForestFunctionalLevel.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,7 @@ try

InModuleScope $script:dscResourceName {
# Load the AD Module Stub, so we can mock the cmdlets, then load the AD types.
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\ActiveDirectoryStub.psm1') -Force

# If one type does not exist, it's assumed the other ones does not exist either.
if (-not ('Microsoft.ActiveDirectory.Management.ADComputer' -as [Type]))
{
$adModuleStub = (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\Microsoft.ActiveDirectory.Management.cs')
Add-Type -Path $adModuleStub
}
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\ActiveDirectory_2019.psm1') -Force

$mockDefaultParameters = @{
ForestIdentity = 'contoso.com'
Expand Down
10 changes: 2 additions & 8 deletions Tests/Unit/MSFT_ADForestProperties.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,8 @@ try
Invoke-TestSetup

InModuleScope $script:dscResourceName {
#Load the AD Module Stub, so we can mock the cmdlets, then load the AD types
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\ActiveDirectoryStub.psm1') -Force

# If one type does not exist, it's assumed the other ones does not exist either.
if (-not ('Microsoft.ActiveDirectory.Management.ADForestMode' -as [Type]))
{
Add-Type -Path (Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -ChildPath 'Unit\Stubs\Microsoft.ActiveDirectory.Management.cs')
}
# Load stub cmdlets and classes.
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\ActiveDirectory_2019.psm1') -Force

$forestName = 'contoso.com'
$testCredential = [System.Management.Automation.PSCredential]::Empty
Expand Down
Loading