github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/backend/unparsed_value_test.go (about)

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