-
Notifications
You must be signed in to change notification settings - Fork 13
[AIENG-404] The plumbing command enhancements for alias and component… #1265
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
kohsuke
wants to merge
2
commits into
main
Choose a base branch
from
AIENG-404
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| from ... import args4p | ||
| from ...app import Application | ||
| from .alias import alias | ||
|
|
||
|
|
||
| @args4p.group(help="Update Smart Tests resources") | ||
| def update(app: Application): | ||
| return app | ||
|
|
||
|
|
||
| update.add_command(alias) |
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 @@ | ||
| from typing import Annotated | ||
|
|
||
| import click | ||
|
|
||
| import smart_tests.args4p.typer as typer | ||
| from smart_tests import args4p | ||
| from smart_tests.app import Application | ||
| from smart_tests.utils.commands import Command | ||
| from smart_tests.utils.fail_fast_mode import set_fail_fast_mode, warn_and_exit_if_fail_fast_mode | ||
| from smart_tests.utils.smart_tests_client import SmartTestsClient | ||
| from smart_tests.utils.tracking import TrackingClient | ||
|
|
||
|
|
||
| @args4p.command(help="Point an alias at a build") | ||
| def alias( | ||
| app: Application, | ||
| build_name: Annotated[str, typer.Option( | ||
| "--build", | ||
| help="Build name to point the alias at", | ||
| metavar="NAME", | ||
| required=True, | ||
| )], | ||
| alias_name: Annotated[str, typer.Option( | ||
| "--alias", | ||
| help="Alias name", | ||
| metavar="NAME", | ||
| required=True, | ||
| )], | ||
| ): | ||
| tracking_client = TrackingClient(Command.UPDATE_ALIAS, app=app) | ||
| client = SmartTestsClient(app=app, tracking_client=tracking_client) | ||
| set_fail_fast_mode(client.is_fail_fast_mode()) | ||
|
|
||
| # TODO: It's not entirely clear to me which layer is responsible for URL encoding | ||
| # this validation logic was copied from record/build.py | ||
| if "/" in alias_name or "%2f" in alias_name.lower(): | ||
| click.echo("--alias must not contain a slash and an encoded slash", err=True) | ||
| raise typer.Exit(1) | ||
| if "%25" in alias_name: | ||
| click.echo("--alias must not contain encoded % (%25)", err=True) | ||
| raise typer.Exit(1) | ||
|
|
||
| try: | ||
| res = client.request("put", f"builds/aliases/{alias_name}", payload={"build": build_name}) | ||
| res.raise_for_status() | ||
| click.echo(f"Alias '{alias_name}' now points to build '{build_name}'") | ||
kohsuke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| except Exception as e: | ||
kohsuke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| warn_and_exit_if_fail_fast_mode(f"Failed to update alias: {e}") | ||
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.
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,69 @@ | ||
| import json | ||
| import os | ||
| from unittest import mock | ||
|
|
||
| import responses # type: ignore | ||
|
|
||
| from tests.cli_test_case import CliTestCase | ||
| from smart_tests.utils.http_client import get_base_url | ||
|
|
||
|
|
||
| class AliasTest(CliTestCase): | ||
| alias_name = "staging-payment-svc" | ||
| build_name_target = "jenkins-main-135" | ||
| error_body = "Build 'jenkins-main-135' not found" | ||
|
|
||
| def alias_url(self): | ||
| return ( | ||
| f"{get_base_url()}/intake/organizations/{self.organization}" | ||
| f"/workspaces/{self.workspace}/builds/aliases/{self.alias_name}" | ||
| ) | ||
|
|
||
| @responses.activate | ||
| @mock.patch.dict(os.environ, {"SMART_TESTS_TOKEN": CliTestCase.smart_tests_token}) | ||
| def test_update_alias(self): | ||
| responses.add(responses.PUT, self.alias_url(), json={}, status=200) | ||
|
|
||
| result = self.cli( | ||
| "update", "alias", | ||
| "--build", self.build_name_target, | ||
| "--alias", self.alias_name, | ||
| ) | ||
| self.assert_success(result) | ||
|
|
||
| put_call = next(c for c in responses.calls if c.request.method == "PUT") | ||
| self.assert_json_orderless_equal( | ||
| {"build": self.build_name_target}, | ||
| json.loads(put_call.request.body) | ||
| ) | ||
| self.assertIn(self.alias_name, result.output) | ||
| self.assertIn(self.build_name_target, result.output) | ||
|
|
||
| @responses.activate | ||
| @mock.patch.dict(os.environ, {"SMART_TESTS_TOKEN": CliTestCase.smart_tests_token}) | ||
| def test_update_alias_build_not_found(self): | ||
| """Without fail-fast mode: prints warning in yellow, exits 0 (doesn't halt CI).""" | ||
| responses.add(responses.PUT, self.alias_url(), json={"reason": self.error_body}, status=404) | ||
|
|
||
| result = self.cli( | ||
| "update", "alias", | ||
| "--build", self.build_name_target, | ||
| "--alias", self.alias_name, | ||
| ) | ||
| self.assert_success(result) | ||
| self.assertIn(self.error_body, result.output) | ||
|
|
||
| @responses.activate | ||
| @mock.patch.dict(os.environ, {"SMART_TESTS_TOKEN": CliTestCase.smart_tests_token}) | ||
| @mock.patch("smart_tests.utils.fail_fast_mode.is_fail_fast_mode", return_value=True) | ||
| def test_update_alias_build_not_found_fail_fast(self, _mock_ffm): | ||
| """With fail-fast mode: prints error in red, exits 1.""" | ||
| responses.add(responses.PUT, self.alias_url(), json={"reason": self.error_body}, status=404) | ||
|
|
||
| result = self.cli( | ||
| "update", "alias", | ||
| "--build", self.build_name_target, | ||
| "--alias", self.alias_name, | ||
| ) | ||
| self.assert_exit_code(result, 1) | ||
| self.assertIn(self.error_body, result.output) |
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.