Skip to content

Commit bc3f7cf

Browse files
BenDavMSbganapa
authored andcommitted
add pester tests
1 parent f00593a commit bc3f7cf

File tree

9 files changed

+863
-0
lines changed

9 files changed

+863
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
$loadEnvPath = Join-Path $PSScriptRoot 'loadEnv.ps1'
2+
if (-Not (Test-Path -Path $loadEnvPath)) {
3+
$loadEnvPath = Join-Path $PSScriptRoot '..\loadEnv.ps1'
4+
}
5+
. ($loadEnvPath)
6+
$TestRecordingFile = Join-Path $PSScriptRoot 'Close-AzsAlert.Recording.json'
7+
$currentPath = $PSScriptRoot
8+
while(-not $mockingPath) {
9+
$mockingPath = Get-ChildItem -Path $currentPath -Recurse -Include 'HttpPipelineMocking.ps1' -File
10+
$currentPath = Split-Path -Path $currentPath -Parent
11+
}
12+
. ($mockingPath | Select-Object -First 1).FullName
13+
14+
InModuleScope Azs.InfrastructureInsights.Admin {
15+
16+
Describe "Alerts" -Tags @('Alert', 'InfrastructureInsightsAdmin') {
17+
18+
it "TestCloseAlert" -Skip:$('TestCloseAlert' -in $global:SkippedTests) {
19+
20+
$global:TestName = 'TestCloseAlert'
21+
22+
$Alerts = Get-AzsAlert -ResourceGroupName $global:ResourceGroupName -Location $global:location
23+
24+
$Alerts | Should Not Be $null
25+
26+
foreach ($Alert in $Alerts) {
27+
28+
$Alert | Should not be $null
29+
$Alert.State | Should not be $null
30+
31+
if ($Alert.State -eq "Active") {
32+
33+
$Alert | Close-AzsAlert -Location $global:location -User "foobar"
34+
35+
return
36+
}
37+
}
38+
}
39+
}
40+
}
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
$loadEnvPath = Join-Path $PSScriptRoot 'loadEnv.ps1'
2+
if (-Not (Test-Path -Path $loadEnvPath)) {
3+
$loadEnvPath = Join-Path $PSScriptRoot '..\loadEnv.ps1'
4+
}
5+
. ($loadEnvPath)
6+
$TestRecordingFile = Join-Path $PSScriptRoot 'Get-AzsAlert.Recording.json'
7+
$currentPath = $PSScriptRoot
8+
9+
while(-not $mockingPath) {
10+
$mockingPath = Get-ChildItem -Path $currentPath -Recurse -Include 'HttpPipelineMocking.ps1' -File
11+
$currentPath = Split-Path -Path $currentPath -Parent
12+
}
13+
. ($mockingPath | Select-Object -First 1).FullName
14+
15+
InModuleScope Azs.InfrastructureInsights.Admin {
16+
17+
Describe "Alerts" -Tags @('Alert', 'InfrastructureInsightsAdmin') {
18+
19+
BeforeEach {
20+
21+
function ValidateAlert {
22+
23+
param(
24+
25+
[Parameter(Mandatory = $true)]
26+
27+
$Alert
28+
29+
)
30+
31+
$Alert | Should Not Be $null
32+
33+
# Resource
34+
$Alert.Id | Should Not Be $null
35+
$Alert.Location | Should Not Be $null
36+
$Alert.Name | Should Not Be $null
37+
$Alert.Type | Should Not Be $null
38+
39+
# Alert
40+
$Alert.AlertId | Should Not Be $null
41+
$Alert.AlertProperty | Should Not Be $null
42+
$Alert.CreatedTimestamp | Should Not Be $null
43+
$Alert.Description | Should Not Be $null
44+
$Alert.FaultTypeId | Should Not Be $null
45+
$Alert.ImpactedResourceDisplayName | Should Not Be $null
46+
$Alert.ImpactedResourceId | Should Not Be $null
47+
$Alert.LastUpdatedTimestamp | Should Not Be $null
48+
$Alert.Remediation | Should Not Be $null
49+
$Alert.ResourceProviderRegistrationId | Should Not Be $null
50+
$Alert.Severity | Should Not Be $null
51+
$Alert.State | Should Not Be $null
52+
$Alert.Title | Should Not Be $null
53+
}
54+
55+
function AssertAlertsAreSame {
56+
param(
57+
[Parameter(Mandatory = $true)]
58+
$Expected,
59+
60+
[Parameter(Mandatory = $true)]
61+
$Found
62+
)
63+
64+
if ($Expected -eq $null) {
65+
$Found | Should Be $null
66+
}
67+
else {
68+
$Found | Should Not Be $null
69+
70+
# Resource
71+
$Found.Id | Should Be $Expected.Id
72+
$Found.Location | Should Be $Expected.Location
73+
$Found.Name | Should Be $Expected.Name
74+
$Found.Type | Should Be $Expected.Type
75+
76+
# Alert
77+
$Found.AlertId | Should Be $Expected.AlertId
78+
79+
if ($Expected.AlertProperties -eq $null) {
80+
81+
$Found.AlertProperties | Should Be $null
82+
83+
}
84+
85+
else {
86+
87+
$Found.AlertProperties | Should Not Be $null
88+
89+
$Found.AlertProperties.Count | Should Be $Expected.AlertProperties.Count
90+
91+
}
92+
93+
94+
95+
$Found.ClosedByUserAlias | Should Be $Expected.ClosedByUserAlias
96+
97+
$Found.ClosedTimestamp | Should Be $Expected.ClosedTimestamp
98+
99+
$Found.CreatedTimestamp | Should Be $Expected.CreatedTimestamp
100+
101+
102+
103+
if ($Expected.Description -eq $null) {
104+
105+
$Found.Description | Should Be $null
106+
107+
}
108+
109+
else {
110+
111+
$Found.Description | Should Not Be $null
112+
113+
$Found.Description.Count | Should Be $Expected.Description.Count
114+
115+
}
116+
117+
118+
119+
$Found.FaultId | Should Be $Expected.FaultId
120+
121+
$Found.FaultTypeId | Should Be $Expected.FaultTypeId
122+
123+
$Found.ImpactedResourceDisplayName | Should Be $Expected.ImpactedResourceDisplayName
124+
125+
$Found.ImpactedResourceId | Should Be $Expected.ImpactedResourceId
126+
127+
$Found.LastUpdatedTimestamp | Should Be $Expected.LastUpdatedTimestamp
128+
129+
130+
131+
if ($Expected.Remediation -eq $null) {
132+
133+
$Found.Remediation | Should Be $null
134+
135+
}
136+
137+
else {
138+
139+
$Found.Remediation | Should Not Be $null
140+
141+
$Found.Remediation.Count | Should Be $Expected.Remediation.Count
142+
143+
}
144+
145+
146+
147+
$Found.ResourceProviderRegistrationId | Should Be $Expected.ResourceProviderRegistrationId
148+
149+
$Found.ResourceRegistrationId | Should Be $Expected.ResourceRegistrationId
150+
151+
$Found.Severity | Should Be $Expected.Severity
152+
153+
$Found.State | Should Be $Expected.State
154+
155+
$Found.Title | Should Be $Expected.Title
156+
157+
158+
159+
}
160+
161+
}
162+
163+
}
164+
165+
it "TestListAlerts" -Skip:$('TestListAlerts' -in $global:SkippedTests) {
166+
167+
$global:TestName = 'TestListAlerts'
168+
169+
$Alerts = Get-AzsAlert -ResourceGroupName $global:ResourceGroupName -Location $global:Location
170+
171+
foreach ($Alert in $Alerts) {
172+
ValidateAlert -Alert $Alert
173+
}
174+
}
175+
176+
it "TestGetAlert" -Skip:$('TestGetAlert' -in $global:SkippedTests) {
177+
178+
$global:TestName = 'TestGetAlert'
179+
180+
$Regions = Get-AzsRegionHealth -ResourceGroupName $global:ResourceGroupName -Location $global:Location
181+
182+
foreach ($Region in $Regions) {
183+
184+
$Alerts = Get-AzsAlert -ResourceGroupName $global:ResourceGroupName -Location $Region.Name
185+
186+
foreach ($Alert in $Alerts) {
187+
188+
$retrieved = Get-AzsAlert -Location $Region.Name -Name $Alert.AlertId
189+
AssertAlertsAreSame -Expected $Alert -Found $retrieved
190+
return
191+
}
192+
}
193+
}
194+
195+
it "TestGetAllAlerts" -Skip:$('TestGetAllAlerts' -in $global:SkippedTests) {
196+
197+
$global:TestName = 'TestGetAllAlerts'
198+
199+
$Regions = Get-AzsRegionHealth -ResourceGroupName $global:ResourceGroupName -Location $global:Location
200+
201+
foreach ($Region in $Regions) {
202+
203+
$Alerts = Get-AzsAlert -ResourceGroupName $global:ResourceGroupName -Location $Region.Name
204+
205+
foreach ($Alert in $Alerts) {
206+
$retrieved = Get-AzsAlert -Location $Region.Name -Name $Alert.AlertId
207+
208+
AssertAlertsAreSame -Expected $Alert -Found $retrieved
209+
}
210+
}
211+
}
212+
213+
it "TestGetAllAlerts" -Skip:$('TestGetAllAlerts' -in $global:SkippedTests) {
214+
215+
$global:TestName = 'TestGetAllAlerts'
216+
217+
$Regions = Get-AzsRegionHealth -ResourceGroupName $global:ResourceGroupName -Location $global:Location
218+
219+
foreach ($Region in $Regions) {
220+
221+
$Alerts = Get-AzsAlert -ResourceGroupName $global:ResourceGroupName -Location $Region.Name
222+
223+
foreach ($Alert in $Alerts) {
224+
$retrieved = $Alert | Get-AzsAlert
225+
AssertAlertsAreSame -Expected $Alert -Found $retrieved
226+
227+
}
228+
}
229+
}
230+
}
231+
}

0 commit comments

Comments
 (0)