From f396aa67f7c1a9ea24b04edc00765cc2b5cd723e Mon Sep 17 00:00:00 2001 From: Vitaly Shatskikh Date: Tue, 25 Jun 2024 01:18:47 +0300 Subject: [PATCH] integration tests for windows --- .github/workflows/ci.yaml | 3 + behavioral-tests/run.sh | 5 ++ .../args-and-signals/custom-fake-server.ps1 | 15 ++++ .../args-and-signals/custom-run.ps1 | 13 ++++ .../args-and-signals/custom-send-break.ps1 | 31 ++++++++ .../args-and-signals/expected.log | 3 + .../windows-tests/custom/expected.log | 3 + .../windows-tests/custom/pplog.env | 36 ++++++++++ .../windows-tests/default/expected.log | 3 + .../windows-tests/fake-server.ps1 | 3 + .../windows-tests/pipe-mode/custom-run.ps1 | 3 + .../windows-tests/pipe-mode/expected.log | 3 + behavioral-tests/windows-tests/run.ps1 | 71 +++++++++++++++++++ 13 files changed, 192 insertions(+) create mode 100644 behavioral-tests/windows-tests/args-and-signals/custom-fake-server.ps1 create mode 100644 behavioral-tests/windows-tests/args-and-signals/custom-run.ps1 create mode 100644 behavioral-tests/windows-tests/args-and-signals/custom-send-break.ps1 create mode 100644 behavioral-tests/windows-tests/args-and-signals/expected.log create mode 100644 behavioral-tests/windows-tests/custom/expected.log create mode 100644 behavioral-tests/windows-tests/custom/pplog.env create mode 100644 behavioral-tests/windows-tests/default/expected.log create mode 100644 behavioral-tests/windows-tests/fake-server.ps1 create mode 100644 behavioral-tests/windows-tests/pipe-mode/custom-run.ps1 create mode 100644 behavioral-tests/windows-tests/pipe-mode/expected.log create mode 100644 behavioral-tests/windows-tests/run.ps1 diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 1daa39f..b0579e1 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -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 diff --git a/behavioral-tests/run.sh b/behavioral-tests/run.sh index cac31c4..1576993 100755 --- a/behavioral-tests/run.sh +++ b/behavioral-tests/run.sh @@ -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 diff --git a/behavioral-tests/windows-tests/args-and-signals/custom-fake-server.ps1 b/behavioral-tests/windows-tests/args-and-signals/custom-fake-server.ps1 new file mode 100644 index 0000000..58d5164 --- /dev/null +++ b/behavioral-tests/windows-tests/args-and-signals/custom-fake-server.ps1 @@ -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" +} + diff --git a/behavioral-tests/windows-tests/args-and-signals/custom-run.ps1 b/behavioral-tests/windows-tests/args-and-signals/custom-run.ps1 new file mode 100644 index 0000000..19309d9 --- /dev/null +++ b/behavioral-tests/windows-tests/args-and-signals/custom-run.ps1 @@ -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 +} \ No newline at end of file diff --git a/behavioral-tests/windows-tests/args-and-signals/custom-send-break.ps1 b/behavioral-tests/windows-tests/args-and-signals/custom-send-break.ps1 new file mode 100644 index 0000000..a8b1f7d --- /dev/null +++ b/behavioral-tests/windows-tests/args-and-signals/custom-send-break.ps1 @@ -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 diff --git a/behavioral-tests/windows-tests/args-and-signals/expected.log b/behavioral-tests/windows-tests/args-and-signals/expected.log new file mode 100644 index 0000000..6c44ade --- /dev/null +++ b/behavioral-tests/windows-tests/args-and-signals/expected.log @@ -0,0 +1,3 @@ +INVALID JSON: "Arguments: ARG1 ARG2\r" +INVALID JSON: "Sleeping...\r" +INVALID JSON: "Going to kill parent process...\r" diff --git a/behavioral-tests/windows-tests/custom/expected.log b/behavioral-tests/windows-tests/custom/expected.log new file mode 100644 index 0000000..251c96c --- /dev/null +++ b/behavioral-tests/windows-tests/custom/expected.log @@ -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 diff --git a/behavioral-tests/windows-tests/custom/pplog.env b/behavioral-tests/windows-tests/custom/pplog.env new file mode 100644 index 0000000..e79a4e9 --- /dev/null +++ b/behavioral-tests/windows-tests/custom/pplog.env @@ -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' diff --git a/behavioral-tests/windows-tests/default/expected.log b/behavioral-tests/windows-tests/default/expected.log new file mode 100644 index 0000000..56dedc9 --- /dev/null +++ b/behavioral-tests/windows-tests/default/expected.log @@ -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 diff --git a/behavioral-tests/windows-tests/fake-server.ps1 b/behavioral-tests/windows-tests/fake-server.ps1 new file mode 100644 index 0000000..441019e --- /dev/null +++ b/behavioral-tests/windows-tests/fake-server.ps1 @@ -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}' diff --git a/behavioral-tests/windows-tests/pipe-mode/custom-run.ps1 b/behavioral-tests/windows-tests/pipe-mode/custom-run.ps1 new file mode 100644 index 0000000..717bcbd --- /dev/null +++ b/behavioral-tests/windows-tests/pipe-mode/custom-run.ps1 @@ -0,0 +1,3 @@ +powershell ..\fake-server.ps1 | + go run ..\..\..\cmd\... | + Tee-Object -FilePath output.log \ No newline at end of file diff --git a/behavioral-tests/windows-tests/pipe-mode/expected.log b/behavioral-tests/windows-tests/pipe-mode/expected.log new file mode 100644 index 0000000..56dedc9 --- /dev/null +++ b/behavioral-tests/windows-tests/pipe-mode/expected.log @@ -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 diff --git a/behavioral-tests/windows-tests/run.ps1 b/behavioral-tests/windows-tests/run.ps1 new file mode 100644 index 0000000..0c5ec9a --- /dev/null +++ b/behavioral-tests/windows-tests/run.ps1 @@ -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 +} \ No newline at end of file