Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 49 additions & 8 deletions tests/unit/lib/iac/test_iac_implementations.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from unittest import TestCase
from unittest.mock import Mock, MagicMock
from unittest.mock import MagicMock
import tempfile
import os
import yaml

from samcli.lib.iac.cdk.cdk_iac import CdkIacImplementation
from samcli.lib.iac.cfn.cfn_iac import CfnIacImplementation
from samcli.lib.iac.plugins_interfaces import SamCliContext, LookupPath


# TODO: Implement these tests when the classes are fully implemented
Expand All @@ -11,14 +14,52 @@ class TestImplementations(TestCase):
def test_cfn_implementation(self):
impl = CfnIacImplementation(MagicMock())
impl.get_iac_file_patterns()
impl.update_packaged_locations(Mock())
impl.write_project(Mock(), Mock())
impl.update_packaged_locations(MagicMock())
impl.write_project(MagicMock(), MagicMock())
self.assertTrue(True)

def test_cdk_implementation(self):
impl = CdkIacImplementation(Mock())
impl = CfnIacImplementation(MagicMock())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this is changed?

impl.get_iac_file_patterns()
impl.read_project(Mock())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to delete this

impl.update_packaged_locations(Mock())
impl.write_project(Mock(), Mock())
impl.update_packaged_locations(MagicMock())
impl.write_project(MagicMock(), MagicMock())
self.assertTrue(True)

def test_read_project_minimal_template(self):
# Create a minimal CloudFormation template
minimal_template = {
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"MyFunction": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Handler": "index.handler",
"Runtime": "python3.8",
"CodeUri": "./src"
}
}
}
}
with tempfile.TemporaryDirectory() as tmpdir:
template_path = os.path.join(tmpdir, "template.yaml")
with open(template_path, "w") as f:
yaml.dump(minimal_template, f)

context = SamCliContext(
command_options_map={"template_file": template_path},
sam_command_name="test",
is_guided=False,
is_debugging=False,
profile=None,
region=None
)
impl = CfnIacImplementation(context)
lookup_paths = [LookupPath(tmpdir)]
project = impl.read_project(lookup_paths)

# Assert the project contains one stack with the expected resource
self.assertEqual(len(project.stacks), 1)
stack = project.stacks[0]
self.assertIn("Resources", stack)
resources = stack["Resources"].section_items
self.assertTrue(any(r.item_id == "MyFunction" for r in resources))