-
Notifications
You must be signed in to change notification settings - Fork 11
Add retry to check_sudo_status and unit test #278
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
21 commits
Select commit
Hold shift + click to select a range
396e2e7
add unit test for check_sudo_status()
feng-j678 c7c86c6
add unit case for insufficient outputline and unexpected output
feng-j678 226f18b
fix mock_insufficient_run_command_output
feng-j678 e16c145
fix mock_insufficient_run_command_output
feng-j678 9812ffb
reomve retry_attempt constant
feng-j678 e600c86
refactor func description
feng-j678 0a49237
remove eol in test_coremain.py
feng-j678 f71559f
mock env_layer instead
feng-j678 e4eb9b3
move mock check_sudo_check logic to new bootstrapper test file
feng-j678 02fd359
remove the import adodbapi
feng-j678 de4e261
refactor Bootstrapper
feng-j678 d3b3d30
move check_sudo_status into a retry method
feng-j678 91ba355
undo check_sudo_status logic and add attempt checks in check_sudo_sta…
feng-j678 5ce76b8
Merge branch 'master' of https://github.com/Azure/LinuxPatchExtension…
feng-j678 7a2159c
change inner logerr to logdebug and move sudo status check log to retry
feng-j678 4060cc4
fix rety try return
feng-j678 ee36fa4
modify the sudo failed log and add line to eol and add data type to c…
feng-j678 c7e377f
add check for None and return false retry when attempts exhausted
feng-j678 1fa1aa2
add logic for handling sudo_status is false or none
feng-j678 e5928f4
raise exception when sudo_status is None or false
feng-j678 ba5660a
set retry to return none and update signature
feng-j678 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,119 @@ | ||
| # Copyright 2020 Microsoft Corporation | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| # Requires Python 2.7+ | ||
| import unittest | ||
|
|
||
| from core.src.bootstrap.Constants import Constants | ||
| from core.tests.library.ArgumentComposer import ArgumentComposer | ||
| from core.tests.library.RuntimeCompositor import RuntimeCompositor | ||
|
|
||
|
|
||
| class TestBootstrapper(unittest.TestCase): | ||
| def setUp(self): | ||
| self.sudo_check_status_attempts = 0 | ||
| Constants.SET_CHECK_SUDO_STATUS_TRUE = False | ||
| argument_composer = ArgumentComposer() | ||
| argument_composer.operation = Constants.ASSESSMENT | ||
| self.argv = argument_composer.get_composed_arguments() | ||
| self.runtime = RuntimeCompositor(self.argv, legacy_mode=True, package_manager_name=Constants.APT) | ||
|
|
||
| def tearDown(self): | ||
| self.sudo_check_status_attempts = 0 | ||
| Constants.SET_CHECK_SUDO_STATUS_TRUE = True | ||
| self.runtime.stop() | ||
|
|
||
| # regions mock | ||
| def mock_false_run_command_output(self, command, no_output=False, chk_err=True): | ||
| """Mock a failed sudo check status command output to test retry logic.""" | ||
| # Mock failure to trigger retry logic in check_sudo_status | ||
| return (1, "[sudo] password for user:\nFalse") | ||
|
|
||
| def mock_insufficient_run_command_output(self, command, no_output=False, chk_err=True): | ||
| """Mock an insufficient output line in sudo check status command output.""" | ||
| # Mock failure to trigger retry logic in check_sudo_status | ||
| return (1, "[sudo] password for user:") | ||
|
|
||
| def mock_unexpected_output_run_command_output(self, command, no_output=False, chk_err=True): | ||
| """Mock an unexpected output line in sudo check status command output.""" | ||
| # Mock failure to trigger retry logic in check_sudo_status | ||
| return (1, "[sudo] password for user:\nUnexpectedOutput") | ||
|
|
||
| def mock_retry_run_command_output(self, command, no_output=False, chk_err=True): | ||
| """Mock 3 failed sudo check status attempts followed by a success on the 4th attempt.""" | ||
| self.sudo_check_status_attempts += 1 | ||
|
|
||
| # Mock failure on the first two attempts | ||
| if self.sudo_check_status_attempts <= 2: | ||
| return (1, "[sudo] password for user:\nFalse") | ||
|
|
||
| # Mock success (True) on the 3rd attempt | ||
| elif self.sudo_check_status_attempts == 3: | ||
| return (0, "uid=0(root) gid=0(root) groups=0(root)\nTrue") | ||
| # end regions mock | ||
|
|
||
| def test_check_sudo_status_all_attempts_failed(self): | ||
| # Set raise_if_not_sudo=False to test the `return False` all attempts failed | ||
| self.runtime.env_layer.run_command_output = self.mock_false_run_command_output | ||
|
|
||
| result = self.runtime.bootstrapper.check_sudo_status_with_retry(raise_if_not_sudo=False) | ||
|
|
||
| # Verify check_sudo_status_with_retry is False | ||
| self.assertEqual(result, None, "Expected check_sudo_status retry to return None after all attempts failed") | ||
|
|
||
| def test_check_sudo_status_throw_exception(self): | ||
| # Set raise_if_not_sudo=True to throw exception) after all retries | ||
| self.runtime.env_layer.run_command_output = self.mock_false_run_command_output | ||
| with self.assertRaises(Exception) as context: | ||
| self.runtime.bootstrapper.check_sudo_status_with_retry(raise_if_not_sudo=True) | ||
|
|
||
| # Verify exception msg contains the expected failure text | ||
| self.assertTrue("Unable to invoke sudo successfully" in str(context.exception)) | ||
|
|
||
| def test_check_sudo_status_insufficient_output_lines(self): | ||
| # Test insufficient output lines to raise exception after all retries | ||
| self.runtime.env_layer.run_command_output = self.mock_insufficient_run_command_output | ||
|
|
||
| with self.assertRaises(Exception) as context: | ||
| self.runtime.bootstrapper.check_sudo_status_with_retry() | ||
|
|
||
| # Verify exception msg contains the expected failure text | ||
| self.assertTrue("Unexpected sudo check result" in str(context.exception)) | ||
|
|
||
| def test_check_sudo_status_unexpected_output_lines(self): | ||
| # Test unexpected output with neither false or true to raise exception after all retries | ||
| self.runtime.env_layer.run_command_output = self.mock_unexpected_output_run_command_output | ||
|
|
||
| with self.assertRaises(Exception) as context: | ||
| self.runtime.bootstrapper.check_sudo_status_with_retry() | ||
|
|
||
| # Verify exception msg contains the expected failure text | ||
| self.assertTrue("Unexpected sudo check result" in str(context.exception)) | ||
|
|
||
| def test_check_sudo_status_succeeds_on_third_attempt(self): | ||
| # Test retry logic in check sudo status after 2 failed attempts followed by success (true) | ||
| self.runtime.env_layer.run_command_output = self.mock_retry_run_command_output | ||
|
|
||
| # Attempt to check sudo status, succeed (true) on the 3rd attempt | ||
| result = self.runtime.bootstrapper.check_sudo_status_with_retry(raise_if_not_sudo=True) | ||
|
|
||
| # Verify the result is success (True) | ||
| self.assertTrue(result, "Expected check_sudo_status to succeed on the 3rd attempts") | ||
|
|
||
| # Verify 3 attempts were made | ||
| self.assertEqual(self.sudo_check_status_attempts, 3, "Expected exactly 3 attempts in check_sudo_status") | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
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
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.