|
| 1 | +BeforeAll { |
| 2 | + . "$PSScriptRoot/utils.ps1" |
| 3 | + $global:SentryPowershellRethrowErrors = $true |
| 4 | + |
| 5 | + function StartSentryForLogTests { |
| 6 | + Start-Sentry { |
| 7 | + $_.Dsn = 'https://key@127.0.0.1/1' |
| 8 | + $_.Experimental.EnableLogs = $true |
| 9 | + $_.Experimental.SetBeforeSendLog([System.Func[Sentry.SentryLog, Sentry.SentryLog]] { |
| 10 | + param([Sentry.SentryLog]$log) |
| 11 | + $script:logs.Add($log) |
| 12 | + return $null |
| 13 | + }) |
| 14 | + $_.Transport = [RecordingTransport]::new() |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + # Logs go through a background batch processor; flush before asserting. |
| 19 | + function FlushLogs { [Sentry.SentrySdk]::Flush([TimeSpan]::FromSeconds(5)) | Out-Null } |
| 20 | +} |
| 21 | + |
| 22 | +AfterAll { |
| 23 | + $global:SentryPowershellRethrowErrors = $false |
| 24 | +} |
| 25 | + |
| 26 | +Describe 'Write-SentryLog' { |
| 27 | + BeforeEach { |
| 28 | + $script:logs = [System.Collections.Generic.List[Sentry.SentryLog]]::new() |
| 29 | + StartSentryForLogTests |
| 30 | + } |
| 31 | + |
| 32 | + AfterEach { |
| 33 | + Stop-Sentry |
| 34 | + } |
| 35 | + |
| 36 | + It 'sends an Info log by default' { |
| 37 | + Write-SentryLog 'hello' |
| 38 | + FlushLogs |
| 39 | + |
| 40 | + $script:logs.Count | Should -Be 1 |
| 41 | + $script:logs[0].Level | Should -Be ([Sentry.SentryLogLevel]::Info) |
| 42 | + $script:logs[0].Message | Should -Be 'hello' |
| 43 | + $script:logs[0].Template | Should -Be 'hello' |
| 44 | + } |
| 45 | + |
| 46 | + It 'accepts the message from the pipeline' { |
| 47 | + 'piped message' | Write-SentryLog -Level Warning |
| 48 | + FlushLogs |
| 49 | + |
| 50 | + $script:logs.Count | Should -Be 1 |
| 51 | + $script:logs[0].Level | Should -Be ([Sentry.SentryLogLevel]::Warning) |
| 52 | + $script:logs[0].Message | Should -Be 'piped message' |
| 53 | + } |
| 54 | + |
| 55 | + It 'sends a log at each supported level' { |
| 56 | + Write-SentryLog -Level Trace 'trace' |
| 57 | + Write-SentryLog -Level Debug 'debug' |
| 58 | + Write-SentryLog -Level Info 'info' |
| 59 | + Write-SentryLog -Level Warning 'warning' |
| 60 | + Write-SentryLog -Level Error 'error' |
| 61 | + Write-SentryLog -Level Fatal 'fatal' |
| 62 | + FlushLogs |
| 63 | + |
| 64 | + $script:logs.Count | Should -Be 6 |
| 65 | + $script:logs[0].Level | Should -Be ([Sentry.SentryLogLevel]::Trace) |
| 66 | + $script:logs[1].Level | Should -Be ([Sentry.SentryLogLevel]::Debug) |
| 67 | + $script:logs[2].Level | Should -Be ([Sentry.SentryLogLevel]::Info) |
| 68 | + $script:logs[3].Level | Should -Be ([Sentry.SentryLogLevel]::Warning) |
| 69 | + $script:logs[4].Level | Should -Be ([Sentry.SentryLogLevel]::Error) |
| 70 | + $script:logs[5].Level | Should -Be ([Sentry.SentryLogLevel]::Fatal) |
| 71 | + } |
| 72 | + |
| 73 | + It 'substitutes parameters into the template' { |
| 74 | + Write-SentryLog -Level Info -Message 'User {0} ran {1}' -Parameters 'alice', 'send-logs.ps1' |
| 75 | + FlushLogs |
| 76 | + |
| 77 | + $script:logs.Count | Should -Be 1 |
| 78 | + $script:logs[0].Template | Should -Be 'User {0} ran {1}' |
| 79 | + $script:logs[0].Message | Should -Be 'User alice ran send-logs.ps1' |
| 80 | + $script:logs[0].Parameters.Length | Should -Be 2 |
| 81 | + } |
| 82 | + |
| 83 | + It 'attaches structured attributes' { |
| 84 | + Write-SentryLog -Level Error -Message 'oops' -Attributes @{ user = 'bob'; retries = 3 } |
| 85 | + FlushLogs |
| 86 | + |
| 87 | + $script:logs.Count | Should -Be 1 |
| 88 | + $userValue = $null |
| 89 | + $script:logs[0].TryGetAttribute('user', [ref] $userValue) | Should -Be $true |
| 90 | + $userValue | Should -Be 'bob' |
| 91 | + $retriesValue = $null |
| 92 | + $script:logs[0].TryGetAttribute('retries', [ref] $retriesValue) | Should -Be $true |
| 93 | + $retriesValue | Should -Be 3 |
| 94 | + } |
| 95 | + |
| 96 | + It 'combines parameters and attributes in a single call' { |
| 97 | + Write-SentryLog -Level Warning ` |
| 98 | + -Message 'host {0} is at {1}%' ` |
| 99 | + -Parameters 'web-1', 92 ` |
| 100 | + -Attributes @{ region = 'us-east-1' } |
| 101 | + FlushLogs |
| 102 | + |
| 103 | + $script:logs.Count | Should -Be 1 |
| 104 | + $script:logs[0].Message | Should -Be 'host web-1 is at 92%' |
| 105 | + $script:logs[0].Parameters.Length | Should -Be 2 |
| 106 | + $regionValue = $null |
| 107 | + $script:logs[0].TryGetAttribute('region', [ref] $regionValue) | Should -Be $true |
| 108 | + $regionValue | Should -Be 'us-east-1' |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +Describe 'Write-SentryLog when Sentry is not started' { |
| 113 | + It 'is a no-op and does not throw' { |
| 114 | + [Sentry.SentrySdk]::IsEnabled | Should -Be $false |
| 115 | + { Write-SentryLog -Level Info 'should be ignored' } | Should -Not -Throw |
| 116 | + } |
| 117 | +} |
0 commit comments