1212from slp_tfplan .slp_tfplan .load .tfplan_loader import TFPlanLoader
1313from slp_tfplan .tests .resources import test_resource_paths
1414from slp_tfplan .tests .util .asserts import assert_resource_values
15- from slp_tfplan .tests .util .builders import build_tfplan , generate_resources , generate_child_modules
15+ from slp_tfplan .tests .util .builders import build_tfplan , generate_resources , generate_child_modules , \
16+ generate_child_modules_instances
1617
1718INVALID_YAML = test_resource_paths .invalid_yaml
1819TF_FILE_YAML_EXCEPTION = JSONDecodeError ('HLC2 cannot be processed as JSON' , doc = 'sample-doc' , pos = 0 )
@@ -73,7 +74,7 @@ def test_load_no_modules(self, yaml_mock):
7374
7475 for i , resource in enumerate (resources ):
7576 i += 1
76- assert resource ['resource_id' ] == f'r{ i } -addr '
77+ assert resource ['resource_id' ] == f'r{ i } -type.r { i } -name '
7778 assert resource ['resource_type' ] == f'r{ i } -type'
7879 assert resource ['resource_name' ] == f'r{ i } -name'
7980
@@ -102,7 +103,7 @@ def test_load_only_modules(self, yaml_mock):
102103 for child_index in range (1 , 3 ):
103104 resource = resources [resource_index ]
104105
105- assert resource ['resource_id' ] == f'r{ child_index } -addr '
106+ assert resource ['resource_id' ] == f'r{ child_index } -type.r { child_index } -name '
106107 assert resource ['resource_type' ] == f'r{ child_index } -type'
107108 assert resource ['resource_name' ] == f'{ module_address } .r{ child_index } -name'
108109
@@ -130,9 +131,9 @@ def test_load_nested_modules(self, yaml_mock):
130131 # AND resource_id, resource_type, resource_name and resource_properties are right
131132 assert len (resources ) == 1
132133
133- assert resource ['resource_id' ] == 'r1-addr '
134+ assert resource ['resource_id' ] == 'r1-type.r1-name '
134135 assert resource ['resource_type' ] == 'r1-type'
135- assert resource ['resource_name' ] == 'cm1-addr.cm1-addr. r1-name'
136+ assert resource ['resource_name' ] == 'cm1-addr.r1-name'
136137
137138 assert_resource_values (resource ['resource_values' ])
138139
@@ -155,15 +156,15 @@ def test_load_complex_structure(self, yaml_mock):
155156 # AND resource_type, resource_name and resource_properties from top level are right
156157 resource = resources [0 ]
157158
158- assert resource ['resource_id' ] == 'r1-addr '
159+ assert resource ['resource_id' ] == 'r1-type.r1-name '
159160 assert resource ['resource_type' ] == 'r1-type'
160161 assert resource ['resource_name' ] == 'r1-name'
161162
162163 assert_resource_values (resource ['resource_values' ])
163164
164165 # AND resource_type, resource_name and resource_properties from child modules are right
165166 resource = resources [1 ]
166- assert resource ['resource_id' ] == 'r1-addr '
167+ assert resource ['resource_id' ] == 'r1-type.r1-name '
167168 assert resource ['resource_type' ] == 'r1-type'
168169 assert resource ['resource_name' ] == 'cm1-addr.r1-name'
169170
@@ -193,7 +194,7 @@ def test_load_resources_same_name(self, yaml_mock):
193194 assert len (resources ) == 1
194195
195196 # AND The duplicated resource is unified and the index is no present in name or id
196- assert resources [0 ]['resource_id' ] == 'r1-addr '
197+ assert resources [0 ]['resource_id' ] == 'r1-type.r1-name '
197198 assert resources [0 ]['resource_name' ] == 'cm1-addr.r1-name'
198199
199200 @patch ('json.loads' )
@@ -230,9 +231,85 @@ def test_load_modules_same_name(self, yaml_mock):
230231 assert len (resources ) == 1
231232
232233 # AND The duplicated resource is unified and the index is not present in name or id
233- assert resources [0 ]['resource_id' ] == 'cm1-addr.r1-addr'
234+ assert resources [0 ]['resource_id' ] == 'cm1-addr.r1-type.r1-name'
235+ assert resources [0 ]['resource_name' ] == 'cm1-addr.r1-name'
236+
237+ @patch ('json.loads' )
238+ def test_load_modules_instances (self , yaml_mock ):
239+ # GIVEN a valid plain Terraform Plan file with only modules
240+ yaml_mock .side_effect = [build_tfplan (
241+ child_modules = generate_child_modules_instances (module_count = 2 , resource_count = 2 ))]
242+
243+ # WHEN TFPlanLoader::load is invoked
244+ tfplan_loader = TFPlanLoader (sources = [b'MOCKED' , b'MOCKED' ])
245+ tfplan_loader .load ()
246+
247+ # THEN TF contents are loaded in TfplanLoader.terraform
248+ assert tfplan_loader .terraform
249+ resources = tfplan_loader .terraform ['resource' ]
250+ assert len (resources ) == 2
251+
252+ # AND resource_id, resource_type, resource_name and resource_properties are right
253+ resource_index = 0
254+ for _ in range (1 , 2 ):
255+
256+ module_address = 'cm-addr'
257+
258+ for child_index in range (1 , 3 ):
259+ resource = resources [resource_index ]
260+
261+ assert (resource ['resource_id' ] == f'{ module_address } .r{ child_index } -type.r{ child_index } -name' )
262+ assert resource ['resource_type' ] == f'r{ child_index } -type'
263+ assert resource ['resource_name' ] == f'{ module_address } .r{ child_index } -name'
264+
265+ assert_resource_values (resource ['resource_values' ])
266+
267+ resource_index += 1
268+
269+ @patch ('json.loads' )
270+ def test_load_modules_mixed (self , yaml_mock ):
271+ # GIVEN a valid plain Terraform Plan file with two instances of a module and two resources each
272+ mixed_modules = generate_child_modules (module_count = 1 , resource_count = 1 )
273+ module_instances = generate_child_modules_instances (module_count = 2 , resource_count = 2 )
274+ mixed_modules .append (module_instances [0 ])
275+ mixed_modules .append (module_instances [1 ])
276+
277+ tfplan = build_tfplan (child_modules = mixed_modules )
278+
279+ # GIVEN a valid plain Terraform Plan file with only modules
280+ yaml_mock .side_effect = [tfplan ]
281+
282+ # WHEN TFPlanLoader::load is invoked
283+ tfplan_loader = TFPlanLoader (sources = [b'MOCKED' , b'MOCKED' ])
284+ tfplan_loader .load ()
285+
286+ # THEN TF contents are loaded in TfplanLoader.terraform
287+ assert tfplan_loader .terraform
288+ resources = tfplan_loader .terraform ['resource' ]
289+ assert len (resources ) == 3
290+
291+ # AND resource_id, resource_type, resource_name and resource_properties are right
292+
293+ assert resources [0 ]['resource_id' ] == 'r1-type.r1-name'
234294 assert resources [0 ]['resource_name' ] == 'cm1-addr.r1-name'
235295
296+ resource_index = 1
297+
298+ for _ in range (1 , 2 ):
299+
300+ module_address = 'cm-addr'
301+
302+ for child_index in range (1 , 2 ):
303+ resource = resources [resource_index ]
304+
305+ assert (resource ['resource_id' ] == f'{ module_address } .r{ child_index } -type.r{ child_index } -name' )
306+ assert resource ['resource_type' ] == f'r{ child_index } -type'
307+ assert resource ['resource_name' ] == f'{ module_address } .r{ child_index } -name'
308+
309+ assert_resource_values (resource ['resource_values' ])
310+
311+ resource_index += 1
312+
236313 @patch ('json.loads' )
237314 def test_load_no_resources (self , yaml_mock ):
238315 # GIVEN a valid Terraform Plan file with no resources
0 commit comments