diff --git a/PowerArubaSW/Public/Ping.ps1 b/PowerArubaSW/Public/Ping.ps1 new file mode 100644 index 0000000..a6e460a --- /dev/null +++ b/PowerArubaSW/Public/Ping.ps1 @@ -0,0 +1,73 @@ +# +# Copyright 2018-2020, Alexis La Goutte +# +# SPDX-License-Identifier: Apache-2.0 +# + +function Test-ArubaSWPing { + + <# + .SYNOPSIS + Send a PING (ICMP) to a target + + .DESCRIPTION + Send a PING (ICMP) to a target + Get the status and latency + + .EXAMPLE + Test-ArubaSWPing -ipv4_address 192.2.0.1 + + Send a PING to IPv4 address 192.2.0.1 + + .EXAMPLE + Test-ArubaSWPing -hostname www.arubanetworks.com + + Send a PING to hostname www.arubanetworks.com + #> + + Param( + [Parameter (Mandatory = $false, ParameterSetName = "ipv4_address")] + [ipaddress]$ipv4_address, + [Parameter (Mandatory = $false, ParameterSetName = "hostname")] + [string]$hostname, + [Parameter (Mandatory = $False)] + [ValidateNotNullOrEmpty()] + [PSObject]$connection = $DefaultArubaSWConnection + ) + + Begin { + } + + Process { + + $uri = "rest/v4/ping" + $dest = New-Object -TypeName PSObject + + if ($PsBoundParameters.ContainsKey('ipv4_address')) { + + $ipv4 = New-Object -TypeName PSObject + $ipv4 | Add-Member -name "version" -MemberType NoteProperty -Value "IAV_IP_V4" + + $ipv4 | Add-Member -name "octets" -MemberType NoteProperty -Value $ipv4_address.ToString() + + $dest | Add-Member -name "ip_address" -Membertype NoteProperty -Value $ipv4 + } + elseif ($PsBoundParameters.ContainsKey('hostname')) { + $dest | Add-Member -name "hostname" -Membertype NoteProperty -Value $hostname + } + else { + throw "You need to use a parameter (-ipv4_address, -hostname)" + } + + $ping = New-Object -TypeName PSObject + $ping | Add-Member -name "destination" -Membertype NoteProperty -Value $dest + + $response = Invoke-ArubaSWWebRequest -method "POST" -body $ping -uri $uri -connection $connection + + $run = ($response | ConvertFrom-Json) + $run + } + + End { + } +} diff --git a/Tests/common.ps1 b/Tests/common.ps1 index 9fb70cf..83507fe 100644 --- a/Tests/common.ps1 +++ b/Tests/common.ps1 @@ -23,9 +23,11 @@ $pester_trunk_trk2 = "trk7" #Port trunk 2 name of Trunk test $pester_stp_port = 3 #Port Number of STP test $pester_cli_port = 3 #Port Number of CLI test $pester_poe_port = 4 #Port Number of PoE test +$pester_dns1 = "1.1.1.1" #DNS server 1 +$pester_dns2 = "8.8.8.8" #DNS server 2 - -if ("Desktop" -eq $PSVersionTable.PsEdition) { # -BeOfType is not same on PowerShell Core and Desktop (get int with Desktop and long with Core for number) +if ("Desktop" -eq $PSVersionTable.PsEdition) { + # -BeOfType is not same on PowerShell Core and Desktop (get int with Desktop and long with Core for number) $script:pester_longint = "int" } else { diff --git a/Tests/credential.example.ps1 b/Tests/credential.example.ps1 index 6b7661a..dd5099c 100644 --- a/Tests/credential.example.ps1 +++ b/Tests/credential.example.ps1 @@ -28,3 +28,5 @@ $script:httpOnly = $false #$script:pester_stp_port = 3 #$script:pester_cli_port = 3 #$script:pester_poe_port = 4 +#$script:pester_dns1 = "1.1.1.1" +#$script:pester_dns2 = "8.8.8.8" \ No newline at end of file diff --git a/Tests/integration/Ping.Tests.ps1 b/Tests/integration/Ping.Tests.ps1 new file mode 100644 index 0000000..0149da1 --- /dev/null +++ b/Tests/integration/Ping.Tests.ps1 @@ -0,0 +1,54 @@ +# +# Copyright 2018-2020, Alexis La Goutte +# +# SPDX-License-Identifier: Apache-2.0 +# + +. ../common.ps1 + +Describe "Ping (ipv4_address)" { + beforeAll { + $script:ipv4_gateway = (Get-ArubaSWSystem).default_gateway.octets + } + + It "Ping a valid IP(v4) address (Default Gateway:$script:ipv4_gateway)" { + $ping = Test-ArubaSWPing -ipv4_address $script:ipv4_gateway + $ping.result | Should -Be "PR_OK" + $ping.rtt_in_milliseconds | Should -Not -BeNullOrEmpty + } + + It "Ping a invalid IP(v4) address (0.0.0.0)" { + $ping = Test-ArubaSWPing -ipv4_address 0.0.0.0 + $ping.result | Should -Be "PR_INVALID_ADDRESS" + } +} + + +Describe "Ping (-hostname)" { + + BeforeAll { + #Always remove DNS Settings... + Remove-ArubaSWDns -noconfirm + } + + It "Ping a hostname (without DNS config)" { + $ping = Test-ArubaSWPing -hostname www.arubanetworks.com + $ping.result | Should -Be "PR_UNABLE_TO_RESOLVE_HOST_NAME" + } + + It "Ping a hostname (with DNS config $pester_dns1 / $pester_dns2)" { + #Configure DNS + Set-ArubaSWDns -mode Manual -server1 $pester_dns1 -server2 $pester_dns2 + $ping = Test-ArubaSWPing -hostname www.arubanetworks.com + $ping.result | Should -Be "PR_OK" + $ping.rtt_in_milliseconds | Should -Not -BeNullOrEmpty + } + + AfterAll { + #Always remove DNS Settings... + Remove-ArubaSWDns -noconfirm + } + +} + +Disconnect-ArubaSW -noconfirm