From 1864fe9992e04c34212721da647f6f007647a78e Mon Sep 17 00:00:00 2001 From: Justin Yoo Date: Thu, 22 Aug 2024 23:10:41 +0900 Subject: [PATCH 1/3] Add test scripts --- README.md | 12 ++++++++ scripts/Run-Tests.ps1 | 52 ++++++++++++++++++++++++++++++++ scripts/run-tests.sh | 69 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 scripts/Run-Tests.ps1 create mode 100644 scripts/run-tests.sh diff --git a/README.md b/README.md index eaebf904..b3906db1 100644 --- a/README.md +++ b/README.md @@ -85,3 +85,15 @@ This provides a proxy server application of Azure OpenAI Service API that round- --parameters environmentName=$AZURE_ENV_NAME ` --parameters location=$AZURE_LOCATION ``` + +## Unit Testing & Integration Testing + +1. Run the following command to run both unit tests and integration tests at once on your local machine. + + ```bash + # zsh/bash + ./scripts/run-tests.sh -c Debug + + # PowerShell + ./scripts/Run-Tests.ps1 -Configuration Debug + ``` diff --git a/scripts/Run-Tests.ps1 b/scripts/Run-Tests.ps1 new file mode 100644 index 00000000..bce3a8e9 --- /dev/null +++ b/scripts/Run-Tests.ps1 @@ -0,0 +1,52 @@ +# Run unit tests and integration tests +Param( + [string] + [Parameter(Mandatory=$false)] + [ValidateSet("Debug", "Release")] + $Configuration = "Debug", + + [switch] + [Parameter(Mandatory=$false)] + $Help +) + +function Show-Usage { + Write-Host " This runs both unit tests and integration tests. + + Usage: $(Split-Path $MyInvocation.ScriptName -Leaf) `` + [-Configuration ] `` + + [-Help] + + Options: + -Configuration Configuration. Possible values are 'Debug' or 'Release'. Default is 'Debug'. + + -Help: Show this message. +" + + Exit 0 +} + +# Builds apps +Write-Host "Building apps..." -ForegroundColor Cyan + +dotnet restore +dotnet build -c $Configuration + +# Runs unit tests +Write-Host "Invoking unit tests..." -ForegroundColor Cyan + +dotnet test ./test/AzureOpenAIProxy.AppHost.Tests -c $Configuration --no-build --logger "trx" --collect:"XPlat Code Coverage" +dotnet test ./test/AzureOpenAIProxy.ApiApp.Tests -c $Configuration --no-build --logger "trx" --collect:"XPlat Code Coverage" + +# Runs integration tests +Write-Host "Invoking integration tests..." -ForegroundColor Cyan + +Start-Process -NoNewWindow "dotnet" @("run", "--project", "./src/AzureOpenAIProxy.AppHost", "--no-build") +Start-Sleep -s 30 + +dotnet test ./test/AzureOpenAIProxy.PlaygroundApp.Tests -c $Configuration --logger "trx" --collect:"XPlat Code Coverage" + +# Cleans up +$process = Get-Process | Where-Object { $_.ProcessName -eq "AzureOpenAIProxy.AppHost" } +Stop-Process -Id $process.Id diff --git a/scripts/run-tests.sh b/scripts/run-tests.sh new file mode 100644 index 00000000..b7e37e0a --- /dev/null +++ b/scripts/run-tests.sh @@ -0,0 +1,69 @@ +#!/bin/bash +# Run unit tests and integration tests + +set -e + +# Function to show usage +usage() { + echo " This runs both unit tests and integration tests. + + Usage: $0 [-c|--config|--configuration ] [-h|--help] + + Options: + -c|--config|--configuration Configuration. Possible values are 'Debug' or 'Release'. Default is 'Debug'. + -h|--help Show this message. +" + exit 0 +} + +# Default configuration +CONFIGURATION="Debug" + +if [[ $# -eq 0 ]]; then + CONFIGURATION="Debug" +fi + +while [[ "$1" != "" ]]; do + case $1 in + -c | --config | --configuration) + shift + CONFIGURATION="$1" + ;; + + -h | --help) + usage + exit 0 + ;; + + *) + usage + exit 0 + ;; + esac + + shift +done + +# Builds apps +echo -e "\e[36mBuilding apps...\e[0m" + +dotnet restore +dotnet build -c $CONFIGURATION + +# Runs unit tests +echo -e "\e[36mInvoking unit tests...\e[0m" + +dotnet test ./test/AzureOpenAIProxy.AppHost.Tests -c $CONFIGURATION --no-build --logger "trx" --collect:"XPlat Code Coverage" +dotnet test ./test/AzureOpenAIProxy.ApiApp.Tests -c $CONFIGURATION --no-build --logger "trx" --collect:"XPlat Code Coverage" + +# Runs integration tests +echo -e "\e[36mInvoking integration tests...\e[0m" + +dotnet run --project ./src/AzureOpenAIProxy.AppHost --no-build & +APP_PID=$! +sleep 30 + +dotnet test ./test/AzureOpenAIProxy.PlaygroundApp.Tests -c $CONFIGURATION --logger "trx" --collect:"XPlat Code Coverage" + +# Cleans up +kill $APP_PID From 06d91f7c2560880aebd53ad68c21aee50912adea Mon Sep 17 00:00:00 2001 From: Justin Yoo Date: Thu, 22 Aug 2024 23:42:28 +0900 Subject: [PATCH 2/3] Update shell scripts --- scripts/Run-Tests.ps1 | 108 ++++++++++++++++++++++-------------------- scripts/run-tests.sh | 8 ++-- 2 files changed, 61 insertions(+), 55 deletions(-) mode change 100644 => 100755 scripts/run-tests.sh diff --git a/scripts/Run-Tests.ps1 b/scripts/Run-Tests.ps1 index bce3a8e9..beec821e 100644 --- a/scripts/Run-Tests.ps1 +++ b/scripts/Run-Tests.ps1 @@ -1,52 +1,56 @@ -# Run unit tests and integration tests -Param( - [string] - [Parameter(Mandatory=$false)] - [ValidateSet("Debug", "Release")] - $Configuration = "Debug", - - [switch] - [Parameter(Mandatory=$false)] - $Help -) - -function Show-Usage { - Write-Host " This runs both unit tests and integration tests. - - Usage: $(Split-Path $MyInvocation.ScriptName -Leaf) `` - [-Configuration ] `` - - [-Help] - - Options: - -Configuration Configuration. Possible values are 'Debug' or 'Release'. Default is 'Debug'. - - -Help: Show this message. -" - - Exit 0 -} - -# Builds apps -Write-Host "Building apps..." -ForegroundColor Cyan - -dotnet restore -dotnet build -c $Configuration - -# Runs unit tests -Write-Host "Invoking unit tests..." -ForegroundColor Cyan - -dotnet test ./test/AzureOpenAIProxy.AppHost.Tests -c $Configuration --no-build --logger "trx" --collect:"XPlat Code Coverage" -dotnet test ./test/AzureOpenAIProxy.ApiApp.Tests -c $Configuration --no-build --logger "trx" --collect:"XPlat Code Coverage" - -# Runs integration tests -Write-Host "Invoking integration tests..." -ForegroundColor Cyan - -Start-Process -NoNewWindow "dotnet" @("run", "--project", "./src/AzureOpenAIProxy.AppHost", "--no-build") -Start-Sleep -s 30 - -dotnet test ./test/AzureOpenAIProxy.PlaygroundApp.Tests -c $Configuration --logger "trx" --collect:"XPlat Code Coverage" - -# Cleans up -$process = Get-Process | Where-Object { $_.ProcessName -eq "AzureOpenAIProxy.AppHost" } -Stop-Process -Id $process.Id +# Run unit tests and integration tests +Param( + [string] + [Parameter(Mandatory=$false)] + [ValidateSet("Debug", "Release")] + $Configuration = "Debug", + + [switch] + [Parameter(Mandatory=$false)] + $Help +) + +function Show-Usage { + Write-Host " This runs both unit tests and integration tests. + + Usage: $(Split-Path $MyInvocation.ScriptName -Leaf) `` + [-Configuration ] `` + + [-Help] + + Options: + -Configuration Configuration. Possible values are 'Debug' or 'Release'. Default is 'Debug'. + + -Help: Show this message. +" + + Exit 0 +} + +# Builds apps +Write-Host "Building apps..." -ForegroundColor Cyan + +dotnet restore +dotnet build -c $Configuration + +# Runs unit tests +Write-Host "Invoking unit tests..." -ForegroundColor Cyan + +dotnet test ./test/AzureOpenAIProxy.AppHost.Tests -c $Configuration --no-build --logger "trx" --collect:"XPlat Code Coverage" +dotnet test ./test/AzureOpenAIProxy.ApiApp.Tests -c $Configuration --no-build --logger "trx" --collect:"XPlat Code Coverage" + +# Runs integration tests +Write-Host "Invoking integration tests..." -ForegroundColor Cyan + +$playwright = Get-ChildItem -File Microsoft.Playwright.dll -Path . -Recurse +$installer = "$($playwright[0].Directory.FullName)/playwright.ps1" +& "$installer" install + +Start-Process -NoNewWindow "dotnet" @("run", "--project", "./src/AzureOpenAIProxy.AppHost", "--no-build") +Start-Sleep -s 30 + +dotnet test ./test/AzureOpenAIProxy.PlaygroundApp.Tests -c $Configuration --logger "trx" --collect:"XPlat Code Coverage" + +# Cleans up +$process = Get-Process | Where-Object { $_.Path -like "*AzureOpenAIProxy.AppHost" } +Stop-Process -Id $process.Id diff --git a/scripts/run-tests.sh b/scripts/run-tests.sh old mode 100644 new mode 100755 index b7e37e0a..a431504e --- a/scripts/run-tests.sh +++ b/scripts/run-tests.sh @@ -45,19 +45,21 @@ while [[ "$1" != "" ]]; do done # Builds apps -echo -e "\e[36mBuilding apps...\e[0m" +echo -e "\033[36mBuilding apps...\033[0m" dotnet restore dotnet build -c $CONFIGURATION # Runs unit tests -echo -e "\e[36mInvoking unit tests...\e[0m" +echo -e "\033[36mInvoking unit tests...\033[0m" dotnet test ./test/AzureOpenAIProxy.AppHost.Tests -c $CONFIGURATION --no-build --logger "trx" --collect:"XPlat Code Coverage" dotnet test ./test/AzureOpenAIProxy.ApiApp.Tests -c $CONFIGURATION --no-build --logger "trx" --collect:"XPlat Code Coverage" # Runs integration tests -echo -e "\e[36mInvoking integration tests...\e[0m" +echo -e "\033[36mInvoking integration tests...\033[0m" + +pwsh ./test/AzureOpenAIProxy.PlaygroundApp.Tests/bin/Debug/net8.0/playwright.ps1 install dotnet run --project ./src/AzureOpenAIProxy.AppHost --no-build & APP_PID=$! From 6c04ad8b8e05ab142462e6ef512d17ae063153e8 Mon Sep 17 00:00:00 2001 From: Justin Yoo Date: Thu, 22 Aug 2024 23:51:06 +0900 Subject: [PATCH 3/3] Update pwsh script --- README.md | 6 ++++++ scripts/Run-Tests.ps1 | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b3906db1..237f114c 100644 --- a/README.md +++ b/README.md @@ -97,3 +97,9 @@ This provides a proxy server application of Azure OpenAI Service API that round- # PowerShell ./scripts/Run-Tests.ps1 -Configuration Debug ``` + + > **NOTE**: While running the integration tests using the script above, you might be seeing the error like + > + > `System.IO.IOException: Failed to bind to address https://127.0.0.1:22000: address already in use.` + > + > It can sometimes happen. However, it doesn't impact on the test results. diff --git a/scripts/Run-Tests.ps1 b/scripts/Run-Tests.ps1 index beec821e..69f36529 100644 --- a/scripts/Run-Tests.ps1 +++ b/scripts/Run-Tests.ps1 @@ -52,5 +52,9 @@ Start-Sleep -s 30 dotnet test ./test/AzureOpenAIProxy.PlaygroundApp.Tests -c $Configuration --logger "trx" --collect:"XPlat Code Coverage" # Cleans up -$process = Get-Process | Where-Object { $_.Path -like "*AzureOpenAIProxy.AppHost" } +$process = if ($IsWindows -eq $true) { + Get-Process | Where-Object { $_.ProcessName -eq "AzureOpenAIProxy.AppHost" } +} else { + Get-Process | Where-Object { $_.Path -like "*AzureOpenAIProxy.AppHost" } +} Stop-Process -Id $process.Id