From 5eae4857cf3b854805c5e73bcf74491a8e6d26fa Mon Sep 17 00:00:00 2001 From: Wes Carroll Date: Thu, 11 Jul 2019 18:14:32 -0400 Subject: [PATCH] Updating tests for multiuse --- Tests/Public/Add-ZertoPeerSite.Tests.ps1 | 92 ++++---- Tests/Public/Checkpoint-ZertoVpg.Tests.ps1 | 78 +++---- Tests/Public/Connect-ZertoAnalytics.Tests.ps1 | 85 ++++++-- Tests/Public/Connect-ZertoServer.Tests.ps1 | 205 ++++++------------ Tests/Public/Disconnect-ZertoServer.Tests.ps1 | 53 ++--- 5 files changed, 247 insertions(+), 266 deletions(-) diff --git a/Tests/Public/Add-ZertoPeerSite.Tests.ps1 b/Tests/Public/Add-ZertoPeerSite.Tests.ps1 index a55ee59..39fd439 100644 --- a/Tests/Public/Add-ZertoPeerSite.Tests.ps1 +++ b/Tests/Public/Add-ZertoPeerSite.Tests.ps1 @@ -1,76 +1,70 @@ #Requires -Modules Pester -$moduleFileName = "ZertoApiWrapper.psd1" -$here = (Split-Path -Parent $MyInvocation.MyCommand.Path).Replace("Tests", "ZertoApiWrapper") -$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") -$file = Get-ChildItem "$here\$sut" -$modulePath = $here -replace "Public", "" -$moduleFile = Get-ChildItem "$modulePath\$moduleFileName" -Get-Module -Name ZertoApiWrapper | Remove-Module -Force -Import-Module $moduleFile -Force +$global:here = (Split-Path -Parent $MyInvocation.MyCommand.Path) +$script:function = ((Split-Path -leaf $MyInvocation.MyCommand.Path).Split('.'))[0] -Describe $file.BaseName -Tag 'Unit' { - - It "Is valid Powershell (Has no script errors)" { - $contents = Get-Content -Path $file -ErrorAction Stop - $errors = $null - $null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors) - $errors | Should -HaveCount 0 +Describe $script:function -Tag 'Unit', 'Source', 'Built' { + BeforeAll { + $script:ScriptBlock = (Get-Command $script:function).ScriptBlock } - Context "$($file.BaseName)::Parameter Unit Tests" { + Context "$script:function::Parameter Unit Tests" { - it "Has a mandatory string parameter for the target host" { - Get-Command $file.BaseName | Should -HaveParameter TargetHost -Mandatory -Type String + It "Has a mandatory string parameter for the target host" { + Get-Command $script:function | Should -HaveParameter TargetHost -Mandatory -Type String } - it "Will not take a non-ip address as a 'TargetHost'" { - {Add-ZertoPeerSite -targetHost 'MyZVMHost' -targetPort '9081'} | should -Throw - {Add-ZertoPeerSite -targetHost '192.168.1.266' -targetPort '9081'} | should -Throw - {Add-ZertoPeerSite -targetHost '192.168.1' -targetPort '9081'} | should -Throw - {Add-ZertoPeerSite -targetHost $null -targetPort '9081'} | should -Throw + It "Will not take a non-ip address as a 'TargetHost'" { + { Add-ZertoPeerSite -targetHost 'MyZVMHost' -targetPort '9081' } | Should -Throw + { Add-ZertoPeerSite -targetHost '192.168.1.266' -targetPort '9081' } | Should -Throw + { Add-ZertoPeerSite -targetHost '192.168.1' -targetPort '9081' } | Should -Throw + { Add-ZertoPeerSite -targetHost $null -targetPort '9081' } | Should -Throw } - it "Has a non-mandatory string parameter for the target port with default value of 9081" { - Get-Command $file.BaseName | Should -HaveParameter TargetPort -Not -Mandatory - Get-Command $file.BaseName | Should -HaveParameter TargetPort -Type Int32 - Get-Command $file.BaseName | Should -HaveParameter TargetPort -DefaultValue 9081 + It "Has a non-mandatory string parameter for the target port with default value of 9081" { + Get-Command Add-ZertoPeerSite | Should -HaveParameter TargetPort -Not -Mandatory + Get-Command Add-ZertoPeerSite | Should -HaveParameter TargetPort -Type Int32 + Get-Command Add-ZertoPeerSite | Should -HaveParameter TargetPort -DefaultValue 9081 } - it "Will not take a non-int as a port" { - {Add-ZertoPeerSite -targetHost '192.168.1.100' -targetPort 'string'} | should -Throw - {Add-ZertoPeerSite -targetHost '192.168.1.100' -targetPort $true} | should -Throw - {Add-ZertoPeerSite -targetHost '192.168.1.100' -targetPort $null} | should -Throw + It "Will not take a non-int as a port" { + { Add-ZertoPeerSite -targetHost '192.168.1.100' -targetPort 'string' } | Should -Throw + { Add-ZertoPeerSite -targetHost '192.168.1.100' -targetPort $true } | Should -Throw + { Add-ZertoPeerSite -targetHost '192.168.1.100' -targetPort $null } | Should -Throw } It "Will fail if the specified port is outside of the range 1024 - 65535" { - {Add-ZertoPeerSite -targetHost '192.168.1.100' -targetPort 1023} | Should -Throw - {Add-ZertoPeerSite -targetHost '192.168.1.100' -targetPort 65536} | Should -Throw - {Add-ZertoPeerSite -targetHost '192.168.1.100' -targetPort 0} | Should -Throw - {Add-ZertoPeerSite -targetHost '192.168.1.100' -targetPort -1} | Should -Throw + { Add-ZertoPeerSite -targetHost '192.168.1.100' -targetPort 1023 } | Should -Throw + { Add-ZertoPeerSite -targetHost '192.168.1.100' -targetPort 65536 } | Should -Throw + { Add-ZertoPeerSite -targetHost '192.168.1.100' -targetPort 0 } | Should -Throw + { Add-ZertoPeerSite -targetHost '192.168.1.100' -targetPort -1 } | Should -Throw } - it "Supports 'SupportsShouldProcess'" { - Get-Command $file.BaseName | Should -HaveParameter WhatIf - Get-Command $file.BaseName | Should -HaveParameter Confirm - $file | Should -FileContentMatch 'SupportsShouldProcess' - $file | Should -FileContentMatch '\$PSCmdlet\.ShouldProcess\(.+\)' + It "Supports 'SupportsShouldProcess'" { + Get-Command $script:function | Should -HaveParameter WhatIf + Get-Command $script:function | Should -HaveParameter Confirm + $script:ScriptBlock | Should -match 'SupportsShouldProcess' + $script:ScriptBlock | Should -match '\$PSCmdlet\.ShouldProcess\(.+\)' } } - Context "$($file.BaseName)::Function Unit Tests" { - - Mock -ModuleName ZertoApiWrapper Invoke-ZertoRestRequest { - return "9a49f42e-2bbd-4bf8-b571-77908a2e5e98.928a122b-1763-4664-ad37-cc00bb883f2f" + Context "Add-ZertoPeerSite::Functional Unit Tests" { + Mock -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest { + return (Get-Content "$global:here\Mocks\TaskId.txt") } - it "Returns a string value" { + It "Returns a string value" { $results = Add-ZertoPeerSite -targetHost '192.168.1.100' -targetPort '9081' - $results | should -Not -BeNullOrEmpty - $results | should -BeOfType "String" - $results | Should -BeExactly "9a49f42e-2bbd-4bf8-b571-77908a2e5e98.928a122b-1763-4664-ad37-cc00bb883f2f" + $results | Should -Not -BeNullOrEmpty + $results | Should -BeOfType "String" + $results | Should -BeExactly "7e79035e-fb8c-47fe-815c-12ddd41708e6.3e4cdd0d-1064-4022-921f-6265ad6d335a" } - Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest + It "Does not return a taskId if '-whatif' is used" { + $results = Add-ZertoPeerSite -targetHost '192.168.1.100' -targetPort '9081' -WhatIf + $results | Should -BeNullOrEmpty + } + + Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -Exactly 1 } } diff --git a/Tests/Public/Checkpoint-ZertoVpg.Tests.ps1 b/Tests/Public/Checkpoint-ZertoVpg.Tests.ps1 index f07f347..3cc79e4 100644 --- a/Tests/Public/Checkpoint-ZertoVpg.Tests.ps1 +++ b/Tests/Public/Checkpoint-ZertoVpg.Tests.ps1 @@ -1,66 +1,60 @@ #Requires -Modules Pester -$moduleFileName = "ZertoApiWrapper.psd1" -$here = (Split-Path -Parent $MyInvocation.MyCommand.Path).Replace("Tests", "ZertoApiWrapper") -$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") -$file = Get-ChildItem "$here\$sut" -$modulePath = $here -replace "Public", "" -$moduleFile = Get-ChildItem "$modulePath\$moduleFileName" -Get-Module -Name ZertoApiWrapper | Remove-Module -Force -Import-Module $moduleFile -Force +$global:here = (Split-Path -Parent $MyInvocation.MyCommand.Path) +$script:function = ((Split-Path -leaf $MyInvocation.MyCommand.Path).Split('.'))[0] -Describe $file.BaseName -Tag 'Unit' { - - It "is valid Powershell (Has no script errors)" { - $contents = Get-Content -Path $file -ErrorAction Stop - $errors = $null - $null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors) - $errors | Should -HaveCount 0 +Describe $script:function -Tag 'Unit', 'Source', 'Built' { + BeforeAll { + $script:ScriptBlock = (Get-Command $script:function).ScriptBlock } Mock -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest { - return "3b687246-ac63-40da-9a59-b99863769eb0.928a122b-1763-4664-ad37-cc00bb883f2f" + return (Get-Content "$global:here\Mocks\TaskId.txt") } - Mock -ModuleName ZertoApiWrapper -CommandName get-zertovpg { - return @{vpgIdentifier = "dddf2fa8-79e2-4e4f-a83b-f66676afea64"} + Mock -ModuleName ZertoApiWrapper -CommandName get-zertovpg -ParameterFilter { $VpgName -eq "MyVpg" } { + return (Get-Content "$global:here\Mocks\VPGInfo.json" -Raw | ConvertFrom-Json) } - Context "$($file.BaseName)::Parameter Unit Tests" { - it "Has a parameter for the VpgName that is Mandatory" { - Get-Command $file.BaseName | Should -HaveParameter vpgName -Mandatory -Type String + Context "$($script:function)::Parameter Unit Tests" { + It "Has a parameter for the VpgName that is Mandatory" { + Get-Command $script:function | Should -HaveParameter vpgName -Mandatory -Type String } - it "Has a parameter for the CheckpointName that is Mandatory" { - Get-Command $file.BaseName | Should -HaveParameter CheckpointName -Mandatory -Type String + It "Has a parameter for the CheckpointName that is Mandatory" { + Get-Command $script:function | Should -HaveParameter CheckpointName -Mandatory -Type String } - it "Throws and error when an empty or null checkpointName is specified" { - {Checkpoint-ZertoVpg -vpgName "MyVpg" -checkpointName ""} | Should -Throw - {Checkpoint-ZertoVpg -vpgName "MyVpg" -checkpointName $null} | Should -Throw + It "Throws and error when an empty or null checkpointName is specified" { + { Checkpoint-ZertoVpg -vpgName "MyVpg" -checkpointName "" } | Should -Throw + { Checkpoint-ZertoVpg -vpgName "MyVpg" -checkpointName $null } | Should -Throw } - it "Throws an error when an empty or null vpgName is specified" { - {Checkpoint-ZertoVpg -vpgName "" -checkpointName "MyCheckPoint"} | Should -Throw - {Checkpoint-ZertoVpg -vpgName $null -checkpointName "MyCheckPoint"} | Should -Throw + It "Throws an error when an empty or null vpgName is specified" { + { Checkpoint-ZertoVpg -vpgName "" -checkpointName "MyCheckPoint" } | Should -Throw + { Checkpoint-ZertoVpg -vpgName $null -checkpointName "MyCheckPoint" } | Should -Throw } - it "Does not support 'SupportsShouldProcess'" { - Get-Command $file.BaseName | Should -Not -HaveParameter WhatIf - Get-Command $file.BaseName | Should -Not -HaveParameter Confirm - $file | Should -Not -FileContentMatch 'SupportsShouldProcess' - $file | Should -Not -FileContentMatch '\$PSCmdlet\.ShouldProcess\(.+\)' + It "Does not support 'SupportsShouldProcess'" { + Get-Command $script:function | Should -Not -HaveParameter WhatIf + Get-Command $script:function | Should -Not -HaveParameter Confirm + $script:ScriptBlock | Should -not -match 'SupportsShouldProcess' + $script:ScriptBlock | Should -not -match '\$PSCmdlet\.ShouldProcess\(.+\)' } } - Context "$($file.BaseName)::Function Unit Tests" { + Context "$($script:function)::Function Unit Tests" { - it "should return a not null or empty string" { + It "should return a not null or empty string" { $results = Checkpoint-ZertoVpg -vpgName "MyVpg" -checkpointName "My Checkpoint Name" - $results | should -not -BeNullOrEmpty - $results | should -BeOfType "String" - $results | should -BeExactly "3b687246-ac63-40da-9a59-b99863769eb0.928a122b-1763-4664-ad37-cc00bb883f2f" + $results | Should -not -BeNullOrEmpty + $results | Should -BeOfType "String" + $results | Should -BeExactly "7e79035e-fb8c-47fe-815c-12ddd41708e6.3e4cdd0d-1064-4022-921f-6265ad6d335a" + } + It "does not return anything when a invalid VPG is defined" { + $results = Checkpoint-ZertoVpg -vpgName "DoesNotExist" -checkpointName "My Checkpoint Name" + $results | Should -Be "Cannot find VPG named DoesNotExist. Please check the name and try again." } - Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest - Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Get-ZertoVpg + Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -Exactly 2 + Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Get-ZertoVpg -Exactly 1 } -} \ No newline at end of file +} diff --git a/Tests/Public/Connect-ZertoAnalytics.Tests.ps1 b/Tests/Public/Connect-ZertoAnalytics.Tests.ps1 index 50d2f9d..fcfd65a 100644 --- a/Tests/Public/Connect-ZertoAnalytics.Tests.ps1 +++ b/Tests/Public/Connect-ZertoAnalytics.Tests.ps1 @@ -1,19 +1,72 @@ #Requires -Modules Pester -$moduleFileName = "ZertoApiWrapper.psd1" -$here = (Split-Path -Parent $MyInvocation.MyCommand.Path).Replace("Tests", "ZertoApiWrapper") -$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") -$file = Get-ChildItem "$here\$sut" -$modulePath = $here -replace "Public", "" -$moduleFile = Get-ChildItem "$modulePath\$moduleFileName" -Get-Module -Name ZertoApiWrapper | Remove-Module -Force -Import-Module $moduleFile -Force +$global:here = (Split-Path -Parent $MyInvocation.MyCommand.Path) +$script:function = ((Split-Path -leaf $MyInvocation.MyCommand.Path).Split('.'))[0] -Describe $file.BaseName -Tag 'Unit' { - - It "is valid Powershell (Has no script errors)" { - $contents = Get-Content -Path $file -ErrorAction Stop - $errors = $null - $null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors) - $errors | Should -HaveCount 0 +Describe $script:function -Tag 'Unit', 'Source', 'Built' { + BeforeAll { + $script:ScriptBlock = (Get-Command $script:function).ScriptBlock } -} \ No newline at end of file + + Context "$($script:function)::Parameter Unit Tests" { + It "Has a parameter for the Required Credentials that is Mandatory" { + Get-Command $script:function | Should -HaveParameter credential -Mandatory -Type PSCredential + } + } + + + + Context "$($script:function)::Function Unit Tests" { + + InModuleScope -ModuleName ZertoApiWrapper { + Mock -CommandName Invoke-ZARestRequest { + return (Get-Content "$global:here\Mocks\ZAToken.json"-Raw | ConvertFrom-Json) + } + + $password = 'NotARealPassword' | ConvertTo-SecureString -AsPlainText -Force + $cred = New-Object pscredential('NotARealUser', $password) + + $results = Connect-ZertoAnalytics -credential $cred + + It "Creates a Script Level Hashtable Variable for the ZertoAnalytics Headers" { + $script:zaHeaders | Should -BeOfType Hashtable + } + + It "the ZertoAnalytics Headers variable contains 2 items" { + $script:zaHeaders.keys | Should -HaveCount 2 + } + + It "the ZertoAnalytics Headers variable has an 'Accept' key" { + $script:zaHeaders.keys | Should -Contain 'Accept' + } + + It "thh ZertoAnalytics headers variable 'Accept' key should be JSON" { + $script:zaHeaders['Accept'] | Should -match 'application/json' + } + + It "the ZertoAnalytics Headers variable has an 'Authorization' key" { + $script:zaHeaders.keys | Should -Contain 'Authorization' + } + + It "the ZertoAnalytics Headers variable 'Authorization' key should start with 'Bearer'" { + $script:zaHeaders['Authorization'] | Should -Match '^Bearer ' + } + + It "Creates a Script Level Variable for the LastActionTime" { + $script:zaLastActionTime | Should -BeOfType Long + } + + It "LastActionTime Variable should be in the past" { + $script:zaLastActionTime | Should -BeLessThan (Get-Date).Ticks + } + + It "Returns Header Information" { + $results | Should -not -BeNullOrEmpty + $results['Authorization'] | Should -MatchExactly 'Bearer N074r34l70k3n' + $results['Accept'] | Should -Match 'application/json' + } + } + + Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Invoke-ZARestRequest -Exactly 1 + } + +} diff --git a/Tests/Public/Connect-ZertoServer.Tests.ps1 b/Tests/Public/Connect-ZertoServer.Tests.ps1 index 59c5e8b..de54536 100644 --- a/Tests/Public/Connect-ZertoServer.Tests.ps1 +++ b/Tests/Public/Connect-ZertoServer.Tests.ps1 @@ -1,128 +1,94 @@ #Requires -Modules Pester -$moduleFileName = "ZertoApiWrapper.psd1" -$here = (Split-Path -Parent $MyInvocation.MyCommand.Path).Replace("Tests", "ZertoApiWrapper") -$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") -$file = Get-ChildItem "$here\$sut" -$modulePath = $here -replace "Public", "" -$moduleFile = Get-ChildItem "$modulePath\$moduleFileName" -Get-Module -Name ZertoApiWrapper | Remove-Module -Force -Import-Module $moduleFile -Force -$userName = "zerto\build" -$password = ConvertTo-SecureString -String "ZertoBuild" -AsPlainText -Force -$credential = New-Object -TypeName System.Management.Automation.PSCredential($userName, $password) -$Server = "192.168.1.100" -$zertoPort = "7669" +$global:here = (Split-Path -Parent $MyInvocation.MyCommand.Path) +$script:function = ((Split-Path -leaf $MyInvocation.MyCommand.Path).Split('.'))[0] -Describe $file.BaseName -Tag Unit { - - It "is valid Powershell (Has no script errors)" { - $contents = Get-Content -Path $file -ErrorAction Stop - $errors = $null - $null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors) - $errors | Should -HaveCount 0 +Describe $script:function -Tag 'Unit', 'Source', 'Built' { + BeforeAll { + $script:ScriptBlock = (Get-Command $script:function).ScriptBlock } - Context "$($file.BaseName)::Parameter Unit Tests" { + Context "$($script:function)::Parameter Unit Tests" { - it "server vairable has a mandatory String parameter" { - Get-Command $file.BaseName | Should -HaveParameter zertoserver -Mandatory -Type String + It "server vairable has a mandatory String parameter" { + Get-Command $script:function | Should -HaveParameter zertoserver -Mandatory -Type String } - it "server variable does not accecpt an empty or null input" { - {Connect-ZertoServer -zertoServer $null -credential $credential} | Should -Throw - {Connect-ZertoServer -zertoServer "" -credential $credential} | Should -Throw + It "server variable does not accecpt an empty or null input" { + { Connect-ZertoServer -zertoServer $null -credential $credential } | Should -Throw + { Connect-ZertoServer -zertoServer "" -credential $credential } | Should -Throw } - it "port variable has a non-mandatory String parameter" { - Get-Command $file.BaseName | Should -HaveParameter zertoPort -Not -Mandatory - Get-Command $file.BaseName | Should -HaveParameter zertoPort -Type String - Get-Command $file.BaseName | Should -HaveParameter zertoPort -DefaultValue "9669" + It "port variable has a non-mandatory String parameter" { + Get-Command $script:function | Should -HaveParameter zertoPort -Not -Mandatory + Get-Command $script:function | Should -HaveParameter zertoPort -Type String + Get-Command $script:function | Should -HaveParameter zertoPort -DefaultValue "9669" } - it "port variable does not accecpt an empty or null input" { - {Connect-ZertoServer -zertoServer "192.168.1.100" -zertoPort "" -credential $credential} | Should -Throw - {Connect-ZertoServer -zertoServer "192.168.1.100" -zertoPort $null -credential $credential} | Should -Throw + It "port variable does not accecpt an empty or null input" { + { Connect-ZertoServer -zertoServer "192.168.1.100" -zertoPort "" -credential $credential } | Should -Throw + { Connect-ZertoServer -zertoServer "192.168.1.100" -zertoPort $null -credential $credential } | Should -Throw } - it "port variable should fall between 1024 and 65535" { - {Connect-ZertoServer -zertoServer $Server -zertoPort 1023 -credential $credential} | Should -Throw - {Connect-ZertoServer -zertoServer $Server -zertoPort 65536 -credential $credential} | Should -Throw - {Connect-ZertoServer -zertoServer $Server -zertoPort 0 -credential $credential} | Should -Throw - {Connect-ZertoServer -zertoServer $Server -zertoPort -1 -credential $credential} | Should -Throw + It "port variable should fall between 1024 and 65535" { + { Connect-ZertoServer -zertoServer $Server -zertoPort 1023 -credential $credential } | Should -Throw + { Connect-ZertoServer -zertoServer $Server -zertoPort 65536 -credential $credential } | Should -Throw + { Connect-ZertoServer -zertoServer $Server -zertoPort 0 -credential $credential } | Should -Throw + { Connect-ZertoServer -zertoServer $Server -zertoPort -1 -credential $credential } | Should -Throw } - it "has a mandatory PSCredential parameter for the credential vairable" { - Get-Command $file.BaseName | Should -HaveParameter credential -Mandatory -Type PSCredential + It "has a mandatory PSCredential parameter for the credential vairable" { + Get-Command $script:function | Should -HaveParameter credential -Mandatory -Type PSCredential } - it "should require a PSCredentialObject for the credentials" { - {Connect-ZertoServer -zertoServer -credential "MyUsername"} | Should -Throw - {Connect-ZertoServer -zertoServer -credential 1234} | Should -Throw - {Connect-ZertoServer -zertoServer -credential $(@{Username = "zerto\build"; Password = 'SecureString'})} | Should -Throw + It "should require a PSCredentialObject for the credentials" { + { Connect-ZertoServer -zertoServer -credential "MyUsername" } | Should -Throw + { Connect-ZertoServer -zertoServer -credential 1234 } | Should -Throw + { Connect-ZertoServer -zertoServer -credential $(@{Username = "zerto\build"; Password = 'SecureString' }) } | Should -Throw } } - InModuleScope ZertoApiWrapper { - Context "$($file.BaseName)::InModuleScope Function Unit Tests" { + InModuleScope -ModuleName ZertoApiWrapper { + Mock -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest { + # Attempted to Mock this per the Mock Below and it blew up. Auth Headers Returns a Dictionary + # and does not index the same way when imported from a JSON file. Need addtional investigation. + $xZertoSession = @("e34da0b0-4bc2-4cda-b316-0384e35bdca5") + $Headers = @{'x-zerto-session' = $xZertoSession } + $results = @{'Headers' = $Headers } + return $results + } - $server = '192.168.1.100' - $userName = "zerto\build" - $password = ConvertTo-SecureString -String "ZertoBuild" -AsPlainText -Force - $credential = New-Object -TypeName System.Management.Automation.PSCredential($userName, $password) + Mock -ModuleName ZertoApiWrapper -CommandName Get-ZertoLocalSite { + return (Get-Content -Path "$global:here\Mocks\LocalSiteInfo.json" -Raw | ConvertFrom-Json) + } + Context "$($script:function)::InModuleScope Function Unit Tests" { - Mock -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest { - $xZertoSession = @("7ecf544d-e7ed-4108-86f3-fb355c51cdfa") - $Headers = @{'x-zerto-session' = $xZertoSession} - $results = @{'Headers' = $Headers} - return $results + BeforeAll { + $server = '192.168.1.100' + $password = ConvertTo-SecureString -String "NotARealPassword" -AsPlainText -Force + $credential = New-Object pscredential('NotARealUser', $password) + $now = $(Get-Date).ticks + Connect-ZertoServer -zertoServer $server -credential $credential } - Mock -ModuleName ZertoApiWrapper -CommandName Get-ZertoLocalSite { - $results = @{ - BandwidthThrottlingInMBs = -1 - ContactEmail = "vSphere-Site01@zerto.com" - ContactName = "vSphere-Site01@zerto.com" - ContactPhone = "066-6666666" - IpAddress = "192.168.200.1" - IsReplicationToSelfEnabled = $True - Link = @{ - href = "https://192.168.24.1:7669/v1/localsite" - identifier = "928a122b-1763-4664-ad37-cc00bb883f2f" - rel = $null - type = "LocalSiteApi" - } - Location = "vSphere-Site01" - SiteName = "vSphere-Site01 at Zerto" - SiteType = "VCenter" - UtcOffsetInMinutes = -240 - Version = "7.0.0" - SiteIdentifier = "928a122b-1763-4664-ad37-cc00bb883f2f" - } - return $results - } - - $now = $(Get-Date).ticks - Connect-ZertoServer -zertoServer $server -credential $credential - - it "Module Scope zvmServer variable tests" { + It "Module Scope zvmServer variable tests" { $script:zvmServer | Should -Not -BeNullOrEmpty $script:zvmServer | Should -Be $server } - it "Module Scope zvmPort variable tests" { + It "Module Scope zvmPort variable tests" { $script:zvmPort | Should -Not -BeNullOrEmpty $script:zvmPort | Should -Be '9669' } - it "Module Scope zvmLastAction variable tests" { + It "Module Scope zvmLastAction variable tests" { $script:zvmLastAction | Should -Not -BeNullOrEmpty $script:zvmLastAction | Should -BeGreaterOrEqual $now } - it "Module Scope zvmHeaders variable tests" { + It "Module Scope zvmHeaders variable tests" { $script:zvmHeaders | Should -Not -BeNullOrEmpty - $script:zvmHeaders | Should -BeOfType Hashtable + $script:zvmHeaders | Should -BeOfType PSCustomObject $script:zvmHeaders.keys.count | Should -BeExactly 2 $script:zvmHeaders.ContainsKey('x-zerto-session') | Should -BeTrue $script:zvmHeaders.ContainsKey('Accept') | Should -BeTrue @@ -130,74 +96,45 @@ Describe $file.BaseName -Tag Unit { $script:zvmHeaders['Accept'] | Should -BeOfType String } - it "Module Scope zvmLocalInfo variable tests" { + It "Module Scope zvmLocalInfo variable tests" { $script:zvmLocalInfo | Should -Not -BeNullOrEmpty - $script:zvmLocalInfo | Should -BeOfType Hashtable - $script:zvmLocalInfo['SiteIdentifier'] | Should -BeOfType String - $script:zvmLocalInfo.ContainsKey('SiteIdentifier') | Should -BeTrue - $script:zvmLocalInfo['SiteIdentifier'] | Should -BeOfType String + $script:zvmLocalInfo | Should -BeOfType PSCustomObject + $script:zvmLocalInfo.SiteIdentifier | Should -BeOfType String } $headers = Connect-ZertoServer -zertoServer $Server -credential $credential -returnHeaders - it "returns a Hashtable with 2 keys" { + It "returns a Hashtable with 2 keys" { $headers | Should -BeOfType Hashtable - $headers.keys.count | should be 2 + $headers.keys.count | Should be 2 } - it "return value has a key called 'x-zerto-session'" { - $headers.ContainsKey('x-zerto-session') | should be $true + It "return value has a key called 'x-zerto-session'" { + $headers.ContainsKey('x-zerto-session') | Should be $true } - it "return key 'x-zerto-session' value should be a string" { - $headers['x-zerto-session'] | should -BeOfType "String" - $headers['x-zerto-session'] | Should -BeExactly "7ecf544d-e7ed-4108-86f3-fb355c51cdfa" + It "return key 'x-zerto-session' value should be a string" { + $headers['x-zerto-session'] | Should -BeOfType "String" + $headers['x-zerto-session'] | Should -BeExactly "e34da0b0-4bc2-4cda-b316-0384e35bdca5" } - it "return value has a key called 'accept'" { - $headers.ContainsKey('accept') | should be $true + It "return value has a key called 'accept'" { + $headers.ContainsKey('accept') | Should be $true } - it "return key 'accept' value should be 'application/json'" { - $headers['accept'] | should be 'application/json' + It "return key 'accept' value should be 'application/json'" { + $headers['accept'] | Should be 'application/json' } - it "should not require a port to be specified" { + It "should not require a port to be specified" { Connect-ZertoServer -zertoServer $Server -credential $credential } - it "returns null when -ReturnHeaders is not used" { + It "returns null when -ReturnHeaders is not used" { Connect-ZertoServer -zertoServer $Server -credential $credential | Should -BeNullOrEmpty } - Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest - Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Get-ZertoLocalSite + Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -Exactly 4 + Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Get-ZertoLocalSite -Exactly 4 } } } -<# -Describe "Connect-ZertoServer" -Tag Integration { - it "file should exist" { - "$filePath\$fileName" | should exist - } - it "has a function called Connect-ZertoServer" { - get-command Connect-ZertoServer | should be $true - } - $headers = Connect-ZertoServer -zertoServer $Server -zertoPort $zertoPort -credential $credential -returnHeaders - it "returns a Hashtable with 2 keys" { - $headers.keys.count | should be 2 - } - it "return value has a key called 'x-zerto-session'" { - $headers.ContainsKey('x-zerto-session') | should be $true - } - it "return key 'x-zerto-session' value should be a string" { - $headers['x-zerto-session'].gettype().name | should be "String" - } - it "return value has a key called 'accept'" { - $headers.ContainsKey('accept') | should be $true - } - it "return key 'accept' value should be 'application/json'" { - $headers['accept'] | should be 'application/json' - } - Disconnect-ZertoServer -} - #> diff --git a/Tests/Public/Disconnect-ZertoServer.Tests.ps1 b/Tests/Public/Disconnect-ZertoServer.Tests.ps1 index 5be178d..9b0c57b 100644 --- a/Tests/Public/Disconnect-ZertoServer.Tests.ps1 +++ b/Tests/Public/Disconnect-ZertoServer.Tests.ps1 @@ -1,37 +1,40 @@ #Requires -Modules Pester -$moduleFileName = "ZertoApiWrapper.psd1" -$here = (Split-Path -Parent $MyInvocation.MyCommand.Path).Replace("Tests", "ZertoApiWrapper") -$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") -$file = Get-ChildItem "$here\$sut" -$modulePath = $here -replace "Public", "" -$moduleFile = Get-ChildItem "$modulePath\$moduleFileName" -Get-Module -Name ZertoApiWrapper | Remove-Module -Force -Import-Module $moduleFile -Force - -Describe $file.BaseName -Tag 'Unit' { - Mock -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest { - $null - } - Mock -ModuleName ZertoApiWrapper -CommandName Remove-Variable { +$global:here = (Split-Path -Parent $MyInvocation.MyCommand.Path) +$script:function = ((Split-Path -leaf $MyInvocation.MyCommand.Path).Split('.'))[0] +Describe $script:function -Tag 'Unit', 'Source', 'Built' { + BeforeAll { + $script:ScriptBlock = (Get-Command $script:function).ScriptBlock } - It "is valid Powershell (Has no script errors)" { - $contents = Get-Content -Path $file -ErrorAction Stop - $errors = $null - $null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors) - $errors | Should -HaveCount 0 - } - - Context "$($file.BaseName)::Parameter Unit Tests" { + Context "$($script:function)::Parameter Unit Tests" { it "Does not take any parameters" { (get-command disconnect-zertoserver).parameters.count | Should -BeExactly 11 } } - Context "$($file.BaseName)::Function Unit Tests" { - it "Does not return anything" { - Disconnect-ZertoServer | Should -BeNullOrEmpty + Context "$($script:function)::Function Unit Tests" { + InModuleScope -ModuleName ZertoApiWrapper { + Mock -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest { + # Attempted to Mock this per the Mock Below and it blew up. Auth Headers Returns a Dictionary + # and does not index the same way when imported from a JSON file. Need addtional investigation. + $xZertoSession = @("e34da0b0-4bc2-4cda-b316-0384e35bdca5") + $Headers = @{'x-zerto-session' = $xZertoSession } + $results = @{'Headers' = $Headers } + return $results + } + + Mock -ModuleName ZertoApiWrapper -CommandName Get-ZertoLocalSite { + return (Get-Content -Path "$global:here\Mocks\LocalSiteInfo.json" -Raw | ConvertFrom-Json) + } + + BeforeAll { + Connect-ZertoServer + } + + It "Does not return anything" { + Disconnect-ZertoServer | Should -BeNullOrEmpty + } } } }