11import random
2- from typing import List
32
43import pytest
54from pytest import mark , param
65
76import slp_tfplan .tests .resources .test_resource_paths as resources
87from otm .otm .entity .otm import OTM
98from sl_util .sl_util .file_utils import get_byte_data
10- from slp_base import IacFileNotValidError
9+ from slp_base import IacFileNotValidError , MappingFileNotValidError
10+ from slp_base .slp_base .errors import ErrorCode
11+ from slp_base .slp_base .mapping import MAX_SIZE as MAPPING_MAX_SIZE , MIN_SIZE as MAPPING_MIN_SIZE
1112from slp_base .tests .util .otm import validate_and_compare
1213from slp_tfplan import TFPlanProcessor
1314from slp_tfplan .tests .util .builders import create_artificial_file , MIN_FILE_SIZE , MAX_TFPLAN_FILE_SIZE , \
1415 MAX_TFGRAPH_FILE_SIZE
1516
1617DEFAULT_MAPPING_FILE = get_byte_data (resources .terraform_iriusrisk_tfplan_aws_mapping )
18+ SECONDARY_DEFAULT_MAPPING_FILE = get_byte_data (resources .terraform_plan_default_mapping )
19+ CONFIG_CLIENT_MAPPING_FILE = get_byte_data (resources .terraform_plan_config_client_mapping )
20+ CONFIG_TRUSTZONE_MAPPING_FILE = get_byte_data (resources .terraform_plan_config_trustzone_mapping )
21+ CONFIG_OVERRIDE_DEFAULT = get_byte_data (resources .terraform_plan_config_override_default )
22+ CONFIG_OVERRIDE_CUSTOM = get_byte_data (resources .terraform_plan_config_override_custom )
1723
1824SAMPLE_VALID_TFPLAN = get_byte_data (resources .tfplan_elb )
1925SAMPLE_VALID_TFGRAPH = get_byte_data (resources .tfgraph_elb )
2430TFPLAN_OFFICIAL = get_byte_data (resources .tfplan_official )
2531TFGRAPH_OFFICIAL = get_byte_data (resources .tfgraph_official )
2632
33+ TFPLAN_AWS_COMPLETE = get_byte_data (resources .tfplan_aws_complete )
34+ TFGRAPH_AWS_COMPLETE = get_byte_data (resources .tfgraph_aws_complete )
35+
36+ TFPLAN_BASE = get_byte_data (resources .tfplan_base )
37+ TFGRAPH_BASE = get_byte_data (resources .tfgraph_base )
38+
2739SAMPLE_ID = 'id'
2840SAMPLE_NAME = 'name'
2941EXCLUDED_REGEX = r"root\[\'dataflows'\]\[.+?\]\['id'\]"
@@ -57,7 +69,7 @@ def test_tfplan_tfgraph_examples(tfplan: bytes, tfgraph: bytes, expected: str):
5769 param ([SAMPLE_VALID_TFPLAN ], id = 'one source' ),
5870 param ([SAMPLE_VALID_TFPLAN ] * random .randint (3 , 10 ), id = 'more than two sources' )
5971])
60- def test_wrong_number_of_parameters (sources : List [bytes ]):
72+ def test_wrong_number_of_parameters (sources : list [bytes ]):
6173 # GIVEN a wrong number of sources
6274
6375 # WHEN TFPlanProcessor::process is invoked
@@ -75,7 +87,7 @@ def test_wrong_number_of_parameters(sources: List[bytes]):
7587 param ([SAMPLE_VALID_TFPLAN , create_artificial_file (MIN_FILE_SIZE - 1 )], id = 'tfgraph too small' ),
7688 param ([SAMPLE_VALID_TFPLAN , create_artificial_file (MAX_TFGRAPH_FILE_SIZE + 1 )], id = 'tfgraph too big' )
7789])
78- def test_invalid_size (sources : List [bytes ]):
90+ def test_invalid_size (sources : list [bytes ]):
7991 # GIVEN a tfplan or tfgraph with an invalid size
8092
8193 # WHEN TFPlanProcessor::process is invoked
@@ -87,6 +99,30 @@ def test_invalid_size(sources: List[bytes]):
8799 assert error .value .title == 'Terraform Plan file is not valid'
88100 assert error .value .message == 'Provided iac_file is not valid. Invalid size'
89101
102+ @mark .parametrize ('mappings' , [
103+ param ([create_artificial_file (MAPPING_MIN_SIZE - 1 ), DEFAULT_MAPPING_FILE ], id = 'mapping file too small' ),
104+ param ([create_artificial_file (MAPPING_MAX_SIZE + 1 ), DEFAULT_MAPPING_FILE ], id = 'mapping file too big' ),
105+ param ([DEFAULT_MAPPING_FILE , create_artificial_file (MAPPING_MIN_SIZE - 1 )], id = 'custom mapping file too small' ),
106+ param ([DEFAULT_MAPPING_FILE , create_artificial_file (MAPPING_MAX_SIZE + 1 )], id = 'custom mapping file too big' )
107+ ])
108+ def test_invalid_mapping_size (mappings : list [bytes ]):
109+ # GIVEN a valid tfplan and tfgraph
110+ tfplan = get_byte_data (resources .tfplan_official )
111+ tfgraph = get_byte_data (resources .tfgraph_official )
112+
113+ # AND a mapping file with an invalid size ('mappings' arg)
114+
115+ # WHEN TFPlanProcessor::process is invoked
116+ # THEN a MappingFileNotValidError is raised
117+ with pytest .raises (MappingFileNotValidError ) as error :
118+ TFPlanProcessor (SAMPLE_ID , SAMPLE_NAME , [tfplan , tfgraph ], mappings ).process ()
119+
120+ # AND the error details are correct
121+ assert ErrorCode .MAPPING_FILE_NOT_VALID == error .value .error_code
122+ assert 'Mapping files are not valid' == error .value .title
123+ assert 'Mapping files are not valid. Invalid size' == error .value .detail
124+ assert 'Mapping files are not valid. Invalid size' == error .value .message
125+
90126def test_two_tfplan ():
91127 # GIVEN two valid TFPLANs
92128 sources = [SAMPLE_VALID_TFPLAN , SAMPLE_VALID_TFPLAN ]
@@ -105,7 +141,7 @@ def test_two_tfplan():
105141 param ([SAMPLE_VALID_TFPLAN , SAMPLE_INVALID_TFGRAPH ], id = 'invalid tfgraph' ),
106142 param ([SAMPLE_INVALID_TFPLAN , SAMPLE_INVALID_TFGRAPH ], id = 'both invalid' )
107143])
108- def test_invalid_sources (sources : List [bytes ]):
144+ def test_invalid_sources (sources : list [bytes ]):
109145 # GIVEN some invalid tfplan
110146
111147 # WHEN TFPlanProcessor::process is invoked
@@ -150,3 +186,65 @@ def test_singleton_grouped_by_category():
150186 assert components [1 ].id == 'aws_cloudwatch_log_group.click_logger_firehose_delivery_stream_log_group'
151187 assert components [1 ].name == 'CloudWatch'
152188 assert components [1 ].type == 'cloudwatch'
189+
190+ def test_aws_complete_sample ():
191+ # GIVEN a valid tfplan and tfgraph
192+ tfplan = TFPLAN_AWS_COMPLETE
193+ tfgraph = TFGRAPH_AWS_COMPLETE
194+
195+ # AND a mapping file with an invalid size ('mappings' arg)
196+ mapping_file = SECONDARY_DEFAULT_MAPPING_FILE
197+
198+ # WHEN TFPlanProcessor::process is invoked
199+ otm = TFPlanProcessor (SAMPLE_ID , SAMPLE_NAME , [tfplan , tfgraph ], [mapping_file ]).process ()
200+
201+ # AND the details are correct
202+ assert len (otm .representations ) == 1
203+ assert len (otm .trustzones ) == 2
204+ assert len (otm .components ) == 15
205+ assert len (otm .dataflows ) == 8
206+
207+ def test_configuration_trustzone_no_client ():
208+ # GIVEN two valid TFPLANs
209+ tfplan = TFPLAN_BASE
210+ tfgraph = TFGRAPH_BASE
211+
212+ # WHEN TFPlanProcessor::process is invoked
213+ # THEN a MappingFileNotValidError exception is raised
214+ with pytest .raises (MappingFileNotValidError ) as error :
215+ TFPlanProcessor (SAMPLE_ID , SAMPLE_NAME , [tfplan , tfgraph ], [CONFIG_TRUSTZONE_MAPPING_FILE ]).process ()
216+
217+ # AND the message says that no multiple tfplan files can be processed at the same time
218+ assert str (error .value .title ) == 'Mapping files are not valid'
219+ assert str (error .value .detail ) == 'Mapping file does not comply with the schema'
220+ assert str (error .value .message ) == "'client' is a required property"
221+
222+ def test_configuration_client_no_trustzone ():
223+ # GIVEN two valid TFPLANs
224+ tfplan = TFPLAN_BASE
225+ tfgraph = TFGRAPH_BASE
226+
227+ # WHEN TFPlanProcessor::process is invoked
228+ # THEN a MappingFileNotValidError exception is raised
229+ with pytest .raises (MappingFileNotValidError ) as error :
230+ TFPlanProcessor (SAMPLE_ID , SAMPLE_NAME , [tfplan , tfgraph ], [CONFIG_CLIENT_MAPPING_FILE ]).process ()
231+
232+ # AND the message says that no multiple tfplan files can be processed at the same time
233+ assert str (error .value .title ) == 'Mapping files are not valid'
234+ assert str (error .value .detail ) == 'Mapping file does not comply with the schema'
235+ assert str (error .value .message ) == "'trustzone' is a required property"
236+
237+ def test_configuration_mapping_override ():
238+ # GIVEN two valid TFPLANs
239+ tfplan = TFPLAN_BASE
240+ tfgraph = TFGRAPH_BASE
241+
242+ # WHEN TFPlanProcessor::process is invoked
243+ otm = TFPlanProcessor (SAMPLE_ID , SAMPLE_NAME , [tfplan , tfgraph ],
244+ [CONFIG_OVERRIDE_DEFAULT , CONFIG_OVERRIDE_CUSTOM ]).process ()
245+
246+ # AND the details are correct
247+ assert len (otm .representations ) == 1
248+ assert len (otm .trustzones ) == 2
249+ assert len (otm .components ) == 15
250+ assert len (otm .dataflows ) == 13
0 commit comments