-
Notifications
You must be signed in to change notification settings - Fork 11
/
azuredeploy.tests.ps1
307 lines (251 loc) · 10.9 KB
/
azuredeploy.tests.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
Function Test-AzureJson {
Param(
[string]
$FilePath
)
Context "JSON Structure" {
$templateProperties = (get-content "$FilePath" -ErrorAction SilentlyContinue | ConvertFrom-Json -ErrorAction SilentlyContinue)
It "should be less than 1 Mb" {
Get-Item $FilePath | Select-Object -ExpandProperty Length | Should -BeLessOrEqual 1073741824
}
It "Converts from JSON" {
$templateProperties | Should -Not -BeNullOrEmpty
}
It "should have a `$schema section" {
$templateProperties."`$schema" | Should -Not -BeNullOrEmpty
}
It "should have a contentVersion section" {
$templateProperties.contentVersion | Should -Not -BeNullOrEmpty
}
It "should have a parameters section" {
$templateProperties.parameters | Should -Not -BeNullOrEmpty
}
It "should have less than 256 parameters" {
$templateProperties.parameters.Length | Should -BeLessOrEqual 256
}
It "might have a variables section" {
$result = $null -eq $templateProperties.variables
if($result){
$result | Should -Be $true
}
else {
Set-TestInconclusive -Message "Section isn't mandatory, however it's a group practice to have it defined"
}
}
It "must have a resources section" {
$templateProperties.resources | Should -Not -BeNullOrEmpty
}
It "might have an outputs section" {
$result = $null -eq $templateProperties.outputs
if($result){
$result | Should -Be $true
}
else {
Set-TestInconclusive -Message "Section isn't mandatory, however it's a group practice to have it defined"
}
}
}
$jsonMainTemplate = Get-Content "$FilePath"
$objMainTemplate = $jsonMainTemplate | ConvertFrom-Json -ErrorAction SilentlyContinue
$parametersUsage = [System.Text.RegularExpressions.RegEx]::Matches($jsonMainTemplate, "parameters(\(\'\w*\'\))") | Select-Object -ExpandProperty Value -Unique
Context "Referenced Parameters" {
ForEach($parameterUsage In $parametersUsage)
{
$parameterUsage = $parameterUsage.SubString($parameterUsage.IndexOf("'") + 1).Replace("')","")
It "should have a parameter called $parameterUsage" {
$objMainTemplate.parameters.$parameterUsage | Should -Not -Be $null
}
}
}
$variablesUsage = [System.Text.RegularExpressions.RegEx]::Matches($jsonMainTemplate, "variables(\(\'\w*\'\))") | Select-Object -ExpandProperty Value -Unique
Context "Referenced Variables" {
ForEach($variableUsage In $variablesUsage)
{
$variableUsage = $variableUsage.SubString($variableUsage.IndexOf("'") + 1).Replace("')","")
It "should have a variable called $variableUsage" {
$objMainTemplate.variables.$variableUsage | Should -Not -Be $null
}
}
}
Context "Missing opening or closing square brackets" {
For($i=0;$i -lt $jsonMainTemplate.Length;$i++) {
$Matches = [System.Text.RegularExpressions.Regex]::Matches($jsonMainTemplate[$i],"\"".*\""")
ForEach($Match In $Matches) {
$PairCharNumber = ($Match.Value.Length - $Match.Value.Replace("[","").Replace("]","").Length) % 2
if($PairCharNumber -ne 0) {
Write-Host $Match.Value
It "should have same amount of opening and closing square brackets (Line $($i + 1))" {
$PairCharNumber | Should -Be 0
}
break
}
}
}
}
Context "Missing opening or closing parenthesis" {
For($i=0;$i -lt $jsonMainTemplate.Length;$i++) {
$Matches = [System.Text.RegularExpressions.Regex]::Matches($jsonMainTemplate[$i],"\"".*\""")
ForEach($Match In $Matches) {
$PairCharNumber = ($Match.Value.Length - $Match.Value.Replace("(","").Replace(")","").Length) % 2
if($PairCharNumber -ne 0) {
It "should have same amount of opening and closing parenthesis (Line $($i + 1))" {
$PairCharNumber | Should -Be 0
}
break
}
}
}
}
Context "Azure API Validation" {
$context = $null
try {$context = Get-AzureRmContext} catch {}
if($null -ne $context.Subscription.Id) {
$Parameters = $objMainTemplate.parameters | Get-Member | Where-Object -Property MemberType -eq -VAlue "NoteProperty" | Select-Object -ExpandProperty Name
$TemplateParameters = @{}
$PesterRGCreated = $false
ForEach($Parameter In $Parameters) {
if(!($objMainTemplate.parameters.$Parameter.defaultValue)) {
if($objMainTemplate.parameters.$Parameter.allowedValues) {
$TemplateParameters.Add($Parameter, $objMainTemplate.parameters.$Parameter.allowedValues[0])
}
else {
switch ($objMainTemplate.parameters.$Parameter.type) {
"bool" {
$TemplateParameters.Add($Parameter, $true)
}
"int" {
if($objMainTemplate.parameters.$Parameter.minValue) {
$TemplateParameters.Add($Parameter, $objMainTemplate.parameters.$Parameter.minValue)
}
else {
$TemplateParameters.Add($Parameter, 1)
}
}
"string" {
if($objMainTemplate.parameters.$Parameter.minValue) {
$TemplateParameters.Add($Parameter, "a" * $objMainTemplate.parameters.$Parameter.minLength)
}
else {
$TemplateParameters.Add($Parameter, "dummystring")
}
}
"securestring" {
$TemplateParameters.Add($Parameter, "dummystring")
}
"array" {
$TemplateParameters.Add($Parameter, @(1,2,3))
}
"object" {
$TemplateParameters.Add($Parameter, (New-Object -TypeName psobject -Property @{"DummyProperty"="DummyValue"}))
}
"secureobject" {
$TemplateParameters.Add($Parameter, (New-Object -TypeName psobject -Property @{"DummyProperty"="DummyValue"}))
}
}
}
}
}
if(!(Get-AzureRmResourceGroup -Name "PesterRG" -ErrorAction SilentlyContinue)) {
New-AzureRmResourceGroup -Name "PesterRG" -Location "East US" | Out-Null
$PesterRGCreated = $true
}
$output = Test-AzureRmResourceGroupDeployment -ResourceGroupName "PesterRG" -TemplateFile $armTemplate.FullName @TemplateParameters
if($PesterRGCreated) {
Remove-AzureRmResourceGroup -Name "PesterRG" | Out-Null
}
It "should be a valid template" {
$output | Should -BeNullOrEmpty
}
ForEach($ValidationError In $output) {
It ("shouldn't have {0}" -f $ValidationError.Code) {
$ValidationError.Message | Should -BeNullOrEmpty
}
}
}
else {
It "should be a valid template" {
Set-TestInconclusive -Message "Not logged to AzureRM for which Test-AzureRmResourceGroupDeployment cannot be executed."
}
}
}
$nestedTemplates = $objMainTemplate.resources | Where-Object -Property Type -IEQ -Value "Microsoft.Resources/deployments"
if($null -ne $nestedTemplates)
{
ForEach($nestedTemplate In $nestedTemplates)
{
If($null -ne $nestedTemplate.properties.templateLink.uri)
{
$nestedTemplateFileName = [System.Text.RegularExpressions.RegEx]::Matches($nestedTemplate.properties.templateLink.uri, "\w*\.json(\?)?").Value
$nestedTemplateFileName = $nestedTemplateFileName.SubString($nestedTemplateFileName.IndexOf("'") + 1).Replace("'","").Replace('?','')
Context "Nested Template: $nestedTemplateFileName" {
It "should exist the nested template at $WorkingFolder\linked\$nestedTemplateFileName" {
"$WorkingFolder\linked\$nestedTemplateFileName" | Should -Exist
}
if(Test-Path "$WorkingFolder\linked\$nestedTemplateFileName")
{
$nestedParameters = (Get-Content "$WorkingFolder\linked\$nestedTemplateFileName" | ConvertFrom-Json).parameters
$requiredNestedParameters = $nestedParameters | Get-Member -MemberType NoteProperty | Where-Object -FilterScript {$null -eq $nestedParameters.$($_.Name).defaultValue} | ForEach-Object -Process {$_.Name}
ForEach($requiredNestedParameter In $requiredNestedParameters)
{
It "should define the parameter: $requiredNestedParameter" {
$nestedTemplate.properties.parameters.$requiredNestedParameter | Should -Not -BeNullOrEmpty
}
It "should set a value for $requiredNestedParameter" {
$nestedTemplate.properties.parameters.$requiredNestedParameter.Value | Should -Not -BeNullOrEmpty
}
}
}
}
}
}
}
}
Function Test-PowershellScript {
Param(
[string]$FilePath
)
It "is a valid Powershell Code"{
$psFile = Get-Content -Path $FilePath -ErrorAction Stop
$errors = $null
$null = [System.Management.Automation.PSParser]::Tokenize($psFile, [ref]$errors)
$errors.Count | Should -Be 0
}
}
$WorkingFolder = Split-Path -Parent $MyInvocation.MyCommand.Path
$armTemplates = Get-ChildItem -Path "$WorkingFolder" -Filter "*.json" -recurse -File | Where-Object -FilterScript {(Get-Content -Path $_.FullName -Raw) -ilike "*schema.management.azure.com/*/deploymentTemplate.json*"}
$powershellScripts = Get-ChildItem -Path "$WorkingFolder" -Filter "*.ps1" -Exclude "*.tests.*" -Recurse -File
#region ARM Template
ForEach($armTemplate In $armTemplates)
{
Describe $armTemplate.FullName.Replace($WorkingFolder,"") {
Test-AzureJson -FilePath $armTemplate.FullName
}
$jsonMainTemplate = Get-Content $armTemplate.FullName
$objMainTemplate = $jsonMainTemplate | ConvertFrom-Json -ErrorAction SilentlyContinue
$mainNestedTemplates = $null
If($objMainTemplate.resources | Where-Object -Property Type -IEQ -Value "Microsoft.Resources/deployments")
{
$mainNestedTemplates = [System.Text.RegularExpressions.RegEx]::Matches($($objMainTemplate.resources | Where-Object -Property Type -IEQ -Value "Microsoft.Resources/deployments" | ForEach-Object -Process {$_.properties.templateLink.uri}), "\'\w*\.json\??\'") | Select-Object -ExpandProperty Value -Unique
}
ForEach($nestedTemplate In $mainNestedTemplates)
{
$nestedTemplate = $nestedTemplate.SubString($nestedTemplate.IndexOf("'") + 1).Replace("'","").Replace('?','')
Describe "Nested: $WorkingFolder\linked\$nestedTemplate" {
It "Should exist" {
"$WorkingFolder\linked\$nestedTemplate" | Should -Exist
}
if(Test-Path $WorkingFolder\linked\$nestedTemplate)
{
Test-AzureJson -FilePath $WorkingFolder\linked\$nestedTemplate
}
}
}
}
#endregion
#region Powershell Scripts
ForEach($powershellScript In $powershellScripts) {
Describe $powershellScript.FullName.Replace($WorkingFolder,"") {
Test-PowershellScript -FilePath $powershellScript.FullName
}
}
#endregion