-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun-simple-test.ps1
More file actions
180 lines (154 loc) · 6.47 KB
/
run-simple-test.ps1
File metadata and controls
180 lines (154 loc) · 6.47 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
param(
[Parameter(Mandatory=$true)]
[string]$MsiFile
)
function Play-CompletionSound {
param([bool]$Success)
if ($Success) {
# Three ascending beeps for success
[Console]::Beep(800, 200)
[Console]::Beep(1000, 200)
[Console]::Beep(1200, 200)
} else {
# Two descending beeps for failure
[Console]::Beep(400, 300)
[Console]::Beep(200, 300)
}
}
Write-Host "=== Simple Chef Docker Container Test ===" -ForegroundColor Cyan
Write-Host "Testing with MSI file: $MsiFile" -ForegroundColor Yellow
# Validate MSI file exists
if (-not (Test-Path $MsiFile)) {
Write-Host "Error: MSI file not found: $MsiFile" -ForegroundColor Red
Play-CompletionSound -Success $false
exit 1
}
$msiPath = Resolve-Path $MsiFile
Write-Host ""
# Clean shared directory
Write-Host "Cleaning shared directory..." -ForegroundColor Yellow
if (Test-Path ".\shared") {
Remove-Item ".\shared\*" -Force -ErrorAction SilentlyContinue
} else {
New-Item -ItemType Directory -Path ".\shared" | Out-Null
}
# Copy test file to shared directory
$testFile = ".\test-chef-ps.rb"
if (Test-Path $testFile) {
Copy-Item $testFile ".\shared\test-chef-ps.rb" -Force
Write-Host "Copied test-chef-ps.rb to shared directory" -ForegroundColor Green
} else {
Write-Host "Warning: test-chef-ps.rb not found in current directory" -ForegroundColor Yellow
}
# Copy MSI to root directory as chef-installer.msi for Dockerfile
$tempMsi = ".\chef-installer.msi"
Copy-Item $msiPath $tempMsi -Force
# Extract version from MSI filename or use "msi" as version
if ($msiPath -match 'chef-(\d+\.\d+\.\d+)') {
$extractedVersion = $matches[1]
$imageTag = "simple-$extractedVersion"
} else {
$extractedVersion = "msi"
$imageTag = "simple"
}
Write-Host "`n=== Building Chef MSI container ===" -ForegroundColor Green
docker build -f Dockerfile.msi -t chef-test:$imageTag .
Remove-Item $tempMsi -Force
if ($LASTEXITCODE -ne 0) {
Write-Host "Failed to build Chef MSI image" -ForegroundColor Red
Play-CompletionSound -Success $false
exit 1
}
# Run pre-check
Write-Host "`n=== Running Chef MSI container ===" -ForegroundColor Green
Write-Host "Verifying Chef installation..." -ForegroundColor Yellow
docker run --rm `
-v "${PWD}\shared:C:\shared" `
chef-test:$imageTag `
powershell -Command {
Write-Host '=== Chef Installation Verification ==='
$chefVersion = (chef-client --version) -replace 'Chef Infra Client: ', ''
Write-Host "Chef Version: $chefVersion"
$timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
$computerName = $env:COMPUTERNAME
$chefClientPath = Get-Command chef-client | Select-Object -ExpandProperty Source
$output = "Chef Version: $chefVersion`nInstallation Method: MSI`nComputer Name: $computerName`nTimestamp: $timestamp`nChef Client Path: $chefClientPath"
$fileTimestamp = Get-Date -Format 'yyyyMMdd-HHmmss'
$outputFile = "C:\shared\chef-$chefVersion-$fileTimestamp.txt"
Write-Host ''
Write-Host "Writing initial results to $outputFile"
$output | Out-File -FilePath $outputFile -Encoding UTF8
Write-Host ''
}
# Run chef recipe
Write-Host "Running Chef recipe..." -ForegroundColor Yellow
docker run --rm `
-e CHEF_LICENSE=accept-silent `
-v "${PWD}\shared:C:\shared" `
-v "${PWD}\cookbooks:C:\cookbooks" `
chef-test:$imageTag `
powershell -Command {
$env:PATH="$env:PATH;C:\opscode\chef\embedded\lib\ruby\gems\3.1.0\gems\chef-powershell-18.1.0\bin\ruby_bin_folder\AMD64;C:\opscode\chef\embedded\lib\ruby\gems\3.1.0\gems\chef-powershell-18.1.0\bin\ruby_bin_folder\AMD64\shared\Microsoft.NETCore.App\5.0.0"
Write-Host "Path: $env:PATH"
chef-client -z -o recipe[test_recipe] --chef-license accept-silent
}
if ($LASTEXITCODE -ne 0) {
Write-Host "Chef MSI execution failed" -ForegroundColor Red
Write-Host "`n=== Chef run failed - Starting interactive container for debugging ===" -ForegroundColor Yellow
Write-Host "You will be dropped into a PowerShell session in the container." -ForegroundColor Cyan
Write-Host "Type 'exit' to leave the container when you're done debugging." -ForegroundColor Cyan
Write-Host ""
# Start an interactive container for debugging
docker run --rm -it `
-e CHEF_LICENSE=accept-silent `
-v "${PWD}\shared:C:\shared" `
-v "${PWD}\cookbooks:C:\cookbooks" `
chef-test:$imageTag `
powershell
Play-CompletionSound -Success $false
exit 1
}
# Run chef-powershell test suite
Write-Host "`n=== Running chef-powershell test suite ===" -ForegroundColor Green
if (Test-Path ".\shared\test-chef-ps.rb") {
docker run --rm `
-e CHEF_LICENSE=accept-silent `
-v "${PWD}\shared:C:\shared" `
chef-test:$imageTag `
powershell -Command {
Write-Host "=== Chef PowerShell Test Suite ==="
$rubyPath = "C:\opscode\chef\embedded\bin\ruby.exe"
$testFile = "C:\shared\test-chef-ps.rb"
if (Test-Path $rubyPath) {
Write-Host "Ruby path: $rubyPath"
Write-Host "Running test file: $testFile"
Write-Host ""
& $rubyPath $testFile
if ($LASTEXITCODE -ne 0) {
Write-Host "`nTest suite failed with exit code: $LASTEXITCODE" -ForegroundColor Red
exit $LASTEXITCODE
} else {
Write-Host "`nTest suite completed successfully" -ForegroundColor Green
}
} else {
Write-Host "Error: Ruby not found at $rubyPath" -ForegroundColor Red
exit 1
}
}
if ($LASTEXITCODE -ne 0) {
Write-Host "`nchef-powershell test suite failed" -ForegroundColor Red
Write-Host "Starting interactive container for debugging..." -ForegroundColor Yellow
docker run --rm -it `
-e CHEF_LICENSE=accept-silent `
-v "${PWD}\shared:C:\shared" `
chef-test:$imageTag `
powershell
Play-CompletionSound -Success $false
exit 1
}
} else {
Write-Host "Warning: test-chef-ps.rb not found in shared directory, skipping test suite" -ForegroundColor Yellow
}
Write-Host "`n=== Test completed successfully ===" -ForegroundColor Green
Write-Host "Check the shared directory for output files" -ForegroundColor Yellow
Play-CompletionSound -Success $true