forked from OctopusDeploy/OctopusDSC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOctopusDSC.Tests.ps1
175 lines (146 loc) · 8.23 KB
/
OctopusDSC.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
Describe "PSScriptAnalyzer" {
Import-Module PSScriptAnalyzer
$excludedRules = @(
'PSUseShouldProcessForStateChangingFunctions'
)
$path = Resolve-Path "$PSCommandPath/../../OctopusDSC/DSCResources"
Write-Output "Running PsScriptAnalyzer against $path"
$results = @(Invoke-ScriptAnalyzer $path -recurse -exclude $excludedRules)
$results | ConvertTo-Json | Out-File PsScriptAnalyzer-DSCResources.log
It "Should have zero PSScriptAnalyzer issues in OctopusDSC/DSCResources" {
$results.length | Should Be 0
}
$path = Resolve-Path "$PSCommandPath/../../OctopusDSC/Tests"
Write-Output "Running PsScriptAnalyzer against $path"
$results = @(Invoke-ScriptAnalyzer $path -recurse -exclude $excludedRules)
$results | ConvertTo-Json | Out-File PsScriptAnalyzer-Tests.log
# it'd be nice to run the PsScriptAnalyzer on `./OctopusDSC/Examples`, but I couldn't get it to detect the DSCModule on mac nor on linux
It "Should have zero PSScriptAnalyzer issues in OctopusDSC/Tests" {
$results.length | Should Be 0
}
}
Describe "OctopusDSC.psd1" {
It "Should be valid" {
$path = Resolve-Path "$PSCommandPath/../../OctopusDSC/DSCResources"
$modules = Get-ChildItem $path -Directory
$path = Resolve-Path "$PSCommandPath/../../OctopusDSC"
Import-LocalizedData -BaseDirectory $path -FileName OctopusDSC.psd1 -BindingVariable Data
$expected = ($Data.DscResourcesToExport | Sort-Object) -join ","
$actual = (($modules.Name | Sort-Object) -join ",")
$actual | should be $expected
}
}
Describe "Mandatory Parameters" {
$path = Resolve-Path "$PSCommandPath/../../OctopusDSC/DSCResources"
$schemaMofFiles = Get-ChildItem $path -Recurse -Filter *.mof
foreach ($schemaMofFile in $schemaMofFiles) {
$schemaMofFileContent = Get-Content $schemaMofFile.FullName
$moduleFile = Get-Item ($schemaMofFile.FullName -replace ".schema.mof", ".psm1")
function Get-ParameterFromFunction($functionName, $ast, $propertyName) {
$filter = { $true }
$result = $ast.FindAll($filter, $true);
$foundMatchingParameter = $false
foreach($param in $result) {
if ($null -ne $param.name -and $param.Name.ToString() -eq "`$$propertyName") {
$function = $param.Parent.Parent.Parent
if ($function -is [System.Management.Automation.Language.FunctionDefinitionAst]) {
$funcName = ([System.Management.Automation.Language.FunctionDefinitionAst]$function).Name
if ($funcName -eq $functionName) {
return $param.Attributes.Extent.Text
}
}
}
}
}
function Test-Parameter($functionName, $ast, $propertyName, $moduleFile, $propertyType) {
$param = Get-ParameterFromFunction -functionName $functionName -ast $ast -propertyName $propertyName
It "Parameter '$propertyName' in function '$functionName' should be marked with [Parameter(Mandatory)] in $($moduleFile.Name) as its a $propertyType property" {
$param -contains "[Parameter(Mandatory)]" | should be $true
}
}
Describe "Module $($moduleFile.Name)" {
$tokens = $null;
$parseErrors = $null;
$ast = [System.Management.Automation.Language.Parser]::ParseFile($moduleFile.FullName, [ref] $tokens, [ref] $parseErrors);
It "The module $($moduleFile.Name) should have no parse errors" {
$parseErrors | should be $null
}
foreach($line in $schemaMofFileContent) {
if ($line -match "\s*(\[\b(Required|Key)\b.*\])\s*(.*) (.*);") {
$propertyType = $matches[2]
$propertyName = $matches[4].Replace("[]", "");
$filter = { $true }
$result = $ast.FindAll($filter, $true);
$foundMatchingParameter = $false
Test-Parameter -functionName "Get-TargetResource" -ast $ast -propertyName $propertyName -moduleFile $moduleFile -propertyType $propertyType
Test-Parameter -functionName "Set-TargetResource" -ast $ast -propertyName $propertyName -moduleFile $moduleFile -propertyType $propertyType
Test-Parameter -functionName "Test-TargetResource" -ast $ast -propertyName $propertyName -moduleFile $moduleFile -propertyType $propertyType
}
}
}
}
}
Describe "Test/Get/Set-TargetResource all implement the same properties" {
$path = Resolve-Path "$PSCommandPath/../../OctopusDSC/DSCResources"
$schemaMofFiles = Get-ChildItem $path -Recurse -Filter *.schema.mof
foreach ($schemaMofFile in $schemaMofFiles) {
$schemaMofFileContent = Get-Content $schemaMofFile.FullName
$moduleFile = Get-Item ($schemaMofFile.FullName -replace ".schema.mof", ".psm1")
function Get-ParameterFromFunction($functionName, $ast, $propertyName) {
$filter = { $true }
$result = $ast.FindAll($filter, $true);
$foundMatchingParameter = $false
foreach($param in $result) {
if ($null -ne $param.name -and $param.Name.ToString() -eq "`$$propertyName") {
$function = $param.Parent.Parent.Parent
if ($function -is [System.Management.Automation.Language.FunctionDefinitionAst]) {
$funcName = ([System.Management.Automation.Language.FunctionDefinitionAst]$function).Name
if ($funcName -eq $functionName) {
return $param
}
}
}
}
}
function Test-Parameter($functionName, $ast, $propertyName, $moduleFile, $propertyType) {
$param = Get-ParameterFromFunction -functionName $functionName -ast $ast -propertyName $propertyName
It "Function '$functionName' should have parameter '$propertyName' in $($moduleFile.Name) as its a defined in the .schema.mof file" {
$param | should not be $null
}
}
Describe "Module $($moduleFile.Name)" {
$tokens = $null;
$parseErrors = $null;
$ast = [System.Management.Automation.Language.Parser]::ParseFile($moduleFile.FullName, [ref] $tokens, [ref] $parseErrors);
It "The module $($moduleFile.Name) should have no parse errors" {
$parseErrors | should be $null
}
foreach($line in $schemaMofFileContent) {
if ($line -match "\s*(\[.*\])\s*(.*) (.*);") {
$propertyType = $matches[1]
$propertyName = $matches[3].Replace("[]", "");
$filter = { $true }
$result = $ast.FindAll($filter, $true);
$foundMatchingParameter = $false
Test-Parameter -functionName "Get-TargetResource" -ast $ast -propertyName $propertyName -moduleFile $moduleFile -propertyType $propertyType
Test-Parameter -functionName "Set-TargetResource" -ast $ast -propertyName $propertyName -moduleFile $moduleFile -propertyType $propertyType
Test-Parameter -functionName "Test-TargetResource" -ast $ast -propertyName $propertyName -moduleFile $moduleFile -propertyType $propertyType
}
}
}
}
}
Describe "Configuration Scenarios" {
$path = Resolve-Path "$PSCommandPath/../../Tests/Scenarios"
$configurationFiles = Get-ChildItem $path -Recurse -Filter *.ps1
foreach ($configurationFile in $configurationFiles) {
$confName = [System.Io.Path]::GetFileNameWithoutExtension($configurationFile.Name)
It "Configuration block in scenario $($configurationFile.Name) should have the same name as the file" {
$configurationFile.FullName | Should -FileContentMatch "Configuration $confName"
}
It "Scenario $($configurationFile.Name) should have a matching spec" {
$specName = $configurationFile.Name.Replace('.ps1', '_spec.rb').ToLower()
(Resolve-Path "$PSCommandPath/../../Tests/Spec/$specName") | Should -Exist
}
}
}