-
Notifications
You must be signed in to change notification settings - Fork 54
Run session notebook against fresh db #678
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
FlorentinD
merged 2 commits into
neo4j:main
from
FlorentinD:notebooksessiontest-on-new-instance
Jul 11, 2024
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| import logging | ||
| import time | ||
| from time import sleep | ||
| from typing import Any, Dict, Optional | ||
|
|
||
| import requests | ||
|
|
||
|
|
||
| class AuraApiCI: | ||
| class AuraAuthToken: | ||
| access_token: str | ||
| expires_in: int | ||
| token_type: str | ||
|
|
||
| def __init__(self, json: Dict[str, Any]) -> None: | ||
| self.access_token = json["access_token"] | ||
| expires_in: int = json["expires_in"] | ||
| self.expires_at = int(time.time()) + expires_in | ||
| self.token_type = json["token_type"] | ||
|
|
||
| def is_expired(self) -> bool: | ||
| return self.expires_at >= int(time.time()) | ||
|
|
||
| def __init__(self, client_id: str, client_secret: str, tenant_id: Optional[str] = None) -> None: | ||
| self._token: Optional[AuraApiCI.AuraAuthToken] = None | ||
| self._logger = logging.getLogger() | ||
| self._auth = (client_id, client_secret) | ||
| self._tenant_id = tenant_id | ||
|
|
||
| def _build_header(self) -> Dict[str, str]: | ||
| return {"Authorization": f"Bearer {self._auth_token()}", "User-agent": "neo4j-graphdatascience-ci"} | ||
|
|
||
| def _auth_token(self) -> str: | ||
| if self._token is None or self._token.is_expired(): | ||
| self._token = self._update_token() | ||
| return self._token.access_token | ||
|
|
||
| def _update_token(self) -> AuraAuthToken: | ||
| data = { | ||
| "grant_type": "client_credentials", | ||
| } | ||
|
|
||
| self._logger.debug("Updating oauth token") | ||
|
|
||
| response = requests.post("https://api-staging.neo4j.io/oauth/token", data=data, auth=self._auth) | ||
|
|
||
| response.raise_for_status() | ||
|
|
||
| return AuraApiCI.AuraAuthToken(response.json()) | ||
|
|
||
| def create_ds_instance(self, name: str) -> Dict[str, Any]: | ||
| return self.create_instance(name, memory="8GB", type="gds") | ||
|
|
||
| def create_instance(self, name: str, memory: str, type: str) -> Dict[str, Any]: | ||
| CREATE_OK_MAX_WAIT_TIME = 10 | ||
|
|
||
| data = { | ||
| "name": name, | ||
| "memory": memory, | ||
| "version": "5", | ||
| "region": "europe-west1", | ||
| "type": type, | ||
| "cloud_provider": "gcp", | ||
| "tenant_id": self.get_tenant_id(), | ||
| } | ||
|
|
||
| should_retry = True | ||
| wait_time = 1 | ||
|
|
||
| while should_retry: | ||
| sleep(wait_time) | ||
| wait_time *= 2 | ||
|
|
||
| response = requests.post( | ||
| "https://api-staging.neo4j.io/v1/instances", | ||
| json=data, | ||
| headers=self._build_header(), | ||
| ) | ||
| should_retry = response.status_code in [500, 502, 503, 504, 405] and CREATE_OK_MAX_WAIT_TIME > wait_time | ||
|
|
||
| if should_retry: | ||
| logging.debug(f"Error code: {response.status_code} - Retrying in {wait_time} s") | ||
|
|
||
| response.raise_for_status() | ||
|
|
||
| return response.json()["data"] # type: ignore | ||
|
|
||
| def check_running(self, db_id: str) -> None: | ||
| RUNNING_MAX_WAIT_TIME = 60 * 5 | ||
|
|
||
| should_retry = True | ||
| wait_time = 1 | ||
|
|
||
| while should_retry: | ||
| sleep(wait_time) | ||
| wait_time *= 2 | ||
|
|
||
| response = requests.get( | ||
| f"https://api-staging.neo4j.io/v1/instances/{db_id}", | ||
| headers=self._build_header(), | ||
| ) | ||
|
|
||
| instance_status = "?" | ||
| if response.status_code == 200: | ||
| instance_status = response.json()["data"]["status"] | ||
|
|
||
| should_retry = ( | ||
| response.status_code in [500, 502, 503, 504] or instance_status == "creating" | ||
| ) and RUNNING_MAX_WAIT_TIME > wait_time | ||
|
|
||
| if should_retry: | ||
| logging.debug( | ||
| f"Status code: {response.status_code}, Status: {instance_status} - Retrying in {wait_time} s" | ||
| ) | ||
|
|
||
| response.raise_for_status() | ||
|
|
||
| def teardown_instance(self, db_id: str) -> None: | ||
| TEARDOWN_MAX_WAIT_TIME = 10 | ||
|
|
||
| should_retry = True | ||
| wait_time = 1 | ||
|
|
||
| while should_retry: | ||
| sleep(wait_time) | ||
| wait_time *= 2 | ||
|
|
||
| response = requests.delete( | ||
| f"https://api-staging.neo4j.io/v1/instances/{db_id}", | ||
| headers=self._build_header(), | ||
| ) | ||
|
|
||
| if response.status_code == 202: | ||
| should_retry = False | ||
|
|
||
| should_retry = (response.status_code in [500, 502, 503, 504]) and TEARDOWN_MAX_WAIT_TIME > wait_time | ||
|
|
||
| if should_retry: | ||
| logging.debug(f"Status code: {response.status_code} - Retrying in {wait_time} s") | ||
|
|
||
| response.raise_for_status() | ||
|
|
||
| def get_tenant_id(self) -> str: | ||
| if self._tenant_id: | ||
| return self._tenant_id | ||
|
|
||
| response = requests.get( | ||
| "https://api-staging.neo4j.io/v1/tenants", | ||
| headers=self._build_header(), | ||
| ) | ||
| response.raise_for_status() | ||
|
|
||
| raw_data = response.json()["data"] | ||
| assert len(raw_data) == 1 | ||
|
|
||
| return raw_data[0]["id"] # type: ignore |
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,45 @@ | ||
| # run `tox -e jupyter-notebook-session-ci` | ||
|
|
||
| import logging | ||
| import os | ||
| import random as rd | ||
| import sys | ||
|
|
||
| from aura_api_ci import AuraApiCI | ||
|
|
||
| logging.basicConfig(level=logging.INFO) | ||
|
|
||
|
|
||
| def main() -> None: | ||
| client_id = os.environ["AURA_API_CLIENT_ID"] | ||
| client_secret = os.environ["AURA_API_CLIENT_SECRET"] | ||
| tenant_id = os.environ.get("TENANT_ID") | ||
| aura_api = AuraApiCI(client_id=client_id, client_secret=client_secret, tenant_id=tenant_id) | ||
|
|
||
| MAX_INT = 1000000 | ||
| instance_name = f"ci-build-{sys.argv[1]}" if len(sys.argv) > 1 else "ci-instance-" + str(rd.randint(0, MAX_INT)) | ||
|
|
||
| create_result = aura_api.create_instance(instance_name, memory="1GB", type="professional-db") | ||
| instance_id = create_result["id"] | ||
| logging.info("Creation of database accepted") | ||
|
|
||
| try: | ||
| aura_api.check_running(instance_id) | ||
| logging.info("Database %s up and running", instance_id) | ||
|
|
||
| uri = (create_result["connection_url"],) | ||
| username = (create_result["username"],) | ||
| password = (create_result["password"],) | ||
|
|
||
| cmd = f"AURA_DB_ADDRESS={uri} AURA_DB_USER={username} AURA_DB_PW={password} tox -e jupyter-notebook-session-ci" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Meanwhile, this test runs with an AuraDB instance and dedicated or AuraDS-based sessions. |
||
|
|
||
| if os.system(cmd) != 0: | ||
| raise Exception("Failed to run notebooks") | ||
|
|
||
| finally: | ||
| aura_api.teardown_instance(instance_id) | ||
| logging.info("Teardown of instance %s successful", instance_id) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
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.
So this file runs tests and notebooks using just an AuraDS instance?
I suggest renaming the file
run_targeting_auradsThere 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.
I realise this will need another TC change