Skip to content

Commit cd722de

Browse files
authored
Merge pull request #78 from dbroeglin/feature/it-tests
Fixed URI protocol/scheme scope (and added integration tests)
2 parents bc8266e + 339698c commit cd722de

12 files changed

+199
-20
lines changed

ITTests/Simple.ITTests.ps1

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
Import-Module Pester
2+
3+
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
4+
Import-Module -Force $here\..\Netscaler\Netscaler.psd1
5+
. $here\TestSupport.ps1
6+
7+
Describe "Netscaler Connection" {
8+
Context "not connected" {
9+
It "should disconnect implicit session" {
10+
$Session = Connect-TestNetscaler
11+
12+
Get-NSHostname
13+
Disconnect-Netscaler
14+
{ Get-NSHostname } | Should Throw "Unauthorized"
15+
}
16+
17+
It "should disconnect explicit session" {
18+
$Session = Connect-TestNetscaler
19+
20+
Get-NSHostname -Session $Session
21+
Disconnect-Netscaler -Session $Session
22+
{ Get-NSHostname -Session $Session } | Should Throw "Unauthorized"
23+
}
24+
}
25+
26+
Context "connection" {
27+
}
28+
}
29+
30+
Describe "Netscaler Get-*" {
31+
$Session = Connect-TestNetscaler
32+
33+
It "should get a certificate" {
34+
$Cert = Get-NSSSLCertificate -Name "ns-server-certificate"
35+
36+
$Cert | Should Not BeNullOrEmpty
37+
$Cert.certkey | Should Be "ns-server-certificate"
38+
}
39+
40+
It "should list certificates" {
41+
$Cert = Get-NSSSLCertificate
42+
43+
$Cert | Should Not BeNullOrEmpty
44+
$Cert.certkey | Should Match "ns-server-certificate|ns-sftrust-certificate"
45+
}
46+
}
47+
48+
Describe "Netscaler" {
49+
$Session = Connect-TestNetscaler
50+
51+
It "should add a LB server" {
52+
New-NSLBServer -Name 'srv-test' -IPAddress 1.2.3.4
53+
54+
Compare-NSConfig $OldConf | Should Match "=> add server srv-test 1.2.3.4"
55+
}
56+
57+
It "should add features" {
58+
Enable-NSFeature -Force -Name "aaa", "lb", "rewrite", "ssl"
59+
60+
Compare-NSConfig $OldConf | Should Match "=> enable ns feature LB SSL AAA REWRITE"
61+
}
62+
63+
BeforeEach { $OldConf = Get-NSConfig }
64+
AfterEach {
65+
Clear-NSConfig -Force -Level Full
66+
# Web Logging and Surge Protection are not disabled by a config clear...
67+
"wl", "sp" | Disable-NSFeature -Force
68+
}
69+
}

ITTests/TestSupport.ps1

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
$TestNsip = $(if ($env:TEST_NSIP) { $env:TEST_NSIP } else { "172.16.124.10" })
2+
$TestUsername = "nsroot"
3+
$TestPassword = "nsroot"
4+
5+
function Connect-TestNetscaler {
6+
$SecurePassword = ConvertTo-SecureString $TestPassword -AsPlainText -Force
7+
$Credential = New-Object System.Management.Automation.PSCredential ($TestUsername, $SecurePassword)
8+
Connect-Netscaler -Hostname $TestNsip -Credential $Credential -PassThru
9+
}
10+
11+
function Compare-NSConfig($Old, $New = $(Get-NSConfig)) {
12+
$Old = $Old | Where-Object { Test-IsMutatingConfigLines $_ }
13+
$New = $New | Where-Object { Test-IsMutatingConfigLines $_ }
14+
Compare-Object $Old $New | ForEach-Object {
15+
"{0} {1}" -f $_.SideIndicator, $_.InputObject
16+
}
17+
}
18+
19+
function Test-IsMutatingConfigLines($line) {
20+
!($_ -match "^set (ns encryptionParams|cluster rsskey|ns rpcNode)")
21+
}

NetScaler/NetScaler.psm1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ limitations under the License.
1818
Set-StrictMode -Version 3
1919

2020
$script:session = $null
21-
$script:protocol = $null
2221

2322
# Load DLLs
2423
#$dll1 = Resolve-Path -Path "$PSScriptRoot\DLL\Newtonsoft.Json.dll"

NetScaler/Private/_InvokeNSRestApi.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ function _InvokeNSRestApi {
6363
)
6464

6565
if ($Stat) {
66-
$uri = "$($Script:protocol)://$($Session.Endpoint)/nitro/v1/stat/$Type"
66+
$uri = $Session.CreateUri("stat", $Type)
6767
} else {
68-
$uri = "$($Script:protocol)://$($Session.Endpoint)/nitro/v1/config/$Type"
68+
$uri = $Session.CreateUri("config", $Type)
6969
}
7070

7171
if (-not [string]::IsNullOrEmpty($Resource)) {

NetScaler/Public/Add-NSIPResource.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ function Add-NSIPResource {
7474
#>
7575
[cmdletbinding(SupportsShouldProcess = $true, ConfirmImpact='Low')]
7676
param(
77-
[parameter(Mandatory)]
7877
$Session = $script:session,
7978

8079
[parameter(Mandatory)]

NetScaler/Public/Connect-NetScaler.ps1

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,28 @@ function Connect-NetScaler {
7171
$endpoint = $Hostname
7272
}
7373

74-
if ($PSBoundParameters.ContainsKey('Https')) {
75-
$Script:protocol = 'https'
74+
if ($Https) {
75+
$scheme = 'https'
7676
} else {
77-
$script:protocol = 'http'
77+
$scheme = 'http'
7878
}
7979

80-
Write-Verbose -Message "Connecting to $endpoint..."
80+
81+
$session = New-Object -TypeName PSObject
82+
$session | Add-Member -NotePropertyName Endpoint -NotePropertyValue $endpoint -TypeName String
83+
$session | Add-Member -NotePropertyName Scheme -NotePropertyValue $scheme -TypeName String
84+
$session | Add-Member -Name Uri -MemberType ScriptProperty -Value {
85+
"$($this.scheme)://$($this.endpoint)/nitro/v1"
86+
}
87+
$session | Add-Member -Name CreateUri -MemberType ScriptMethod -Value {
88+
Param(
89+
[String]$service,
90+
[String]$type
91+
)
92+
"$($this.Uri)/$service/$type"
93+
}
94+
95+
Write-Verbose -Message "Connecting to $($session.Uri)..."
8196

8297
try {
8398
$login = @{
@@ -91,7 +106,7 @@ function Connect-NetScaler {
91106

92107
$saveSession = @{}
93108
$params = @{
94-
Uri = "$($Script:protocol)://$endpoint/nitro/v1/config/login"
109+
Uri = "$($session.Uri)/config/login"
95110
Method = 'POST'
96111
Body = $loginJson
97112
SessionVariable = 'saveSession'
@@ -108,12 +123,10 @@ function Connect-NetScaler {
108123
throw $_
109124
}
110125

111-
$session = New-Object -TypeName PSObject
112-
$session | Add-Member -NotePropertyName Endpoint -NotePropertyValue $endpoint -TypeName String
113-
$session | Add-Member -NotePropertyName WebSession -NotePropertyValue $saveSession -TypeName Microsoft.PowerShell.Commands.WebRequestSession
114-
126+
$session | Add-Member -NotePropertyName WebSession -NotePropertyValue $saveSession -TypeName Microsoft.PowerShell.Commands.WebRequestSession
115127
$script:session = $session
116128

129+
117130
if ($PSBoundParameters.ContainsKey('PassThru')) {
118131
return $session
119132
}

NetScaler/Public/Disable-NSFeature.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ function Disable-NSFeature {
4848
param(
4949
$Session = $script:session,
5050

51-
[parameter(Mandatory,ValueFromPipeline = $true, ValueFromPipelineByPropertyName)]
52-
[string[]]$Name = (Read-Host -Prompt 'NetScaler feature'),
51+
[parameter(Mandatory, ValueFromPipeline = $true, ValueFromPipelineByPropertyName)]
52+
[string[]]$Name,
5353

5454
[switch]$Force,
5555

NetScaler/Public/Disconnect-NetScaler.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function Disconnect-NetScaler {
4545
try {
4646
Write-Verbose -Message 'Logging out of NetScaler'
4747
$params = @{
48-
Uri = "$($script:protocol)://$($Session.Endpoint)/nitro/v1/config/logout"
48+
Uri = $Session.CreateUri("config", "logout")
4949
Body = ConvertTo-Json -InputObject @{logout = @{}}
5050
Method = 'POST'
5151
ContentType = 'application/json'

NetScaler/Public/Restart-NetScaler.ps1

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ function Restart-NetScaler {
6767
#>
6868
[cmdletbinding(SupportsShouldProcess = $true, ConfirmImpact='High')]
6969
param(
70-
[parameter(Mandatory)]
7170
$Session = $script:session,
7271

7372
[switch]$SaveConfig,
@@ -120,7 +119,7 @@ function Restart-NetScaler {
120119
$response = $null
121120
try {
122121
$params = @{
123-
Uri = "$($script:protocol)://$ip/nitro/v1/config"
122+
Uri = $Session.CreateUri("config")
124123
Method = 'GET'
125124
ContentType = 'application/json'
126125
ErrorVariable = 'connectTestError'

NetScaler/Public/Set-NSHostname.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ function Set-NSHostname {
4646
#>
4747
[cmdletbinding(SupportsShouldProcess = $true, ConfirmImpact='high')]
4848
param(
49-
[parameter(Mandatory)]
5049
$Session = $script:session,
5150

5251
[parameter(Mandatory)]

0 commit comments

Comments
 (0)