Test Case for check inspector_ec2_scan_enabled#262
Test Case for check inspector_ec2_scan_enabled#262Gunjan-Katre-Comprinno wants to merge 2 commits intocomprinnotech:testcases_devfrom
Conversation
|
|
||
| report = self.check.execute(self.mock_session) | ||
|
|
||
| assert report.status == CheckStatus.UNKNOWN |
There was a problem hiding this comment.
🛠 Areas for Improvement
⚠️ 1. Missing a Case Where No ec2 Key Exists in resourceState
In real-world scenarios, it’s possible that the ec2 key is not present at all (e.g., misconfigured Inspector2 response or service not onboarded yet).
Recommendation:
Add a test like this:
def test_ec2_scan_status_missing(self):
self.mock_inspector_client.batch_get_account_status.return_value = {
"accounts": [{}]
}
report = self.check.execute(self.mock_session)
assert report.status == CheckStatus.UNKNOWN
assert any("transitional" in r.summary.lower() or "unknown" in r.status.name.lower()
for r in report.resource_ids_status)This would ensure the check handles partial or malformed responses gracefully.
⚠️ 2. ClientError Not Explicitly Tested
While you do simulate a generic Exception, testing for AWS-specific ClientError would align with how other AWS SDKs (like IAM or S3) often fail.
Why it matters: It helps ensure your except Exception block is robust enough to catch and format structured errors cleanly.
Suggestion:
from botocore.exceptions import ClientError
def test_client_error_handling(self):
self.mock_inspector_client.batch_get_account_status.side_effect = ClientError(
error_response={"Error": {"Code": "AccessDenied", "Message": "Access denied"}},
operation_name="BatchGetAccountStatus"
)
report = self.check.execute(self.mock_session)
assert report.status == CheckStatus.UNKNOWN
assert any("access denied" in r.summary.lower() for r in report.resource_ids_status)⚠️ 3. Account ID Not Used in Summary
The check fetches account_id from STS, but that value is never surfaced in the summary. Including it might help users correlate findings, especially in org-level scanning setups.
Not mandatory, but worth considering for clarity.
Certainly! Here's the test description for
TestInspectorEC2ScanEnabledin the format you requested:Context
This change adds a unit test for the
inspector_ec2_scan_enabledcheck, which validates whether Amazon Inspector2 EC2 standard scanning is enabled for the account. This aligns with AWS security best practices and supports automated detection of vulnerable EC2 instances.Fixes potential oversight in test coverage for Inspector2 EC2 resource scanning status.
Description
The test suite
TestInspectorEC2ScanEnabledincludes the following scenarios:ENABLED→ check passes.DISABLED→ check fails.SUSPENDED→ check fails.TRANSITIONING→ check returns unknown.Dependencies: Mocked AWS clients (
inspector2,sts) usingunittest.mock. No external AWS access is required.Checklist
License
I confirm that my contribution is made under the terms of the Apache 2.0 license.