-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathvalidate.ps1
More file actions
339 lines (281 loc) · 9.95 KB
/
Copy pathvalidate.ps1
File metadata and controls
339 lines (281 loc) · 9.95 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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Randomly validates Pike resource/datasource mappings against test files
.DESCRIPTION
Randomly selects resources/datasources from Pike's lookup maps and:
- Checks if test files exist in terraform/{provider}/backup/
- Optionally runs empirical lifecycle tests
- Compares discovered permissions vs JSON mappings
- Reports discrepancies
.PARAMETER Provider
Cloud provider to test (google, aws, azurerm)
.PARAMETER Resources
Number of random resources to validate (default: 10)
.PARAMETER Datasources
Number of random datasources to validate (default: 5)
.PARAMETER RunTests
Actually run terraform apply/destroy lifecycle tests
.PARAMETER Seed
Random seed for reproducible selection (default: random)
.EXAMPLE
pwsh validate.ps1 -Provider google -Resources 10 -Datasources 5
pwsh validate.ps1 -Provider google -Resources 5 -RunTests
pwsh validate.ps1 -Provider google -Resources 10 -Seed 42
#>
param(
[ValidateSet("google", "aws", "azurerm")]
[string]$Provider = "google",
[int]$Resources = 10,
[int]$Datasources = 5,
[switch]$RunTests,
[int]$Seed = (Get-Random)
)
function path() {
if ($Env:OS -eq "Windows_NT") {
$slash = "\"
}
else {
$slash = "/"
}
$path = $args[0]
for ($i = 1; $i -lt $args.count; $i++) {
$path += $slash + $args[$i]
}
return $path
}
# Provider configurations
$providerConfigs = @{
google = @{
Name = "GCP"
ResourceLookupFile = "gcp.go"
ResourceLookupMap = "gCPTfLookup"
DataLookupFile = "gcp_datasource.go"
DataLookupMap = "TFLookup"
BackupDir = "terraform/google/backup"
MakeCommand = "make -C terraform/google"
}
aws = @{
Name = "AWS"
ResourceLookupFile = "aws.go"
ResourceLookupMap = "tFLookup"
DataLookupFile = "aws_datasource.go"
DataLookupMap = "tFLookupDataAWS"
BackupDir = "terraform/aws/backup"
MakeCommand = "make -C terraform/aws"
}
azurerm = @{
Name = "Azure"
ResourceLookupFile = "azure.go"
ResourceLookupMap = "tFLookupAzure"
DataLookupFile = "azure_datasource.go"
DataLookupMap = "TFLookupAzureData"
BackupDir = "terraform/azurerm/backup"
MakeCommand = "make -C terraform/azurerm"
}
}
function Extract-LookupMapKeys {
param(
[string]$FilePath,
[string]$MapName
)
if (-not (Test-Path $FilePath)) {
Write-Host " ⚠ File not found: $FilePath" -ForegroundColor Yellow
return @()
}
$content = Get-Content $FilePath -Raw
# Extract all resource names from the lookup map
$matches = [regex]::Matches($content, '^\s*"([^"]+)":\s+\w+,\s*(?://.*)?$', [System.Text.RegularExpressions.RegexOptions]::Multiline)
$keys = @()
foreach ($match in $matches) {
$keys += $match.Groups[1].Value
}
return $keys
}
function Find-TestFile {
param(
[string]$ResourceName,
[string]$BackupDir,
[string]$Type # "resource" or "data"
)
$basePath = path $PSScriptRoot $BackupDir
if (-not (Test-Path $basePath)) {
return $null
}
# Check for different naming patterns
if ($Type -eq "data") {
$patterns = @(
"data.$ResourceName.tf",
"$ResourceName.tf"
)
}
else {
$patterns = @(
"$ResourceName.tf",
"resource.$ResourceName.tf"
)
}
foreach ($pattern in $patterns) {
$testFile = path $basePath $pattern
if (Test-Path $testFile) {
return $testFile
}
}
return $null
}
function Select-RandomItems {
param(
[array]$Items,
[int]$Count,
[int]$Seed
)
if ($Items.Count -eq 0) {
return @()
}
$actualCount = [Math]::Min($Count, $Items.Count)
# Use seed for reproducibility
Get-Random -InputObject $Items -Count $actualCount -SetSeed $Seed
}
function Test-ResourceLifecycle {
param(
[string]$TestFile,
[string]$MakeCommand
)
# This would run the actual terraform lifecycle
# For now, return placeholder
return @{
Success = $true
DiscoveredPermissions = @()
Errors = @()
}
}
# Main script
$config = $providerConfigs[$Provider]
Write-Host "`nPike Validation - Random Resource Testing" -ForegroundColor Cyan
Write-Host ("═" * 70)
Write-Host "Provider: $($config.Name)" -ForegroundColor Gray
Write-Host "Resources: $Resources" -ForegroundColor Gray
Write-Host "Datasources: $Datasources" -ForegroundColor Gray
Write-Host "Seed: $Seed" -ForegroundColor Gray
Write-Host "Run Tests: $RunTests" -ForegroundColor Gray
Write-Host ("═" * 70)
# Extract resource keys from lookup maps
Write-Host "`n📋 Reading lookup maps..." -ForegroundColor Cyan
$resourceLookupPath = path $PSScriptRoot "src" $config.ResourceLookupFile
$allResources = Extract-LookupMapKeys -FilePath $resourceLookupPath -MapName $config.ResourceLookupMap
Write-Host " Found $($allResources.Count) resources in $($config.ResourceLookupMap)" -ForegroundColor Gray
$dataLookupPath = path $PSScriptRoot "src" $config.DataLookupFile
$allDatasources = Extract-LookupMapKeys -FilePath $dataLookupPath -MapName $config.DataLookupMap
Write-Host " Found $($allDatasources.Count) datasources in $($config.DataLookupMap)" -ForegroundColor Gray
# Select random resources
Write-Host "`n🎲 Selecting random items (seed: $Seed)..." -ForegroundColor Cyan
$selectedResources = Select-RandomItems -Items $allResources -Count $Resources -Seed $Seed
Write-Host " Selected $($selectedResources.Count) resources" -ForegroundColor Gray
$selectedDatasources = Select-RandomItems -Items $allDatasources -Count $Datasources -Seed ($Seed + 1)
Write-Host " Selected $($selectedDatasources.Count) datasources" -ForegroundColor Gray
# Validate resources
Write-Host "`n📦 Validating Resources" -ForegroundColor Cyan
Write-Host ("─" * 70)
$resourceResults = @()
foreach ($resourceName in $selectedResources) {
$testFile = Find-TestFile -ResourceName $resourceName -BackupDir $config.BackupDir -Type "resource"
$result = @{
Name = $resourceName
Type = "resource"
TestFile = $testFile
HasTestFile = $null -ne $testFile
Status = "unknown"
}
if ($testFile) {
Write-Host " ✓ $resourceName" -ForegroundColor Green
Write-Host " Test file: $testFile" -ForegroundColor DarkGray
$result.Status = "has_test"
if ($RunTests) {
Write-Host " Running lifecycle tests..." -ForegroundColor Cyan
# TODO: Implement actual test run
$result.Status = "tested"
}
}
else {
Write-Host " ⚠ $resourceName" -ForegroundColor Yellow
Write-Host " No test file found in $($config.BackupDir)" -ForegroundColor DarkGray
$result.Status = "no_test"
}
$resourceResults += $result
}
# Validate datasources
Write-Host "`n📊 Validating Datasources" -ForegroundColor Cyan
Write-Host ("─" * 70)
$datasourceResults = @()
foreach ($dsName in $selectedDatasources) {
$testFile = Find-TestFile -ResourceName $dsName -BackupDir $config.BackupDir -Type "data"
$result = @{
Name = $dsName
Type = "datasource"
TestFile = $testFile
HasTestFile = $null -ne $testFile
Status = "unknown"
}
if ($testFile) {
Write-Host " ✓ $dsName" -ForegroundColor Green
Write-Host " Test file: $testFile" -ForegroundColor DarkGray
$result.Status = "has_test"
if ($RunTests) {
Write-Host " Running lifecycle tests..." -ForegroundColor Cyan
# TODO: Implement actual test run
$result.Status = "tested"
}
}
else {
Write-Host " ⚠ $dsName" -ForegroundColor Yellow
Write-Host " No test file found in $($config.BackupDir)" -ForegroundColor DarkGray
$result.Status = "no_test"
}
$datasourceResults += $result
}
# Summary
Write-Host "`n" + ("═" * 70)
Write-Host "📈 Summary" -ForegroundColor Cyan
Write-Host ("─" * 70)
$allResults = $resourceResults + $datasourceResults
$withTests = ($allResults | Where-Object { $_.HasTestFile }).Count
$withoutTests = ($allResults | Where-Object { -not $_.HasTestFile }).Count
$tested = ($allResults | Where-Object { $_.Status -eq "tested" }).Count
Write-Host "`nResources with test files: $withTests / $($allResults.Count)" -ForegroundColor Green
Write-Host "Resources without test files: $withoutTests / $($allResults.Count)" -ForegroundColor Yellow
if ($RunTests) {
Write-Host "Successfully tested: $tested / $withTests" -ForegroundColor Green
}
# Missing test files report
if ($withoutTests -gt 0) {
Write-Host "`n⚠ Missing Test Files:" -ForegroundColor Yellow
foreach ($item in $allResults | Where-Object { -not $_.HasTestFile }) {
Write-Host " - $($item.Name) [$($item.Type)]" -ForegroundColor DarkGray
}
}
# Coverage percentage
$coveragePercent = [math]::Round(($withTests / $allResults.Count) * 100, 2)
Write-Host "`nTest Coverage: $coveragePercent%" -ForegroundColor Cyan
# Save results to JSON
$reportPath = path $PSScriptRoot ".pike" "validation-report-$(Get-Date -Format 'yyyyMMdd-HHmmss').json"
$reportDir = Split-Path $reportPath -Parent
if (-not (Test-Path $reportDir)) {
New-Item -ItemType Directory -Path $reportDir | Out-Null
}
$report = @{
Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Provider = $Provider
Seed = $Seed
Summary = @{
TotalTested = $allResults.Count
WithTestFiles = $withTests
WithoutTestFiles = $withoutTests
CoveragePercent = $coveragePercent
}
Resources = $resourceResults
Datasources = $datasourceResults
}
$report | ConvertTo-Json -Depth 10 | Out-File -FilePath $reportPath -Encoding UTF8
Write-Host "`n💾 Report saved to: $reportPath" -ForegroundColor Cyan
Write-Host "`n✓ Validation complete!" -ForegroundColor Green
Write-Host ""