-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathProviderTests.ps1
189 lines (154 loc) · 7.46 KB
/
ProviderTests.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# ----------------------------------------------------------------------------------
#
# Copyright Microsoft Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------------------
<#
.SYNOPSIS
Tests registering and un-registering resource providers.
#>
function Test-AzureProvider
{
$defaultProviders = Get-AzureRmResourceProvider
Assert-True { $defaultProviders.Length -gt 0 }
$allProviders = Get-AzureRmResourceProvider -ListAvailable
Assert-True { $allProviders.Length -gt $defaultProviders.Length }
$ErrorActionPreference = "SilentlyContinue"
$Error.Clear()
$nonProviders = Get-AzureRmResourceProvider -Location "abc"
Assert-True { $Error[0].Contains("Provided location is not supported") }
Assert-True { $nonProviders.Length -eq 0 }
$ErrorActionPreference = "Stop"
$globalProviders = Get-AzureRmResourceProvider -Location "global"
Assert-True { $globalProviders.Length -gt 0 }
Register-AzureRmResourceProvider -ProviderNamespace "Microsoft.ApiManagement"
$endTime = [DateTime]::UtcNow.AddMinutes(5)
while ([DateTime]::UtcNow -lt $endTime -and @(Get-AzureRmResourceProvider -ProviderNamespace "Microsoft.ApiManagement")[0].RegistrationState -ne "Registered")
{
[Microsoft.WindowsAzure.Commands.Utilities.Common.TestMockSupport]::Delay(1000)
}
$provider = Get-AzureRmResourceProvider -ProviderNamespace "Microsoft.ApiManagement"
Assert-True { $provider[0].RegistrationState -eq "Registered" }
Unregister-AzureRmResourceProvider -ProviderNamespace "Microsoft.ApiManagement"
while ([DateTime]::UtcNow -lt $endTime -and @(Get-AzureRmResourceProvider -ProviderNamespace "Microsoft.ApiManagement")[0].RegistrationState -ne "Unregistered")
{
[Microsoft.WindowsAzure.Commands.Utilities.Common.TestMockSupport]::Delay(1000)
}
$provider = Get-AzureRmResourceProvider -ProviderNamespace "Microsoft.ApiManagement"
Assert-True { $provider[0].RegistrationState -eq "Unregistered" }
}
<#
.SYNOPSIS
Tests getting resource provider with zone mappings.
#>
function Test-AzureProvider-WithZoneMappings
{
$testProvider = Get-AzureRmResourceProvider -ProviderNamespace "Providers.Test"
Assert-True { $testProvider.Count -gt 0 }
$statefulResources = $testProvider | where-object {$_.ResourceTypes.ResourceTypeName -contains "statefulResources"}
Assert-NotNull { $statefulResources }
Assert-NotNull { $statefulResources.ZoneMappings }
Assert-True { $statefulResources.ZoneMappings.Count -eq 2 }
Assert-True { $statefulResources.ZoneMappings["West Europe"] -contains "3"}
}
<#
.SYNOPSIS
Tests querying for a resource provider's operations/actions
#>
function Test-AzureProviderOperation
{
# Get all operations by all providers
$allActions = Get-AzureRmProviderOperation
Assert-True { $allActions.Length -gt 0 }
# Get all operations of microsoft.insights provider
$insightsActions = Get-AzureRmProviderOperation Microsoft.Insights/*
$insightsActions
Assert-True { $insightsActions.Length -gt 0 }
Assert-True { $allActions.Length -gt $insightsActions.Length }
# Get all operations of microsoft.eventgrid provider
$eventgridActions = Get-AzureRmProviderOperation Microsoft.EventGrid/*
$eventgridActions
Assert-True { $eventgridActions.Length -gt 0 }
Assert-True { $allActions.Length -gt $eventgridActions.Length }
# Filter non-Microsoft.Insights operations and match the lengths
$nonInsightsActions = $allActions | Where-Object { $_.Operation.ToLower().StartsWith("microsoft.insights/") -eq $false }
$actualLength = $allActions.Length - $nonInsightsActions.Length;
$expectedLength = $insightsActions.Length;
Assert-True { $actualLength -eq $expectedLength }
foreach ($action in $insightsActions)
{
Assert-True { $action.Operation.ToLower().StartsWith("microsoft.insights/"); }
Assert-NotNull $action.isDataAction
}
# Case insenstive search
$insightsCaseActions = Get-AzureRmProviderOperation MicROsoFt.InSIghTs/*
Assert-True { $insightsCaseActions.Length -gt 0 }
Assert-True { $insightsCaseActions.Length -eq $insightsActions.Length }
foreach ($action in $insightsCaseActions)
{
Assert-True { $action.Operation.ToLower().Startswith("microsoft.insights/"); }
Assert-NotNull $action.isDataAction
}
# Get all Read operations of microsoft.insights provider
$insightsReadActions = Get-AzureRmProviderOperation Microsoft.Insights/*/read
Assert-True { $insightsReadActions.Length -gt 0 }
Assert-True { $insightsActions.Length -gt $insightsReadActions.Length }
foreach ($action in $insightsReadActions)
{
Assert-True { $action.Operation.ToLower().EndsWith("/read"); }
Assert-True { $action.Operation.ToLower().StartsWith("microsoft.insights/");}
Assert-NotNull $action.isDataAction
}
# Get all Read operations of all providers
$readActions = Get-AzureRmProviderOperation */read
Assert-True { $readActions.Length -gt 0 }
Assert-True { $readActions.Length -lt $allActions.Length }
Assert-True { $readActions.Length -gt $insightsReadActions.Length }
foreach ($action in $readActions)
{
Assert-True { $action.Operation.ToLower().EndsWith("/read"); }
Assert-NotNull $action.isDataAction
}
# Get a particular operation
$action = Get-AzureRmProviderOperation Microsoft.OperationalInsights/workspaces/usages/read
Assert-AreEqual $action.Operation.ToLower() "Microsoft.OperationalInsights/workspaces/usages/read".ToLower();
# Get an invalid operation
$action = Get-AzureRmProviderOperation Microsoft.OperationalInsights/workspaces/usages/read/123
Assert-True { $action.Length -eq 0 }
# Get operations for non-existing provider
$exceptionMessage = "Provider 'NonExistentProvider' not found.";
Assert-Throws { Get-AzureRmProviderOperation NonExistentProvider/* } $exceptionMessage
# Get operations for non-existing provider
Assert-Throws { Get-AzureRmProviderOperation NonExistentProvider/servers/read } $exceptionMessage
# Get operations with invalid search string parts
$exceptionMessage = "Individual parts in the search string should either equal * or not contain *.";
Assert-Throws {Get-AzureRmProviderOperation Microsoft.ClassicCompute/virtual*/read } $exceptionMessage
# Get operations with invalid provider name
$exceptionMessage = "To get all operations under Microsoft.Sql, please specify the search string as Microsoft.Sql/*.";
Assert-Throws {Get-AzureRmProviderOperation Microsoft.Sql } $exceptionMessage
# Get operations with ? in search string
$exceptionMessage = "Wildcard character ? is not supported.";
Assert-Throws {Get-AzureRmProviderOperation Microsoft.Sql/servers/*/rea? } $exceptionMessage
}
<#
.SYNOPSIS
Tests querying for a resource provider's operations/actions
#>
function Test-AzureProviderOperationDataActions
{
# Get all operations of microsoft.insights provider
$storageDataActions = Get-AzureRmProviderOperation Microsoft.Storage/storageAccounts/blobServices/containers/blobs/*
foreach ($action in $storageDataActions)
{
Assert-NotNull $action
Assert-AreEqual $action.isDataAction $true
}
}