|
9 | 9 | "slices" |
10 | 10 | "strings" |
11 | 11 | "testing" |
| 12 | + "testing/fstest" |
12 | 13 |
|
13 | 14 | "github.com/hashicorp/hcl/v2" |
14 | 15 | "github.com/stretchr/testify/assert" |
@@ -48,6 +49,7 @@ func Test_Extract(t *testing.T) { |
48 | 49 | presetsFuncs func(t *testing.T, presets []types.Preset) |
49 | 50 | presets map[string]assertPreset |
50 | 51 | warnings []*regexp.Regexp |
| 52 | + secretRequirements []types.SecretRequirement |
51 | 53 | }{ |
52 | 54 | { |
53 | 55 | name: "bad param values", |
@@ -657,6 +659,38 @@ func Test_Extract(t *testing.T) { |
657 | 659 | prebuildCount(1), |
658 | 660 | }, |
659 | 661 | }, |
| 662 | + { |
| 663 | + name: "secrets basic", |
| 664 | + dir: "secretsbasic", |
| 665 | + secretRequirements: []types.SecretRequirement{ |
| 666 | + {Env: "GITHUB_TOKEN", HelpMessage: "Add a GitHub PAT"}, |
| 667 | + {File: "~/.aws/credentials", HelpMessage: "Add AWS creds"}, |
| 668 | + }, |
| 669 | + }, |
| 670 | + { |
| 671 | + name: "secrets conditional off", |
| 672 | + dir: "secretsconditional", |
| 673 | + input: preview.Input{ |
| 674 | + ParameterValues: map[string]string{"use_github": "false"}, |
| 675 | + }, |
| 676 | + params: map[string]assertParam{ |
| 677 | + "use_github": ap().value("false"), |
| 678 | + }, |
| 679 | + secretRequirements: nil, |
| 680 | + }, |
| 681 | + { |
| 682 | + name: "secrets conditional on", |
| 683 | + dir: "secretsconditional", |
| 684 | + input: preview.Input{ |
| 685 | + ParameterValues: map[string]string{"use_github": "true"}, |
| 686 | + }, |
| 687 | + params: map[string]assertParam{ |
| 688 | + "use_github": ap().value("true"), |
| 689 | + }, |
| 690 | + secretRequirements: []types.SecretRequirement{ |
| 691 | + {Env: "GITHUB_TOKEN", HelpMessage: "Add a GitHub PAT"}, |
| 692 | + }, |
| 693 | + }, |
660 | 694 | { |
661 | 695 | name: "override", |
662 | 696 | dir: "override", |
@@ -756,6 +790,10 @@ func Test_Extract(t *testing.T) { |
756 | 790 | require.True(t, ok, "unknown variable %s", variable.Name) |
757 | 791 | check(t, variable) |
758 | 792 | } |
| 793 | + |
| 794 | + // Assert secret requirements |
| 795 | + require.ElementsMatch(t, tc.secretRequirements, output.SecretRequirements, |
| 796 | + "secret requirements do not match expected") |
759 | 797 | }) |
760 | 798 | } |
761 | 799 | } |
@@ -1105,3 +1143,79 @@ DiagLoop: |
1105 | 1143 |
|
1106 | 1144 | assert.Equal(t, []string{}, checks, "missing expected diagnostic errors") |
1107 | 1145 | } |
| 1146 | + |
| 1147 | +func Test_SecretRequirementErrors(t *testing.T) { |
| 1148 | + t.Parallel() |
| 1149 | + tests := []struct { |
| 1150 | + name string |
| 1151 | + tf string |
| 1152 | + wantDiag string // substring match on summary+" "+detail |
| 1153 | + }{ |
| 1154 | + { |
| 1155 | + name: "missing help_message", |
| 1156 | + tf: ` |
| 1157 | +data "coder_secret" "x" { |
| 1158 | + env = "X" |
| 1159 | +} |
| 1160 | +`, |
| 1161 | + wantDiag: `help_message`, |
| 1162 | + }, |
| 1163 | + { |
| 1164 | + name: "help_message null", |
| 1165 | + tf: ` |
| 1166 | +data "coder_secret" "x" { |
| 1167 | + env = "X" |
| 1168 | + help_message = null |
| 1169 | +} |
| 1170 | +`, |
| 1171 | + wantDiag: `help_message`, |
| 1172 | + }, |
| 1173 | + { |
| 1174 | + name: "help_message wrong type (number)", |
| 1175 | + tf: ` |
| 1176 | +data "coder_secret" "x" { |
| 1177 | + env = "X" |
| 1178 | + help_message = 42 |
| 1179 | +} |
| 1180 | +`, |
| 1181 | + wantDiag: `Expected a string`, |
| 1182 | + }, |
| 1183 | + { |
| 1184 | + name: "neither env nor file", |
| 1185 | + tf: ` |
| 1186 | +data "coder_secret" "x" { |
| 1187 | + help_message = "need one" |
| 1188 | +} |
| 1189 | +`, |
| 1190 | + wantDiag: `Exactly one of "env" or "file" must be set`, |
| 1191 | + }, |
| 1192 | + { |
| 1193 | + name: "both env and file", |
| 1194 | + tf: ` |
| 1195 | +data "coder_secret" "x" { |
| 1196 | + env = "X" |
| 1197 | + file = "~/y" |
| 1198 | + help_message = "ok" |
| 1199 | +} |
| 1200 | +`, |
| 1201 | + wantDiag: `Exactly one of "env" or "file" must be set`, |
| 1202 | + }, |
| 1203 | + } |
| 1204 | + for _, tc := range tests { |
| 1205 | + t.Run(tc.name, func(t *testing.T) { |
| 1206 | + t.Parallel() |
| 1207 | + fsys := fstest.MapFS{"main.tf": &fstest.MapFile{Data: []byte(tc.tf)}} |
| 1208 | + _, diags := preview.Preview(context.Background(), preview.Input{}, fsys) |
| 1209 | + require.True(t, diags.HasErrors(), "expected errors; got %v", diags) |
| 1210 | + var found bool |
| 1211 | + for _, d := range diags { |
| 1212 | + if strings.Contains(d.Summary+" "+d.Detail, tc.wantDiag) { |
| 1213 | + found = true |
| 1214 | + break |
| 1215 | + } |
| 1216 | + } |
| 1217 | + require.True(t, found, |
| 1218 | + "no diag matching %q; got: %v", tc.wantDiag, diags) |
| 1219 | + }) |
| 1220 | + } |
| 1221 | +} |
0 commit comments