-
Notifications
You must be signed in to change notification settings - Fork 11
Add the python sdk wrapper for test-server #34
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
Merged
Changes from all commits
Commits
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,6 @@ | ||
| include LICENSE | ||
wanlin31 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| include README.md | ||
| include install.py | ||
| include checksums.json | ||
|
|
||
| prune sample | ||
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,83 @@ | ||
| # Build the Python test server sdk | ||
|
|
||
| ## Create virtual enviroment | ||
| ``` | ||
| python3 -m venv ~/env | ||
| source ~/env/bin/activate | ||
| ``` | ||
|
|
||
| ## Install all the dependencies | ||
| ``` | ||
| pip3 install -r requirements.txt | ||
| ``` | ||
|
|
||
| ## Build python wheel | ||
|
|
||
| ```sh | ||
| # Ensure a clean build | ||
| rm -rf build/ dist/ *.egg-info/ src/test_server_sdk/bin/ && find . -depth -name "__pycache__" -type d -exec rm -rf {} \; | ||
wanlin31 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Build the python wheel | ||
| python3 -m build | ||
| ``` | ||
|
|
||
| # User the Python test server sdk | ||
|
|
||
| ## Installation of the Python Test Server sdk | ||
|
|
||
| ```sh | ||
| # Install from the dist, use --force-reinstall to alwasy install fresh | ||
| pip install --force-reinstall dist/test_server_sdk-0.1.0-py3-none-any.whl | ||
|
|
||
| # Check on the files | ||
| pip show -f test_server_sdk | ||
| ``` | ||
| You should see something very similar to this output, note tehat the `test_server_sdk/bin/` folder exist and contains the golang test-server: | ||
| ``` | ||
| Name: test-server-sdk | ||
| Version: 0.1.0 | ||
| Summary: A python wrapper for test-server. | ||
| Home-page: https://github.com/google/test-server/sdks/python | ||
| Author: | ||
| Author-email: Google LLC <googleapis-packages@google.com> | ||
| License-Expression: Apache-2.0 | ||
| Location: /usr/local/google/home/wanlindu/env/lib/python3.13/site-packages | ||
| Requires: PyYAML, requests | ||
| Required-by: | ||
| Files: | ||
| src/test_server_sdk/__init__.py | ||
| src/test_server_sdk/__pycache__/__init__.cpython-313.pyc | ||
| src/test_server_sdk/__pycache__/test_server_wrapper.cpython-313.pyc | ||
| src/test_server_sdk/test_server_wrapper.py | ||
| test_server_sdk-0.1.0.dist-info/INSTALLER | ||
| test_server_sdk-0.1.0.dist-info/METADATA | ||
| test_server_sdk-0.1.0.dist-info/RECORD | ||
| test_server_sdk-0.1.0.dist-info/REQUESTED | ||
| test_server_sdk-0.1.0.dist-info/WHEEL | ||
| test_server_sdk-0.1.0.dist-info/direct_url.json | ||
| test_server_sdk-0.1.0.dist-info/licenses/LICENSE | ||
| test_server_sdk-0.1.0.dist-info/top_level.txt | ||
| test_server_sdk/__init__.py | ||
| test_server_sdk/__pycache__/__init__.cpython-313.pyc | ||
| test_server_sdk/__pycache__/test_server_wrapper.cpython-313.pyc | ||
| test_server_sdk/bin/CHANGELOG.md | ||
| test_server_sdk/bin/LICENSE | ||
| test_server_sdk/bin/README.md | ||
| test_server_sdk/bin/test-server | ||
| test_server_sdk/test_server_wrapper.py | ||
| ``` | ||
|
|
||
| ## Python Configuring the Python Test Server SDK | ||
|
|
||
| The Python `TestServer` is a convenient wrapper around the core Go test-server executable. You can configure it using parameters that directly correspond to the Go server's command-line flags. | ||
|
|
||
| You have the flexibility to provide these settings by passing them directly to the `TestServer` class, using environment variables, or creating custom `pytest` fixtures. | ||
|
|
||
| ### Configuration Options | ||
|
|
||
| | Go Flag / ENV | Initialization Parameter | Description | Default Value | Sample Implementation (refer to the `python/sample/conftest.py` file) | | ||
| | :--- | :--- | :--- | :--- | :--- | | ||
| | `record` / `replay` | **`mode`** | Sets the server to either `'record'` or `'replay'`. | `'replay'` | Set via the `--record` pytest flag. | | ||
| | `--config` | **`config_path`** | The file path to the server's configuration file. | -- | Set via environment variable. | | ||
| | `--recording-dir` | **`recording_dir`** | The directory for saving or retrieving recordings. | -- | Set via environment variable. | | ||
| | -- | **`teardown_timeout`**| An optional grace period (in seconds) to wait before forcefully shutting down the server. | `5` | Left out to use default value | | ||
|
|
||
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,36 @@ | ||
| [build-system] | ||
| requires = ["setuptools>=61.0", "wheel", "twine>=6.1.0", "packaging>=24.2", "pkginfo>=1.12.0"] | ||
| build-backend = "setuptools.build_meta" | ||
|
|
||
| [project] | ||
| name = "test-server-sdk" | ||
| version = "0.1.0" | ||
| authors = [ | ||
| { name = "Google LLC", email = "googleapis-packages@google.com" }, | ||
| ] | ||
| description = "A python wrapper for test-server." | ||
| readme = "README.md" | ||
| license = "Apache-2.0" | ||
| requires-python = ">=3.9" | ||
| classifiers = [ | ||
| "Intended Audience :: Developers", | ||
| "Operating System :: OS Independent", | ||
| "Programming Language :: Python", | ||
| "Topic :: Internet", | ||
| "Topic :: Software Development :: Libraries :: Python Modules", | ||
| ] | ||
| dependencies = [ | ||
| "requests", | ||
| "PyYAML" | ||
| ] | ||
|
|
||
| [project.urls] | ||
| Homepage = "https://github.com/google/test-server/sdks/python" | ||
| Issues = "https://github.com/google/test-server/issues" | ||
|
|
||
| [tool.setuptools] | ||
| cmdclass = { bdist_wheel = "install.CustomBuild" } | ||
|
|
||
| [tool.setuptools.packages.find] | ||
| where = ["src", "."] | ||
| exclude = ["sample*"] |
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,2 @@ | ||
| requests | ||
| PyYAML |
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,15 @@ | ||
| To run the sample, nevigate to sdks/python/samples | ||
|
|
||
| ```sh | ||
| # ensure a force update | ||
| pip install --force-reinstall ../dist/test_server_sdk-0.1.0-py3-none-any.whl | ||
|
|
||
| # check what is in the package | ||
| pip show -f test_server_sdk | ||
|
|
||
| # run test with replay mode | ||
| pytest -sv | ||
|
|
||
| # run test with record mode | ||
| pytest -sv --record | ||
| ``` |
Empty file.
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,48 @@ | ||
| import pytest | ||
| import os | ||
| from pathlib import Path | ||
| from test_server_sdk.test_server_wrapper import TestServer | ||
|
|
||
|
|
||
| # 2. Get paths from environment variables or use defaults | ||
| config_path_from_env = os.getenv("TEST_CONFIG_PATH") | ||
| recordings_dir_from_env = os.getenv("TEST_RECORDINGS_DIR") | ||
|
|
||
| # 3. Set the final paths, converting the string from the env var to a Path object | ||
| SAMPLE_PACKAGE_ROOT = Path(__file__).resolve().parent | ||
| CONFIG_FILE_PATH = Path(config_path_from_env) if config_path_from_env else SAMPLE_PACKAGE_ROOT / "test-data" / "config" / "test-server-config.yml" | ||
| RECORDINGS_DIR = Path(recordings_dir_from_env) if recordings_dir_from_env else SAMPLE_PACKAGE_ROOT / "test-data" / "recordings" | ||
|
|
||
| def pytest_addoption(parser): | ||
| """Adds the --record command-line option to pytest.""" | ||
| parser.addoption( | ||
| "--record", action="store_true", default=False, help="Run test-server in record mode." | ||
| ) | ||
|
|
||
| @pytest.fixture(scope="session") | ||
| def test_server_mode(request): | ||
| """ | ||
| Returns 'record' or 'replay' based on the --record command-line flag. | ||
| This fixture can be used by any test. | ||
| """ | ||
| return "record" if request.config.getoption("--record") else "replay" | ||
|
|
||
| @pytest.fixture(scope="class") | ||
| def managed_server(test_server_mode): | ||
| """ | ||
| A fixture that starts the test-server before any tests in a class run, | ||
| and stops it after they have all finished. | ||
| """ | ||
| print(f"\n[PyTest] Using test-server mode: '{test_server_mode}'") | ||
|
|
||
| # The TestServer context manager handles start and stop automatically | ||
| with TestServer( | ||
| config_path=str(CONFIG_FILE_PATH), | ||
| recording_dir=str(RECORDINGS_DIR), | ||
| mode=test_server_mode | ||
| ) as server: | ||
| print(f"[PyTest] Test-server started with PID: {server.process.pid}") | ||
| # The 'yield' passes control to the tests. | ||
| yield server | ||
| # Code after yield runs after the last test in the class finishes | ||
| print(f"\n[PyTest] Test-server with PID: {server.process.pid} stopped.") |
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,7 @@ | ||
| endpoints: | ||
| - target_host: github.com | ||
| target_type: https | ||
| target_port: 443 | ||
| source_type: http | ||
| source_port: 17080 | ||
| health: /healthz |
48 changes: 48 additions & 0 deletions
48
...est-data/recordings/396bab503f4b90ad88858bb4467d59b9e827dcd82e33f41c230e1b8fb43bdc8c.json
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,48 @@ | ||
| { | ||
| "recordID": "396bab503f4b90ad88858bb4467d59b9e827dcd82e33f41c230e1b8fb43bdc8c", | ||
| "interactions": [ | ||
| { | ||
| "request": { | ||
| "method": "GET", | ||
| "url": "/", | ||
| "request": "GET / HTTP/1.1", | ||
| "headers": { | ||
| "Accept": "*/*", | ||
| "Accept-Encoding": "gzip, deflate", | ||
| "Connection": "keep-alive", | ||
| "User-Agent": "python-requests/2.32.5" | ||
| }, | ||
| "bodySegments": [ | ||
| null | ||
| ], | ||
| "previousRequest": "b4d6e60a9b97e7b98c63df9308728c5c88c0b40c398046772c63447b94608b4d", | ||
| "serverAddress": "github.com", | ||
| "port": 443, | ||
| "protocol": "https" | ||
| }, | ||
| "shaSum": "396bab503f4b90ad88858bb4467d59b9e827dcd82e33f41c230e1b8fb43bdc8c", | ||
| "response": { | ||
| "statusCode": 200, | ||
| "headers": { | ||
| "Accept-Ranges": "bytes", | ||
| "Cache-Control": "max-age=0, private, must-revalidate", | ||
| "Content-Encoding": "gzip", | ||
| "Content-Language": "en-US", | ||
| "Content-Security-Policy": "default-src 'none'; base-uri 'self'; child-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/; connect-src 'self' uploads.github.com www.githubstatus.com collector.github.com raw.githubusercontent.com api.github.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com *.rel.tunnels.api.visualstudio.com wss://*.rel.tunnels.api.visualstudio.com objects-origin.githubusercontent.com copilot-proxy.githubusercontent.com proxy.individual.githubcopilot.com proxy.business.githubcopilot.com proxy.enterprise.githubcopilot.com *.actions.githubusercontent.com wss://*.actions.githubusercontent.com productionresultssa0.blob.core.windows.net/ productionresultssa1.blob.core.windows.net/ productionresultssa2.blob.core.windows.net/ productionresultssa3.blob.core.windows.net/ productionresultssa4.blob.core.windows.net/ productionresultssa5.blob.core.windows.net/ productionresultssa6.blob.core.windows.net/ productionresultssa7.blob.core.windows.net/ productionresultssa8.blob.core.windows.net/ productionresultssa9.blob.core.windows.net/ productionresultssa10.blob.core.windows.net/ productionresultssa11.blob.core.windows.net/ productionresultssa12.blob.core.windows.net/ productionresultssa13.blob.core.windows.net/ productionresultssa14.blob.core.windows.net/ productionresultssa15.blob.core.windows.net/ productionresultssa16.blob.core.windows.net/ productionresultssa17.blob.core.windows.net/ productionresultssa18.blob.core.windows.net/ productionresultssa19.blob.core.windows.net/ github-production-repository-image-32fea6.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com insights.github.com wss://alive.github.com wss://alive-staging.github.com api.githubcopilot.com api.individual.githubcopilot.com api.business.githubcopilot.com api.enterprise.githubcopilot.com github.githubassets.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com copilot-workspace.githubnext.com objects-origin.githubusercontent.com; frame-ancestors 'none'; frame-src viewscreen.githubusercontent.com notebooks.githubusercontent.com www.youtube-nocookie.com; img-src 'self' data: blob: github.githubassets.com media.githubusercontent.com camo.githubusercontent.com identicons.github.com avatars.githubusercontent.com private-avatars.githubusercontent.com github-cloud.s3.amazonaws.com objects.githubusercontent.com release-assets.githubusercontent.com secured-user-images.githubusercontent.com/ user-images.githubusercontent.com/ private-user-images.githubusercontent.com opengraph.githubassets.com copilotprodattachments.blob.core.windows.net/github-production-copilot-attachments/ github-production-user-asset-6210df.s3.amazonaws.com customer-stories-feed.github.com spotlights-feed.github.com objects-origin.githubusercontent.com *.githubusercontent.com images.ctfassets.net/8aevphvgewt8/; manifest-src 'self'; media-src github.com user-images.githubusercontent.com/ secured-user-images.githubusercontent.com/ private-user-images.githubusercontent.com github-production-user-asset-6210df.s3.amazonaws.com gist.github.com github.githubassets.com assets.ctfassets.net/8aevphvgewt8/ videos.ctfassets.net/8aevphvgewt8/; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com; upgrade-insecure-requests; worker-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/", | ||
| "Content-Type": "text/html; charset=utf-8", | ||
| "Date": "Thu, 28 Aug 2025 16:27:20 GMT", | ||
| "Etag": "W/\"386abcfaf4ff44a2cbfe6a90d3b1d6bf\"", | ||
| "Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin", | ||
| "Server": "github.com", | ||
| "Set-Cookie": "_gh_sess=LU54Wf8u3%2B7e1lbnM9LXqhwCyguiOMnGz%2FNGGANwWRg6UkobMXf3bqzDnwD64hIz5SAYSEM3L7IW%2FnzMF6AocWUE6uTxfE93SbuD8kvB1sbsBJlG3DKsg9eZicTdzOZihy3aCv1Y7syr0ZSlp3eKncPgpTAYlwoQGkTDoL1ly84KK2%2F9uGLbKTT2Q1dzWO7KAuP8znAQ98y%2FifLijPjexup%2BWvn95A4SCywsc3XcDgHYP%2BFh6na7uM1pO5toR9li4h2DpBlF5vne2m%2B0jDC0Mg%3D%3D--mDFUX5IeIv53eGZl--dmN4h2k8jrFI5%2Be%2FFUNaYw%3D%3D; Path=/; HttpOnly; Secure; SameSite=Lax, _octo=GH1.1.778961946.1756398441; Path=/; Domain=github.com; Expires=Fri, 28 Aug 2026 16:27:21 GMT; Secure; SameSite=Lax, logged_in=no; Path=/; Domain=github.com; Expires=Fri, 28 Aug 2026 16:27:21 GMT; HttpOnly; Secure; SameSite=Lax", | ||
| "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload", | ||
| "Vary": "X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, X-Requested-With, Accept-Language,Accept-Encoding, Accept, X-Requested-With", | ||
| "X-Content-Type-Options": "nosniff", | ||
| "X-Frame-Options": "deny", | ||
| "X-Github-Request-Id": "C832:288B0E:2DB350:2E5DA4:68B08369", | ||
| "X-Xss-Protection": "0" | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.