-
Notifications
You must be signed in to change notification settings - Fork 127
Enable azdev for Azure/azure-cli CI automation test #188
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
7 commits
Select commit
Hold shift + click to select a range
b617803
azdev test: remove --ci example
8f54efa
add ProfileContext
b948ea2
pytest>=4.4.0
6aa36ea
add incremental test strategt for CI
9606d17
refine wtih ProfileContext and AzureDevOpsContext
1870c90
Make pytest run with forked when in CI and be aware of OS platform
c37cf7f
add incremental test filter
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # ----------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for | ||
| # license information. | ||
| # ----------------------------------------------------------------------------- | ||
|
|
||
| import abc | ||
| from knack.util import CLIError | ||
|
|
||
| from azdev.utilities import get_path_table, git_util | ||
|
|
||
|
|
||
| # @wrapt.decorator | ||
| # def cli_release_scenario(wrapped, _, args, kwargs): | ||
| # """ | ||
| # Filter out those files in Azure CLI release stage | ||
| # """ | ||
| # # TODO | ||
| # # if instance.resolved: | ||
| # # return instance | ||
|
|
||
| # return wrapped(*args, **kwargs) | ||
|
|
||
|
|
||
| class AzureDevOpsContext(abc.ABC): | ||
| def __init__(self, git_repo, git_source, git_target): | ||
| """ | ||
| :param git_source: could be commit id, branch name or any valid value for git diff | ||
| :param git_target: could be commit id, branch name or any valid value for git diff | ||
| """ | ||
| self.git_repo = git_repo | ||
| self.git_source = git_source | ||
| self.git_target = git_target | ||
|
|
||
| @abc.abstractmethod | ||
| def filter(self, test_index): | ||
| pass | ||
|
|
||
|
|
||
| class CLIAzureDevOpsContext(AzureDevOpsContext): | ||
| """ | ||
| Assemble strategy of incremental test on Azure DevOps Environment for Azure CLI | ||
| """ | ||
| def __init__(self, git_repo, git_source, git_target): | ||
| super().__init__(git_repo, git_source, git_target) | ||
|
|
||
| if not any([self.git_source, self.git_target, self.git_repo]): | ||
| raise CLIError('usage error: [--src NAME] --tgt NAME --repo PATH --cli-ci') | ||
|
|
||
| if not all([self.git_target, self.git_repo]): | ||
| raise CLIError('usage error: [--src NAME] --tgt NAME --repo PATH --cli-ci') | ||
|
|
||
| @property | ||
| def modified_files(self): | ||
| modified_files = git_util.diff_branches(self.git_repo, self.git_source, self.git_target) | ||
| return [f for f in modified_files if f.startswith('src/')] | ||
|
|
||
| def filter(self, test_index): | ||
| """ | ||
| Strategy on Azure CLI pull request verification stage. | ||
|
|
||
| :return: a list of modified packages | ||
| """ | ||
|
|
||
| modified_packages = git_util.summarize_changed_mods(self.modified_files) | ||
|
|
||
| if any(core_package in modified_packages for core_package in ['core', 'testsdk', 'telemetry']): | ||
| path_table = get_path_table() | ||
|
|
||
| # tests under all packages | ||
| return list(path_table['mod'].keys()) + list(path_table['core'].keys()) + list(path_table['ext'].keys()) | ||
|
|
||
| return modified_packages |
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 @@ | ||
| # ----------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for | ||
| # license information. | ||
| # ----------------------------------------------------------------------------- | ||
|
|
||
| import traceback | ||
|
|
||
| from knack.log import get_logger | ||
| from knack.util import CLIError | ||
|
|
||
| from azdev.utilities import call, cmd | ||
| from azdev.utilities import display | ||
|
|
||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
|
|
||
| class ProfileContext: | ||
| def __init__(self, profile_name=None): | ||
| self.target_profile = profile_name | ||
|
|
||
| self.origin_profile = current_profile() | ||
|
|
||
| def __enter__(self): | ||
| if self.target_profile is None or self.target_profile == self.origin_profile: | ||
| display('The tests are set to run against current profile "{}"'.format(self.origin_profile)) | ||
| else: | ||
| result = cmd('az cloud update --profile {}'.format(self.target_profile), | ||
| 'Switching to target profile "{}"...'.format(self.target_profile)) | ||
| if result.exit_code != 0: | ||
| raise CLIError(result.error.output.decode('utf-8')) | ||
|
|
||
| def __exit__(self, exc_type, exc_val, exc_tb): | ||
| if self.target_profile is not None and self.target_profile != self.origin_profile: | ||
| display('Switching back to origin profile "{}"...'.format(self.origin_profile)) | ||
| call('az cloud update --profile {}'.format(self.origin_profile)) | ||
|
|
||
| if exc_tb: | ||
| display('') | ||
| traceback.print_exception(exc_type, exc_val, exc_tb) | ||
|
|
||
|
|
||
| def current_profile(): | ||
| return cmd('az cloud show --query profile -otsv', show_stderr=False).result |
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,5 @@ | ||
| # ----------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for | ||
| # license information. | ||
| # ----------------------------------------------------------------------------- |
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,33 @@ | ||
| # ----------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for | ||
| # license information. | ||
| # ----------------------------------------------------------------------------- | ||
|
|
||
| import unittest | ||
|
|
||
| from knack.util import CLIError | ||
|
|
||
| from azdev.operations.tests.profile_context import ProfileContext | ||
|
|
||
|
|
||
| class TestProfileContext(unittest.TestCase): | ||
|
|
||
| def test_profile_ok(self): | ||
| target_profiles = ['latest', '2017-03-09-profile', '2018-03-01-hybrid', '2019-03-01-hybrid'] | ||
|
|
||
| for profile in target_profiles: | ||
| with ProfileContext(profile): | ||
| self.assertEqual(1, 1) | ||
|
|
||
| def test_unsupported_profile(self): | ||
| unknown_profile = 'unknown-profile' | ||
|
|
||
| with self.assertRaises(CLIError): | ||
| with ProfileContext(unknown_profile): | ||
| pass | ||
|
|
||
| def test_raise_inner_exception(self): | ||
| with self.assertRaises(Exception): | ||
| with ProfileContext('latest'): | ||
| raise Exception('inner Exception') |
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
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.
The added
-xargument means:This is a breaking change to the original behavior.