-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathMSFT_xScriptResource.Integration.Tests.ps1
129 lines (107 loc) · 5.3 KB
/
MSFT_xScriptResource.Integration.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
$errorActionPreference = 'Stop'
Set-StrictMode -Version 'Latest'
# Import CommonTestHelper for Enter-DscResourceTestEnvironment, Exit-DscResourceTestEnvironment
$script:testsFolderFilePath = Split-Path $PSScriptRoot -Parent
$script:commonTestHelperFilePath = Join-Path -Path $testsFolderFilePath -ChildPath 'CommonTestHelper.psm1'
Import-Module -Name $commonTestHelperFilePath
$script:testEnvironment = Enter-DscResourceTestEnvironment `
-DscResourceModuleName 'xPSDesiredStateConfiguration' `
-DscResourceName 'MSFT_xScriptResource' `
-TestType 'Integration'
try
{
Describe 'xScript Integration Tests' {
BeforeAll {
# Import xScript module for Get-TargetResource, Test-TargetResource
$moduleRootFilePath = Split-Path -Path $script:testsFolderFilePath -Parent
$dscResourcesFolderFilePath = Join-Path -Path $moduleRootFilePath -ChildPath 'DscResources'
$scriptResourceFolderFilePath = Join-Path -Path $dscResourcesFolderFilePath -ChildPath 'MSFT_xScriptResource'
$scriptResourceModuleFilePath = Join-Path -Path $scriptResourceFolderFilePath -ChildPath 'MSFT_xScriptResource.psm1'
Import-Module -Name $scriptResourceModuleFilePath
$script:configurationNoCredentialFilePath = Join-Path -Path $PSScriptRoot -ChildPath 'MSFT_xScriptResource_NoCredential.config.ps1'
$script:configurationWithCredentialFilePath = Join-Path -Path $PSScriptRoot -ChildPath 'MSFT_xScriptResource_WithCredential.config.ps1'
# Cannot use $TestDrive here because script is run outside of Pester
$script:testFilePath = Join-Path -Path $env:SystemDrive -ChildPath 'TestFile.txt'
if (Test-Path -Path $script:testFilePath)
{
Remove-Item -Path $script:testFilePath -Force
}
}
AfterAll {
if (Test-Path -Path $script:testFilePath)
{
Remove-Item -Path $script:testFilePath -Force
}
}
Context 'Get, set, and test scripts specified and Credential not specified' {
if (Test-Path -Path $script:testFilePath)
{
Remove-Item -Path $script:testFilePath -Force
}
$configurationName = 'TestScriptNoCredential'
# Cannot use $TestDrive here because script is run outside of Pester
$resourceParameters = @{
FilePath = $script:testFilePath
FileContent = 'Test file content'
}
It 'Should have removed test file before config runs' {
Test-Path -Path $resourceParameters.FilePath | Should Be $false
}
It 'Should compile and apply the MOF without throwing' {
{
. $script:configurationNoCredentialFilePath -ConfigurationName $configurationName
& $configurationName -OutputPath $TestDrive @resourceParameters
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
} | Should Not Throw
}
It 'Should have created test file' {
Test-Path -Path $resourceParameters.FilePath | Should Be $true
}
It 'Should have set file content correctly' {
Get-Content -Path $resourceParameters.FilePath -Raw | Should Be "$($resourceParameters.FileContent)`r`n"
}
}
Context 'Get, set, and test scripts specified and Credential specified' {
if (Test-Path -Path $script:testFilePath)
{
Remove-Item -Path $script:testFilePath -Force
}
$configurationName = 'TestScriptWithCredential'
# Cannot use $TestDrive here because script is run outside of Pester
$resourceParameters = @{
FilePath = $script:testFilePath
FileContent = 'Test file content'
Credential = Get-AppVeyorAdministratorCredential
}
It 'Should have removed test file before config runs' {
Test-Path -Path $resourceParameters.FilePath | Should Be $false
}
$configData = @{
AllNodes = @(
@{
NodeName = 'localhost'
PSDscAllowPlainTextPassword = $true
PSDscAllowDomainUser = $true
}
)
}
It 'Should compile and apply the MOF without throwing' {
{
. $script:configurationWithCredentialFilePath -ConfigurationName $configurationName
& $configurationName -OutputPath $TestDrive -ConfigurationData $configData @resourceParameters
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
} | Should Not Throw
}
It 'Should have created test file' {
Test-Path -Path $resourceParameters.FilePath | Should Be $true
}
It 'Should have set file content correctly' {
Get-Content -Path $resourceParameters.FilePath -Raw | Should Be "$($resourceParameters.FileContent)`r`n"
}
}
}
}
finally
{
Exit-DscResourceTestEnvironment -TestEnvironment $script:testEnvironment
}