mirror of
https://github.com/trailofbits/algo.git
synced 2025-09-05 19:43:22 +02:00
Fix Python linting issues in test file
- Sort imports according to ruff standards - Remove trailing whitespace from blank lines - Remove unnecessary 'r' mode argument from open() - Add trailing newline at end of file All tests still pass after linting fixes. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
95e9cd2b4d
commit
6d1f0640de
1 changed files with 31 additions and 31 deletions
|
@ -5,11 +5,11 @@ Verifies that get_aws_connection_info() works without the deprecated boto3 param
|
||||||
Addresses issue #14822.
|
Addresses issue #14822.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import sys
|
|
||||||
import os
|
|
||||||
import unittest
|
|
||||||
from unittest.mock import patch, MagicMock
|
|
||||||
import importlib.util
|
import importlib.util
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
# Add the library directory to the path
|
# Add the library directory to the path
|
||||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../../library'))
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../../library'))
|
||||||
|
@ -17,7 +17,7 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../../library'))
|
||||||
|
|
||||||
class TestLightsailBoto3Fix(unittest.TestCase):
|
class TestLightsailBoto3Fix(unittest.TestCase):
|
||||||
"""Test that lightsail_region_facts.py works without boto3 parameter."""
|
"""Test that lightsail_region_facts.py works without boto3 parameter."""
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
"""Set up test fixtures."""
|
"""Set up test fixtures."""
|
||||||
# Mock the ansible module_utils since we're testing outside of Ansible
|
# Mock the ansible module_utils since we're testing outside of Ansible
|
||||||
|
@ -26,19 +26,19 @@ class TestLightsailBoto3Fix(unittest.TestCase):
|
||||||
'ansible.module_utils.ec2': MagicMock(),
|
'ansible.module_utils.ec2': MagicMock(),
|
||||||
'ansible.module_utils.aws.core': MagicMock(),
|
'ansible.module_utils.aws.core': MagicMock(),
|
||||||
}
|
}
|
||||||
|
|
||||||
# Apply mocks
|
# Apply mocks
|
||||||
self.patches = []
|
self.patches = []
|
||||||
for module_name, mock_module in self.mock_modules.items():
|
for module_name, mock_module in self.mock_modules.items():
|
||||||
patcher = patch.dict('sys.modules', {module_name: mock_module})
|
patcher = patch.dict('sys.modules', {module_name: mock_module})
|
||||||
patcher.start()
|
patcher.start()
|
||||||
self.patches.append(patcher)
|
self.patches.append(patcher)
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
"""Clean up patches."""
|
"""Clean up patches."""
|
||||||
for patcher in self.patches:
|
for patcher in self.patches:
|
||||||
patcher.stop()
|
patcher.stop()
|
||||||
|
|
||||||
def test_lightsail_region_facts_imports(self):
|
def test_lightsail_region_facts_imports(self):
|
||||||
"""Test that lightsail_region_facts can be imported."""
|
"""Test that lightsail_region_facts can be imported."""
|
||||||
try:
|
try:
|
||||||
|
@ -48,24 +48,24 @@ class TestLightsailBoto3Fix(unittest.TestCase):
|
||||||
os.path.join(os.path.dirname(__file__), '../../library/lightsail_region_facts.py')
|
os.path.join(os.path.dirname(__file__), '../../library/lightsail_region_facts.py')
|
||||||
)
|
)
|
||||||
module = importlib.util.module_from_spec(spec)
|
module = importlib.util.module_from_spec(spec)
|
||||||
|
|
||||||
# This should not raise an error
|
# This should not raise an error
|
||||||
spec.loader.exec_module(module)
|
spec.loader.exec_module(module)
|
||||||
|
|
||||||
# Verify the module loaded
|
# Verify the module loaded
|
||||||
self.assertIsNotNone(module)
|
self.assertIsNotNone(module)
|
||||||
self.assertTrue(hasattr(module, 'main'))
|
self.assertTrue(hasattr(module, 'main'))
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.fail(f"Failed to import lightsail_region_facts: {e}")
|
self.fail(f"Failed to import lightsail_region_facts: {e}")
|
||||||
|
|
||||||
def test_get_aws_connection_info_called_without_boto3(self):
|
def test_get_aws_connection_info_called_without_boto3(self):
|
||||||
"""Test that get_aws_connection_info is called without boto3 parameter."""
|
"""Test that get_aws_connection_info is called without boto3 parameter."""
|
||||||
# Mock get_aws_connection_info to track calls
|
# Mock get_aws_connection_info to track calls
|
||||||
mock_get_aws_connection_info = MagicMock(
|
mock_get_aws_connection_info = MagicMock(
|
||||||
return_value=('us-west-2', None, {})
|
return_value=('us-west-2', None, {})
|
||||||
)
|
)
|
||||||
|
|
||||||
with patch('ansible.module_utils.ec2.get_aws_connection_info', mock_get_aws_connection_info):
|
with patch('ansible.module_utils.ec2.get_aws_connection_info', mock_get_aws_connection_info):
|
||||||
# Import the module
|
# Import the module
|
||||||
spec = importlib.util.spec_from_file_location(
|
spec = importlib.util.spec_from_file_location(
|
||||||
|
@ -73,12 +73,12 @@ class TestLightsailBoto3Fix(unittest.TestCase):
|
||||||
os.path.join(os.path.dirname(__file__), '../../library/lightsail_region_facts.py')
|
os.path.join(os.path.dirname(__file__), '../../library/lightsail_region_facts.py')
|
||||||
)
|
)
|
||||||
module = importlib.util.module_from_spec(spec)
|
module = importlib.util.module_from_spec(spec)
|
||||||
|
|
||||||
# Mock AnsibleModule
|
# Mock AnsibleModule
|
||||||
mock_ansible_module = MagicMock()
|
mock_ansible_module = MagicMock()
|
||||||
mock_ansible_module.params = {}
|
mock_ansible_module.params = {}
|
||||||
mock_ansible_module.check_mode = False
|
mock_ansible_module.check_mode = False
|
||||||
|
|
||||||
with patch('ansible.module_utils.basic.AnsibleModule', return_value=mock_ansible_module):
|
with patch('ansible.module_utils.basic.AnsibleModule', return_value=mock_ansible_module):
|
||||||
# Execute the module
|
# Execute the module
|
||||||
try:
|
try:
|
||||||
|
@ -90,39 +90,39 @@ class TestLightsailBoto3Fix(unittest.TestCase):
|
||||||
except Exception:
|
except Exception:
|
||||||
# We expect some exceptions since we're mocking, but we want to check the call
|
# We expect some exceptions since we're mocking, but we want to check the call
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Verify get_aws_connection_info was called
|
# Verify get_aws_connection_info was called
|
||||||
if mock_get_aws_connection_info.called:
|
if mock_get_aws_connection_info.called:
|
||||||
# Get the call arguments
|
# Get the call arguments
|
||||||
call_args = mock_get_aws_connection_info.call_args
|
call_args = mock_get_aws_connection_info.call_args
|
||||||
|
|
||||||
# Ensure boto3=True is NOT in the arguments
|
# Ensure boto3=True is NOT in the arguments
|
||||||
if call_args:
|
if call_args:
|
||||||
# Check positional arguments
|
# Check positional arguments
|
||||||
if call_args[0]: # args
|
if call_args[0]: # args
|
||||||
self.assertTrue(len(call_args[0]) <= 1,
|
self.assertTrue(len(call_args[0]) <= 1,
|
||||||
"get_aws_connection_info should be called with at most 1 positional arg (module)")
|
"get_aws_connection_info should be called with at most 1 positional arg (module)")
|
||||||
|
|
||||||
# Check keyword arguments
|
# Check keyword arguments
|
||||||
if call_args[1]: # kwargs
|
if call_args[1]: # kwargs
|
||||||
self.assertNotIn('boto3', call_args[1],
|
self.assertNotIn('boto3', call_args[1],
|
||||||
"get_aws_connection_info should not be called with boto3 parameter")
|
"get_aws_connection_info should not be called with boto3 parameter")
|
||||||
|
|
||||||
def test_no_boto3_parameter_in_source(self):
|
def test_no_boto3_parameter_in_source(self):
|
||||||
"""Verify that boto3 parameter is not present in the source code."""
|
"""Verify that boto3 parameter is not present in the source code."""
|
||||||
lightsail_path = os.path.join(os.path.dirname(__file__), '../../library/lightsail_region_facts.py')
|
lightsail_path = os.path.join(os.path.dirname(__file__), '../../library/lightsail_region_facts.py')
|
||||||
|
|
||||||
with open(lightsail_path, 'r') as f:
|
with open(lightsail_path) as f:
|
||||||
content = f.read()
|
content = f.read()
|
||||||
|
|
||||||
# Check that boto3=True is not in the file
|
# Check that boto3=True is not in the file
|
||||||
self.assertNotIn('boto3=True', content,
|
self.assertNotIn('boto3=True', content,
|
||||||
"boto3=True parameter should not be present in lightsail_region_facts.py")
|
"boto3=True parameter should not be present in lightsail_region_facts.py")
|
||||||
|
|
||||||
# Check that boto3 parameter is not used with get_aws_connection_info
|
# Check that boto3 parameter is not used with get_aws_connection_info
|
||||||
self.assertNotIn('get_aws_connection_info(module, boto3', content,
|
self.assertNotIn('get_aws_connection_info(module, boto3', content,
|
||||||
"get_aws_connection_info should not be called with boto3 parameter")
|
"get_aws_connection_info should not be called with boto3 parameter")
|
||||||
|
|
||||||
def test_regression_issue_14822(self):
|
def test_regression_issue_14822(self):
|
||||||
"""
|
"""
|
||||||
Regression test for issue #14822.
|
Regression test for issue #14822.
|
||||||
|
@ -131,12 +131,12 @@ class TestLightsailBoto3Fix(unittest.TestCase):
|
||||||
# This test documents the specific issue that was fixed
|
# This test documents the specific issue that was fixed
|
||||||
# The boto3 parameter was deprecated and removed in amazon.aws collection
|
# The boto3 parameter was deprecated and removed in amazon.aws collection
|
||||||
# that comes with Ansible 11.x
|
# that comes with Ansible 11.x
|
||||||
|
|
||||||
lightsail_path = os.path.join(os.path.dirname(__file__), '../../library/lightsail_region_facts.py')
|
lightsail_path = os.path.join(os.path.dirname(__file__), '../../library/lightsail_region_facts.py')
|
||||||
|
|
||||||
with open(lightsail_path, 'r') as f:
|
with open(lightsail_path) as f:
|
||||||
lines = f.readlines()
|
lines = f.readlines()
|
||||||
|
|
||||||
# Find the line that calls get_aws_connection_info
|
# Find the line that calls get_aws_connection_info
|
||||||
for line_num, line in enumerate(lines, 1):
|
for line_num, line in enumerate(lines, 1):
|
||||||
if 'get_aws_connection_info' in line and 'region' in line:
|
if 'get_aws_connection_info' in line and 'region' in line:
|
||||||
|
@ -144,7 +144,7 @@ class TestLightsailBoto3Fix(unittest.TestCase):
|
||||||
# Verify it doesn't have boto3=True
|
# Verify it doesn't have boto3=True
|
||||||
self.assertNotIn('boto3', line,
|
self.assertNotIn('boto3', line,
|
||||||
f"Line {line_num} should not contain boto3 parameter")
|
f"Line {line_num} should not contain boto3 parameter")
|
||||||
|
|
||||||
# Verify the correct format
|
# Verify the correct format
|
||||||
self.assertIn('get_aws_connection_info(module)', line,
|
self.assertIn('get_aws_connection_info(module)', line,
|
||||||
f"Line {line_num} should call get_aws_connection_info(module) without boto3")
|
f"Line {line_num} should call get_aws_connection_info(module) without boto3")
|
||||||
|
@ -154,4 +154,4 @@ class TestLightsailBoto3Fix(unittest.TestCase):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Add table
Reference in a new issue