-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions_test.go
260 lines (217 loc) · 6.16 KB
/
functions_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
package hclconfig
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/jumppad-labs/hclconfig/test_fixtures/structs"
"github.com/stretchr/testify/require"
"github.com/zclconf/go-cty/cty"
)
func TestCreateFunctionCreatesFunctionWithCorrectInParameters(t *testing.T) {
myfunc := func(a string, b int) (int, error) {
return 0, nil
}
ctyFunc, err := createCtyFunctionFromGoFunc(myfunc)
require.NoError(t, err)
require.Equal(t, cty.String, ctyFunc.Params()[0].Type)
require.Equal(t, cty.Number, ctyFunc.Params()[1].Type)
}
func TestCreateFunctionWithInvalidInParameterReturnsError(t *testing.T) {
myfunc := func(a string, complex func() error) (int, error) {
return 0, nil
}
_, err := createCtyFunctionFromGoFunc(myfunc)
require.Error(t, err)
}
func TestCreateFunctionCreatesFunctionWithCorrectOutParameters(t *testing.T) {
myfunc := func(a string, b int) (int, error) {
return 0, nil
}
ctyFunc, err := createCtyFunctionFromGoFunc(myfunc)
require.NoError(t, err)
rt, err := ctyFunc.ReturnType([]cty.Type{cty.String, cty.Number})
require.NoError(t, err)
require.Equal(t, cty.Number, rt)
}
func TestCreateFunctionWithInvalidOutParameterReturnsError(t *testing.T) {
myfunc := func(a string, b int) func() error {
return func() error {
return fmt.Errorf("oops")
}
}
_, err := createCtyFunctionFromGoFunc(myfunc)
require.Error(t, err)
myfunc2 := func(a string, b int) int {
return 1
}
_, err = createCtyFunctionFromGoFunc(myfunc2)
require.Error(t, err)
}
func TestCreateFunctionCallsFunction(t *testing.T) {
myfunc := func(a, b int) (int, error) {
return a + b, nil
}
ctyFunc, err := createCtyFunctionFromGoFunc(myfunc)
require.NoError(t, err)
val, err := ctyFunc.Call([]cty.Value{cty.NumberIntVal(2), cty.NumberIntVal(3)})
require.NoError(t, err)
bf := val.AsBigFloat()
i, _ := bf.Int64()
require.Equal(t, int64(5), i)
}
func TestCreateFunctionHandlesInputParams(t *testing.T) {
type testCase struct {
name string
f interface{}
}
cases := []testCase{
{
name: "integer input parameters",
f: func(a, b int) (int, error) {
return a + b, nil
},
},
{
name: "int16 input parameters",
f: func(a, b int16) (int16, error) {
return a + b, nil
},
},
{
name: "int32 input parameters",
f: func(a, b int32) (int32, error) {
return a + b, nil
},
},
{
name: "int64 input parameters",
f: func(a, b int64) (int64, error) {
return a + b, nil
},
},
{
name: "uint input parameters",
f: func(a, b uint) (uint, error) {
return a + b, nil
},
},
{
name: "uint16 input parameters",
f: func(a, b uint16) (uint16, error) {
return a + b, nil
},
},
{
name: "uint32 input parameters",
f: func(a, b uint32) (uint32, error) {
return a + b, nil
},
},
{
name: "uint64 input parameters",
f: func(a, b uint64) (uint64, error) {
return a + b, nil
},
},
{
name: "float32 input parameters",
f: func(a, b float32) (float32, error) {
return a + b, nil
},
},
{
name: "float64 input parameters",
f: func(a, b float64) (float64, error) {
return a + b, nil
},
},
{
name: "string input parameters",
f: func(a, b string) (string, error) {
return a + b, nil
},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
_, err := createCtyFunctionFromGoFunc(c.f)
require.NoError(t, err)
})
}
}
func TestParseProcessesDefaultFunctionsWithFile(t *testing.T) {
absoluteFolderPath, err := filepath.Abs("./test_fixtures/functions/default.hcl")
if err != nil {
t.Fatal(err)
}
os.Setenv("MYENV", "myvalue")
t.Cleanup(func() {
os.Unsetenv("MYENV")
})
p := setupParser(t)
c, err := p.ParseFile(absoluteFolderPath)
require.NoError(t, err)
r, err := c.FindResource("resource.container.default")
require.NoError(t, err)
cont := r.(*structs.Container)
home, _ := os.UserHomeDir()
require.Equal(t, "3", cont.Env["len_string"])
require.Equal(t, "2", cont.Env["len_collection"])
require.Equal(t, "myvalue", cont.Env["env"])
require.Equal(t, home, cont.Env["home"])
require.Contains(t, cont.Env["file"], "container")
require.Contains(t, cont.Env["dir"], filepath.Dir(absoluteFolderPath))
require.Contains(t, cont.Env["trim"], "foo bar")
require.Equal(t, "one", cont.DNS[0])
require.Equal(t, "two", cont.DNS[1])
require.Equal(t, "123", cont.Entrypoint[0])
require.Equal(t, "abc", cont.Entrypoint[1])
require.Equal(t, "one", cont.Command[0])
require.Equal(t, "two", cont.Command[1])
}
func TestParseProcessesDefaultFunctionsWithDirectory(t *testing.T) {
absoluteFolderPath, err := filepath.Abs("./test_fixtures/functions")
if err != nil {
t.Fatal(err)
}
os.Setenv("MYENV", "myvalue")
t.Cleanup(func() {
os.Unsetenv("MYENV")
})
p := setupParser(t)
p.RegisterFunction("constant_number", func() (int, error) { return 42, nil })
c, err := p.ParseDirectory(absoluteFolderPath)
require.NoError(t, err)
r, err := c.FindResource("resource.container.default")
require.NoError(t, err)
cont := r.(*structs.Container)
home, _ := os.UserHomeDir()
require.Equal(t, "3", cont.Env["len_string"])
require.Equal(t, "2", cont.Env["len_collection"])
require.Equal(t, "myvalue", cont.Env["env"])
require.Equal(t, home, cont.Env["home"])
require.Contains(t, cont.Env["file"], "container")
require.Contains(t, cont.Env["dir"], absoluteFolderPath)
require.Contains(t, cont.Env["trim"], "foo bar")
// template
require.Contains(t, cont.Env["template_file"], "Hello Raymond")
require.Contains(t, cont.Env["template_file"], "43 is a number")
require.Contains(t, cont.Env["template_file"], "cheese\n ham\n pineapple")
require.Contains(t, cont.Env["template_file"], "foo = bar")
require.Contains(t, cont.Env["template_file"], "x = 1")
}
func TestParseProcessesCustomFunctions(t *testing.T) {
absoluteFolderPath, err := filepath.Abs("./test_fixtures/functions/custom.hcl")
if err != nil {
t.Fatal(err)
}
p := setupParser(t)
p.RegisterFunction("constant_number", func() (int, error) { return 42, nil })
c, err := p.ParseFile(absoluteFolderPath)
require.NoError(t, err)
r, err := c.FindResource("resource.container.custom")
require.NoError(t, err)
cont := r.(*structs.Container)
require.Equal(t, "42", cont.Env["len"])
}