Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ jobs:
- run: "./pplog.exe -v"
- shell: bash # naked `echo` won't work on windows
run: "./pplog.exe -d bash -c 'echo \\{\\\"time\\\":\\\"2024-12-02T12:00:00Z\\\",\\\"level\\\":\\\"INFO\\\",\\\"msg\\\":\\\"Hello\\\"\\}'"
- run: |
.\behavioral-tests\windows-tests\run.ps1
shell: pwsh
test:
runs-on: ubuntu-latest
timeout-minutes: 10
Expand Down
5 changes: 5 additions & 0 deletions behavioral-tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ for cs in $(find . -mindepth 1 -maxdepth 1 -type d | sort)
do
(
cd $cs
if test ! -e expected.log
then
echo "SKIP: $cs"
exit 0
fi
if test -x ./custom-run.sh
then
./custom-run.sh
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Write-Output "Arguments: $args"

$sendCtrlBreakScriptPath = Join-Path -Path $PSScriptRoot -ChildPath "custom-send-break.ps1"
$proc = Start-Process -FilePath "powershell.exe" -ArgumentList "-File `"$sendCtrlBreakScriptPath`"" -PassThru -NoNewWindow

try {
while ($true) {
Start-Sleep -Seconds 5
}
} finally {
# windows does not allow to write into pipe after interruption (Write-Output)
# we can only write to console directly
Write-Host "Getting SIGNAL SIGINT. Exiting"
}

13 changes: 13 additions & 0 deletions behavioral-tests/windows-tests/args-and-signals/custom-run.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
try {
go run ..\..\..\cmd\... powershell .\custom-fake-server.ps1 ARG1 ARG2 |
Tee-Object -FilePath output.log
} finally {
# there is windows limitation: Write-Output (write to pipe)
# does not write anything after interruption.
# we can write directly to console using Write-Host,
# but Tee-Object does not receive this record too.
# the only way to get record in output log file is
# to write it manually:
# $msg = 'some graceful shutdown logs..."'
# $msg | Out-File -FilePath "output.log" -Append
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Add-Type @"
using System;
using System.Runtime.InteropServices;

public class NativeMethods {
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GenerateConsoleCtrlEvent(uint dwCtrlEvent, uint dwProcessGroupId);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool FreeConsole();

public const uint CTRL_C_EVENT = 0;
public const uint CTRL_BREAK_EVENT = 1;
}
"@

function Send-CtrlBreakEvent {
param (
[int]$parentPid
)

[NativeMethods]::GenerateConsoleCtrlEvent([NativeMethods]::CTRL_C_EVENT, 0)
[NativeMethods]::FreeConsole()
}

Write-Output 'Sleeping...'
Start-Sleep -Seconds 1
Write-Output 'Going to kill parent process...'

$parentPid = (Get-WmiObject Win32_Process -Filter "ProcessId=$PID").ParentProcessId
$null = Send-CtrlBreakEvent -parentPid $parentPid
3 changes: 3 additions & 0 deletions behavioral-tests/windows-tests/args-and-signals/expected.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
INVALID JSON: "Arguments: ARG1 ARG2\r"
INVALID JSON: "Sleeping...\r"
INVALID JSON: "Going to kill parent process...\r"
3 changes: 3 additions & 0 deletions behavioral-tests/windows-tests/custom/expected.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
12:00:00 INFO OK source.file=/Users/slog/main.go source.function=main.main source.line=14 user=1
NOJSON: Broken raw error message
12:05:00 INFO OK source.file=/Users/slog/main.go source.function=main.main source.line=14 user=2
36 changes: 36 additions & 0 deletions behavioral-tests/windows-tests/custom/pplog.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# PPLog config
#
# GitHub: https://github.com/michurin/human-readable-json-logging
# Install: go install -v github.com/michurin/human-readable-json-logging/cmd/...@latest
#
# Configuration file syntax: systemd env-files https://www.freedesktop.org/software/systemd/man/latest/systemd.exec.html#EnvironmentFile=
#
# Configuration variables:
# - PPLOG_LOGLINE for JSON-lines
# - PPLOG_ERRLINE for non-JSON-lines
#
# Templates: go standard test/template: https://pkg.go.dev/text/template
#
# Colors: terminal standard color sequences. `\e` considering as '\033' (escape)
#
# Color hints:
#
# Text colors Text High Background Hi Background Decoration
# ------------------ ------------------ ------------------ ------------------- --------------------
# \e[30mBlack \e[0m \e[90mBlack \e[0m \e[40mBlack \e[0m \e[100mBlack \e[0m \e[1mBold \e[0m
# \e[31mRed \e[0m \e[91mRed \e[0m \e[41mRed \e[0m \e[101mRed \e[0m \e[4mUnderline \e[0m
# \e[32mGreen \e[0m \e[92mGreen \e[0m \e[42mGreen \e[0m \e[102mGreen \e[0m \e[7mReverse \e[0m
# \e[33mYellow \e[0m \e[93mYellow \e[0m \e[43mYellow \e[0m \e[103mYellow \e[0m
# \e[34mBlue \e[0m \e[94mBlue \e[0m \e[44mBlue \e[0m \e[104mBlue \e[0m Combinations
# \e[35mMagenta\e[0m \e[95mMagenta\e[0m \e[45mMagenta\e[0m \e[105mMagenta\e[0m -----------------------
# \e[36mCyan \e[0m \e[96mCyan \e[0m \e[46mCyan \e[0m \e[106mCyan \e[0m \e[1;4;103;31mWARN\e[0m
# \e[37mWhite \e[0m \e[97mWhite \e[0m \e[47mWhite \e[0m \e[107mWhite \e[0m

PPLOG_LOGLINE='
{{- if .time }}{{ .time | tmf "2006-01-02T15:04:05Z07:00" "15:04:05" }}{{ end }}
{{- if .level }} {{ if eq .level "INFO" }}\e[32m{{ end }}{{ if eq .level "ERROR" }}\e[91m{{ end }}{{ .level }}\e[0m{{ end }}
{{- if .msg }} \e[97m{{ .msg }}\e[0m{{ end }}
{{- range .ALL | rm "time" "level" "msg" }} \e[33m{{ .K }}\e[0m={{ .V }}{{ end }}
'

PPLOG_ERRLINE='\e[7mNOJSON:\e[0m \e[97m{{ .TEXT | trimSpace }}\e[0m'
3 changes: 3 additions & 0 deletions behavioral-tests/windows-tests/default/expected.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2024-12-02T12:00:00-05:00 [INFO] OK source.file=/Users/slog/main.go source.function=main.main source.line=14 user=1
INVALID JSON: "Broken raw error message\r"
2024-12-02T12:05:00-05:00 [INFO] OK source.file=/Users/slog/main.go source.function=main.main source.line=14 user=2
3 changes: 3 additions & 0 deletions behavioral-tests/windows-tests/fake-server.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Write-Output '{"time":"2024-12-02T12:00:00-05:00","level":"INFO","source":{"function":"main.main","file":"/Users/slog/main.go","line":14},"msg":"OK","user":1}'
Write-Output 'Broken raw error message'
Write-Output '{"time":"2024-12-02T12:05:00-05:00","level":"INFO","source":{"function":"main.main","file":"/Users/slog/main.go","line":14},"msg":"OK","user":2}'
3 changes: 3 additions & 0 deletions behavioral-tests/windows-tests/pipe-mode/custom-run.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
powershell ..\fake-server.ps1 |
go run ..\..\..\cmd\... |
Tee-Object -FilePath output.log
3 changes: 3 additions & 0 deletions behavioral-tests/windows-tests/pipe-mode/expected.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2024-12-02T12:00:00-05:00 [INFO] OK source.file=/Users/slog/main.go source.function=main.main source.line=14 user=1
INVALID JSON: "Broken raw error message\r"
2024-12-02T12:05:00-05:00 [INFO] OK source.file=/Users/slog/main.go source.function=main.main source.line=14 user=2
71 changes: 71 additions & 0 deletions behavioral-tests/windows-tests/run.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
$invoke_path = $(Get-Location)
$tests_path = Split-Path -Parent $MyInvocation.MyCommand.Path

function doTests
{
param ($InvokePath)

Set-Location -Path $InvokePath

$directories = Get-ChildItem -Directory

$failedTests = @() # TODO remove after debug

foreach ($cs in $directories)
{
Set-Location -Path $cs.FullName

Write-Output "" | Tee-Object -FilePath output.log

if (Test-Path .\custom-run.ps1 -PathType Leaf)
{
$cmd = {
.\custom-run.ps1
}
}
else
{
$cmd = {
go run ..\..\..\cmd\... powershell ..\fake-server.ps1 | Tee-Object -FilePath output.log
}
}

$P = Start-Process -PassThru -FilePath "powershell.exe" -ArgumentList "-Command", $cmd.ToString()
$P.WaitForExit()

Write-Output $cs.name
Get-Content -Path output.log

$diff = Compare-Object -ReferenceObject (Get-Content expected.log) -DifferenceObject (Get-Content output.log) -SyncWindow 0

if ($null -ne $diff)
{
Write-Output "${cs}: difference detected in log files"
Write-Output $diff
$failedTests += $cs
# TODO just for debug... failfast later
# throw "Difference detected in log files."
}
else {
Remove-Item -Path output.log -Force
}
}

# TODO remove after debug
if ($failedTests.Count -gt 0) {
$cnt = $failedTests.Count
Write-Output "Difference detected in log files in ${cnt} tests: $failedTests"
throw "Difference detected in log files."
}

}

try {
doTests $tests_path
Write-Output "OK"
} catch {
Write-Output "FAIL"
exit 1
} finally {
Set-Location $invoke_path
}