-
Notifications
You must be signed in to change notification settings - Fork 804
[wmi] better com_errors handling
#2790
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
Closed
Closed
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
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,43 @@ | ||
| """ | ||
| List WMI "known" `com_errors` errors. | ||
|
|
||
| Translate to user intelligible exceptions. | ||
| """ | ||
|
|
||
| _user_exception_by_com_errors = {} | ||
|
|
||
|
|
||
| def com_error(error_id): | ||
| """ | ||
| A decorator that assigns an `error_id` to an intelligible exception. | ||
| """ | ||
| def set_exception(exception): | ||
| _user_exception_by_com_errors[error_id] = exception | ||
| return exception | ||
| return set_exception | ||
|
|
||
|
|
||
| def raise_on_com_error(error): | ||
| """ | ||
| Raise the user exception associated with the given `com_error` or | ||
| fall back to the original exception. | ||
| """ | ||
| raise _user_exception_by_com_errors.get(error[0], error) | ||
|
|
||
|
|
||
| # List of user exceptions | ||
| class WMIException(Exception): | ||
| """ | ||
| Abtract exception for WMI. | ||
| """ | ||
| def __init__(self): | ||
| """ | ||
| Use the class docstring as an exception message. | ||
| """ | ||
| super(WMIException, self).__init__(self.__doc__) | ||
|
|
||
|
|
||
| @com_error(-2147217392) | ||
| class WMIInvalidClass(WMIException): | ||
| """WMI class is invalid.""" | ||
| pass |
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,55 @@ | ||
| # stdlib | ||
| import unittest | ||
|
|
||
| # datadog | ||
| from checks.libs.wmi.exceptions import com_error, raise_on_com_error | ||
|
|
||
|
|
||
| class MockedWMICOMError(Exception): | ||
| """ | ||
| Mocking a WMI `com_error` error. | ||
| """ | ||
| def __init__(self, error_id): | ||
| super(MockedWMICOMError, self).__init__() | ||
| self.error_id = error_id | ||
|
|
||
| def __getitem__(self, index): | ||
| if index == 0: | ||
| return self.error_id | ||
|
|
||
| raise NotImplementedError | ||
|
|
||
|
|
||
| class TestWMIExceptions(unittest.TestCase): | ||
| """ | ||
| Unit testing for WMI `com_error` errors and user exceptions. | ||
| """ | ||
| def test_register_com_error(self): | ||
| """ | ||
| `com_error` decorator register and map a WMI `com_error` to an user exception. | ||
| """ | ||
| # Mock a WMI `com_error` | ||
| sample_error = MockedWMICOMError(123456) | ||
|
|
||
| # Map it to an user exception | ||
| @com_error(sample_error.error_id) | ||
| class WMISampleException(Exception): | ||
| """ | ||
| Sample WMI exception. | ||
| """ | ||
| pass | ||
|
|
||
| # Assert that the exception is raised | ||
| with self.assertRaises(WMISampleException): | ||
| raise_on_com_error(sample_error) | ||
|
|
||
| def test_unregistred_com_error(self): | ||
| """ | ||
| Unknown `com_error` errors remain intact. | ||
| """ | ||
| # Mock a WMI `com_error`, do not register it | ||
| sample_error = MockedWMICOMError(456789) | ||
|
|
||
| # Assert that the original exception is raised | ||
| with self.assertRaises(MockedWMICOMError): | ||
| raise_on_com_error(sample_error) |
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.
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.
I get why you have implemented theNevermind.mutefeature. But do we really need it? None of the checks initiate the sampler with a different value here, no?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 Datadog integration checks relying on
wmihavemutesetFalse. This allows the check to raise, and the exception message to be used by thedd-agentinfo page.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.
Yeah, I had looked at the top of the PR yesterday, and forgot about
mutebeingFalsethere. Thanks for the explanation though.