-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
sdklike_test.go
283 lines (266 loc) · 8.08 KB
/
sdklike_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package backendbase
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/zclconf/go-cty-debug/ctydebug"
"github.com/zclconf/go-cty/cty"
)
func TestSDKLikePath(t *testing.T) {
tests := []struct {
Input string
Want cty.Path
}{
{
"foo",
cty.GetAttrPath("foo"),
},
{
"foo.bar",
cty.GetAttrPath("foo").GetAttr("bar"),
},
{
"foo.bar.baz",
cty.GetAttrPath("foo").GetAttr("bar").GetAttr("baz"),
},
}
for _, test := range tests {
t.Run(test.Input, func(t *testing.T) {
got := SDKLikePath(test.Input)
if !test.Want.Equals(got) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
}
})
}
}
func TestSDKLikeEnvDefault(t *testing.T) {
t.Setenv("FALLBACK_A", "fallback a")
t.Setenv("FALLBACK_B", "fallback b")
t.Setenv("FALLBACK_UNSET", "")
t.Setenv("FALLBACK_UNSET_1", "")
t.Setenv("FALLBACK_UNSET_2", "")
tests := map[string]struct {
Value string
EnvNames []string
Want string
}{
"value is set": {
"hello",
[]string{"FALLBACK_A", "FALLBACK_B"},
"hello",
},
"value is not set, but both fallbacks are": {
"",
[]string{"FALLBACK_A", "FALLBACK_B"},
"fallback a",
},
"value is not set, and first callback isn't set": {
"",
[]string{"FALLBACK_UNSET", "FALLBACK_B"},
"fallback b",
},
"value is not set, and second callback isn't set": {
"",
[]string{"FALLBACK_A", "FALLBACK_UNSET"},
"fallback a",
},
"nothing is set": {
"",
[]string{"FALLBACK_UNSET_1", "FALLBACK_UNSET_2"},
"",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
got := SDKLikeEnvDefault(test.Value, test.EnvNames...)
if got != test.Want {
t.Errorf("wrong result\nvalue: %s\nenvs: %s\n\ngot: %s\nwant: %s", test.Value, test.EnvNames, got, test.Want)
}
})
}
}
func TestSDKLikeRequiredWithEnvDefault(t *testing.T) {
// This intentionally doesn't duplicate all of the test cases from
// TestSDKLikeEnvDefault, since SDKLikeRequiredWithEnvDefault is
// just a thin wrapper which adds an error check.
t.Setenv("FALLBACK_UNSET", "")
_, err := SDKLikeRequiredWithEnvDefault("attr_name", "", "FALLBACK_UNSET")
if err == nil {
t.Fatalf("unexpected success; want error")
}
if got, want := err.Error(), `attribute "attr_name" is required`; got != want {
t.Errorf("wrong error\ngot: %s\nwant: %s", got, want)
}
}
func TestSDKLikeData(t *testing.T) {
d := NewSDKLikeData(cty.ObjectVal(map[string]cty.Value{
"string": cty.StringVal("hello"),
"int": cty.NumberIntVal(5),
"float": cty.NumberFloatVal(0.5),
"bool": cty.True,
"null_string": cty.NullVal(cty.String),
"null_number": cty.NullVal(cty.Number),
"null_bool": cty.NullVal(cty.Bool),
}))
t.Run("string", func(t *testing.T) {
got := d.String("string")
want := "hello"
if got != want {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, want)
}
})
t.Run("null string", func(t *testing.T) {
got := d.String("null_string")
want := ""
if got != want {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, want)
}
})
t.Run("int as string", func(t *testing.T) {
// This is allowed as a convenience for backends that want to
// allow environment-based default values for integer values,
// since environment variables are always strings and so they'd
// need to do their own parsing afterwards anyway.
got := d.String("int")
want := "5"
if got != want {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, want)
}
})
t.Run("bool as string", func(t *testing.T) {
// This is allowed as a convenience for backends that want to
// allow environment-based default values for bool values,
// since environment variables are always strings and so they'd
// need to do their own parsing afterwards anyway.
got := d.String("bool")
want := "true"
if got != want {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, want)
}
})
t.Run("int", func(t *testing.T) {
got, err := d.Int64("int")
want := int64(5)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if got != want {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, want)
}
})
t.Run("int with fractional part", func(t *testing.T) {
got, err := d.Int64("float")
if err == nil {
t.Fatalf("unexpected success; want error\ngot: %#v", got)
}
// Legacy SDK exposed the strconv.ParseInt implementation detail in
// its error message, and so for now we do the same. Maybe we'll
// improve this later, but it would probably be better to wean
// the backends off using the "SDKLike" helper altogether instead.
if got, want := err.Error(), `strconv.ParseInt: parsing "0.5": invalid syntax`; got != want {
t.Errorf("wrong error\ngot: %s\nwant: %s", got, want)
}
})
t.Run("null number as int", func(t *testing.T) {
got, err := d.Int64("null_number")
want := int64(0)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if got != want {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, want)
}
})
t.Run("bool", func(t *testing.T) {
got := d.Bool("bool")
want := true
if got != want {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, want)
}
})
t.Run("null bool", func(t *testing.T) {
// Assuming false for a null is quite questionable, but it's what
// the legacy SDK did and so we'll follow its lead.
got := d.Bool("null_bool")
want := false
if got != want {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, want)
}
})
}
func TestSDKLikeApplyEnvDefaults(t *testing.T) {
t.Setenv("FALLBACK_BEEP", "beep from environment")
t.Setenv("FALLBACK_UNUSED", "unused from environment")
t.Setenv("FALLBACK_EMPTY", "")
t.Run("success", func(t *testing.T) {
defs := SDKLikeDefaults{
"string_set_fallback": {
Fallback: "fallback not used",
},
"string_set_env": {
EnvVars: []string{"FALLBACK_UNUSED"},
},
"string_fallback_null": {
Fallback: "boop from fallback",
},
"string_fallback_empty": {
Fallback: "boop from fallback",
},
"string_env_null": {
EnvVars: []string{"FALLBACK_BEEP", "FALLBACK_UNUSED"},
Fallback: "unused",
},
"string_env_empty": {
EnvVars: []string{"FALLBACK_BEEP", "FALLBACK_UNUSED"},
Fallback: "unused",
},
"string_env_unsetfirst": {
EnvVars: []string{"FALLBACK_EMPTY", "FALLBACK_BEEP"},
Fallback: "unused",
},
"string_env_unsetsecond": {
EnvVars: []string{"FALLBACK_BEEP", "FALLBACK_EMPTY"},
Fallback: "unused",
},
"string_nothing_null": {
EnvVars: []string{"FALLBACK_EMPTY"},
},
"string_nothing_empty": {
EnvVars: []string{"FALLBACK_EMPTY"},
},
}
got, err := defs.ApplyTo(cty.ObjectVal(map[string]cty.Value{
"string_set_fallback": cty.StringVal("set in config"),
"string_set_env": cty.StringVal("set in config"),
"string_fallback_null": cty.NullVal(cty.String),
"string_fallback_empty": cty.StringVal(""),
"string_env_null": cty.NullVal(cty.String),
"string_env_empty": cty.StringVal(""),
"string_env_unsetfirst": cty.NullVal(cty.String),
"string_env_unsetsecond": cty.NullVal(cty.String),
"string_nothing_null": cty.NullVal(cty.String),
"string_nothing_empty": cty.StringVal(""),
"passthru": cty.EmptyObjectVal,
}))
want := cty.ObjectVal(map[string]cty.Value{
"string_set_fallback": cty.StringVal("set in config"),
"string_set_env": cty.StringVal("set in config"),
"string_fallback_null": cty.StringVal("boop from fallback"),
"string_fallback_empty": cty.StringVal("boop from fallback"),
"string_env_null": cty.StringVal("beep from environment"),
"string_env_empty": cty.StringVal("beep from environment"),
"string_env_unsetfirst": cty.StringVal("beep from environment"),
"string_env_unsetsecond": cty.StringVal("beep from environment"),
"string_nothing_null": cty.NullVal(cty.String),
"string_nothing_empty": cty.StringVal(""),
"passthru": cty.EmptyObjectVal,
})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if diff := cmp.Diff(want, got, ctydebug.CmpOptions); diff != "" {
t.Errorf("wrong result\n%s", diff)
}
})
}