11import pytest
22from pathlib import Path
33from tfsumpy .plan_analyzer import LocalPlanAnalyzer
4+ from tfsumpy .context import Context
5+ import json
46
57@pytest .fixture
68def sample_plan_path ():
79 return str (Path (__file__ ).parent / 'sample1.json' )
810
911@pytest .fixture
10- def plan_analyzer ():
11- return LocalPlanAnalyzer ()
12+ def context ():
13+ """Create a test context with default configuration"""
14+ return Context (debug = True )
15+
16+ @pytest .fixture
17+ def plan_analyzer (context ):
18+ """Create a plan analyzer with test context"""
19+ return LocalPlanAnalyzer (context = context )
1220
1321def test_generate_report (plan_analyzer , sample_plan_path ):
22+ """Test report generation with sample plan"""
1423 report = plan_analyzer .generate_report (sample_plan_path )
1524
1625 # Test summary statistics
@@ -29,19 +38,22 @@ def test_generate_report(plan_analyzer, sample_plan_path):
2938 s3_resource = next (r for r in resources if r ['resource_type' ] == 'aws_s3_bucket' )
3039 assert s3_resource ['action' ] == 'create'
3140 assert 'example' in s3_resource ['identifier' ]
41+ assert s3_resource ['module' ] == 'root' # Test module information
3242
3343 # Verify EC2 instance update
3444 ec2_resource = next (r for r in resources if r ['resource_type' ] == 'aws_instance' )
3545 assert ec2_resource ['action' ] == 'update'
3646 assert 'web_server' in ec2_resource ['identifier' ]
47+ assert ec2_resource ['module' ] == 'root'
3748
3849 # Verify security group deletion
3950 sg_resource = next (r for r in resources if r ['resource_type' ] == 'aws_security_group' )
4051 assert sg_resource ['action' ] == 'delete'
4152 assert 'obsolete' in sg_resource ['identifier' ]
42-
53+ assert sg_resource [ 'module' ] == 'root'
4354
4455def test_sanitization (plan_analyzer ):
56+ """Test text sanitization with sensitive data"""
4557 sensitive_text = """
4658 {
4759 "access_key": "AKIAXXXXXXXXXXXXXXXX",
@@ -57,3 +69,30 @@ def test_sanitization(plan_analyzer):
5769 assert '[SECRET-REDACTED]' in sanitized
5870 assert '192.168.1.1' not in sanitized
5971 assert '[IP-REDACTED]' in sanitized
72+
73+ def test_module_handling (plan_analyzer , tmp_path ):
74+ """Test handling of module-based resources"""
75+ # Create a sample plan with module resources
76+ module_plan = {
77+ "resource_changes" : [
78+ {
79+ "address" : "module.network.aws_vpc.main" ,
80+ "type" : "aws_vpc" ,
81+ "change" : {"actions" : ["create" ]},
82+ "module" : "network"
83+ }
84+ ]
85+ }
86+
87+ plan_path = tmp_path / "module_plan.json"
88+ with open (plan_path , "w" ) as f :
89+ json .dump (module_plan , f )
90+
91+ report = plan_analyzer .generate_report (str (plan_path ))
92+ resources = report ['summary' ]['resources' ]
93+
94+ assert len (resources ) == 1
95+ vpc_resource = resources [0 ]
96+ assert vpc_resource ['module' ] == 'network'
97+ assert vpc_resource ['resource_type' ] == 'aws_vpc'
98+ assert vpc_resource ['action' ] == 'create'
0 commit comments