From 048377a5cbc3b31a443e9892638c78b41b775037 Mon Sep 17 00:00:00 2001 From: James Sudbury Date: Mon, 22 Mar 2021 07:02:44 +0000 Subject: [PATCH] xDnServer: Increased coverage (#215) - DnsRecordBase - Added unit tests to get code coverage on unimplemented method calls (ensuring the `throw` statements get called). - DnsRecordSrv - Changed logic for calculating the record's hostname. --- CHANGELOG.md | 3 ++ source/Classes/002.DnsRecordSrv.ps1 | 2 +- tests/Unit/Classes/DnsRecordBase.tests.ps1 | 32 +++++++++++++++++++ tests/Unit/Classes/DnsRecordSrv.tests.ps1 | 3 +- .../Unit/Classes/DnsRecordSrvScoped.tests.ps1 | 2 +- 5 files changed, 39 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b3133e32..87cda7e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - xDnsServerRootHint - Added integration tests ([issue #174](https://github.com/dsccommunity/xDnsServer/issues/174)). - Added a class `DnsRecordBase` that is used as the base class for the resources that create DNS records. + - Added unit tests to get code coverage on unimplemented method calls (ensuring the `throw` statements get called) - DnsRecordSrv - Added new resource to manage SRV records - DnsRecordSrvScoped @@ -92,6 +93,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - xDnsServerForwarder - When providing an empty collection the resource will enforce that no forwarders are present. +- DnsRecordSrv + - Changed logic for calculating the record's hostname ### Removed diff --git a/source/Classes/002.DnsRecordSrv.ps1 b/source/Classes/002.DnsRecordSrv.ps1 index f653e475..d287b560 100644 --- a/source/Classes/002.DnsRecordSrv.ps1 +++ b/source/Classes/002.DnsRecordSrv.ps1 @@ -55,7 +55,7 @@ class DnsRecordSrv : DnsRecordBase hidden [System.String] getRecordHostName() { - return "_$($this.SymbolicName)._$($this.Protocol)".ToLower() + return $this.getRecordHostName($this.SymbolicName, $this.Protocol) } hidden [System.String] getRecordHostName($aSymbolicName, $aProtocol) diff --git a/tests/Unit/Classes/DnsRecordBase.tests.ps1 b/tests/Unit/Classes/DnsRecordBase.tests.ps1 index 1a873970..a2a57e6f 100644 --- a/tests/Unit/Classes/DnsRecordBase.tests.ps1 +++ b/tests/Unit/Classes/DnsRecordBase.tests.ps1 @@ -40,6 +40,24 @@ InModuleScope $ProjectName { $instance.GetType().Name | Should -Be 'DnsRecordBase' } } + + Context 'Unimplemented methods' { + It 'Should throw when GetResourceRecord() is called' { + { $instance = [DnsRecordBase]::new().GetResourceRecord() } | Should -Throw + } + + It 'Should throw when AddResourceRecord() is called' { + { $instance = [DnsRecordBase]::new().AddResourceRecord() } | Should -Throw + } + + It 'Should throw when ModifyResourceRecord(...) is called' { + { $instance = [DnsRecordBase]::new().ModifyResourceRecord($null, $null) } | Should -Throw + } + + It 'Should throw when NewDscResourceObjectFromRecord(...) is called' { + { $instance = [DnsRecordBase]::new().NewDscResourceObjectFromRecord($null) } | Should -Throw + } + } } Describe 'Testing DnsRecordBase Get Method' -Tag 'Get', 'DnsRecord', 'DnsRecordBase' { @@ -57,6 +75,20 @@ InModuleScope $ProjectName { } } + Context 'Testing $null value passed to Set()' { + BeforeAll { + $script:instanceDesiredState = [DnsRecordBase]::new() + $script:instanceDesiredState.ZoneName = 'contoso.com' + $script:instanceDesiredState.TimeToLive = $null + $script:instanceDesiredState.DnsServer = 'localhost' + $script:instanceDesiredState.Ensure = 'Present' + } + + It 'Should throw when Set() is called' { + { $script:instanceDesiredState.Set() } | Should -Throw + } + } + Context 'Testing subclassed (implemented) functionality' { BeforeAll { class MockRecordDoesNotExist : DnsRecordBase diff --git a/tests/Unit/Classes/DnsRecordSrv.tests.ps1 b/tests/Unit/Classes/DnsRecordSrv.tests.ps1 index cb424b90..d157a067 100644 --- a/tests/Unit/Classes/DnsRecordSrv.tests.ps1 +++ b/tests/Unit/Classes/DnsRecordSrv.tests.ps1 @@ -266,7 +266,7 @@ InModuleScope $ProjectName { Priority = 30 # Incorrect Weight = 30 TimeToLive = '01:00:00' - } + }, @{ SymbolicName = 'xmpp' ZoneName = 'contoso.com' @@ -364,6 +364,7 @@ InModuleScope $ProjectName { # Set a wrong value $mockRecord.RecordData.Priority = 300 + $mockRecord.RecordData.Weight = 400 return $mockRecord } diff --git a/tests/Unit/Classes/DnsRecordSrvScoped.tests.ps1 b/tests/Unit/Classes/DnsRecordSrvScoped.tests.ps1 index b91d5e6d..65fc8b9d 100644 --- a/tests/Unit/Classes/DnsRecordSrvScoped.tests.ps1 +++ b/tests/Unit/Classes/DnsRecordSrvScoped.tests.ps1 @@ -276,7 +276,7 @@ InModuleScope $ProjectName { Priority = 30 # Incorrect Weight = 30 TimeToLive = '01:00:00' - } + }, @{ SymbolicName = 'xmpp' ZoneName = 'contoso.com'