forked from microsoft/PowerShell-DSC-for-Linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonitorOnlyMode.Tests.ps1
303 lines (258 loc) · 16 KB
/
MonitorOnlyMode.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
<#
.SYNOPSIS
Test suite for MonitorOnly Configuration Mode
Copyright (c) Microsoft Corporation, 2017
#>
Describe 'MonitorOnly Configuration Mode' -Tags @('BVT') {
BeforeAll {
$dscLinuxTestHelperPath = Join-Path -Path $PSScriptRoot -ChildPath 'Dsclinux.Tests.Helper.psm1'
Import-Module -Name $dscLinuxTestHelperPath -DisableNameChecking
# Install Modules needed for compliation on Windows machine
$modulesNeeded = @('nx')
$modulesToInstall = @()
foreach ($moduleName in $modulesNeeded)
{
$moduleNeeded = Get-Module -Name $moduleName -ListAvailable -ErrorAction 'SilentlyContinue'
$moduleInstalled = $null -ne $moduleNeeded
if (-not $moduleInstalled)
{
$modulesToInstall += $moduleName
}
}
if ($modulesToInstall.Count -gt 0)
{
Install-Module -Name $modulesToInstall
}
# Create a Cim session to Client Linux machine on which that we are testing DSC Functionality
$connectionConfiguration = Get-TestConfiguration
$script:linuxClientName = $connectionConfiguration.LinuxClient.Name
$rootUser = $connectionConfiguration.LinuxClient.UserName
$rootPassword = $connectionConfiguration.LinuxClient.Password
$secureRootPassword = ConvertTo-SecureString -String $rootPassword -AsPlainText -Force
$rootCredential = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList @($rootUser, $secureRootPassword)
$script:session = CreateCimSession -Credential $rootCredential -Server $linuxClientName
# Set up file path script variables
$configurationFolderPath = Join-Path -Path $PSScriptRoot -ChildPath 'Configurations'
$testMetaconfigurationFilePath = Join-Path -Path $configurationFolderPath -ChildPath 'MetaConfigPush.ps1'
$testConfigurationFilePath = Join-Path -Path $configurationFolderPath -ChildPath 'FileProviderTestConfig1.ps1'
# Generate metaconfiguration with MonitorOnly mode
$script:lcmSettingsMonitorOnly = @{
RefreshMode = 'PUSH'
ConfigurationMode = 'MonitorOnly'
}
$script:metaconfigurationMonitorOnlyOutput = Join-Path -Path $TestDrive -ChildPath 'MetaConfigPushMonitorOnly'
$null = & $testMetaconfigurationFilePath -TargetClient $script:linuxClientName -Output $script:metaconfigurationMonitorOnlyOutput @lcmSettingsMonitorOnly
# Generate configuartion with file absent
$script:fileAbsentParameters = @{
Ensure = 'Absent'
Type = 'File'
DestinationPath = '/tmp/dsctestfile'
Contents = 'DSC test contents'
}
$script:configurationFileAbsentOutput = Join-Path -Path $TestDrive -ChildPath 'TestFileAbsentConfig'
$null = & $testConfigurationFilePath -TargetClient $script:linuxClientName -Output $script:configurationFileAbsentOutput @fileAbsentParameters
# Generate configuartion with file present
$script:filePresentParameters = @{
Ensure = 'Present'
Type = 'File'
DestinationPath = '/tmp/dsctestfile'
Contents = 'DSC test contents'
}
$script:configurationFilePresentOutput = Join-Path -Path $TestDrive -ChildPath 'TestFilePresentConfig'
$null = & $testConfigurationFilePath -TargetClient $script:linuxClientName -Output $script:configurationFilePresentOutput @filePresentParameters
}
AfterAll {
$null = Remove-CimSession -CimSession $script:session
}
$applyConfigurationModes = @('ApplyOnly', 'ApplyAndMonitor', 'ApplyAndAutocorrect')
foreach ($configurationMode in $applyConfigurationModes)
{
# Generate metaconfiguration with specified apply configuration mode
$script:lcmSettingsApply = @{
RefreshMode = 'PUSH'
ConfigurationMode = $configurationMode
}
$script:metaconfigurationApplyOutput = Join-Path -Path $TestDrive -ChildPath ('MetaConfigPush' + $configurationMode)
$null = & $testMetaconfigurationFilePath -TargetClient $script:linuxClientName -Output $script:metaconfigurationApplyOutput @lcmSettingsApply
Context "$configurationMode Mode - Ensure set to Absent and unsure if file exists or not" {
# Send metaconfig
It "Should set LCM ConfigurationMode to $configurationMode without throwing" {
{ Set-DscLocalConfigurationManager -Path $script:metaconfigurationApplyOutput -CimSession $script:session } | Should Not Throw
}
# Validate metaconfig
It "Should retrieve the LCM settings with expected RefreshMode of PUSH and ConfigurationMode of $configurationMode" {
$currentMetaConfig = Get-DscLocalConfigurationManager -CimSession $script:session
$currentMetaConfig | Should Not Be $null
$currentMetaConfig.RefreshMode | Should Be $script:lcmSettingsApply.RefreshMode
$currentMetaConfig.ConfigurationMode | Should Be $script:lcmSettingsApply.ConfigurationMode
}
# Push configuration
It 'Should push a configuration without throwing' {
{ Start-DscConfiguration -Path $script:configurationFileAbsentOutput -CimSession $script:session -Wait -Force } | Should Not Throw
}
# Verify Get-DscConfiguration
It 'Should retrieve DSC configuration with Ensure property as Absent' {
$dscConfig = Get-DscConfiguration -CimSession $script:session
$dscConfig | Should Not Be $null
$dscConfig.Ensure | Should Be 'Absent'
$dscConfig.DestinationPath | Should Be $script:fileAbsentParameters.DestinationPath
$dscConfig.Contents | Should Be $script:fileAbsentParameters.Contents
}
# Verify Test-DscConfiguration
It 'Should test DSC configuration and return InDesiredState as true' {
$testDscConfig = Test-DscConfiguration -CimSession $script:session -Detailed
$testDscConfig.InDesiredState | Should Be $true
}
}
Context 'MonitorOnly Mode - Ensure set to Absent and file does not exist' {
# Send metaconfig
It 'Should set LCM ConfigurationMode to MonitorOnly without throwing' {
{ Set-DscLocalConfigurationManager -Path $script:metaconfigurationMonitorOnlyOutput -CimSession $script:session } | Should Not Throw
}
# Validate metaconfig
It 'Should retrieve the LCM settings with expected RefreshMode of PUSH and ConfigurationMode of MonitorOnly' {
$currentMetaConfig = Get-DscLocalConfigurationManager -CimSession $script:session
$currentMetaConfig | Should Not Be $null
$currentMetaConfig.RefreshMode | Should Be $script:lcmSettingsMonitorOnly.RefreshMode
$currentMetaConfig.ConfigurationMode | Should Be $script:lcmSettingsMonitorOnly.ConfigurationMode
}
It 'Should push a configuration without throwing' {
{ Start-DscConfiguration -Path $script:configurationFileAbsentOutput -CimSession $script:session -Wait -Force } | Should Not Throw
}
# Verify Get-DscConfiguration
It 'Should retrieve DSC configuration with Ensure property as Absent' {
$dscConfig = Get-DscConfiguration -CimSession $script:session
$dscConfig | Should Not Be $null
$dscConfig.Ensure | Should Be 'Absent'
$dscConfig.DestinationPath | Should Be $script:fileAbsentParameters.DestinationPath
$dscConfig.Contents | Should Be $script:fileAbsentParameters.Contents
}
# Verify Test-DscConfiguration
It 'Should test DSC configuration and return InDesiredState as true' {
$testDscConfig = Test-DscConfiguration -CimSession $script:session -Detailed
$testDscConfig.InDesiredState | Should Be $true
}
}
Context 'MonitorOnly Mode - Ensure set to Present and file does not exist' {
It 'Should push a configuration without throwing' {
{ Start-DscConfiguration -Path $script:configurationFilePresentOutput -CimSession $script:session -Wait -Force } | Should Not Throw
}
# Verify Get-DscConfiguration
It 'Should retrieve DSC configuration with Ensure property as Absent' {
$dscConfig = Get-DscConfiguration -CimSession $script:session
$dscConfig | Should Not Be $null
$dscConfig.Ensure | Should Be 'Absent'
$dscConfig.DestinationPath | Should Be $script:filePresentParameters.DestinationPath
$dscConfig.Contents | Should Be $script:filePresentParameters.Contents
}
# Verify Test-DscConfiguration
It 'Should test DSC configuration and return InDesiredState as false' {
$testDscConfig = Test-DscConfiguration -CimSession $script:session -Detailed
$testDscConfig.InDesiredState | Should Be $false
}
}
Context "$configurationMode Mode - Ensure set to Present and file does not exist" {
# Send metaconfig
It "Should set LCM ConfigurationMode to $configurationMode without throwing" {
{ Set-DscLocalConfigurationManager -Path $script:metaconfigurationApplyOutput -CimSession $script:session } | Should Not Throw
}
# Validate metaconfig
It "Should retrieve the LCM settings with expected RefreshMode of PUSH and ConfigurationMode of $configurationMode" {
$currentMetaConfig = Get-DscLocalConfigurationManager -CimSession $script:session
$currentMetaConfig | Should Not Be $null
$currentMetaConfig.RefreshMode | Should Be $script:lcmSettingsApply.RefreshMode
$currentMetaConfig.ConfigurationMode | Should Be $script:lcmSettingsApply.ConfigurationMode
}
# Send command to start existing config
It 'Should start existing configuration without throwing' {
{ Start-DscConfiguration -UseExisting -CimSession $script:session -Wait -Force } | Should Not Throw
}
# Verify Get-DscConfiguration
It 'Should retrieve DSC configuration with Ensure property as Present' {
$dscConfig = Get-DscConfiguration -CimSession $script:session
$dscConfig | Should Not Be $null
$dscConfig.Ensure | Should Be 'Present'
$dscConfig.DestinationPath | Should Be $script:filePresentParameters.DestinationPath
$dscConfig.Contents | Should Be $script:filePresentParameters.Contents
}
# Verify Test-DscConfiguration
It 'Should test DSC configuration and return InDesiredState as true' {
$testDscConfig = Test-DscConfiguration -CimSession $script:session -Detailed
$testDscConfig.InDesiredState | Should Be $true
}
}
Context 'MonitorOnly Mode - Ensure set to Present and file exists' {
# Send metaconfig with MonitorOnly
It 'Should set LCM ConfigurationMode to MonitorOnly without throwing' {
{ Set-DscLocalConfigurationManager -Path $script:metaconfigurationMonitorOnlyOutput -CimSession $script:session } | Should Not Throw
}
# Validate metaconfig
It 'Should retrieve the LCM settings with expected RefreshMode of PUSH and ConfigurationMode of MonitorOnly' {
$currentMetaConfig = Get-DscLocalConfigurationManager -CimSession $script:session
$currentMetaConfig | Should Not Be $null
$currentMetaConfig.RefreshMode | Should Be $script:lcmSettingsMonitorOnly.RefreshMode
$currentMetaConfig.ConfigurationMode | Should Be $script:lcmSettingsMonitorOnly.ConfigurationMode
}
# Verify Get-DscConfiguration
It 'Should retrieve DSC configuration with Ensure property as Present' {
$dscConfig = Get-DscConfiguration -CimSession $script:session
$dscConfig | Should Not Be $null
$dscConfig.Ensure | Should Be 'Present'
$dscConfig.DestinationPath | Should Be $script:filePresentParameters.DestinationPath
$dscConfig.Contents | Should Be $script:filePresentParameters.Contents
}
# Verify Test-DscConfiguration
It 'Should test DSC configuration and return InDesiredState as true' {
$testDscConfig = Test-DscConfiguration -CimSession $script:session -Detailed
$testDscConfig.InDesiredState | Should Be $true
}
}
Context 'MonitorOnly Mode - Ensure set to Absent and file exists' {
It 'Should push a configuration without throwing' {
{ Start-DscConfiguration -Path $script:configurationFileAbsentOutput -CimSession $script:session -Wait -Force } | Should Not Throw
}
# Verify Get-DscConfiguration
It 'Should retrieve DSC configuration with Ensure property as Present' {
$dscConfig = Get-DscConfiguration -CimSession $script:session
$dscConfig | Should Not Be $null
$dscConfig.Ensure | Should Be 'Present'
$dscConfig.DestinationPath | Should Be $script:fileAbsentParameters.DestinationPath
$dscConfig.Contents | Should Be $script:fileAbsentParameters.Contents
}
# Verify Test-DscConfiguration
It 'Should test DSC configuration and return InDesiredState as false' {
$testDscConfig = Test-DscConfiguration -CimSession $script:session -Detailed
$testDscConfig.InDesiredState | Should Be $false
}
}
Context "$configurationMode Mode - Ensure set to Absent and file exists" {
# Send metaconfig
It "Should set LCM ConfigurationMode to $configurationMode without throwing" {
{ Set-DscLocalConfigurationManager -Path $script:metaconfigurationApplyOutput -CimSession $script:session } | Should Not Throw
}
# Validate metaconfig
It "Should retrieve the LCM settings with expected RefreshMode of PUSH and ConfigurationMode of $configurationMode" {
$currentMetaConfig = Get-DscLocalConfigurationManager -CimSession $script:session
$currentMetaConfig | Should Not Be $null
$currentMetaConfig.RefreshMode | Should Be $script:lcmSettingsApply.RefreshMode
$currentMetaConfig.ConfigurationMode | Should Be $script:lcmSettingsApply.ConfigurationMode
}
It 'Should push a configuration without throwing' {
{ Start-DscConfiguration -Path $script:configurationFileAbsentOutput -CimSession $script:session -Wait -Force } | Should Not Throw
}
# Verify Get-DscConfiguration
It 'Should retrieve DSC configuration with Ensure property as Absent' {
$dscConfig = Get-DscConfiguration -CimSession $script:session
$dscConfig | Should Not Be $null
$dscConfig.Ensure | Should Be 'Absent'
$dscConfig.DestinationPath | Should Be $script:fileAbsentParameters.DestinationPath
$dscConfig.Contents | Should Be $script:fileAbsentParameters.Contents
}
# Verify Test-DscConfiguration
It 'Should test DSC configuration and return InDesiredState as true' {
$testDscConfig = Test-DscConfiguration -CimSession $script:session -Detailed
$testDscConfig.InDesiredState | Should Be $true
}
}
}
}