Skip to content

feat: Serverside integrationtests #2109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .github/workflows/android-smoke-test-run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ jobs:
adb wait-for-device
adb shell input keyevent 82
adb devices -l
pwsh ./scripts/smoke-test-android.ps1 -IsIntegrationTest -WarnIfFlaky
pwsh ./scripts/smoke-test-android.ps1 -WarnIfFlaky

- name: Upload artifacts on failure
if: ${{ failure() }}
Expand Down
216 changes: 101 additions & 115 deletions scripts/smoke-test-android.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,57 +22,19 @@ Write-Host "# |___/_| |_|\___/|_|\_\___| |_| |___|___/ |_| #"
Write-Host "# #"
Write-Host "#####################################################"

if ($IsIntegrationTest)
{
$BuildDir = $(GetNewProjectBuildPath)
$ApkFileName = "test.apk"
$ProcessName = "com.DefaultCompany.$(GetNewProjectName)"

if ($Action -eq "Build")
{
$buildCallback = {
Write-Host "::group::Gradle build $BuildDir"
Push-Location $BuildDir
try
{
MakeExecutable "./gradlew"
& ./gradlew --info --no-daemon assembleRelease | ForEach-Object {
Write-Host " $_"
}
if (-not $?)
{
throw "Gradle execution failed"
}
Copy-Item -Path launcher/build/outputs/apk/release/launcher-release.apk -Destination $ApkFileName
}
finally
{
Pop-Location
Write-Host "::endgroup::"
}
}

$symbolServerOutput = RunWithSymbolServer -Callback $buildCallback
CheckSymbolServerOutput 'Android' $symbolServerOutput $UnityVersion
return;
}
}
else
{
$BuildDir = "samples/artifacts/builds/Android"
$ApkFileName = "IL2CPP_Player.apk"
$ProcessName = "io.sentry.samples.unityofbugs"
}
$BuildDir = $(GetNewProjectBuildPath)
$ApkFileName = "test.apk"
$ProcessName = "com.DefaultCompany.$(GetNewProjectName)"
$TestActivityName = "$ProcessName/com.unity3d.player.UnityPlayerActivity"
$FallBackTestActivityName = "$ProcessName/com.unity3d.player.UnityPlayerGameActivity"

$_ArtifactsPath = ((Test-Path env:ARTIFACTS_PATH) ? $env:ARTIFACTS_PATH : "./$BuildDir/../test-artifacts/") `
+ $(Get-Date -Format "HHmmss")
$_ArtifactsPath = (Test-Path env:ARTIFACTS_PATH) ? $env:ARTIFACTS_PATH : (Join-Path $BuildDir "../test-artifacts/" $(Get-Date -Format "HHmmss"))

function ArtifactsPath
{
if (-not (Test-Path $_ArtifactsPath))
{
New-Item $_ArtifactsPath -ItemType Directory | Out-Null
New-Item $_ArtifactsPath -ItemType Directory -Force | Out-Null
}
$_ArtifactsPath.Replace('\', '/')
}
Expand Down Expand Up @@ -256,18 +218,18 @@ function RunTest([string] $Name, [string] $SuccessString, [string] $FailureStrin
{
Write-Host "::group::Test: '$name'"

Write-Host "Clearing logcat from '$device'"
Write-Host " Clearing logcat from '$device'"
adb -s $device logcat -c

$activityName = $TestActivityName

Write-Host "Setting configuration"
Write-Host " Setting configuration"

# Mark the full-screen notification as acknowledged
adb -s $device shell "settings put secure immersive_mode_confirmations confirmed"
adb -s $device shell "input keyevent KEYCODE_HOME"

Write-Host "Starting app '$activityName'"
Write-Host " Starting app '$activityName'"

# Start the adb command as a background job so we can wait for it to finish with a timeout
$job = Start-Job -ScriptBlock {
Expand All @@ -280,32 +242,32 @@ function RunTest([string] $Name, [string] $SuccessString, [string] $FailureStrin
if ($null -eq $completed) {
Stop-Job $job
Remove-Job $job -Force
Write-Host "Activity start timed out after 60 seconds"
Write-Host " Activity start timed out after 60 seconds"
return $false
}

$output = Receive-Job $job
Remove-Job $job

Write-Host "Checking if activity started"
Write-Host " Checking if activity started"

# Check if the activity failed to start
if ($output -match "Error type 3" -or $output -match "Activity class \{$activityName\} does not exist.")
{
$activityName = $FallBackTestActivityName
Write-Host "Trying fallback activity $activityName"
Write-Host " Trying fallback activity $activityName"

$output = & adb -s $device shell am start -n $activityName -e test $Name -W 2>&1

# Check if the fallback activity failed to start
if ($output -match "Error type 3" -or $output -match "Activity class \{$activityName\} does not exist.")
{
Write-Host "Activity does not exist"
Write-Host " Activity does not exist"
return $false
}
}

Write-Host "Activity started successfully"
Write-Host " Activity started successfully"

$appPID = PidOf $device $ProcessName
if ($null -eq $appPID)
Expand All @@ -315,9 +277,9 @@ function RunTest([string] $Name, [string] $SuccessString, [string] $FailureStrin
return $false
}

Write-Host "Retrieved ID for '$ProcessName': $appPID"
Write-Host " Retrieved ID for '$ProcessName': $appPID"

Write-Host "Waiting for tests to run..."
Write-Host " Waiting for tests to run..."

$processFinished = $false
$logCache = @()
Expand All @@ -332,10 +294,10 @@ function RunTest([string] $Name, [string] $SuccessString, [string] $FailureStrin
$logCache = ProcessNewLogs -newLogs $newLogs -lastLogCount ([ref]$lastLogCount) -logCache $logCache

# The SmokeTester logs "SmokeTester is quitting." in OnApplicationQuit() to reliably inform when tests finish running.
# For crash tests, we expect to see a native crash log "terminating with uncaught exception of type char const*".
if (($newLogs | Select-String "SmokeTester is quitting.") -or ($newLogs | Select-String "terminating with uncaught exception of type char const*"))
# For crash tests, we're checking for `sentry-native` logging "crash has been captured" to reliably inform when tests finished running.
if (($newLogs | Select-String "SmokeTester is quitting.") -or ($newLogs | Select-String "crash has been captured"))
{
Write-Host "Process finished marker detected. Finish waiting for tests to run."
Write-Host " Process finished marker detected. Finish waiting for tests to run."
$processFinished = $true
break
}
Expand All @@ -345,21 +307,24 @@ function RunTest([string] $Name, [string] $SuccessString, [string] $FailureStrin

if ($processFinished)
{
Write-Host "'$Name' test finished running."
Write-Host " '$Name' test finished running."
}
else
{
Write-Host "'$Name' tests timed out. See logcat for more details."
Write-Host " '$Name' tests timed out. See logcat for more details."
}

Write-Host "::endgroup::"

# Fetch the latest logs from the device
$logCache = ProcessNewLogs -newLogs $newLogs -lastLogCount ([ref]$lastLogCount) -logCache $logCache

Write-Host "::group::logcat"
$logCache | ForEach-Object { Write-Host $_ }
Write-Host "::endgroup::"
if ($env:CI)
{
Write-Host "::group::logcat"
$logCache | ForEach-Object { Write-Host " " + $_ }
Write-Host "::endgroup::"
}

$lineWithSuccess = $logCache | Select-String $SuccessString
$lineWithFailure = $logCache | Select-String $FailureString
Expand Down Expand Up @@ -412,60 +377,81 @@ function RunTestWithRetry([string] $Name, [string] $SuccessString, [string] $Fai
}

$results = @{
smokeTestPassed = $false
hasntCrashedTestPassed = $false
crashTestPassed = $false
hasCrashTestPassed = $false
}

$results.smoketestPassed = RunTestWithRetry -Name "smoke" -SuccessString "SMOKE TEST: PASS" -FailureString "SMOKE TEST: FAIL" -MaxRetries 3
$results.hasntCrashedTestPassed = RunTestWithRetry -Name "hasnt-crashed" -SuccessString "HASNT-CRASHED TEST: PASS" -FailureString "HASNT-CRASHED TEST: FAIL" -MaxRetries 3

try
{
CrashTestWithServer -SuccessString "POST /api/12345/envelope/ HTTP/1.1`" 200 -b'1f8b08000000000000" -CrashTestCallback {
$results.crashTestPassed = RunTest -Name "crash" -SuccessString "CRASH TEST: Issuing a native crash" -FailureString "CRASH TEST: FAIL"
$results.hasCrashTestPassed = RunTest -Name "has-crashed" -SuccessString "HAS-CRASHED TEST: PASS" -FailureString "HAS-CRASHED TEST: FAIL"
}
}
catch
{
Write-Host "Caught exception: $_"
Write-Host $_.ScriptStackTrace
OnError $device $deviceApi
exit 1
smokeTestServerPassed = $false
smokeTestGamePassed = $false
hasntCrashedTestPassed = $true
crashTestPassed = $true
hasCrashTestPassed = $true
}

$failed = $false

if (-not $results.smoketestPassed)
{
Write-Host "Smoke test failed"
$failed = $true
$results.smokeTestServerPassed = SmokeTestWithServer -EnvelopeDir (ArtifactsPath) -RunGameCallback {
$results.smokeTestGamePassed = RunTest -Name "smoke" -SuccessString "SMOKE TEST: PASS" -FailureString "SMOKE TEST: FAIL"
}

if (-not $results.hasntCrashedTestPassed)
{
Write-Host "HasntCrashed test failed"
$failed = $true
}

if (-not $results.crashTestPassed)
{
Write-Host "Crash test failed"
$failed = $true
}

if (-not $results.hasCrashTestPassed)
{
Write-Host "HasCrashed test failed"
$failed = $true
}

if ($failed)
{
exit 1
}

Write-Host "All tests passed" -ForegroundColor Green
$results.hasntCrashedTestPassed = RunTest -Name "hasnt-crashed" -SuccessString "HASNT-CRASHED TEST: PASS" -FailureString "HASNT-CRASHED TEST: FAIL"

# $results.smoketestPassed = RunTestWithRetry -Name "smoke" -SuccessString "SMOKE TEST: PASS" -FailureString "SMOKE TEST: FAIL" -MaxRetries 3
# $results.hasntCrashedTestPassed = RunTestWithRetry -Name "hasnt-crashed" -SuccessString "HASNT-CRASHED TEST: PASS" -FailureString "HASNT-CRASHED TEST: FAIL" -MaxRetries 3

# try
# {
# CrashTestWithServer -SuccessString "POST /api/12345/envelope/ HTTP/1.1`" 200 -b'1f8b08000000000000" -CrashTestCallback {
# $results.crashTestPassed = RunTest -Name "crash" -SuccessString "CRASH TEST: Issuing a native crash" -FailureString "CRASH TEST: FAIL"
# $results.hasCrashTestPassed = RunTest -Name "has-crashed" -SuccessString "HAS-CRASHED TEST: PASS" -FailureString "HAS-CRASHED TEST: FAIL"
# }
# }
# catch
# {
# Write-Host "Caught exception: $_"
# Write-Host $_.ScriptStackTrace
# OnError $device $deviceApi
# exit 1
# }

# try
# {
# TraceIdTestWithServer -EnvelopeDir (ArtifactsPath) -TraceIdTestCallback {
# $results.crashTestPassed = RunTest -Name "trace-id" -SuccessString "CRASH TEST: Issuing a native crash" -FailureString "CRASH TEST: FAIL"
# $results.hasCrashTestPassed = RunTest -Name "has-crashed" -SuccessString "HAS-CRASHED TEST: PASS" -FailureString "HAS-CRASHED TEST: FAIL"
# }
# }
# catch
# {
# Write-Host "Caught exception: $_"
# Write-Host $_.ScriptStackTrace
# OnError $device $deviceApi
# exit 1
# }

# $failed = $false

# if (-not $results.smoketestPassed)
# {
# Write-Host "Smoke test failed"
# $failed = $true
# }

# if (-not $results.hasntCrashedTestPassed)
# {
# Write-Host "HasntCrashed test failed"
# $failed = $true
# }

# if (-not $results.crashTestPassed)
# {
# Write-Host "Crash test failed"
# $failed = $true
# }

# if (-not $results.hasCrashTestPassed)
# {
# Write-Host "HasCrashed test failed"
# $failed = $true
# }

# if ($failed)
# {
# exit 1
# }

# Write-Host "All tests passed" -ForegroundColor Green
exit 0
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public override void Configure(SentryUnityOptions options)
options.TracesSampleRate = 1.0d;
options.PerformanceAutoInstrumentationEnabled = true;

options.CreateHttpMessageHandler = () => SmokeTester.t;
// options.CreateHttpMessageHandler = () => SmokeTester.t;
SmokeTester.CrashedLastRun = () =>
{
if (options.CrashedLastRun != null)
Expand Down
19 changes: 19 additions & 0 deletions test/Scripts.Integration.Test/Scripts/SmokeTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public void Start()
{
HasCrashedTest();
}
else if (arg == "trace-id")
{
TraceIdTest();
}
else if (arg != null)
{
Debug.Log($"Unknown command line argument: {arg}");
Expand Down Expand Up @@ -209,6 +213,21 @@ public static void HasCrashedTest()
t.Pass();
}

public static void TraceIdTest()
{
t.Start("TRACE-ID-TEST");

var ex = new Exception("Trace ID exception");
SentrySdk.CaptureException(ex);

Debug.Log("TRACE-ID-TEST TEST: Issuing a native crash (c++ unhandled exception)");
throw_cpp();

// shouldn't execute because the previous call should have failed
Debug.Log("CRASH TEST: FAIL - unexpected code executed...");
Application.Quit(-1);
}

private static void AddContext()
{
SentrySdk.AddBreadcrumb("crumb", "bread", "error", new Dictionary<string, string>() { { "foo", "bar" } }, BreadcrumbLevel.Critical);
Expand Down
Loading
Loading