github.com/jaredpalmer/terraform@v1.1.0-alpha20210908.0.20210911170307-88705c943a03/internal/backend/unparsed_value_test.go (about)

     1  package backend
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  	"github.com/hashicorp/hcl/v2"
     9  	"github.com/zclconf/go-cty/cty"
    10  
    11  	"github.com/hashicorp/terraform/internal/configs"
    12  	"github.com/hashicorp/terraform/internal/terraform"
    13  	"github.com/hashicorp/terraform/internal/tfdiags"
    14  )
    15  
    16  func TestParseVariableValuesUndeclared(t *testing.T) {
    17  	vv := map[string]UnparsedVariableValue{
    18  		"undeclared0": testUnparsedVariableValue("0"),
    19  		"undeclared1": testUnparsedVariableValue("1"),
    20  		"undeclared2": testUnparsedVariableValue("2"),
    21  		"undeclared3": testUnparsedVariableValue("3"),
    22  		"undeclared4": testUnparsedVariableValue("4"),
    23  		"declared1":   testUnparsedVariableValue("5"),
    24  	}
    25  	decls := map[string]*configs.Variable{
    26  		"declared1": {
    27  			Name:        "declared1",
    28  			Type:        cty.String,
    29  			ParsingMode: configs.VariableParseLiteral,
    30  			DeclRange: hcl.Range{
    31  				Filename: "fake.tf",
    32  				Start:    hcl.Pos{Line: 2, Column: 1, Byte: 0},
    33  				End:      hcl.Pos{Line: 2, Column: 1, Byte: 0},
    34  			},
    35  		},
    36  		"missing1": {
    37  			Name:        "missing1",
    38  			Type:        cty.String,
    39  			ParsingMode: configs.VariableParseLiteral,
    40  			DeclRange: hcl.Range{
    41  				Filename: "fake.tf",
    42  				Start:    hcl.Pos{Line: 3, Column: 1, Byte: 0},
    43  				End:      hcl.Pos{Line: 3, Column: 1, Byte: 0},
    44  			},
    45  		},
    46  		"missing2": {
    47  			Name:        "missing1",
    48  			Type:        cty.String,
    49  			ParsingMode: configs.VariableParseLiteral,
    50  			Default:     cty.StringVal("default for missing2"),
    51  			DeclRange: hcl.Range{
    52  				Filename: "fake.tf",
    53  				Start:    hcl.Pos{Line: 4, Column: 1, Byte: 0},
    54  				End:      hcl.Pos{Line: 4, Column: 1, Byte: 0},
    55  			},
    56  		},
    57  	}
    58  
    59  	gotVals, diags := ParseVariableValues(vv, decls)
    60  	for _, diag := range diags {
    61  		t.Logf("%s: %s", diag.Description().Summary, diag.Description().Detail)
    62  	}
    63  	if got, want := len(diags), 4; got != want {
    64  		t.Fatalf("wrong number of diagnostics %d; want %d", got, want)
    65  	}
    66  
    67  	const undeclSingular = `Value for undeclared variable`
    68  	const undeclPlural = `Values for undeclared variables`
    69  	const missingRequired = `No value for required variable`
    70  
    71  	if got, want := diags[0].Description().Summary, undeclSingular; got != want {
    72  		t.Errorf("wrong summary for diagnostic 0\ngot:  %s\nwant: %s", got, want)
    73  	}
    74  	if got, want := diags[1].Description().Summary, undeclSingular; got != want {
    75  		t.Errorf("wrong summary for diagnostic 1\ngot:  %s\nwant: %s", got, want)
    76  	}
    77  	if got, want := diags[2].Description().Summary, undeclPlural; got != want {
    78  		t.Errorf("wrong summary for diagnostic 2\ngot:  %s\nwant: %s", got, want)
    79  	}
    80  	if got, want := diags[2].Description().Detail, "3 other variable(s)"; !strings.Contains(got, want) {
    81  		t.Errorf("wrong detail for diagnostic 2\ngot:  %s\nmust contain: %s", got, want)
    82  	}
    83  	if got, want := diags[3].Description().Summary, missingRequired; got != want {
    84  		t.Errorf("wrong summary for diagnostic 3\ngot:  %s\nwant: %s", got, want)
    85  	}
    86  
    87  	wantVals := terraform.InputValues{
    88  		"declared1": {
    89  			Value:      cty.StringVal("5"),
    90  			SourceType: terraform.ValueFromNamedFile,
    91  			SourceRange: tfdiags.SourceRange{
    92  				Filename: "fake.tfvars",
    93  				Start:    tfdiags.SourcePos{Line: 1, Column: 1, Byte: 0},
    94  				End:      tfdiags.SourcePos{Line: 1, Column: 1, Byte: 0},
    95  			},
    96  		},
    97  		"missing1": {
    98  			Value:      cty.DynamicVal,
    99  			SourceType: terraform.ValueFromConfig,
   100  			SourceRange: tfdiags.SourceRange{
   101  				Filename: "fake.tf",
   102  				Start:    tfdiags.SourcePos{Line: 3, Column: 1, Byte: 0},
   103  				End:      tfdiags.SourcePos{Line: 3, Column: 1, Byte: 0},
   104  			},
   105  		},
   106  		"missing2": {
   107  			Value:      cty.StringVal("default for missing2"),
   108  			SourceType: terraform.ValueFromConfig,
   109  			SourceRange: tfdiags.SourceRange{
   110  				Filename: "fake.tf",
   111  				Start:    tfdiags.SourcePos{Line: 4, Column: 1, Byte: 0},
   112  				End:      tfdiags.SourcePos{Line: 4, Column: 1, Byte: 0},
   113  			},
   114  		},
   115  	}
   116  	if diff := cmp.Diff(wantVals, gotVals, cmp.Comparer(cty.Value.RawEquals)); diff != "" {
   117  		t.Errorf("wrong result\n%s", diff)
   118  	}
   119  }
   120  
   121  type testUnparsedVariableValue string
   122  
   123  func (v testUnparsedVariableValue) ParseVariableValue(mode configs.VariableParsingMode) (*terraform.InputValue, tfdiags.Diagnostics) {
   124  	return &terraform.InputValue{
   125  		Value:      cty.StringVal(string(v)),
   126  		SourceType: terraform.ValueFromNamedFile,
   127  		SourceRange: tfdiags.SourceRange{
   128  			Filename: "fake.tfvars",
   129  			Start:    tfdiags.SourcePos{Line: 1, Column: 1, Byte: 0},
   130  			End:      tfdiags.SourcePos{Line: 1, Column: 1, Byte: 0},
   131  		},
   132  	}, nil
   133  }