nmpAgents/demo/tests/test_aws_infrastructure.py

53 lines
2.5 KiB
Python

"""Regression checks for Lambda routing without AWS credentials or calls."""
import importlib.util
from pathlib import Path
import sys
import types
import unittest
from unittest.mock import Mock, patch
class InfrastructureTests(unittest.TestCase):
def setUp(self):
fake_boto3 = types.ModuleType("boto3")
fake_boto3.client = Mock()
path = Path(__file__).resolve().parents[1] / "src/lambdas/aws_infrastructure/aws_infrastructure.py"
spec = importlib.util.spec_from_file_location("infrastructure_under_test", path)
self.module = importlib.util.module_from_spec(spec)
with patch.dict(sys.modules, {"boto3": fake_boto3}):
spec.loader.exec_module(self.module)
self.module.ec2 = Mock()
self.module.lambda_client = Mock()
def test_lambda_operations_route_to_lambda_api(self):
for operation in ("listar_lambdas", "listar", "list"):
with self.subTest(operation=operation):
self.module.lambda_client.reset_mock()
self.module.lambda_client.list_functions.return_value = {
"Functions": [{"FunctionName": "demo-function", "Runtime": "python3.14"}]
}
result = self.module.lambda_handler({"operation": operation}, None)
self.assertEqual(result["count"], 1)
self.assertEqual(result["functions"][0]["functionName"], "demo-function")
self.module.lambda_client.list_functions.assert_called_once_with()
self.module.ec2.describe_instances.assert_not_called()
def test_ec2_default_and_explicit_operation_are_preserved(self):
for event in ({}, {"operation": "listar_ec2"}):
with self.subTest(event=event):
self.module.ec2.reset_mock()
self.module.ec2.describe_instances.return_value = {"Reservations": []}
self.assertEqual(self.module.lambda_handler(event, None), {"count": 0, "instances": []})
self.module.ec2.describe_instances.assert_called_once_with()
self.module.lambda_client.list_functions.assert_not_called()
def test_unknown_operation_does_not_call_aws(self):
result = self.module.lambda_handler({"operation": "delete"}, None)
self.assertEqual(result, {"error": "Operacion no soportada", "operation": "delete"})
self.module.ec2.describe_instances.assert_not_called()
self.module.lambda_client.list_functions.assert_not_called()
if __name__ == "__main__":
unittest.main()