-
Notifications
You must be signed in to change notification settings - Fork 13
[AIENG-196] Add the new command for the early flake detection #1130
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import click | ||
|
|
||
| from launchable.utils.click import GroupWithAlias | ||
|
|
||
| from .flake_detection import flake_detection | ||
|
|
||
|
|
||
| @click.group(cls=GroupWithAlias) | ||
| def retry(): | ||
| pass | ||
|
|
||
|
|
||
| retry.add_command(flake_detection, 'flake-detection') |
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,86 @@ | ||
| import os | ||
| import sys | ||
|
|
||
| import click | ||
|
|
||
| from launchable.app import Application | ||
| from launchable.commands.helper import find_or_create_session | ||
| from launchable.commands.test_path_writer import TestPathWriter | ||
| from launchable.utils.click import ignorable_error | ||
| from launchable.utils.env_keys import REPORT_ERROR_KEY | ||
| from launchable.utils.launchable_client import LaunchableClient | ||
| from launchable.utils.tracking import Tracking, TrackingClient | ||
|
|
||
| from ...utils.commands import Command | ||
|
|
||
|
|
||
| @click.group(help="Early flake detection") | ||
| @click.option( | ||
| '--session', | ||
| 'session', | ||
| help='In the format builds/<build-name>/test_sessions/<test-session-id>', | ||
| type=str, | ||
| required=True | ||
| ) | ||
| @click.option( | ||
| '--confidence', | ||
| help='Confidence level for flake detection', | ||
| type=click.Choice(['low', 'medium', 'high'], case_sensitive=False), | ||
| required=True, | ||
| ) | ||
| @click.pass_context | ||
| def flake_detection(ctx, confidence, session): | ||
| tracking_client = TrackingClient(Command.FLAKE_DETECTION, app=ctx.obj) | ||
| client = LaunchableClient(app=ctx.obj, tracking_client=tracking_client, test_runner=ctx.invoked_subcommand) | ||
| session_id = None | ||
| try: | ||
| session_id = find_or_create_session( | ||
| context=ctx, | ||
| session=session, | ||
| build_name=None, | ||
| tracking_client=tracking_client | ||
| ) | ||
| except click.UsageError as e: | ||
| click.echo(click.style(str(e), fg="red"), err=True) | ||
| sys.exit(1) | ||
| except Exception as e: | ||
| tracking_client.send_error_event( | ||
| event_name=Tracking.ErrorEvent.INTERNAL_CLI_ERROR, | ||
| stack_trace=str(e), | ||
| ) | ||
| if os.getenv(REPORT_ERROR_KEY): | ||
| raise e | ||
| else: | ||
| click.echo(ignorable_error(e), err=True) | ||
| if session_id is None: | ||
| return | ||
|
|
||
| class FlakeDetection(TestPathWriter): | ||
| def __init__(self, app: Application): | ||
| super(FlakeDetection, self).__init__(app=app) | ||
|
|
||
| def run(self): | ||
| test_paths = [] | ||
| try: | ||
| res = client.request( | ||
| "get", | ||
| "retry/flake-detection", | ||
| params={ | ||
| "confidence": confidence.upper(), | ||
| "session-id": os.path.basename(session_id), | ||
| "test-runner": ctx.invoked_subcommand}) | ||
| res.raise_for_status() | ||
| test_paths = res.json().get("testPaths", []) | ||
| if test_paths: | ||
| self.print(test_paths) | ||
| except Exception as e: | ||
| tracking_client.send_error_event( | ||
| event_name=Tracking.ErrorEvent.INTERNAL_CLI_ERROR, | ||
| stack_trace=str(e), | ||
| ) | ||
| if os.getenv(REPORT_ERROR_KEY): | ||
| raise e | ||
| else: | ||
| click.echo(ignorable_error(e), err=True) | ||
|
|
||
| ctx.obj = FlakeDetection(app=ctx.obj) |
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
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,83 @@ | ||
| import os | ||
| from unittest import mock | ||
|
|
||
| import responses # type: ignore | ||
|
|
||
| from launchable.utils.http_client import get_base_url | ||
| from tests.cli_test_case import CliTestCase | ||
|
|
||
|
|
||
| class FlakeDetectionTest(CliTestCase): | ||
| @responses.activate | ||
| @mock.patch.dict(os.environ, {"LAUNCHABLE_TOKEN": CliTestCase.launchable_token}) | ||
| def test_flake_detection_success(self): | ||
| mock_json_response = { | ||
| "testPaths": [ | ||
| [{"type": "file", "name": "test_flaky_1.py"}], | ||
| [{"type": "file", "name": "test_flaky_2.py"}], | ||
| ] | ||
| } | ||
| responses.add( | ||
| responses.GET, | ||
| f"{get_base_url()}/intake/organizations/{self.organization}/workspaces/{self.workspace}/retry/flake-detection", | ||
| json=mock_json_response, | ||
| status=200, | ||
| ) | ||
| result = self.cli( | ||
| "retry", | ||
| "flake-detection", | ||
| "--session", | ||
| self.session, | ||
| "--confidence", | ||
| "high", | ||
| "file", | ||
| mix_stderr=False, | ||
| ) | ||
| self.assert_success(result) | ||
| self.assertIn("test_flaky_1.py", result.stdout) | ||
| self.assertIn("test_flaky_2.py", result.stdout) | ||
|
|
||
| @responses.activate | ||
| @mock.patch.dict(os.environ, {"LAUNCHABLE_TOKEN": CliTestCase.launchable_token}) | ||
| def test_flake_detection_no_flakes(self): | ||
| mock_json_response = {"testPaths": []} | ||
| responses.add( | ||
| responses.GET, | ||
| f"{get_base_url()}/intake/organizations/{self.organization}/workspaces/{self.workspace}/retry/flake-detection", | ||
| json=mock_json_response, | ||
| status=200, | ||
| ) | ||
| result = self.cli( | ||
| "retry", | ||
| "flake-detection", | ||
| "--session", | ||
| self.session, | ||
| "--confidence", | ||
| "low", | ||
| "file", | ||
| mix_stderr=False, | ||
| ) | ||
| self.assert_success(result) | ||
| self.assertEqual(result.stdout, "") | ||
|
|
||
| @responses.activate | ||
| @mock.patch.dict(os.environ, {"LAUNCHABLE_TOKEN": CliTestCase.launchable_token}) | ||
| def test_flake_detection_api_error(self): | ||
| responses.add( | ||
| responses.GET, | ||
| f"{get_base_url()}/intake/organizations/{self.organization}/workspaces/{self.workspace}/retry/flake-detection", | ||
| status=500, | ||
| ) | ||
| result = self.cli( | ||
| "retry", | ||
| "flake-detection", | ||
| "--session", | ||
| self.session, | ||
| "--confidence", | ||
| "medium", | ||
| "file", | ||
| mix_stderr=False, | ||
| ) | ||
| self.assert_exit_code(result, 0) | ||
| self.assertIn("Error", result.stderr) | ||
| self.assertEqual(result.stdout, "") |
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.
For my information:
Why do we need it here?
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.
To register this test runner as the command. By adding that line, we can do something like this: