Skip to content

Commit

Permalink
Update DnsServerDsc.Common module and unit tests (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju authored Nov 25, 2023
1 parent 85d00b4 commit 2552eeb
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 114 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Update to use the build worker `windows-latest` for the necessary stages
of the pipeline, and `ubuntu-latest` for the other stages.
- Update pipeline to use _GitVersion.Tool_ installed by `dotnet`.
- DnsServerDsc.Common
- Added unit tests for `Convert-RootHintsToHashtable`.

### Fixed

- DnsServerDsc.Common
- Removed unit tests for functions no longer part of the module.
- Removed functions from the module manifest that are no longer
part of the module.
- DnRecordBase
- Update comment regarding use of `using module` statement.
- ResourceBase
Expand Down
3 changes: 0 additions & 3 deletions source/Modules/DnsServerDsc.Common/DnsServerDsc.Common.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @(
'Remove-CommonParameter'
'ConvertTo-CimInstance'
'ConvertTo-FollowRfc1034'
'ConvertTo-HashTable'
'Convert-RootHintsToHashtable'
'Test-DscDnsParameterState'
)
Expand Down
177 changes: 66 additions & 111 deletions tests/Unit/DnsServerDsc.Common.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,92 +16,11 @@ Import-Module $script:subModuleFile -Force -ErrorAction 'Stop'
#endregion HEADER

InModuleScope $script:subModuleName {
Describe 'DnsServerDsc.Common\Remove-CommonParameter' {
$removeCommonParameter = @{
Parameter1 = 'value1'
Parameter2 = 'value2'
Verbose = $true
Debug = $true
ErrorAction = 'Stop'
WarningAction = 'Stop'
InformationAction = 'Stop'
ErrorVariable = 'errorVariable'
WarningVariable = 'warningVariable'
OutVariable = 'outVariable'
OutBuffer = 'outBuffer'
PipelineVariable = 'pipelineVariable'
InformationVariable = 'informationVariable'
WhatIf = $true
Confirm = $true
UseTransaction = $true
}

Context 'Hashtable contains all common parameters' {
It 'Should not throw exception' {
{ $script:result = Remove-CommonParameter -Hashtable $removeCommonParameter -Verbose } | Should -Not -Throw
}

It 'Should have retained parameters in the hashtable' {
$script:result.Contains('Parameter1') | Should -Be $true
$script:result.Contains('Parameter2') | Should -Be $true
}

It 'Should have removed the common parameters from the hashtable' {
$script:result.Contains('Verbose') | Should -Be $false
$script:result.Contains('Debug') | Should -Be $false
$script:result.Contains('ErrorAction') | Should -Be $false
$script:result.Contains('WarningAction') | Should -Be $false
$script:result.Contains('InformationAction') | Should -Be $false
$script:result.Contains('ErrorVariable') | Should -Be $false
$script:result.Contains('WarningVariable') | Should -Be $false
$script:result.Contains('OutVariable') | Should -Be $false
$script:result.Contains('OutBuffer') | Should -Be $false
$script:result.Contains('PipelineVariable') | Should -Be $false
$script:result.Contains('InformationVariable') | Should -Be $false
$script:result.Contains('WhatIf') | Should -Be $false
$script:result.Contains('Confirm') | Should -Be $false
$script:result.Contains('UseTransaction') | Should -Be $false
}
}
}

Describe 'DnsServerDsc.Common\ConvertTo-CimInstance' {
$hashtable = @{
k1 = 'v1'
k2 = 100
k3 = 1, 2, 3
}

Context 'The array contains the expected record count' {
It 'Should not throw exception' {
{ $script:result = [CimInstance[]] ($hashtable | ConvertTo-CimInstance -Verbose) } | Should -Not -Throw
}

It "Record count should be $($hashTable.Count)" {
$script:result.Count | Should -Be $hashtable.Count
}

It 'Result should be of type CimInstance[]' {
$script:result.GetType().Name | Should -Be 'CimInstance[]'
}

It 'Value "k1" in the CimInstance array should be "v1"' {
($script:result | Where-Object Key -eq k1).Value | Should -Be 'v1'
}

It 'Value "k2" in the CimInstance array should be "100"' {
($script:result | Where-Object Key -eq k2).Value | Should -Be 100
}

It 'Value "k3" in the CimInstance array should be "1,2,3"' {
($script:result | Where-Object Key -eq k3).Value | Should -Be '1,2,3'
}
}
}

Describe 'DnsServerDsc.Common\ConvertTo-FollowRfc1034' {
$hostname = 'mail.contoso.com'
$convertedHostname = 'mail.contoso.com.'
BeforeAll {
$hostname = 'mail.contoso.com'
$convertedHostname = 'mail.contoso.com.'
}

Context 'The hostname is not converted' {
It 'Should not throw exception' {
Expand All @@ -120,37 +39,73 @@ InModuleScope $script:subModuleName {
}
}

Describe 'DnsServerDsc.Common\ConvertTo-HashTable' {
[CimInstance[]]$cimInstances = ConvertTo-CimInstance -Hashtable @{
k1 = 'v1'
k2 = 100
k3 = 1, 2, 3
Describe 'Convert-RootHintsToHashtable' {
BeforeAll {
$emptyRootHints = @()
$rootHintWithoutIP = @(
@{
NameServer = @{
RecordData = @{
NameServer = 'ns1'
}
}
IPAddress = $null
}
)
$rootHintWithIPv4 = @(
@{
NameServer = @{
RecordData = @{
NameServer = 'ns2'
}
}
IPAddress = @{
RecordData = @{
IPv6Address = @{
IPAddressToString = '192.0.2.1'
}
}
}
}
)
$rootHintWithIPv6 = @(
@{
NameServer = @{
RecordData = @{
NameServer = 'ns3'
}
}
IPAddress = @{
RecordData = @{
IPv6Address = @{
IPAddressToString = '2001:db8::1'
}
}
}
}
)
}

Context 'The array contains the expected record count' {
It 'Should not throw exception' {
{ $script:result = $cimInstances | ConvertTo-HashTable -Verbose } | Should -Not -Throw
}

It "Record count should be $($cimInstances.Count)" {
$script:result.Count | Should -Be $cimInstances.Count
}

It 'Result should be of type [System.Collections.Hashtable]' {
$script:result | Should -BeOfType [System.Collections.Hashtable]
}
It 'Returns an empty hashtable when the input array is empty' {
$result = Convert-RootHintsToHashtable -RootHints $emptyRootHints
$result.Count | Should -Be 0
}

It 'Value "k1" in the hashtable should be "v1"' {
$script:result.k1 | Should -Be 'v1'
}
It 'Correctly skips elements without an IPAddress' {
$result = Convert-RootHintsToHashtable -RootHints $rootHintWithoutIP
$result.Count | Should -Be 0
}

It 'Value "k2" in the hashtable should be "100"' {
$script:result.k2 | Should -Be 100
}
It 'Correctly handles elements with an IPv4 address' {
$result = Convert-RootHintsToHashtable -RootHints $rootHintWithIPv4
$result.Count | Should -Be 1
$result.ns2 | Should -Be '192.0.2.1'
}

It 'Value "k3" in the hashtable should be "1,2,3"' {
$script:result.k3 | Should -Be '1,2,3'
}
It 'Correctly handles elements with an IPv6 address' {
$result = Convert-RootHintsToHashtable -RootHints $rootHintWithIPv6
$result.Count | Should -Be 1
$result.ns3 | Should -Be '2001:db8::1'
}
}
}

0 comments on commit 2552eeb

Please sign in to comment.