Skip to content
This repository was archived by the owner on Aug 26, 2025. It is now read-only.

Commit 076e5ae

Browse files
committed
bump version
1 parent 21930ba commit 076e5ae

File tree

1 file changed

+66
-99
lines changed

1 file changed

+66
-99
lines changed

tftest.py

Lines changed: 66 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import weakref
3535
import re
3636

37-
__version__ = '1.5.4'
37+
__version__ = '1.5.5'
3838

3939
_LOGGER = logging.getLogger('tftest')
4040

@@ -52,11 +52,13 @@ class TerraformTestError(Exception):
5252

5353
def parse_args(init_vars=None, tf_vars=None, targets=None, **kw):
5454
"""Convert method arguments for use in Terraform commands.
55+
5556
Args:
5657
init_vars: dict of key/values converted to -backend-config='k=v' form, or
5758
string argument converted to -backend-config=arg
5859
tf_vars: dict of key/values converted to -var k=v form.
5960
**kw: converted to the appropriate Terraform flag.
61+
6062
Returns:
6163
A list of command arguments for use with subprocess.
6264
"""
@@ -140,6 +142,7 @@ def parse_args(init_vars=None, tf_vars=None, targets=None, **kw):
140142

141143
return cmd_args
142144

145+
143146
class TerraformJSONBase(object):
144147
"Base class for JSON wrappers."
145148

@@ -302,7 +305,7 @@ def _cleanup(cls, tfdir, filenames, binary, deep=True):
302305
os.unlink(path)
303306
if not deep:
304307
return
305-
308+
306309
if binary == 'terraform':
307310
path = os.path.join(tfdir, '.terraform')
308311
if os.path.isdir(path):
@@ -315,51 +318,15 @@ def _cleanup(cls, tfdir, filenames, binary, deep=True):
315318
if os.path.isdir(path):
316319
shutil.rmtree(path)
317320

318-
def tg_global_doc(tg_func):
319-
doc = """
320-
all: Runs Terragrunt command on all subfolders if True
321-
tg_config: Path to the Terragrunt config file. Default is terragrunt.hcl.
322-
tg_tfpath: Path to the Terraform binary. Default is terraform (on PATH).
323-
tg_no_auto_init: Don't automatically run 'terraform init' during other terragrunt commands.
324-
You must run 'terragrunt init' manually.
325-
tg_no_auto_retry: Don't automatically re-run command in case of transient errors.
326-
tg_non_interactive: Assume "yes" for all prompts.
327-
tg_working_dir: The path to the Terraform templates. Default is current directory.
328-
tg_download_dir: The path where to download Terraform code. Default is .terragrunt_cache in
329-
the working directory.
330-
tg_source: Download Terraform configurations from the specified source into a temporary folder,
331-
and run Terraform in that temporary folder.
332-
tg_source_update: Delete the contents of the temporary folder to clear out any old, cached
333-
source code before downloading new source code into it.
334-
tg_iam_role: Assume the specified IAM role before executing Terraform. Can also be set via the
335-
TERRAGRUNT_IAM_ROLE environment variable.
336-
tg_ignore_dependency_errors: *-all commands continue processing components even if a dependency
337-
fails.
338-
tg_ignore_dependency_order: *-all commands will be run disregarding the dependencies
339-
tg_ignore_external_dependencies: *-all commands will not attempt to include external
340-
dependencies
341-
tg_include_external_dependencies: *-all commands will include external dependencies
342-
tg_parallelism: *-all commands parallelism set to at most N modules
343-
tg_exclude_dir: Unix-style glob of directories to exclude when running *-all commands
344-
tg_include_dir: Unix-style glob of directories to include when running *-all commands
345-
tg_check: Enable check mode in the hclfmt command.
346-
tg_hclfmt_file: The path to a single terragrunt.hcl file that the hclfmt command should run on.
347-
tg_override_attr: A key=value attribute to override in a provider block as part of the
348-
aws-provider-patch command. May be specified multiple times.
349-
tg_debug: Write terragrunt_debug.tfvars to working folder to help root-cause issues.
350-
"""
351-
tg_func.__doc__ += doc
352-
return tg_func
353-
354321
def _plan(self, all=False, output=False, **kw):
355322
"""
356-
Run Terragrunt or Terraform plan command and optionally returning the plan output.
323+
Run Terragrunt or Terraform plan and optionally return the plan output.
357324
358325
Args:
359326
all: Runs Terragrunt command on all subfolders if True
360327
output: Returns the output of the plan command
361328
"""
362-
329+
363330
cmd_args = parse_args(**kw)
364331

365332
if 'out' not in kw:
@@ -380,13 +347,13 @@ def _plan(self, all=False, output=False, **kw):
380347

381348
def _abspath(self, path):
382349
"""Make relative path absolute from base dir."""
383-
350+
384351
# print(inspect.getdoc(self.setup))
385352
return path if path.startswith('/') else os.path.join(self._basedir, path)
386353

387354
def setup(self, all=False, extra_files=None, plugin_dir=None, init_vars=None,
388-
backend=True, cleanup_on_exit=True, tg_non_interactive=False,
389-
tg_source_update=False, tg_config=None, tg_working_dir=None,
355+
backend=True, cleanup_on_exit=True, tg_non_interactive=False,
356+
tg_source_update=False, tg_config=None, tg_working_dir=None,
390357
**kw):
391358
"""Setup method to use in test fixtures.
392359
@@ -423,29 +390,28 @@ def setup(self, all=False, extra_files=None, plugin_dir=None, init_vars=None,
423390
_LOGGER.warning('no such file {}'.format(link_src))
424391
self._finalizer = weakref.finalize(
425392
self, self._cleanup, self.tfdir, filenames, self.binary, deep=cleanup_on_exit)
426-
393+
427394
if self.binary == 'terragrunt':
428395
return self.tg_init(all=all, init_vars=init_vars,
429396
backend=backend, plugin_dir=plugin_dir,
430-
tg_non_interactive=tg_non_interactive, tg_source_update=tg_source_update,
397+
tg_non_interactive=tg_non_interactive, tg_source_update=tg_source_update,
431398
tg_config=tg_config, tg_working_dir=tg_working_dir, **kw)
432399

433400
return self.tf_init(plugin_dir=plugin_dir, init_vars=init_vars, backend=backend)
434-
401+
435402
def tf_init(self, input=False, no_color=True, plugin_dir=None,
436-
init_vars=None, backend=True):
403+
init_vars=None, backend=True):
437404
"""Run Terraform or Terragrunt init command."""
438-
cmd_args = parse_args(input=input, no_color=no_color,
405+
cmd_args = parse_args(input=input, no_color=no_color,
439406
backend=backend, plugin_dir=plugin_dir,
440407
init_vars=init_vars)
441408
return self.execute_command('init', *cmd_args).out
442409

443-
@tg_global_doc
444410
def tg_init(self, all=False, input=False, no_color=True, plugin_dir=None,
445-
init_vars=None, backend=True, tg_non_interactive=True,
411+
init_vars=None, backend=True, tg_non_interactive=True,
446412
tg_source_update=False, tg_config=None, tg_working_dir=None, **kw):
447413
"""Run Terragrunt init command."""
448-
cmd_args = parse_args(input=input, no_color=no_color,
414+
cmd_args = parse_args(input=input, no_color=no_color,
449415
backend=backend, plugin_dir=plugin_dir,
450416
init_vars=init_vars, tg_non_interactive=tg_non_interactive,
451417
tg_source_update=tg_source_update, tg_config=tg_config,
@@ -455,48 +421,47 @@ def tg_init(self, all=False, input=False, no_color=True, plugin_dir=None,
455421
else:
456422
return self.execute_command('init', *cmd_args).out
457423

458-
def tf_validate(self, no_color=True, json=None):
424+
def validate(self, no_color=True, json=None):
459425
"""Run Terraform or Terragrunt validate command."""
460426
cmd_args = parse_args(no_color=True, json=None)
461427
return self.execute_command('validate', *cmd_args).out
462428

463-
@tg_global_doc
464-
def tg_validate(self, no_color=True, json=None, tg_non_interactive=True,
429+
def tg_validate(self, no_color=True, json=None, tg_non_interactive=True,
465430
tg_source_update=False, tg_config=None, tg_working_dir=None, **kw):
466431
"""Run Terragrunt validate command."""
467-
cmd_args = parse_args(no_color=no_color, json=json, tg_non_interactive=tg_non_interactive,
468-
tg_source_update=tg_source_update, tg_config=tg_config, tg_working_dir=tg_working_dir, **kw)
432+
cmd_args = parse_args(no_color=no_color, json=json, tg_non_interactive=tg_non_interactive,
433+
tg_source_update=tg_source_update, tg_config=tg_config, tg_working_dir=tg_working_dir, **kw)
469434
if all:
470435
return self.execute_command('run-all', 'validate', *cmd_args).out
471436
else:
472437
return self.execute_command('validate', *cmd_args).out
473438

474-
def tf_plan(self, input=False, no_color=True, refresh=True, tf_vars=None, targets=None, output=False, tf_var_file=None):
439+
def plan(self, input=False, no_color=True, refresh=True, tf_vars=None,
440+
targets=None, output=False, tf_var_file=None):
475441
"""
476442
Run Terraform plan command, optionally returning parsed plan output.
477443
478444
Args:
479445
input: Ask for input for variables if not directly set.
480446
no_color: If specified, output won't contain any color.
481447
refresh: Update state prior to checking for differences.
482-
tf_vars: Dict of variables in the Terraform configuration.
483-
targets: List of resources to target. Operation will be limited to this resource
448+
tf_vars: Dict of variables in the Terraform configuration.
449+
targets: List of resources to target. Operation will be limited to this resource
484450
and its dependencies
485-
output: Determines if output will be returned.
451+
output: Determines if output will be returned.
486452
tf_var_file: Path to terraform variable configuration file relative to `self.tfdir`.
487-
"""
488-
result = self._plan('plan', input=input, no_color=no_color, refresh=refresh,
453+
"""
454+
result = self._plan('plan', input=input, no_color=no_color, refresh=refresh,
489455
tf_vars=tf_vars, targets=targets, output=output, tf_var_file=tf_var_file)
490456

491457
try:
492458
return TerraformPlanOutput(json.loads(result.out))
493459
except json.JSONDecodeError as e:
494460
raise TerraformTestError('Error decoding plan output: {}'.format(e))
495-
496-
@tg_global_doc
461+
497462
def tg_plan(self, all=False, input=False, no_color=True, refresh=True,
498-
tf_vars=None, targets=None, output=False, tf_var_file=None,
499-
tg_non_interactive=True, tg_source_update=False, tg_config=None,
463+
tf_vars=None, targets=None, output=False, tf_var_file=None,
464+
tg_non_interactive=True, tg_source_update=False, tg_config=None,
500465
tg_working_dir=None, **kw):
501466
"""
502467
Run Terragrunt plan command, optionally returning parsed plan output.
@@ -505,29 +470,29 @@ def tg_plan(self, all=False, input=False, no_color=True, refresh=True,
505470
input: Ask for input for variables if not directly set.
506471
no_color: If specified, output won't contain any color.
507472
refresh: Update state prior to checking for differences.
508-
tf_vars: Dict of variables in the Terraform configuration.
509-
targets: List of resources to target. Operation will be limited to this resource
473+
tf_vars: Dict of variables in the Terraform configuration.
474+
targets: List of resources to target. Operation will be limited to this resource
510475
and its dependencies
511-
output: Determines if output will be returned.
476+
output: Determines if output will be returned.
512477
tf_var_file: Path to terraform variable configuration file relative to `self.tfdir`.
513478
"""
514479
result = self._plan(all=all, output=output,
515-
input=input, no_color=no_color,
516-
refresh=refresh, tf_vars=tf_vars,
480+
input=input, no_color=no_color,
481+
refresh=refresh, tf_vars=tf_vars,
517482
targets=targets, tf_var_file=tf_var_file,
518-
tg_non_interactive=tg_non_interactive, tg_source_update=tg_source_update,
483+
tg_non_interactive=tg_non_interactive, tg_source_update=tg_source_update,
519484
tg_config=tg_config, tg_working_dir=tg_working_dir, **kw)
520485
if not output:
521486
return result
522487

523488
if all:
524-
#TODO: Find better way to parse result other than regex
489+
# TODO: Find better way to parse result other than regex
525490
plans = re.split('\n(?=\\{"format_version"\\:)', result.out)
526491
plan_output = []
527492
for plan in plans:
528493
try:
529-
out = TerraformPlanOutput(json.loads(plan))
530-
#TODO: Find a way to distinguish each plan from each other (couldn't find an attr in `out` to use as a key to pair with `out` value)
494+
out = TerraformPlanOutput(json.loads(plan))
495+
# TODO: Find a way to distinguish each plan from each other (couldn't find an attr in `out` to use as a key to pair with `out` value)
531496
# for now returns list of tftest.TerraformPlanModule objects
532497
plan_output.append(out)
533498
except json.JSONDecodeError as e:
@@ -539,44 +504,44 @@ def tg_plan(self, all=False, input=False, no_color=True, refresh=True,
539504
except json.JSONDecodeError as e:
540505
raise TerraformTestError('Error decoding plan output: {}'.format(e))
541506

542-
def tf_apply(self, input=False, no_color=True, auto_approve=True, tf_vars=None, targets=None, tf_var_file=None):
507+
def apply(self, input=False, no_color=True, auto_approve=True,
508+
tf_vars=None, targets=None, tf_var_file=None):
543509
"""
544510
Run Terraform apply command.
545-
511+
546512
Args:
547513
input: Ask for input for variables if not directly set.
548514
no_color: If specified, output won't contain any color.
549515
auto_approve: Skip interactive approval of plan before applying.
550-
tf_vars: Dict of variables in the Terraform configuration.
551-
targets: List of resources to target. Operation will be limited to this resource
516+
tf_vars: Dict of variables in the Terraform configuration.
517+
targets: List of resources to target. Operation will be limited to this resource
552518
and its dependencies
553519
tf_var_file: Path to terraform variable configuration file relative to `self.tfdir`.
554520
"""
555-
cmd_args = parse_args(input=input, no_color=no_color,
556-
auto_approve=auto_approve, tf_vars=tf_vars,
521+
cmd_args = parse_args(input=input, no_color=no_color,
522+
auto_approve=auto_approve, tf_vars=tf_vars,
557523
targets=targets, tf_var_file=tf_var_file)
558524
return self.execute_command('apply', *cmd_args).out
559525

560-
@tg_global_doc
561-
def tg_apply(self, all=False, input=False, no_color=True, auto_approve=True, tf_vars=None,
562-
targets=None, tf_var_file=None, tg_non_interactive=True,
526+
def tg_apply(self, all=False, input=False, no_color=True, auto_approve=True, tf_vars=None,
527+
targets=None, tf_var_file=None, tg_non_interactive=True,
563528
tg_source_update=False, tg_config=None, tg_working_dir=None, **kw):
564529
"""
565530
Run Terragrunt apply command.
566-
531+
567532
Args:
568533
input: Ask for input for variables if not directly set.
569534
no_color: If specified, output won't contain any color.
570535
auto_approve: Skip interactive approval of plan before applying.
571-
tf_vars: Dict of variables in the Terraform configuration.
572-
targets: List of resources to target. Operation will be limited to this resource
536+
tf_vars: Dict of variables in the Terraform configuration.
537+
targets: List of resources to target. Operation will be limited to this resource
573538
and its dependencies
574539
tf_var_file: Path to terraform variable configuration file relative to `self.tfdir`.
575540
"""
576-
cmd_args = parse_args(input=input, no_color=no_color,
577-
auto_approve=auto_approve, tf_vars=tf_vars,
541+
cmd_args = parse_args(input=input, no_color=no_color,
542+
auto_approve=auto_approve, tf_vars=tf_vars,
578543
targets=targets, tf_var_file=tf_var_file,
579-
tg_non_interactive=tg_non_interactive, tg_source_update=tg_source_update,
544+
tg_non_interactive=tg_non_interactive, tg_source_update=tg_source_update,
580545
tg_config=tg_config, tg_working_dir=tg_working_dir, **kw)
581546

582547
if all:
@@ -588,33 +553,35 @@ def _output(self, all=False, name=None, **kw):
588553
cmd_args = parse_args(**kw)
589554
if name:
590555
cmd_args.append(name)
591-
556+
592557
if all:
593558
output = self.execute_command('run-all', 'output', *cmd_args).out
594559
else:
595560
output = self.execute_command('output', *cmd_args).out
596561
return output
597562

598-
def tf_output(self, name=None, no_color=True, json_format=True):
563+
def output(self, name=None, no_color=True, json_format=True):
599564
"""Run Terraform output command."""
600-
output = self._output(name=name, no_color=no_color, json_format=json_format)
565+
output = self._output(name=name, no_color=no_color,
566+
json_format=json_format)
601567
_LOGGER.debug('output %s', output)
602568
if json_format:
603569
try:
604570
output = TerraformValueDict(json.loads(output))
605571
except json.JSONDecodeError as e:
606572
_LOGGER.warning('error decoding output: {}'.format(e))
607573
return output
608-
609-
def tg_output(self, all=False, name=None, no_color=True, json_format=True,
610-
tg_non_interactive=True, tg_source_update=False, tg_config=None,
574+
575+
def tg_output(self, all=False, name=None, no_color=True, json_format=True,
576+
tg_non_interactive=True, tg_source_update=False, tg_config=None,
611577
tg_working_dir=None, **kw):
612578
"""Run Terragrunt output command."""
613579
output = self._output(all=all, name=name, no_color=no_color, json_format=json_format,
614-
tg_non_interactive=tg_non_interactive, tg_source_update=tg_source_update,
580+
tg_non_interactive=tg_non_interactive, tg_source_update=tg_source_update,
615581
tg_config=tg_config, tg_working_dir=tg_working_dir, **kw)
616-
617-
# TODO: Figure out how to parse terragrunt run-all output command to return dict of {directory: output}
582+
583+
# TODO: Figure out how to parse terragrunt run-all output command to return
584+
# dict of {directory: output}
618585
_LOGGER.debug('output %s', output)
619586
try:
620587
output = TerraformValueDict(json.loads(output))

0 commit comments

Comments
 (0)