-
Notifications
You must be signed in to change notification settings - Fork 50
bug: replace cron job restarting agentex pod for oidc refresh with mongoclient refresh #338
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
Open
michael-chou359
wants to merge
2
commits into
main
Choose a base branch
from
mc/oidc-refresh
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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
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
Empty file.
71 changes: 71 additions & 0 deletions
71
agentex/tests/integration/config/test_mongodb_oidc_refresh_integration.py
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,71 @@ | ||
| """Integration tests for the MongoDB client-refresh swap against a real Mongo. | ||
|
|
||
| The unit tests mock the client; these prove the build-validate-swap-drain works | ||
| end-to-end against a live MongoDB container: data written before the swap is still | ||
| readable after it, the post-swap client is fully functional, and the superseded | ||
| client is drained and closed. (The container doesn't speak GCP OIDC, so the OIDC | ||
| gate is forced on to exercise the swap path itself.) | ||
| """ | ||
|
|
||
| from unittest.mock import AsyncMock | ||
|
|
||
| import pytest | ||
| from src.config.dependencies import GlobalDependencies, Singleton | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def deps(mongodb_connection_string): | ||
| """Fresh GlobalDependencies wired to the test Mongo container.""" | ||
| Singleton._instances.pop(GlobalDependencies, None) | ||
| instance = GlobalDependencies() | ||
| instance.environment_variables = instance.environment_variables.model_copy( | ||
| update={ | ||
| "MONGODB_URI": mongodb_connection_string, | ||
| "MONGODB_DATABASE_NAME": "agentex_oidc_refresh_test", | ||
| } | ||
| ) | ||
| instance.mongodb_client = instance._build_mongodb_client(mongodb_connection_string) | ||
| instance.mongodb_database = instance.mongodb_client["agentex_oidc_refresh_test"] | ||
| yield instance | ||
| Singleton._instances.pop(GlobalDependencies, None) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.integration | ||
| async def test_refresh_preserves_data_and_drains_old_client(deps, monkeypatch): | ||
| # Treat the container URI as OIDC so the refresh path actually runs, and | ||
| # collapse the drain delay so the close completes within the test. | ||
| monkeypatch.setattr(deps, "_mongodb_uses_oidc", lambda: True) | ||
| original_close_after_delay = deps._close_mongodb_client_after_delay | ||
|
|
||
| async def fast_close(client, delay=0.0): | ||
| await original_close_after_delay(client, delay=0.0) | ||
|
|
||
| monkeypatch.setattr(deps, "_close_mongodb_client_after_delay", fast_close) | ||
|
|
||
| collection = "docs" | ||
| await deps.mongodb_database[collection].insert_one({"_id": "before", "n": 1}) | ||
|
|
||
| old_client = deps.mongodb_client | ||
| old_client.close = AsyncMock(wraps=old_client.close) | ||
|
|
||
| await deps.refresh_mongodb_client() | ||
|
|
||
| # A genuinely new client is now installed. | ||
| assert deps.mongodb_client is not old_client | ||
|
|
||
| # The new client can write, and reads the doc written before the swap. | ||
| await deps.mongodb_database[collection].insert_one({"_id": "after", "n": 2}) | ||
| ids = { | ||
| doc["_id"] | ||
| async for doc in deps.mongodb_database[collection].find({}, {"_id": 1}) | ||
| } | ||
| assert ids == {"before", "after"} | ||
|
|
||
| # The superseded client is drained and closed. | ||
| for task in list(deps._mongodb_close_tasks): | ||
| await task | ||
| old_client.close.assert_awaited_once() | ||
|
|
||
| await deps.mongodb_client.drop_database("agentex_oidc_refresh_test") | ||
| await deps.mongodb_client.close() |
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.
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.
This refresh swaps only
GlobalDependencies.mongodb_database, but long-lived consumers can keep usingAsyncDatabaseand collection objects from the old client. For example, the Temporal worker builds retention repositories once at startup, andMongoDBCRUDRepository.__init__storesself.collection = db[collection_name]. After the first refresh, those activities still use collections bound toold_client; once this delayed close runs, retention cleanup Mongo operations can fail even though the global dependency points at a fresh client.Artifacts
Repro: pytest harness for cached Mongo collection across OIDC refresh
Repro: verbose pytest output showing cached collection uses the closed superseded client
Prompt To Fix With AI