-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathMSFT_xWebConfigProperty.Integration.Tests.ps1
More file actions
193 lines (161 loc) · 8.11 KB
/
MSFT_xWebConfigProperty.Integration.Tests.ps1
File metadata and controls
193 lines (161 loc) · 8.11 KB
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
$script:dscModuleName = 'xWebAdministration'
$script:dscResourceFriendlyName = 'xWebConfigProperty'
$script:dscResourceName = "MSFT_$($script:dscResourceFriendlyName)"
#region HEADER
# Integration Test Template Version: 1.3.0
[String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
(-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
{
& git @('clone', 'https://github.com/PowerShell/DscResource.Tests.git', (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))
}
Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResource.Tests' -ChildPath 'TestHelper.psm1')) -Force
# Ensure the WebAdministration module is imported into the current session!
Import-Module WebAdministration -Force
$TestEnvironment = Initialize-TestEnvironment `
-DSCModuleName $script:dscModuleName `
-DSCResourceName $script:dscResourceName `
-TestType Integration
#endregion
[string] $tempName = "$($script:dscResourceName)_" + (Get-Date).ToString('yyyyMMdd_HHmmss')
# Using try/finally to always cleanup.
try
{
$configurationFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:dscResourceName).config.ps1"
. $configurationFile
$null = Backup-WebConfiguration -Name $tempName
#region Integration Tests
Describe "$($script:dscResourceName)_Integration" {
# Create the website we'll use for testing purposes.
$websiteName = New-Guid
if (-not(Get-Website -Name $websiteName))
{
$websitePhysicalPath = "$($TestDrive)\$($websiteName)"
New-Item -Path $websitePhysicalPath -ItemType Directory -Force | Out-Null
New-Website -Name $websiteName -PhysicalPath $websitePhysicalPath | Out-Null
}
$ConfigurationData = @{
AllNodes = @(
@{
NodeName = 'localhost'
WebsitePath = "IIS:\Sites\$($websiteName)"
Filter = 'system.webServer/directoryBrowse'
Location = ''
PropertyName = 'enabled'
AddValue = $true
UpdateValue = $false
IntegerFilter = '/SYSTEM.WEB/TRACE'
IntergerPropertyName = 'requestLimit'
IntegerValue = [string](Get-Random -Minimum 11 -Maximum 1000)
}
)
}
$startDscConfigurationParameters = @{
Path = $TestDrive
ComputerName = 'localhost'
Wait = $true
Verbose = $true
Force = $true
}
$websitePath = $ConfigurationData.AllNodes.WebsitePath
$filter = $ConfigurationData.AllNodes.Filter
$propertyName = $ConfigurationData.AllNodes.PropertyName
$addValue = $ConfigurationData.AllNodes.AddValue
$updateValue = $ConfigurationData.AllNodes.UpdateValue
$integerFilter = $ConfigurationData.AllNodes.IntegerFilter
$intergerPropertyName = $ConfigurationData.AllNodes.IntergerPropertyName
$integerValue = $ConfigurationData.AllNodes.IntegerValue
Context 'When Adding Property' {
It 'Should compile and apply the MOF without throwing' {
{
& "$($script:dscResourceName)_Add" -OutputPath $TestDrive -ConfigurationData $ConfigurationData
Start-DscConfiguration @startDscConfigurationParameters
} | Should -Not -Throw
}
It 'Should be able to call Get-DscConfiguration without throwing' {
{ Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw
}
It 'Should return $true for Test-DscConfiguration' {
Test-DscConfiguration | Should Be $true
}
It 'Should have the correct value of the configuration property' {
$value = (Get-WebConfigurationProperty -PSPath $websitePath -Filter $filter -Name $propertyName).Value
$value | Should -Be $addValue
}
}
Context 'When Updating a Property' {
It 'Should compile and apply the MOF without throwing' {
{
& "$($script:dscResourceName)_Update" -OutputPath $TestDrive -ConfigurationData $ConfigurationData
Start-DscConfiguration @startDscConfigurationParameters
} | Should -Not -Throw
}
It 'Should update the configuration property correctly' {
$value = (Get-WebConfigurationProperty -PSPath $websitePath -Filter $filter -Name $propertyName).Value
$value | Should -Be $updateValue
}
It 'Should be able to call Get-DscConfiguration without throwing' {
{ Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw
}
It 'Should return $true for Test-DscConfiguration' {
Test-DscConfiguration | Should Be $true
}
}
Context 'When Removing a Property' {
It 'Should compile and apply the MOF without throwing' {
{
& "$($script:dscResourceName)_Remove" -OutputPath $TestDrive -ConfigurationData $ConfigurationData
Start-DscConfiguration @startDscConfigurationParameters
} | Should -Not -Throw
}
It 'Should remove configuration property' {
# Get the value.
# Because configuration properties can be inherited (& I'm not aware of a reliable way to determine if the value returned is inherited or set explicitly),
# we instead read the config file as XML directly & attempt to locate the property under test.
$value = ([xml] ((Get-WebConfigFile -PSPath $websitePath) | Get-Content)).SelectSingleNode("//$($filter)/@$($propertyName)")
$value | Should -Be $null
}
It 'Should be able to call Get-DscConfiguration without throwing' {
{ Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw
}
It 'Should return $true for Test-DscConfiguration' {
Test-DscConfiguration | Should Be $true
}
}
Context 'When Updating a Integer Property' {
It 'Should compile and apply the MOF without throwing' {
{
& "$($script:dscResourceName)_Integer" -OutputPath $TestDrive -ConfigurationData $ConfigurationData
Start-DscConfiguration @startDscConfigurationParameters
} | Should -Not -Throw
}
It 'Should be able to call Get-DscConfiguration without throwing' {
{ Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw
}
It 'Should update the configuration integer property correctly' {
[string] $value = (Get-WebConfigurationProperty -PSPath $websitePath -Filter $integerFilter -Name $intergerPropertyName).Value
$value | Should -Be $integerValue
}
It 'Should return $true for Test-DscConfiguration' {
Test-DscConfiguration | Should Be $true
}
}
# Remove the website we created for testing purposes.
if (Get-Website -Name $websiteName)
{
Remove-Website -Name $websiteName
Remove-Item -Path $websitePhysicalPath -Force -Recurse
}
}
#endregion
}
finally
{
#region FOOTER
Restore-TestEnvironment -TestEnvironment $TestEnvironment
# Addresses Issue #385: xWebConfigPropertyCollection: Timing issue in integration tests
Start-Sleep -Seconds 4
Restore-WebConfiguration -Name $tempName
Remove-WebConfigurationBackup -Name $tempName
#endregion
}