forked from google/gnostic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gnostic_test.go
453 lines (401 loc) · 10.9 KB
/
gnostic_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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package main
import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func testCompiler(t *testing.T, inputFile string, referenceFile string, expectErrors bool) {
textFile := strings.Replace(filepath.Base(inputFile), filepath.Ext(inputFile), ".text", 1)
errorsFile := strings.Replace(filepath.Base(inputFile), filepath.Ext(inputFile), ".errors", 1)
// remove any preexisting output files
os.Remove(textFile)
os.Remove(errorsFile)
// run the compiler
var err error
var cmd = exec.Command(
"gnostic",
inputFile,
"--text-out=.",
"--errors-out=.",
"--resolve-refs")
//t.Log(cmd.Args)
err = cmd.Run()
if err != nil && !expectErrors {
t.Logf("Compile failed: %+v", err)
t.FailNow()
}
// verify the output against a reference
var outputFile string
if expectErrors {
outputFile = errorsFile
} else {
outputFile = textFile
}
err = exec.Command("diff", outputFile, referenceFile).Run()
if err != nil {
t.Logf("Diff failed: %+v", err)
t.FailNow()
} else {
// if the test succeeded, clean up
os.Remove(textFile)
os.Remove(errorsFile)
}
}
func testNormal(t *testing.T, inputFile string, referenceFile string) {
testCompiler(t, inputFile, referenceFile, false)
}
func testErrors(t *testing.T, inputFile string, referenceFile string) {
testCompiler(t, inputFile, referenceFile, true)
}
func TestPetstoreJSON(t *testing.T) {
testNormal(t,
"examples/v2.0/json/petstore.json",
"test/v2.0/petstore.text")
}
func TestPetstoreYAML(t *testing.T) {
testNormal(t,
"examples/v2.0/yaml/petstore.yaml",
"test/v2.0/petstore.text")
}
func TestSeparateYAML(t *testing.T) {
testNormal(t,
"examples/v2.0/yaml/petstore-separate/spec/swagger.yaml",
"test/v2.0/yaml/petstore-separate/spec/swagger.text")
}
func TestSeparateJSON(t *testing.T) {
testNormal(t,
"examples/v2.0/json/petstore-separate/spec/swagger.json",
"test/v2.0/yaml/petstore-separate/spec/swagger.text") // yaml and json results should be identical
}
func TestRemotePetstoreJSON(t *testing.T) {
testNormal(t,
"https://raw.githubusercontent.com/googleapis/openapi-compiler/master/examples/v2.0/json/petstore.json",
"test/v2.0/petstore.text")
}
func TestRemotePetstoreYAML(t *testing.T) {
testNormal(t,
"https://raw.githubusercontent.com/googleapis/openapi-compiler/master/examples/v2.0/yaml/petstore.yaml",
"test/v2.0/petstore.text")
}
func TestRemoteSeparateYAML(t *testing.T) {
testNormal(t,
"https://raw.githubusercontent.com/googleapis/openapi-compiler/master/examples/v2.0/yaml/petstore-separate/spec/swagger.yaml",
"test/v2.0/yaml/petstore-separate/spec/swagger.text")
}
func TestRemoteSeparateJSON(t *testing.T) {
testNormal(t,
"https://raw.githubusercontent.com/googleapis/openapi-compiler/master/examples/v2.0/json/petstore-separate/spec/swagger.json",
"test/v2.0/yaml/petstore-separate/spec/swagger.text")
}
func TestErrorBadProperties(t *testing.T) {
testErrors(t,
"examples/errors/petstore-badproperties.yaml",
"test/errors/petstore-badproperties.errors")
}
func TestErrorUnresolvedRefs(t *testing.T) {
testErrors(t,
"examples/errors/petstore-unresolvedrefs.yaml",
"test/errors/petstore-unresolvedrefs.errors")
}
func TestErrorMissingVersion(t *testing.T) {
testErrors(t,
"examples/errors/petstore-missingversion.yaml",
"test/errors/petstore-missingversion.errors")
}
func testPlugin(t *testing.T, plugin string, inputFile string, outputFile string, referenceFile string) {
// remove any preexisting output files
os.Remove(outputFile)
// run the compiler
var err error
output, err := exec.Command(
"gnostic",
"--"+plugin+"-out=-",
inputFile).Output()
if err != nil {
t.Logf("Compile failed: %+v", err)
t.FailNow()
}
_ = ioutil.WriteFile(outputFile, output, 0644)
err = exec.Command("diff", outputFile, referenceFile).Run()
if err != nil {
t.Logf("Diff failed: %+v", err)
t.FailNow()
} else {
// if the test succeeded, clean up
os.Remove(outputFile)
}
}
func TestSamplePluginWithPetstore(t *testing.T) {
testPlugin(t,
"summary",
"examples/v2.0/yaml/petstore.yaml",
"sample-petstore.out",
"test/v2.0/yaml/sample-petstore.out")
}
func TestErrorInvalidPluginInvocations(t *testing.T) {
var err error
output, err := exec.Command(
"gnostic",
"examples/v2.0/yaml/petstore.yaml",
"--errors-out=-",
"--plugin-out=foo=bar,:abc",
"--plugin-out=,foo=bar:abc",
"--plugin-out=foo=:abc",
"--plugin-out==bar:abc",
"--plugin-out=,,:abc",
"--plugin-out=foo=bar=baz:abc",
).Output()
if err == nil {
t.Logf("Invalid invocations were accepted")
t.FailNow()
}
outputFile := "invalid-plugin-invocation.errors"
_ = ioutil.WriteFile(outputFile, output, 0644)
err = exec.Command("diff", outputFile, "test/errors/invalid-plugin-invocation.errors").Run()
if err != nil {
t.Logf("Diff failed: %+v", err)
t.FailNow()
} else {
// if the test succeeded, clean up
os.Remove(outputFile)
}
}
func TestValidPluginInvocations(t *testing.T) {
var err error
output, err := exec.Command(
"gnostic",
"examples/v2.0/yaml/petstore.yaml",
"--errors-out=-",
// verify an invocation with no parameters
"--summary-out=!", // "!" indicates that no output should be generated
// verify single pair of parameters
"--summary-out=a=b:!",
// verify multiple parameters
"--summary-out=a=b,c=123,xyz=alphabetagammadelta:!",
// verify that special characters / . - _ can be included in parameter keys and values
"--summary-out=a/b/c=x/y/z:!",
"--summary-out=a.b.c=x.y.z:!",
"--summary-out=a-b-c=x-y-z:!",
"--summary-out=a_b_c=x_y_z:!",
).Output()
if len(output) != 0 {
t.Logf("Valid invocations generated invalid errors\n%s", string(output))
t.FailNow()
}
if err != nil {
t.Logf("Valid invocations were not accepted")
t.FailNow()
}
}
func TestExtensionHandlerWithLibraryExample(t *testing.T) {
outputFile := "library-example-with-ext.text.out"
inputFile := "test/library-example-with-ext.json"
referenceFile := "test/library-example-with-ext.text.out"
os.Remove(outputFile)
// run the compiler
var err error
command := exec.Command(
"gnostic",
"--x-sampleone",
"--x-sampletwo",
"--text-out="+outputFile,
"--resolve-refs",
inputFile)
_, err = command.Output()
if err != nil {
t.Logf("Compile failed for command %v: %+v", command, err)
t.FailNow()
}
//_ = ioutil.WriteFile(outputFile, output, 0644)
err = exec.Command("diff", outputFile, referenceFile).Run()
if err != nil {
t.Logf("Diff failed: %+v", err)
t.FailNow()
} else {
// if the test succeeded, clean up
os.Remove(outputFile)
}
}
func TestJSONOutput(t *testing.T) {
inputFile := "test/library-example-with-ext.json"
textFile := "sample.text"
jsonFile := "sample.json"
textFile2 := "sample2.text"
jsonFile2 := "sample2.json"
os.Remove(textFile)
os.Remove(jsonFile)
os.Remove(textFile2)
os.Remove(jsonFile2)
var err error
// Run the compiler once.
command := exec.Command(
"gnostic",
"--text-out="+textFile,
"--json-out="+jsonFile,
inputFile)
_, err = command.Output()
if err != nil {
t.Logf("Compile failed for command %v: %+v", command, err)
t.FailNow()
}
// Run the compiler again, this time on the generated output.
command = exec.Command(
"gnostic",
"--text-out="+textFile2,
"--json-out="+jsonFile2,
jsonFile)
_, err = command.Output()
if err != nil {
t.Logf("Compile failed for command %v: %+v", command, err)
t.FailNow()
}
// Verify that both models have the same internal representation.
err = exec.Command("diff", textFile, textFile2).Run()
if err != nil {
t.Logf("Diff failed: %+v", err)
t.FailNow()
} else {
// if the test succeeded, clean up
os.Remove(textFile)
os.Remove(jsonFile)
os.Remove(textFile2)
os.Remove(jsonFile2)
}
}
func TestYAMLOutput(t *testing.T) {
inputFile := "test/library-example-with-ext.json"
textFile := "sample.text"
yamlFile := "sample.yaml"
textFile2 := "sample2.text"
yamlFile2 := "sample2.yaml"
os.Remove(textFile)
os.Remove(yamlFile)
os.Remove(textFile2)
os.Remove(yamlFile2)
var err error
// Run the compiler once.
command := exec.Command(
"gnostic",
"--text-out="+textFile,
"--yaml-out="+yamlFile,
inputFile)
_, err = command.Output()
if err != nil {
t.Logf("Compile failed for command %v: %+v", command, err)
t.FailNow()
}
// Run the compiler again, this time on the generated output.
command = exec.Command(
"gnostic",
"--text-out="+textFile2,
"--yaml-out="+yamlFile2,
yamlFile)
_, err = command.Output()
if err != nil {
t.Logf("Compile failed for command %v: %+v", command, err)
t.FailNow()
}
// Verify that both models have the same internal representation.
err = exec.Command("diff", textFile, textFile2).Run()
if err != nil {
t.Logf("Diff failed: %+v", err)
t.FailNow()
} else {
// if the test succeeded, clean up
os.Remove(textFile)
os.Remove(yamlFile)
os.Remove(textFile2)
os.Remove(yamlFile2)
}
}
func testBuilder(version string, t *testing.T) {
var err error
pbFile := "petstore-" + version + ".pb"
yamlFile := "petstore.yaml"
jsonFile := "petstore.json"
textFile := "petstore.text"
textReference := "test/" + version + ".0/petstore.text"
os.Remove(pbFile)
os.Remove(textFile)
os.Remove(yamlFile)
os.Remove(jsonFile)
// Generate petstore.pb.
command := exec.Command(
"petstore-builder",
"--"+version)
_, err = command.Output()
if err != nil {
t.Logf("Command %v failed: %+v", command, err)
t.FailNow()
}
// Convert petstore.pb to yaml and json.
command = exec.Command(
"gnostic",
pbFile,
"--json-out="+jsonFile,
"--yaml-out="+yamlFile)
_, err = command.Output()
if err != nil {
t.Logf("Command %v failed: %+v", command, err)
t.FailNow()
}
// Read petstore.yaml, resolve references, and export text.
command = exec.Command(
"gnostic",
yamlFile,
"--resolve-refs",
"--text-out="+textFile)
_, err = command.Output()
if err != nil {
t.Logf("Command %v failed: %+v", command, err)
t.FailNow()
}
// Verify that the generated text matches our reference.
err = exec.Command("diff", textFile, textReference).Run()
if err != nil {
t.Logf("Diff failed: %+v", err)
t.FailNow()
}
// Read petstore.json, resolve references, and export text.
command = exec.Command(
"gnostic",
jsonFile,
"--resolve-refs",
"--text-out="+textFile)
_, err = command.Output()
if err != nil {
t.Logf("Command %v failed: %+v", command, err)
t.FailNow()
}
// Verify that the generated text matches our reference.
err = exec.Command("diff", textFile, textReference).Run()
if err != nil {
t.Logf("Diff failed: %+v", err)
t.FailNow()
}
// if the test succeeded, clean up
os.Remove(pbFile)
os.Remove(textFile)
os.Remove(yamlFile)
os.Remove(jsonFile)
}
func TestBuilderV2(t *testing.T) {
testBuilder("v2", t)
}
func TestBuilderV3(t *testing.T) {
testBuilder("v3", t)
}
// OpenAPI 3.0 tests
func TestPetstoreYAML_30(t *testing.T) {
testNormal(t,
"examples/v3.0/yaml/petstore.yaml",
"test/v3.0/petstore.text")
}
func TestPetstoreJSON_30(t *testing.T) {
testNormal(t,
"examples/v3.0/json/petstore.json",
"test/v3.0/petstore.text")
}