-
Notifications
You must be signed in to change notification settings - Fork 116
Add a smoke test after deployment #69
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| """Minimal end-to-end browser test using Playwright. | ||
|
|
||
| Usage: | ||
| python scripts/e2e_chat_playwright.py https://your-app.azurecontainerapps.io | ||
|
|
||
| Only one argument is accepted: the base URL of the deployed app. | ||
| No environment variables or azd lookups are performed. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| import sys | ||
| import time | ||
|
|
||
| from playwright.sync_api import Playwright, expect, sync_playwright | ||
|
|
||
|
|
||
| def run_test(pw: Playwright, base_url: str) -> None: | ||
| # Internal test configuration (adjust here if needed) | ||
| message = "Hi" | ||
| timeout = 60 # seconds | ||
| headless = True | ||
| expected_substring = None # Set to a string to force exact substring match | ||
| greeting_regex = r"\b(H(i|ello))\b" | ||
| browser = pw.chromium.launch(headless=headless) | ||
| context = browser.new_context() | ||
| page = context.new_page() | ||
|
|
||
| if not base_url.startswith("http"): | ||
| raise ValueError("Base URL must start with http/https") | ||
| base_url = base_url.rstrip("/") | ||
|
|
||
| url = base_url | ||
| if not url.endswith("/"): | ||
| url += "/" | ||
| print(f"Navigating to {url}") | ||
| page.goto(url, wait_until="domcontentloaded") | ||
|
|
||
| textbox = page.get_by_role("textbox", name="Ask ChatGPT") | ||
| textbox.click() | ||
| textbox.fill(message) | ||
| textbox.press("Enter") | ||
| # Redundant click in case Enter doesn't submit on some platforms | ||
| page.get_by_role("button", name="Send").click() | ||
|
|
||
| # Wait for the last assistant message content div that is not the typing indicator | ||
| content_locator = page.locator(".toast-body.message-content").last | ||
| # Poll until the content no longer contains 'Typing...' and has some text | ||
| start = time.time() | ||
| while time.time() - start < timeout: | ||
| txt = content_locator.inner_text().strip() | ||
| if txt and "Typing..." not in txt: | ||
| break | ||
| time.sleep(0.5) | ||
| else: | ||
| raise RuntimeError("Timeout waiting for assistant response") | ||
|
|
||
| txt_final = content_locator.inner_text().strip() | ||
| if expected_substring: | ||
| expect(content_locator).to_contain_text(expected_substring) | ||
| else: | ||
| if not re.search(greeting_regex, txt_final, flags=re.IGNORECASE): | ||
| raise RuntimeError( | ||
| f"Assistant response did not match greeting regex '{greeting_regex}'. Got: {txt_final[:120]}" | ||
| ) | ||
| if len(txt_final) < 2: | ||
| raise RuntimeError("Assistant response too short") | ||
| print("Assistant response snippet:", txt_final[:160]) | ||
|
|
||
| # Cleanup | ||
| context.close() | ||
| browser.close() | ||
|
|
||
|
|
||
| def main() -> int: | ||
| if len(sys.argv) != 2: | ||
| print("Usage: python scripts/e2e_chat_playwright.py <base_url>", file=sys.stderr) | ||
| return 1 | ||
| base_url = sys.argv[1] | ||
| try: | ||
| with sync_playwright() as pw: | ||
| run_test(pw, base_url) | ||
| print("Playwright E2E test succeeded.") | ||
| return 0 | ||
| except Exception as e: # broad for CLI convenience | ||
| print(f"Playwright E2E test failed: {e}", file=sys.stderr) | ||
| return 1 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,8 +1,8 @@ | ||||||||||
| import json | ||||||||||
| import os | ||||||||||
|
|
||||||||||
| import azure.identity.aio | ||||||||||
| import openai | ||||||||||
| from azure.identity.aio import AzureDeveloperCliCredential, ManagedIdentityCredential, get_bearer_token_provider | ||||||||||
| from openai import AsyncOpenAI | ||||||||||
| from quart import ( | ||||||||||
| Blueprint, | ||||||||||
| Response, | ||||||||||
|
|
@@ -17,55 +17,47 @@ | |||||||||
|
|
||||||||||
| @bp.before_app_serving | ||||||||||
| async def configure_openai(): | ||||||||||
| bp.model_name = os.getenv("OPENAI_MODEL", "gpt-4o") | ||||||||||
| openai_host = os.getenv("OPENAI_HOST", "github") | ||||||||||
|
|
||||||||||
| if openai_host == "local": | ||||||||||
| bp.model_name = os.getenv("OPENAI_MODEL", "gpt-4o") | ||||||||||
| current_app.logger.info("Using model %s from local OpenAI-compatible API with no key", bp.model_name) | ||||||||||
| bp.openai_client = openai.AsyncOpenAI(api_key="no-key-required", base_url=os.getenv("LOCAL_OPENAI_ENDPOINT")) | ||||||||||
| bp.openai_client = AsyncOpenAI(api_key="no-key-required", base_url=os.getenv("LOCAL_OPENAI_ENDPOINT")) | ||||||||||
| current_app.logger.info("Using local OpenAI-compatible API service with no key") | ||||||||||
| elif openai_host == "github": | ||||||||||
| bp.model_name = os.getenv("OPENAI_MODEL", "openai/gpt-4o") | ||||||||||
| current_app.logger.info("Using model %s from GitHub models with GITHUB_TOKEN as key", bp.model_name) | ||||||||||
| bp.openai_client = openai.AsyncOpenAI( | ||||||||||
| bp.model_name = f"openai/{bp.model_name}" | ||||||||||
| bp.openai_client = AsyncOpenAI( | ||||||||||
| api_key=os.environ["GITHUB_TOKEN"], | ||||||||||
| base_url="https://models.github.ai/inference", | ||||||||||
| ) | ||||||||||
| current_app.logger.info("Using GitHub models with GITHUB_TOKEN as key") | ||||||||||
| elif os.getenv("AZURE_OPENAI_KEY_FOR_CHATVISION"): | ||||||||||
| # Authenticate using an Azure OpenAI API key | ||||||||||
| # This is generally discouraged, but is provided for developers | ||||||||||
| # that want to develop locally inside the Docker container. | ||||||||||
| bp.openai_client = AsyncOpenAI( | ||||||||||
| base_url=os.environ["AZURE_OPENAI_ENDPOINT"], | ||||||||||
| api_key=os.getenv("AZURE_OPENAI_KEY_FOR_CHATVISION"), | ||||||||||
| ) | ||||||||||
| current_app.logger.info("Using Azure OpenAI with key") | ||||||||||
| elif os.getenv("RUNNING_IN_PRODUCTION"): | ||||||||||
| client_id = os.environ["AZURE_CLIENT_ID"] | ||||||||||
| azure_credential = ManagedIdentityCredential(client_id=client_id) | ||||||||||
| token_provider = get_bearer_token_provider(azure_credential, "https://cognitiveservices.azure.com/.default") | ||||||||||
| bp.openai_client = AsyncOpenAI( | ||||||||||
| base_url=os.environ["AZURE_OPENAI_ENDPOINT"] + "/openai/v1/", | ||||||||||
| api_key=token_provider, | ||||||||||
| ) | ||||||||||
| current_app.logger.info("Using Azure OpenAI with managed identity credential for client ID %s", client_id) | ||||||||||
| else: | ||||||||||
| # Use an Azure OpenAI endpoint instead, | ||||||||||
| # either with a key or with keyless authentication | ||||||||||
| bp.model_name = os.getenv("OPENAI_MODEL", "gpt-4o") | ||||||||||
| if os.getenv("AZURE_OPENAI_KEY_FOR_CHATVISION"): | ||||||||||
| # Authenticate using an Azure OpenAI API key | ||||||||||
| # This is generally discouraged, but is provided for developers | ||||||||||
| # that want to develop locally inside the Docker container. | ||||||||||
| current_app.logger.info("Using model %s from Azure OpenAI with key", bp.model_name) | ||||||||||
| bp.openai_client = openai.AsyncOpenAI( | ||||||||||
| base_url=os.environ["AZURE_OPENAI_ENDPOINT"], | ||||||||||
| api_key=os.getenv("AZURE_OPENAI_KEY_FOR_CHATVISION"), | ||||||||||
| ) | ||||||||||
| elif os.getenv("RUNNING_IN_PRODUCTION"): | ||||||||||
| client_id = os.getenv("AZURE_CLIENT_ID") | ||||||||||
| current_app.logger.info( | ||||||||||
| "Using model %s from Azure OpenAI with managed identity credential for client ID %s", | ||||||||||
| bp.model_name, | ||||||||||
| client_id, | ||||||||||
| ) | ||||||||||
| azure_credential = azure.identity.aio.ManagedIdentityCredential(client_id=client_id) | ||||||||||
| else: | ||||||||||
| tenant_id = os.environ["AZURE_TENANT_ID"] | ||||||||||
| current_app.logger.info( | ||||||||||
| "Using model %s from Azure OpenAI with Azure Developer CLI credential for tenant ID: %s", | ||||||||||
| bp.model_name, | ||||||||||
| tenant_id, | ||||||||||
| ) | ||||||||||
| azure_credential = azure.identity.aio.AzureDeveloperCliCredential(tenant_id=tenant_id) | ||||||||||
| token_provider = azure.identity.aio.get_bearer_token_provider( | ||||||||||
| azure_credential, "https://cognitiveservices.azure.com/.default" | ||||||||||
| ) | ||||||||||
| bp.openai_client = openai.AsyncOpenAI( | ||||||||||
| base_url=os.environ["AZURE_OPENAI_ENDPOINT"] + "/openai/v1/", | ||||||||||
| api_key=token_provider, | ||||||||||
| ) | ||||||||||
| tenant_id = os.environ["AZURE_TENANT_ID"] | ||||||||||
|
||||||||||
| tenant_id = os.environ["AZURE_TENANT_ID"] | |
| tenant_id = os.getenv("AZURE_TENANT_ID") | |
| if tenant_id is None: | |
| raise RuntimeError("AZURE_TENANT_ID environment variable is not set. Please set it to continue.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
os.environdirectly will raise aKeyErrorif the environment variable is missing. Consider usingos.getenv()with a descriptive error message or add proper error handling to provide clearer feedback when the required environment variable is not set.