Skip to content

Commit

Permalink
Merge pull request #114 from alagoutte/ping
Browse files Browse the repository at this point in the history
Add Test-ArubaSWPing cmdlet
  • Loading branch information
alagoutte authored Nov 21, 2021
2 parents 0493236 + eae3a0e commit 945f13b
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 2 deletions.
73 changes: 73 additions & 0 deletions PowerArubaSW/Public/Ping.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#
# Copyright 2018-2020, Alexis La Goutte <alexis dot lagoutte at gmail dot com>
#
# 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 {
}
}
6 changes: 4 additions & 2 deletions Tests/common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions Tests/credential.example.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
54 changes: 54 additions & 0 deletions Tests/integration/Ping.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#
# Copyright 2018-2020, Alexis La Goutte <alexis dot lagoutte at gmail dot com>
#
# 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

0 comments on commit 945f13b

Please sign in to comment.