16
16
)
17
17
from terrareg .module_extractor import GitModuleExtractor , ModuleExtractor
18
18
import terrareg .models
19
+ import terrareg .config
19
20
20
21
21
22
class TestGitModuleExtractor (TerraregUnitTest ):
@@ -253,36 +254,46 @@ def test_description_extraction_when_disabled(self, mock_models):
253
254
with unittest .mock .patch ('terrareg.config.Config.AUTOGENERATE_MODULE_PROVIDER_DESCRIPTION' , False ):
254
255
assert module_extractor ._extract_description (test_text ) == None
255
256
256
- def test_run_tf_init (self ):
257
+ @pytest .mark .parametrize ('config_product, expected_binary' , [
258
+ (terrareg .config .Product .TERRAFORM , 'terraform' ),
259
+ (terrareg .config .Product .OPENTOFU , 'tofu' ),
260
+ ])
261
+ def test_run_tf_init (self , config_product , expected_binary ):
257
262
"""Test running terraform init"""
258
263
with unittest .mock .patch ('terrareg.module_extractor.subprocess.check_call' , unittest .mock .MagicMock ()) as check_output_mock , \
259
- unittest .mock .patch ("terrareg.module_extractor.ModuleExtractor._create_terraform_rc_file" , unittest .mock .MagicMock ()) as mock_create_terraform_rc_file :
264
+ unittest .mock .patch ("terrareg.module_extractor.ModuleExtractor._create_terraform_rc_file" , unittest .mock .MagicMock ()) as mock_create_terraform_rc_file , \
265
+ unittest .mock .patch ('terrareg.config.Config.PRODUCT' , config_product ):
260
266
261
267
module_extractor = GitModuleExtractor (module_version = None )
262
268
263
269
assert module_extractor ._run_tf_init (module_path = '/tmp/mock-patch/to/module' ) is True
264
270
265
271
check_output_mock .assert_called_once_with (
266
- [os .path .join (os .getcwd (), 'bin' , 'terraform' ), 'init' ],
272
+ [os .path .join (os .getcwd (), 'bin' , expected_binary ), 'init' ],
267
273
cwd = '/tmp/mock-patch/to/module'
268
274
)
269
275
mock_create_terraform_rc_file .assert_called_once_with ()
270
276
271
- def test_run_tf_init_error (self ):
277
+ @pytest .mark .parametrize ('config_product, expected_binary' , [
278
+ (terrareg .config .Product .TERRAFORM , 'terraform' ),
279
+ (terrareg .config .Product .OPENTOFU , 'tofu' ),
280
+ ])
281
+ def test_run_tf_init_error (self , config_product , expected_binary ):
272
282
"""Test running terraform init with error returned"""
273
283
274
284
def raise_exception (* args , ** kwargs ):
275
285
raise subprocess .CalledProcessError (cmd = "test" , returncode = 2 )
276
286
277
287
with unittest .mock .patch ('terrareg.module_extractor.subprocess.check_call' , unittest .mock .MagicMock (side_effect = raise_exception )) as mock_check_call , \
278
- unittest .mock .patch ("terrareg.module_extractor.ModuleExtractor._create_terraform_rc_file" , unittest .mock .MagicMock ()) as mock_create_terraform_rc_file :
288
+ unittest .mock .patch ("terrareg.module_extractor.ModuleExtractor._create_terraform_rc_file" , unittest .mock .MagicMock ()) as mock_create_terraform_rc_file , \
289
+ unittest .mock .patch ('terrareg.config.Config.PRODUCT' , config_product ):
279
290
280
291
module_extractor = GitModuleExtractor (module_version = None )
281
292
282
293
assert module_extractor ._run_tf_init (module_path = '/tmp/mock-patch/to/module' ) is False
283
294
284
295
mock_check_call .assert_called_once_with (
285
- [os .path .join (os .getcwd (), 'bin' , 'terraform' ), 'init' ],
296
+ [os .path .join (os .getcwd (), 'bin' , expected_binary ), 'init' ],
286
297
cwd = '/tmp/mock-patch/to/module'
287
298
)
288
299
mock_create_terraform_rc_file .assert_called_once_with ()
@@ -391,7 +402,11 @@ def test_override_tf_backend(self, file_contents, expected_backend_file):
391
402
shutil .rmtree (temp_dir )
392
403
393
404
394
- def test_switch_terraform_versions (self ):
405
+ @pytest .mark .parametrize ('config_product' , [
406
+ terrareg .config .Product .TERRAFORM ,
407
+ terrareg .config .Product .OPENTOFU ,
408
+ ])
409
+ def test_switch_terraform_versions (self , config_product ):
395
410
"""Test switching terraform versions."""
396
411
module_extractor = GitModuleExtractor (module_version = None )
397
412
mock_lock = unittest .mock .MagicMock ()
@@ -400,16 +415,19 @@ def test_switch_terraform_versions(self):
400
415
with unittest .mock .patch ('terrareg.module_extractor.ModuleExtractor.TERRAFORM_LOCK' , mock_lock ), \
401
416
unittest .mock .patch ('terrareg.module_extractor.subprocess.check_output' , unittest .mock .MagicMock ()) as check_output_mock , \
402
417
unittest .mock .patch ('terrareg.config.Config.DEFAULT_TERRAFORM_VERSION' , 'unittest-tf-version' ), \
403
- unittest .mock .patch ('terrareg.config.Config.TERRAFORM_ARCHIVE_MIRROR' , 'https://localhost-archive/mirror/terraform' ):
418
+ unittest .mock .patch ('terrareg.config.Config.TERRAFORM_ARCHIVE_MIRROR' , 'https://localhost-archive/mirror/terraform' ), \
419
+ unittest .mock .patch ('terrareg.config.Config.PRODUCT' , config_product ):
404
420
405
421
with module_extractor ._switch_terraform_versions (module_path = '/tmp/mock-patch/to/module' ):
406
422
pass
407
423
408
424
mock_lock .acquire .assert_called_once_with (blocking = True , timeout = 60 )
425
+ expected_bin = f"{ os .getcwd ()} /bin/" + ('terraform' if config_product is terrareg .config .Product .TERRAFORM else 'tofu' )
409
426
expected_env = os .environ .copy ()
410
- expected_env ['TF_VERSION' ] = "unittest-tf-version"
427
+ expected_env ['TF_DEFAULT_VERSION' ] = "unittest-tf-version"
428
+ expected_env ['TF_PRODUCT' ] = 'terraform' if config_product is terrareg .config .Product .TERRAFORM else 'opentofu'
411
429
check_output_mock .assert_called_once_with (
412
- ["tfswitch" , "--mirror" , "https://localhost-archive/mirror/terraform" , "--bin" , f" { os . getcwd () } /bin /terraform" ],
430
+ ["tfswitch" , "--bin" , expected_bin , "-- mirror" , "https://localhost-archive/mirror/terraform" ],
413
431
env = expected_env ,
414
432
cwd = "/tmp/mock-patch/to/module"
415
433
)
@@ -446,17 +464,22 @@ def test_switch_terraform_versions_with_lock(self):
446
464
mock_lock .acquire .assert_called_once_with (blocking = True , timeout = 60 )
447
465
check_output_mock .assert_not_called ()
448
466
449
- def test_get_graph_data (self ):
467
+ @pytest .mark .parametrize ('config_product, expected_binary' , [
468
+ (terrareg .config .Product .TERRAFORM , 'terraform' ),
469
+ (terrareg .config .Product .OPENTOFU , 'tofu' ),
470
+ ])
471
+ def test_get_graph_data (self , config_product , expected_binary ):
450
472
"""Test call to terraform graph to generate graph output"""
451
473
module_extractor = GitModuleExtractor (module_version = None )
452
474
453
475
with unittest .mock .patch ('terrareg.module_extractor.subprocess.check_output' ,
454
- unittest .mock .MagicMock (return_value = "Output graph data" .encode ("utf-8" ))) as mock_check_output :
476
+ unittest .mock .MagicMock (return_value = "Output graph data" .encode ("utf-8" ))) as mock_check_output , \
477
+ unittest .mock .patch ('terrareg.config.Config.PRODUCT' , config_product ):
455
478
456
479
module_extractor ._get_graph_data (module_path = '/tmp/mock-patch/to/module' )
457
480
458
481
mock_check_output .assert_called_once_with (
459
- [os .getcwd () + ' /bin/terraform ' , 'graph' ],
482
+ [f' { os .getcwd ()} /bin/{ expected_binary } ' , 'graph' ],
460
483
cwd = '/tmp/mock-patch/to/module'
461
484
)
462
485
0 commit comments