github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/cloud/cloud_variables_test.go (about) 1 package cloud 2 3 import ( 4 "testing" 5 6 "github.com/google/go-cmp/cmp" 7 "github.com/hashicorp/hcl/v2" 8 "github.com/hashicorp/terraform/internal/backend" 9 "github.com/hashicorp/terraform/internal/configs" 10 "github.com/hashicorp/terraform/internal/terraform" 11 "github.com/hashicorp/terraform/internal/tfdiags" 12 "github.com/zclconf/go-cty/cty" 13 ) 14 15 func TestParseCloudRunVariables(t *testing.T) { 16 t.Run("populates variables from allowed sources", func(t *testing.T) { 17 vv := map[string]backend.UnparsedVariableValue{ 18 "undeclared": testUnparsedVariableValue{source: terraform.ValueFromCLIArg, value: cty.StringVal("0")}, 19 "declaredFromConfig": testUnparsedVariableValue{source: terraform.ValueFromConfig, value: cty.StringVal("1")}, 20 "declaredFromNamedFileMapString": testUnparsedVariableValue{source: terraform.ValueFromNamedFile, value: cty.MapVal(map[string]cty.Value{"foo": cty.StringVal("bar")})}, 21 "declaredFromNamedFileBool": testUnparsedVariableValue{source: terraform.ValueFromNamedFile, value: cty.BoolVal(true)}, 22 "declaredFromNamedFileNumber": testUnparsedVariableValue{source: terraform.ValueFromNamedFile, value: cty.NumberIntVal(2)}, 23 "declaredFromNamedFileListString": testUnparsedVariableValue{source: terraform.ValueFromNamedFile, value: cty.ListVal([]cty.Value{cty.StringVal("2a"), cty.StringVal("2b")})}, 24 "declaredFromNamedFileNull": testUnparsedVariableValue{source: terraform.ValueFromNamedFile, value: cty.NullVal(cty.String)}, 25 "declaredFromNamedMapComplex": testUnparsedVariableValue{source: terraform.ValueFromNamedFile, value: cty.MapVal(map[string]cty.Value{"foo": cty.ObjectVal(map[string]cty.Value{"qux": cty.ListVal([]cty.Value{cty.BoolVal(true), cty.BoolVal(false)})})})}, 26 "declaredFromCLIArg": testUnparsedVariableValue{source: terraform.ValueFromCLIArg, value: cty.StringVal("3")}, 27 "declaredFromEnvVar": testUnparsedVariableValue{source: terraform.ValueFromEnvVar, value: cty.StringVal("4")}, 28 } 29 30 decls := map[string]*configs.Variable{ 31 "declaredFromConfig": { 32 Name: "declaredFromConfig", 33 Type: cty.String, 34 ConstraintType: cty.String, 35 ParsingMode: configs.VariableParseLiteral, 36 DeclRange: hcl.Range{ 37 Filename: "fake.tf", 38 Start: hcl.Pos{Line: 2, Column: 1, Byte: 0}, 39 End: hcl.Pos{Line: 2, Column: 1, Byte: 0}, 40 }, 41 }, 42 "declaredFromNamedFileMapString": { 43 Name: "declaredFromNamedFileMapString", 44 Type: cty.Map(cty.String), 45 ConstraintType: cty.Map(cty.String), 46 ParsingMode: configs.VariableParseHCL, 47 DeclRange: hcl.Range{ 48 Filename: "fake.tf", 49 Start: hcl.Pos{Line: 2, Column: 1, Byte: 0}, 50 End: hcl.Pos{Line: 2, Column: 1, Byte: 0}, 51 }, 52 }, 53 "declaredFromNamedFileBool": { 54 Name: "declaredFromNamedFileBool", 55 Type: cty.Bool, 56 ConstraintType: cty.Bool, 57 ParsingMode: configs.VariableParseLiteral, 58 DeclRange: hcl.Range{ 59 Filename: "fake.tf", 60 Start: hcl.Pos{Line: 2, Column: 1, Byte: 0}, 61 End: hcl.Pos{Line: 2, Column: 1, Byte: 0}, 62 }, 63 }, 64 "declaredFromNamedFileNumber": { 65 Name: "declaredFromNamedFileNumber", 66 Type: cty.Number, 67 ConstraintType: cty.Number, 68 ParsingMode: configs.VariableParseLiteral, 69 DeclRange: hcl.Range{ 70 Filename: "fake.tf", 71 Start: hcl.Pos{Line: 2, Column: 1, Byte: 0}, 72 End: hcl.Pos{Line: 2, Column: 1, Byte: 0}, 73 }, 74 }, 75 "declaredFromNamedFileListString": { 76 Name: "declaredFromNamedFileListString", 77 Type: cty.List(cty.String), 78 ConstraintType: cty.List(cty.String), 79 ParsingMode: configs.VariableParseHCL, 80 DeclRange: hcl.Range{ 81 Filename: "fake.tf", 82 Start: hcl.Pos{Line: 2, Column: 1, Byte: 0}, 83 End: hcl.Pos{Line: 2, Column: 1, Byte: 0}, 84 }, 85 }, 86 "declaredFromNamedFileNull": { 87 Name: "declaredFromNamedFileNull", 88 Type: cty.String, 89 ConstraintType: cty.String, 90 ParsingMode: configs.VariableParseHCL, 91 DeclRange: hcl.Range{ 92 Filename: "fake.tf", 93 Start: hcl.Pos{Line: 2, Column: 1, Byte: 0}, 94 End: hcl.Pos{Line: 2, Column: 1, Byte: 0}, 95 }, 96 }, 97 "declaredFromNamedMapComplex": { 98 Name: "declaredFromNamedMapComplex", 99 Type: cty.DynamicPseudoType, 100 ConstraintType: cty.DynamicPseudoType, 101 ParsingMode: configs.VariableParseHCL, 102 DeclRange: hcl.Range{ 103 Filename: "fake.tf", 104 Start: hcl.Pos{Line: 2, Column: 1, Byte: 0}, 105 End: hcl.Pos{Line: 2, Column: 1, Byte: 0}, 106 }, 107 }, 108 "declaredFromCLIArg": { 109 Name: "declaredFromCLIArg", 110 Type: cty.String, 111 ConstraintType: cty.String, 112 ParsingMode: configs.VariableParseLiteral, 113 DeclRange: hcl.Range{ 114 Filename: "fake.tf", 115 Start: hcl.Pos{Line: 2, Column: 1, Byte: 0}, 116 End: hcl.Pos{Line: 2, Column: 1, Byte: 0}, 117 }, 118 }, 119 "declaredFromEnvVar": { 120 Name: "declaredFromEnvVar", 121 Type: cty.String, 122 ConstraintType: cty.String, 123 ParsingMode: configs.VariableParseLiteral, 124 DeclRange: hcl.Range{ 125 Filename: "fake.tf", 126 Start: hcl.Pos{Line: 2, Column: 1, Byte: 0}, 127 End: hcl.Pos{Line: 2, Column: 1, Byte: 0}, 128 }, 129 }, 130 "missing": { 131 Name: "missing", 132 Type: cty.String, 133 ConstraintType: cty.String, 134 Default: cty.StringVal("2"), 135 ParsingMode: configs.VariableParseLiteral, 136 DeclRange: hcl.Range{ 137 Filename: "fake.tf", 138 Start: hcl.Pos{Line: 2, Column: 1, Byte: 0}, 139 End: hcl.Pos{Line: 2, Column: 1, Byte: 0}, 140 }, 141 }, 142 } 143 wantVals := make(map[string]string) 144 wantVals["declaredFromNamedFileBool"] = "true" 145 wantVals["declaredFromNamedFileNumber"] = "2" 146 wantVals["declaredFromNamedFileListString"] = `["2a", "2b"]` 147 wantVals["declaredFromNamedFileNull"] = "null" 148 wantVals["declaredFromNamedFileMapString"] = "{\n foo = \"bar\"\n}" 149 wantVals["declaredFromNamedMapComplex"] = "{\n foo = {\n qux = [true, false]\n }\n}" 150 wantVals["declaredFromCLIArg"] = `"3"` 151 wantVals["declaredFromEnvVar"] = `"4"` 152 153 gotVals, diags := ParseCloudRunVariables(vv, decls) 154 if diff := cmp.Diff(wantVals, gotVals, cmp.Comparer(cty.Value.RawEquals)); diff != "" { 155 t.Errorf("wrong result\n%s", diff) 156 } 157 158 if got, want := len(diags), 1; got != want { 159 t.Fatalf("expected 1 variable error: %v, got %v", diags.Err(), want) 160 } 161 162 if got, want := diags[0].Description().Summary, "Value for undeclared variable"; got != want { 163 t.Errorf("wrong summary for diagnostic 0\ngot: %s\nwant: %s", got, want) 164 } 165 }) 166 } 167 168 type testUnparsedVariableValue struct { 169 source terraform.ValueSourceType 170 value cty.Value 171 } 172 173 func (v testUnparsedVariableValue) ParseVariableValue(mode configs.VariableParsingMode) (*terraform.InputValue, tfdiags.Diagnostics) { 174 return &terraform.InputValue{ 175 Value: v.value, 176 SourceType: v.source, 177 SourceRange: tfdiags.SourceRange{ 178 Filename: "fake.tfvars", 179 Start: tfdiags.SourcePos{Line: 1, Column: 1, Byte: 0}, 180 End: tfdiags.SourcePos{Line: 1, Column: 1, Byte: 0}, 181 }, 182 }, nil 183 }