-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.ps1
More file actions
422 lines (339 loc) · 16.9 KB
/
test.ps1
File metadata and controls
422 lines (339 loc) · 16.9 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# ============================================
# Script Manager Comprehensive Test Suite
# ============================================
$global:PassCount = 0
$global:FailCount = 0
Write-Host "`n=== Script Manager Test Suite v2.0 ===`n" -ForegroundColor Cyan
# ------------------------------------------
# ASSERTION ENGINE
# ------------------------------------------
function Assert {
param (
[bool]$Condition,
[string]$Message
)
if ($Condition) {
$global:PassCount++
Write-Host "[PASS] $Message" -ForegroundColor Green
} else {
$global:FailCount++
Write-Host "[FAIL] $Message" -ForegroundColor Red
}
}
function Assert-Contains {
param($Output, $Expected, $Message)
Assert ($Output -match [regex]::Escape($Expected)) $Message
if ($Output -notmatch [regex]::Escape($Expected)) {
Write-Host "---- Expected: $Expected" -ForegroundColor DarkGray
Write-Host "---- Got: $($Output.Substring(0, [Math]::Min(200, $Output.Length)))" -ForegroundColor DarkYellow
}
}
function Assert-NotContains {
param($Output, $NotExpected, $Message)
Assert ($Output -notmatch [regex]::Escape($NotExpected)) $Message
}
function Assert-JsonValid {
param($JsonString, $Message)
try {
$null = $JsonString | ConvertFrom-Json
Assert $true $Message
} catch {
Assert $false $Message
}
}
function Assert-FileExists {
param($Path, $Message)
Assert (Test-Path $Path) $Message
}
# ============================================
# SETUP
# ============================================
Write-Host "`n=== Setup ===" -ForegroundColor Cyan
# Build
go build -o sm.exe main.go 2>&1 | Out-Null
Assert-FileExists "sm.exe" "Build succeeded"
# Clean slate
Remove-Item "$env:LOCALAPPDATA\sm\*" -Force -Recurse -ErrorAction SilentlyContinue
# ============================================
# TEST GROUP 1: Runtime Detection
# ============================================
Write-Host "`n=== Test Group 1: Runtime Detection ===" -ForegroundColor Cyan
$detectOutput = .\sm.exe runtimes detect --set-defaults 2>&1 | Out-String
Assert-Contains $detectOutput "python" "Runtime detection found Python"
$listRuntimes = .\sm.exe runtimes list 2>&1 | Out-String
Assert-Contains $listRuntimes "python" "Runtime list shows Python"
# ============================================
# TEST GROUP 2: Script Management (Add, List, Filters)
# ============================================
Write-Host "`n=== Test Group 2: Script Management ===" -ForegroundColor Cyan
# Create test scripts
"print('Deploy app')" | Out-File -Encoding UTF8 deploy.py
"print('Backup database')" | Out-File -Encoding UTF8 backup.py
"print('Monitor system')" | Out-File -Encoding UTF8 monitor.py
# Add scripts with tags
.\sm.exe script add deploy.py --name deploy --tag automation --tag production 2>&1 | Out-Null
.\sm.exe script add backup.py --name backup --tag automation --tag database 2>&1 | Out-Null
.\sm.exe script add monitor.py --name monitor --tag ops 2>&1 | Out-Null
# List all scripts
$listAll = .\sm.exe script list 2>&1 | Out-String
Assert-Contains $listAll "deploy" "List contains deploy"
Assert-Contains $listAll "backup" "List contains backup"
Assert-Contains $listAll "monitor" "List contains monitor"
# Table format
$listTable = .\sm.exe script list -f table 2>&1 | Out-String
Assert-Contains $listTable "deploy" "Table format works"
# JSON format
$listJson = .\sm.exe script list -f json 2>&1 | Out-String
Assert-JsonValid $listJson "JSON format is valid"
# Filter by tag
$filterTag = .\sm.exe script list --tag automation 2>&1 | Out-String
Assert-Contains $filterTag "deploy" "Tag filter finds deploy"
Assert-Contains $filterTag "backup" "Tag filter finds backup"
Assert-NotContains $filterTag "monitor" "Tag filter excludes monitor"
# Filter by name
$filterName = .\sm.exe script list --name backup 2>&1 | Out-String
Assert-Contains $filterName "backup" "Name filter finds backup"
Assert-NotContains $filterName "deploy" "Name filter excludes deploy"
# ============================================
# TEST GROUP 3: Script Execution
# ============================================
Write-Host "`n=== Test Group 3: Script Execution ===" -ForegroundColor Cyan
# Run script
$runOutput = .\sm.exe script run deploy 2>&1 | Out-String
Assert-Contains $runOutput "Deploy app" "Script executed successfully"
Assert-Contains $runOutput "success" "Execution status is success"
# Run with args
"import sys; print(f'Args: {sys.argv[1:]}')" | Out-File -Encoding UTF8 args-test.py
.\sm.exe script add args-test.py --name args-test 2>&1 | Out-Null
$argsOutput = .\sm.exe run args-test arg1 arg2 2>&1 | Out-String
Assert-Contains $argsOutput "arg1" "Script received arguments"
# ============================================
# TEST GROUP 4: Versioning (Manual)
# ============================================
Write-Host "`n=== Test Group 4: Manual Versioning ===" -ForegroundColor Cyan
# Create initial script
"print('Version 1')" | Out-File -Encoding UTF8 version-test.py
.\sm.exe script add version-test.py --name vtest 2>&1 | Out-Null
# Check initial version
$versionList1 = .\sm.exe version list vtest 2>&1 | Out-String
Assert-Contains $versionList1 "v1.0.0" "Initial version v1.0.0 created"
# Manual update version
"print('Version 2')" | Out-File -Encoding UTF8 version-test-v2.py
.\sm.exe version create vtest version-test-v2.py 2>&1 | Out-Null
$versionList2 = .\sm.exe version list vtest 2>&1 | Out-String
Assert-Contains $versionList2 "v1.0.1" "Update created v1.0.1"
# Rollback
.\sm.exe version rollback vtest v1.0.0 2>&1 | Out-Null
$content = Get-Content version-test.py -Raw
Assert-Contains $content "Version 1" "Rollback restored original content"
$versionList3 = .\sm.exe version list vtest 2>&1 | Out-String
Assert-Contains $versionList3 "v1.0.2" "Rollback created v1.0.2"
# ============================================
# TEST GROUP 5: Tag Management
# ============================================
Write-Host "`n=== Test Group 5: Tag Management ===" -ForegroundColor Cyan
# Add tags
.\sm.exe tag add deploy critical,scheduled 2>&1 | Out-Null
$infoAfterTag = .\sm.exe script info deploy 2>&1 | Out-String
Assert-Contains $infoAfterTag "critical" "Tag 'critical' added"
Assert-Contains $infoAfterTag "scheduled" "Tag 'scheduled' added"
# Idempotent add (duplicate)
.\sm.exe tag add deploy critical 2>&1 | Out-Null
$checkDuplicate = .\sm.exe tag list deploy 2>&1 | Out-String
$criticalCount = ([regex]::Matches($checkDuplicate, "critical")).Count
Assert ($criticalCount -eq 1) "Duplicate tags not added (idempotent)"
# Remove tag
.\sm.exe tag remove deploy scheduled 2>&1 | Out-Null
$infoAfterRemove = .\sm.exe script info deploy 2>&1 | Out-String
Assert-NotContains $infoAfterRemove "scheduled" "Tag 'scheduled' removed"
Assert-Contains $infoAfterRemove "critical" "Tag 'critical' still exists"
# List all tags
$allTags = .\sm.exe tag list --all 2>&1 | Out-String
Assert-Contains $allTags "automation" "All tags shows 'automation'"
Assert-Contains $allTags "critical" "All tags shows 'critical'"
# ============================================
# TEST GROUP 6: Script Rename
# ============================================
Write-Host "`n=== Test Group 6: Script Rename ===" -ForegroundColor Cyan
# Rename script
.\sm.exe script rename monitor system-monitor 2>&1 | Out-Null
$infoRenamed = .\sm.exe script info system-monitor 2>&1 | Out-String
Assert-Contains $infoRenamed "system-monitor" "Script renamed successfully"
# Old name should not exist
$oldNameCheck = .\sm.exe script info monitor 2>&1 | Out-String
Assert-Contains $oldNameCheck "not found" "Old name no longer accessible"
# ============================================
# TEST GROUP 7: Script Info
# ============================================
Write-Host "`n=== Test Group 7: Script Info ===" -ForegroundColor Cyan
$info = .\sm.exe script info deploy 2>&1 | Out-String
Assert-Contains $info "Name:" "Info shows name"
Assert-Contains $info "ID:" "Info shows ID"
Assert-Contains $info "Language:" "Info shows language"
Assert-Contains $info "Tags" "Info shows tags section"
Assert-Contains $info "Run Count:" "Info shows execution stats"
# JSON format
$infoJson = .\sm.exe script info deploy -f json 2>&1 | Out-String
Assert-JsonValid $infoJson "Info JSON format is valid"
# ============================================
# TEST GROUP 8: Execution History
# ============================================
Write-Host "`n=== Test Group 8: Execution History ===" -ForegroundColor Cyan
# Run script to create history
.\sm.exe script run backup 2>&1 | Out-Null
.\sm.exe script run backup 2>&1 | Out-Null
# Check history
$history = .\sm.exe script history backup 2>&1 | Out-String
Assert-Contains $history "success" "History shows executions"
Assert-Contains $history "Duration:" "History shows duration"
# JSON format
$historyJson = .\sm.exe script history backup -f json 2>&1 | Out-String
Assert-JsonValid $historyJson "History JSON format is valid"
# ============================================
# TEST GROUP 12: Script Removal with Options
# ============================================
Write-Host "`n=== Test Group 12: Script Removal with Options ===" -ForegroundColor Cyan
# Create a test script for removal
"print('Test removal')" | Out-File -Encoding UTF8 removal-test.py
.\sm.exe script add removal-test.py --name removal-test 2>&1 | Out-Null
.\sm.exe script run removal-test --log 2>&1 | Out-Null
# Get script ID for later verification
$removalInfo = .\sm.exe script info removal-test -f json 2>&1 | ConvertFrom-Json
$removalId = $removalInfo.id
# Remove with default (keep history)
.\sm.exe script script remove removal-test --force 2>&1 | Out-Null
$listAfterRemove = .\sm.exe list 2>&1 | Out-String
Assert-NotContains $listAfterRemove "removal-test" "Script removed from list"
# Verify history preserved
$historyPreserved = .\sm.exe script history $removalId 2>&1 | Out-String
Assert-Contains $historyPreserved "DELETED SCRIPT" "History accessible after deletion"
# Create another script to test deletion with flags
"print('Delete everything')" | Out-File -Encoding UTF8 delete-all-test.py
.\sm.exe script add delete-all-test.py --name delete-all 2>&1 | Out-Null
.\sm.exe script run delete-all --log 2>&1 | Out-Null
# Get ID before deletion
$deleteInfo = .\sm.exe script info delete-all -f json 2>&1 | ConvertFrom-Json
$deleteId = $deleteInfo.id
# Remove with delete-history flag
.\sm.exe script remove delete-all --force --delete-history 2>&1 | Out-Null
# History should not be accessible
$historyCheck = .\sm.exe script history $deleteId 2>&1 | Out-String
Assert-Contains $historyCheck "no executions found" "History deleted with --delete-history flag"
# ============================================
# TEST GROUP 10: Configuration Management
# ============================================
Write-Host "`n=== Test Group 10: Configuration Management ===" -ForegroundColor Cyan
# Initialize (creates config if not exists)
.\sm.exe init --force 2>&1 | Out-Null
# List config
$configList = .\sm.exe config list 2>&1 | Out-String
Assert-Contains $configList "Data:" "Config list shows data directory"
Assert-Contains $configList "Retention Count:" "Config list shows retention settings"
# Get specific config value
$retentionCount = .\sm.exe config get execution.retention.count 2>&1 | Out-String
Assert-Contains $retentionCount "execution.retention.count = " "Config get returns value"
# Set config value
.\sm.exe config set execution.retention.count 50 2>&1 | Out-Null
$newValue = .\sm.exe config get execution.retention.count 2>&1 | Out-String
Assert-Contains $newValue "50" "Config set updates value"
# Set log retention
.\sm.exe config set execution.logs.retention_days 60 2>&1 | Out-Null
$logRetention = .\sm.exe config get execution.logs.retention_days 2>&1 | Out-String
Assert-Contains $logRetention "60" "Log retention config works"
# Set detection enabled
.\sm.exe config set detection.enabled false 2>&1 | Out-Null
$detectionValue = .\sm.exe config get detection.enabled 2>&1 | Out-String
Assert-Contains $detectionValue "false" "Boolean config values work"
# ============================================
# TEST GROUP 11: Cleanup Old Executions & Logs
# ============================================
Write-Host "`n=== Test Group 11: Cleanup Old Executions & Logs ===" -ForegroundColor Cyan
# Dry run cleanup
$cleanupDry = .\sm.exe cleanup --dry-run 2>&1 | Out-String
Assert-Contains $cleanupDry "Cleanup configuration:" "Cleanup dry-run shows configuration"
Assert-Contains $cleanupDry "DRY RUN" "Cleanup indicates dry-run mode"
# Actual cleanup with custom retention
$cleanup = .\sm.exe cleanup --keep 1 --older-than 0 2>&1 | Out-String
Assert-Contains $cleanup "Cleaning" "Cleanup executed"
# Cleanup with log retention
$cleanupLogs = .\sm.exe cleanup --logs-older-than 365 --dry-run 2>&1 | Out-String
Assert-Contains $cleanupLogs "log folder" "Cleanup handles log folders"
# ============================================
# TEST GROUP 13: Chain Execution
# ============================================
Write-Host "`n=== Test Group 13: Chain Execution ===" -ForegroundColor Cyan
# Create test scripts for chaining
"print('Step 1: Build')" | Out-File -Encoding UTF8 chain-build.py
"print('Step 2: Test')" | Out-File -Encoding UTF8 chain-test.py
"print('Step 3: Deploy')" | Out-File -Encoding UTF8 chain-deploy.py
"import sys; print('Step 4: Fail'); sys.exit(1)" | Out-File -Encoding UTF8 chain-fail.py
.\sm.exe script add chain-build.py --name chain-build 2>&1 | Out-Null
.\sm.exe script add chain-test.py --name chain-test 2>&1 | Out-Null
.\sm.exe script add chain-deploy.py --name chain-deploy 2>&1 | Out-Null
.\sm.exe script add chain-fail.py --name chain-fail 2>&1 | Out-Null
# Test simple chain (all success)
$simpleChain = .\sm.exe chain chain-build chain-test chain-deploy 2>&1 | Out-String
Assert-Contains $simpleChain "Starting chain execution" "Chain starts with header"
Assert-Contains $simpleChain "Step 1/3" "Chain shows step numbers"
Assert-Contains $simpleChain "Step 2/3" "Chain executes second step"
Assert-Contains $simpleChain "Step 3/3" "Chain executes third step"
Assert-Contains $simpleChain "Chain Summary" "Chain shows summary"
Assert-Contains $simpleChain "Succeeded: 3" "Chain counts successes"
Assert-Contains $simpleChain "Chain completed successfully" "Chain completes successfully"
# Test chain with failure (should stop)
$failChain = .\sm.exe chain chain-build chain-fail chain-deploy 2>&1 | Out-String
Assert-Contains $failChain "Failed" "Chain detects failure"
Assert-Contains $failChain "Chain stopped" "Chain stops on failure"
Assert-NotContains $failChain "Step 3/3" "Chain does not execute steps after failure"
# Test chain with --continue flag
$continueChain = .\sm.exe chain --continue chain-build chain-fail chain-deploy 2>&1 | Out-String
Assert-Contains $continueChain "Continuing to next step" "Chain continues on failure with --continue"
Assert-Contains $continueChain "Step 3/3" "Chain executes remaining steps with --continue"
Assert-Contains $continueChain "Succeeded: 2" "Chain counts partial successes"
Assert-Contains $continueChain "Failed: 1" "Chain counts failures"
# Test chain with --log flag
$logChain = .\sm.exe chain --log chain-build chain-test 2>&1 | Out-String
Assert-Contains $logChain "Log saved:" "Chain saves log with --log flag"
# Test chain file
$chainFileContent = @"
# Test chain file
chain-build
chain-test
chain-deploy
"@
$chainFileContent | Out-File -Encoding UTF8 test-workflow.chain
$chainFile = .\sm.exe chain test-workflow.chain 2>&1 | Out-String
Assert-Contains $chainFile "Step 1/3" "Chain file executes first step"
Assert-Contains $chainFile "Step 2/3" "Chain file executes second step"
Assert-Contains $chainFile "Step 3/3" "Chain file executes third step"
Assert-Contains $chainFile "Chain completed successfully" "Chain file completes successfully"
# Test chain file with comments and empty lines
$chainFileWithComments = @"
# Build and test workflow
chain-build
# Run tests
chain-test
# Deploy if all good
chain-deploy
"@
$chainFileWithComments | Out-File -Encoding UTF8 commented-workflow.chain
$commentedChain = .\sm.exe chain commented-workflow.chain 2>&1 | Out-String
Assert-Contains $commentedChain "Steps: 3" "Chain file ignores comments and empty lines"
Assert-Contains $commentedChain "Chain completed successfully" "Chain file with comments works"
# ============================================
# TEST SUMMARY
# ============================================
Write-Host "`n=== TEST SUMMARY ===" -ForegroundColor Cyan
Write-Host "Passed: $global:PassCount" -ForegroundColor Green
Write-Host "Failed: $global:FailCount" -ForegroundColor Red
if ($global:FailCount -eq 0) {
Write-Host "`n🎉 ALL TESTS PASSED SUCCESSFULLY!" -ForegroundColor Green
exit 0
} else {
Write-Host "`n❌ $global:FailCount TEST(S) FAILED!" -ForegroundColor Red
exit 1
}
# ============================================
# Cleanup
# ============================================
Remove-Item deploy.py, backup.py, monitor.py, args-test.py, version-test.py, version-test-v2.py, removal-test.py, delete-all-test.py, chain-build.py, chain-test.py, chain-deploy.py, chain-fail.py, test-workflow.chain, commented-workflow.chain -ErrorAction SilentlyContinue