forked from openfaas/faas-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_test.go
247 lines (212 loc) · 8.05 KB
/
store_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
package commands
import (
"encoding/json"
"testing"
storeV2 "github.com/openfaas/faas-cli/schema/store/v2"
)
func matchFilteredOutout(t *testing.T, expectedOuputFunctions, filteredFunctions []storeV2.StoreFunction, platform string) {
if len(expectedOuputFunctions) != len(filteredFunctions) {
t.Errorf("Length did not match, expected: %v, got: %v", len(expectedOuputFunctions), len(filteredFunctions))
}
var isFound bool
for _, expectedFunction := range expectedOuputFunctions {
isFound = false
for _, filteredFunction := range filteredFunctions {
_, hasPlatform := filteredFunction.Images[platform]
if expectedFunction.Name == filteredFunction.Name && hasPlatform {
isFound = true
}
}
if !isFound {
t.Errorf("Function %s not present in the output", expectedFunction.Name)
}
}
}
func getInputStoreFunctions(t *testing.T) []storeV2.StoreFunction {
inputJSONBytes := []byte(`[{
"title": "NodeInfo",
"name": "nodeinfo",
"description": "Get info about the machine that you're deployed on. Tells CPU count, hostname, OS, and Uptime",
"images": {
"arm64": "functions/nodeinfo:arm64",
"armhf": "functions/nodeinfo:latest-armhf",
"x86_64": "functions/nodeinfo:latest"
},
"fprocess": "node main.js",
"network": "func_functions",
"repo_url": "https://github.com/openfaas/faas/tree/master/sample-functions/NodeInfo"
},
{
"title": "sha512sum",
"name": "sha512sum",
"description": "Generate shasums in 512 format",
"images": {
"arm64": "functions/alpine:latest-arm64"
},
"fprocess": "sha512sum",
"network": "func_functions",
"repo_url": "https://github.com/openfaas/faas/tree/master/sample-functions/AlpineFunction"
},
{
"title": "Figlet",
"name": "figlet",
"description": "Generate ASCII logos with the figlet CLI",
"images": {
"armhf": "functions/figlet:latest-armhf",
"x86_64": "functions/figlet:0.9.6"
},
"fprocess": "figlet",
"repo_url": "https://github.com/openfaas/faas/tree/master/sample-functions/figlet"
}]`)
var inputFunctions []storeV2.StoreFunction
err := json.Unmarshal(inputJSONBytes, &inputFunctions)
if err != nil {
t.Errorf(err.Error())
}
return inputFunctions
}
func Test_filterStoreList_x86_64(t *testing.T) {
outputJSONBytes := []byte(`[{
"title": "NodeInfo",
"name": "nodeinfo",
"description": "Get info about the machine that you're deployed on. Tells CPU count, hostname, OS, and Uptime",
"images": {
"arm64": "functions/nodeinfo:arm64",
"armhf": "functions/nodeinfo:latest-armhf",
"x86_64": "functions/nodeinfo:latest"
},
"fprocess": "node main.js",
"network": "func_functions",
"repo_url": "https://github.com/openfaas/faas/tree/master/sample-functions/NodeInfo"
}, {
"title": "Figlet",
"name": "figlet",
"description": "Generate ASCII logos with the figlet CLI",
"images": {
"armhf": "functions/figlet:latest-armhf",
"x86_64": "functions/figlet:0.9.6"
},
"fprocess": "figlet",
"repo_url": "https://github.com/openfaas/faas/tree/master/sample-functions/figlet"
}]`)
inputFunctions := getInputStoreFunctions(t)
var expectedOuputFunctions []storeV2.StoreFunction
err := json.Unmarshal(outputJSONBytes, &expectedOuputFunctions)
if err != nil {
t.Errorf(err.Error())
}
filteredFunctions := filterStoreList(inputFunctions, "x86_64")
matchFilteredOutout(t, expectedOuputFunctions, filteredFunctions, "x86_64")
}
func Test_filterStoreList_armhf(t *testing.T) {
outputJSONBytes := []byte(`[{
"title": "NodeInfo",
"name": "nodeinfo",
"description": "Get info about the machine that you're deployed on. Tells CPU count, hostname, OS, and Uptime",
"images": {
"arm64": "functions/nodeinfo:arm64",
"armhf": "functions/nodeinfo:latest-armhf",
"x86_64": "functions/nodeinfo:latest"
},
"fprocess": "node main.js",
"network": "func_functions",
"repo_url": "https://github.com/openfaas/faas/tree/master/sample-functions/NodeInfo"
}, {
"title": "Figlet",
"name": "figlet",
"description": "Generate ASCII logos with the figlet CLI",
"images": {
"armhf": "functions/figlet:latest-armhf",
"x86_64": "functions/figlet:0.9.6"
},
"fprocess": "figlet",
"repo_url": "https://github.com/openfaas/faas/tree/master/sample-functions/figlet"
}]`)
inputFunctions := getInputStoreFunctions(t)
var expectedOuputFunctions []storeV2.StoreFunction
err := json.Unmarshal(outputJSONBytes, &expectedOuputFunctions)
if err != nil {
t.Errorf(err.Error())
}
filteredFunctions := filterStoreList(inputFunctions, "armhf")
matchFilteredOutout(t, expectedOuputFunctions, filteredFunctions, "armhf")
}
func Test_filterStoreList_arm64(t *testing.T) {
outputJSONBytes := []byte(`[{
"title": "NodeInfo",
"name": "nodeinfo",
"description": "Get info about the machine that you're deployed on. Tells CPU count, hostname, OS, and Uptime",
"images": {
"arm64": "functions/nodeinfo:arm64",
"armhf": "functions/nodeinfo:latest-armhf",
"x86_64": "functions/nodeinfo:latest"
},
"fprocess": "node main.js",
"network": "func_functions",
"repo_url": "https://github.com/openfaas/faas/tree/master/sample-functions/NodeInfo"
},{
"title": "sha512sum",
"name": "sha512sum",
"description": "Generate shasums in 512 format",
"images": {
"arm64": "functions/alpine:latest-arm64"
},
"fprocess": "sha512sum",
"network": "func_functions",
"repo_url": "https://github.com/openfaas/faas/tree/master/sample-functions/AlpineFunction"
}]`)
inputFunctions := getInputStoreFunctions(t)
var expectedOuputFunctions []storeV2.StoreFunction
err := json.Unmarshal(outputJSONBytes, &expectedOuputFunctions)
if err != nil {
t.Errorf(err.Error())
}
filteredFunctions := filterStoreList(inputFunctions, "arm64")
matchFilteredOutout(t, expectedOuputFunctions, filteredFunctions, "arm64")
}
func Test_filterStoreList_other(t *testing.T) {
outputJSONBytes := []byte(`[]`)
inputFunctions := getInputStoreFunctions(t)
var expectedOuputFunctions []storeV2.StoreFunction
err := json.Unmarshal(outputJSONBytes, &expectedOuputFunctions)
if err != nil {
t.Errorf(err.Error())
}
filteredFunctions := filterStoreList(inputFunctions, "other")
matchFilteredOutout(t, expectedOuputFunctions, filteredFunctions, "other")
}
func Test_getStorePlatforms(t *testing.T) {
var expectedPlatforms = []string{"arm64", "armhf", "x86_64"}
inputFunctions := getInputStoreFunctions(t)
actualPlatforms := getStorePlatforms(inputFunctions)
if len(expectedPlatforms) != len(actualPlatforms) {
t.Errorf("Length of platforms did not match, expected: %d, got: %d", len(expectedPlatforms), len(actualPlatforms))
}
for _, expectedPlatform := range expectedPlatforms {
isFound := false
for _, actualPlatform := range actualPlatforms {
if expectedPlatform == actualPlatform {
isFound = true
}
}
if !isFound {
t.Errorf("Expected value '%s' not found in actual platforms, expected: %v, actual: %v", expectedPlatform, expectedPlatforms, actualPlatforms)
}
}
}
func Test_storeFindFunction_Positive(t *testing.T) {
inputFunctions := getInputStoreFunctions(t)
expectedFunctionName := "nodeinfo"
actualFunction := storeFindFunction(expectedFunctionName, inputFunctions)
if actualFunction.Name != expectedFunctionName {
t.Errorf("Function %s not found in store", expectedFunctionName)
}
}
func Test_storeFindFunction_Negative(t *testing.T) {
inputFunctions := getInputStoreFunctions(t)
expectedFunctionName := "openfaas-ocr"
actualFunction := storeFindFunction(expectedFunctionName, inputFunctions)
if actualFunction != nil {
t.Errorf("Function %s is found in store", expectedFunctionName)
}
}