github.com/mponton/terratest@v0.44.0/modules/terraform/plan.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  
     8  	"github.com/mponton/terratest/modules/logger"
     9  	"github.com/mponton/terratest/modules/testing"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  // InitAndPlan runs terraform init and plan with the given options and returns stdout/stderr from the plan command.
    14  // This will fail the test if there is an error in the command.
    15  func InitAndPlan(t testing.TestingT, options *Options) string {
    16  	out, err := InitAndPlanE(t, options)
    17  	require.NoError(t, err)
    18  	return out
    19  }
    20  
    21  // InitAndPlanE runs terraform init and plan with the given options and returns stdout/stderr from the plan command.
    22  func InitAndPlanE(t testing.TestingT, options *Options) (string, error) {
    23  	if _, err := InitE(t, options); err != nil {
    24  		return "", err
    25  	}
    26  
    27  	return PlanE(t, options)
    28  }
    29  
    30  // Plan runs terraform plan with the given options and returns stdout/stderr.
    31  // This will fail the test if there is an error in the command.
    32  func Plan(t testing.TestingT, options *Options) string {
    33  	out, err := PlanE(t, options)
    34  	require.NoError(t, err)
    35  	return out
    36  }
    37  
    38  // PlanE runs terraform plan with the given options and returns stdout/stderr.
    39  func PlanE(t testing.TestingT, options *Options) (string, error) {
    40  	return RunTerraformCommandE(t, options, FormatArgs(options, "plan", "-input=false", "-lock=false")...)
    41  }
    42  
    43  // InitAndPlanAndShow runs terraform init, then terraform plan, and then terraform show with the given options, and
    44  // returns the json output of the plan file. This will fail the test if there is an error in the command.
    45  func InitAndPlanAndShow(t testing.TestingT, options *Options) string {
    46  	jsonOut, err := InitAndPlanAndShowE(t, options)
    47  	require.NoError(t, err)
    48  	return jsonOut
    49  }
    50  
    51  // InitAndPlanAndShowE runs terraform init, then terraform plan, and then terraform show with the given options, and
    52  // returns the json output of the plan file.
    53  func InitAndPlanAndShowE(t testing.TestingT, options *Options) (string, error) {
    54  	if options.PlanFilePath == "" {
    55  		return "", PlanFilePathRequired
    56  	}
    57  
    58  	_, err := InitAndPlanE(t, options)
    59  	if err != nil {
    60  		return "", err
    61  	}
    62  	return ShowE(t, options)
    63  }
    64  
    65  // InitAndPlanAndShowWithStructNoLog runs InitAndPlanAndShowWithStruct without logging and also by allocating a
    66  // temporary plan file destination that is discarded before returning the struct.
    67  func InitAndPlanAndShowWithStructNoLogTempPlanFile(t testing.TestingT, options *Options) *PlanStruct {
    68  	oldLogger := options.Logger
    69  	options.Logger = logger.Discard
    70  	defer func() { options.Logger = oldLogger }()
    71  
    72  	tmpFile, err := ioutil.TempFile("", "terratest-plan-file-")
    73  	require.NoError(t, err)
    74  	require.NoError(t, tmpFile.Close())
    75  	defer require.NoError(t, os.Remove(tmpFile.Name()))
    76  
    77  	options.PlanFilePath = tmpFile.Name()
    78  	return InitAndPlanAndShowWithStruct(t, options)
    79  }
    80  
    81  // InitAndPlanAndShowWithStruct runs terraform init, then terraform plan, and then terraform show with the given
    82  // options, and parses the json result into a go struct. This will fail the test if there is an error in the command.
    83  func InitAndPlanAndShowWithStruct(t testing.TestingT, options *Options) *PlanStruct {
    84  	plan, err := InitAndPlanAndShowWithStructE(t, options)
    85  	require.NoError(t, err)
    86  	return plan
    87  }
    88  
    89  // InitAndPlanAndShowWithStructE runs terraform init, then terraform plan, and then terraform show with the given options, and
    90  // parses the json result into a go struct.
    91  func InitAndPlanAndShowWithStructE(t testing.TestingT, options *Options) (*PlanStruct, error) {
    92  	jsonOut, err := InitAndPlanAndShowE(t, options)
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  	return parsePlanJson(jsonOut)
    97  }
    98  
    99  // InitAndPlanWithExitCode runs terraform init and plan with the given options and returns exitcode for the plan command.
   100  // This will fail the test if there is an error in the command.
   101  func InitAndPlanWithExitCode(t testing.TestingT, options *Options) int {
   102  	exitCode, err := InitAndPlanWithExitCodeE(t, options)
   103  	require.NoError(t, err)
   104  	return exitCode
   105  }
   106  
   107  // InitAndPlanWithExitCodeE runs terraform init and plan with the given options and returns exitcode for the plan command.
   108  func InitAndPlanWithExitCodeE(t testing.TestingT, options *Options) (int, error) {
   109  	if _, err := InitE(t, options); err != nil {
   110  		return DefaultErrorExitCode, err
   111  	}
   112  
   113  	return PlanExitCodeE(t, options)
   114  }
   115  
   116  // PlanExitCode runs terraform plan with the given options and returns the detailed exitcode.
   117  // This will fail the test if there is an error in the command.
   118  func PlanExitCode(t testing.TestingT, options *Options) int {
   119  	exitCode, err := PlanExitCodeE(t, options)
   120  	require.NoError(t, err)
   121  	return exitCode
   122  }
   123  
   124  // PlanExitCodeE runs terraform plan with the given options and returns the detailed exitcode.
   125  func PlanExitCodeE(t testing.TestingT, options *Options) (int, error) {
   126  	return GetExitCodeForTerraformCommandE(t, options, FormatArgs(options, "plan", "-input=false", "-detailed-exitcode")...)
   127  }
   128  
   129  // TgPlanAllExitCode runs terragrunt plan-all with the given options and returns the detailed exitcode.
   130  // This will fail the test if there is an error in the command.
   131  func TgPlanAllExitCode(t testing.TestingT, options *Options) int {
   132  	exitCode, err := TgPlanAllExitCodeE(t, options)
   133  	require.NoError(t, err)
   134  	return exitCode
   135  }
   136  
   137  // TgPlanAllExitCodeE runs terragrunt plan-all with the given options and returns the detailed exitcode.
   138  func TgPlanAllExitCodeE(t testing.TestingT, options *Options) (int, error) {
   139  	if options.TerraformBinary != "terragrunt" {
   140  		return 1, fmt.Errorf("terragrunt must be set as TerraformBinary to use this method")
   141  	}
   142  
   143  	return GetExitCodeForTerraformCommandE(t, options, FormatArgs(options, "run-all", "plan", "--input=false",
   144  		"--lock=true", "--detailed-exitcode")...)
   145  }
   146  
   147  // Custom errors
   148  
   149  var (
   150  	PlanFilePathRequired = fmt.Errorf("You must set PlanFilePath on options struct to use this function.")
   151  )