forked from go-swagger/go-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoreschemavalidation_test.go
517 lines (463 loc) · 16 KB
/
moreschemavalidation_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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// Copyright 2015 go-swagger maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package generator
import (
"bytes"
"io/ioutil"
"log"
"os"
"path"
"strings"
"sync"
"testing"
"github.com/go-openapi/loads"
"github.com/go-openapi/spec"
"github.com/go-openapi/swag"
"github.com/stretchr/testify/assert"
)
// modelExpectations is a test structure to capture expected codegen lines of code
type modelExpectations struct {
GeneratedFile string
ExpectedLines []string
NotExpectedLines []string
ExpectedLogs []string
NotExpectedLogs []string
ExpectFailure bool
}
// modelTestRun is a test structure to configure generations options to test a spec
type modelTestRun struct {
FixtureOpts *GenOpts
Definitions map[string]*modelExpectations
}
// AddExpectations adds expected / not expected sets of lines of code to the current run
func (r *modelTestRun) AddExpectations(file string, expectedCode, notExpectedCode, expectedLogs, notExpectedLogs []string) {
k := strings.ToLower(swag.ToJSONName(strings.TrimSuffix(file, ".go")))
if def, ok := r.Definitions[k]; ok {
def.ExpectedLines = append(def.ExpectedLines, expectedCode...)
def.NotExpectedLines = append(def.NotExpectedLines, notExpectedCode...)
def.ExpectedLogs = append(def.ExpectedLogs, expectedLogs...)
def.NotExpectedLogs = append(def.NotExpectedLogs, notExpectedLogs...)
return
}
r.Definitions[k] = &modelExpectations{
GeneratedFile: file,
ExpectedLines: expectedCode,
NotExpectedLines: notExpectedCode,
ExpectedLogs: expectedLogs,
NotExpectedLogs: notExpectedLogs,
}
}
// ExpectedFor returns the map of model expectations from the run for a given model definition
func (r *modelTestRun) ExpectedFor(definition string) *modelExpectations {
if def, ok := r.Definitions[strings.ToLower(definition)]; ok {
return def
}
return nil
}
func (r *modelTestRun) WithMinimalFlatten(minimal bool) *modelTestRun {
r.FixtureOpts.FlattenOpts.Minimal = minimal
return r
}
// modelFixture is a test structure to launch configurable test runs on a given spec
type modelFixture struct {
SpecFile string
Description string
Runs []*modelTestRun
}
// Add adds a new run to the provided model fixture
func (f *modelFixture) AddRun(expandSpec bool) *modelTestRun {
opts := &GenOpts{}
opts.IncludeValidator = true
opts.IncludeModel = true
opts.ValidateSpec = false
opts.Spec = f.SpecFile
if err := opts.EnsureDefaults(); err != nil {
panic(err)
}
// sets gen options (e.g. flatten vs expand) - full flatten is the default setting for this test (NOT the default CLI option!)
opts.FlattenOpts.Expand = expandSpec
opts.FlattenOpts.Minimal = false
defs := make(map[string]*modelExpectations, 150)
run := &modelTestRun{
FixtureOpts: opts,
Definitions: defs,
}
f.Runs = append(f.Runs, run)
return run
}
// ExpectedBy returns the expectations from another run of the current fixture, recalled by its index in the list of planned runs
func (f *modelFixture) ExpectedFor(index int, definition string) *modelExpectations {
if index > len(f.Runs)-1 {
return nil
}
if def, ok := f.Runs[index].Definitions[strings.ToLower(definition)]; ok {
return def
}
return nil
}
// newModelFixture is a test utility to build a new test plan for a spec file.
// The returned structure may be then used to add runs and expectations to each run.
func newModelFixture(specFile string, description string) *modelFixture {
// lookup if already here
for _, fix := range testedModels {
if fix.SpecFile == specFile {
return fix
}
}
runs := make([]*modelTestRun, 0, 2)
fix := &modelFixture{
SpecFile: specFile,
Description: description,
Runs: runs,
}
testedModels = append(testedModels, fix)
return fix
}
// all tested specs: init at the end of this source file
// you may append to those with different initXXX() funcs below.
var (
modelTestMutex = &sync.Mutex{}
testedModels []*modelFixture
// convenient vars for (not) matching some lines
noLines []string
todo []string
validatable []string
warning []string
)
func init() {
testedModels = make([]*modelFixture, 0, 50)
noLines = []string{}
todo = []string{`TODO`}
validatable = append([]string{`Validate(`}, todo...)
warning = []string{`warning`}
}
// initModelFixtures loads all tests to be performed
func initModelFixtures() {
initFixtureSimpleAllOf()
initFixtureComplexAllOf()
initFixtureIsNullable()
initFixtureItching()
initFixtureAdditionalProps()
initFixtureTuple()
initFixture1479Part()
initFixture1198()
initFixture1042()
initFixture1042V2()
initFixture979()
initFixture842()
initFixture607()
initFixture1336()
initFixtureErrors()
initFixture844Variations()
initFixtureMoreAddProps()
// a more stringent verification of this known fixture
initTodolistSchemavalidation()
initFixture1537()
initFixture1537v2()
// more maps and nullability checks
initFixture15365()
initFixtureNestedMaps()
initFixtureDeepMaps()
// format "byte" validation
initFixture1548()
// more tuples
initFixtureSimpleTuple()
// allOf with properties
initFixture1617()
// type realiasing
initFixtureRealiasedTypes()
// required base type
initFixture1993()
// allOf marshallers
initFixture2071()
}
/* Template initTxxx() to prepare and load a fixture:
func initTxxx() {
// testing xxx.yaml with expand (--with-expand)
f := newModelFixture("xxx.yaml", "A test blg")
// makes a run with expandSpec=false (full flattening)
thisRun := f.AddRun(false)
// loads expectations for model abc
thisRun.AddExpectations("abc.go", []string{
`line {`,
` more codegen `,
`}`,
},
// not expected
noLines,
// output in Log
noLines,
noLines)
// loads expectations for model abcDef
thisRun.AddExpectations("abc_def.go", []string{}, []string{}, noLines, noLines)
}
*/
func TestModelGenerateDefinition(t *testing.T) {
// exercise the top level model generation func
log.SetOutput(ioutil.Discard)
defer func() {
log.SetOutput(os.Stdout)
}()
fixtureSpec := "../fixtures/bugs/1487/fixture-is-nullable.yaml"
assert := assert.New(t)
gendir, erd := ioutil.TempDir(".", "model-test")
defer func() {
_ = os.RemoveAll(gendir)
}()
if assert.NoError(erd) {
opts := &GenOpts{}
opts.IncludeValidator = true
opts.IncludeModel = true
opts.ValidateSpec = false
opts.Spec = fixtureSpec
opts.ModelPackage = "models"
opts.Target = gendir
if err := opts.EnsureDefaults(); err != nil {
panic(err)
}
// sets gen options (e.g. flatten vs expand) - flatten is the default setting
opts.FlattenOpts.Minimal = false
err := GenerateDefinition([]string{"thingWithNullableDates"}, opts)
assert.NoErrorf(err, "Expected GenerateDefinition() to run without error")
err = GenerateDefinition(nil, opts)
assert.NoErrorf(err, "Expected GenerateDefinition() to run without error")
opts.TemplateDir = gendir
err = GenerateDefinition([]string{"thingWithNullableDates"}, opts)
assert.NoErrorf(err, "Expected GenerateDefinition() to run without error")
err = GenerateDefinition([]string{"thingWithNullableDates"}, nil)
assert.Errorf(err, "Expected GenerateDefinition() return an error when no option is passed")
opts.TemplateDir = "templates"
err = GenerateDefinition([]string{"thingWithNullableDates"}, opts)
assert.Errorf(err, "Expected GenerateDefinition() to croak about protected templates")
opts.TemplateDir = ""
err = GenerateDefinition([]string{"myAbsentDefinition"}, opts)
assert.Errorf(err, "Expected GenerateDefinition() to return an error when the model is not in spec")
opts.Spec = "pathToNowhere"
err = GenerateDefinition([]string{"thingWithNullableDates"}, opts)
assert.Errorf(err, "Expected GenerateDefinition() to return an error when the spec is not reachable")
}
}
func TestMoreModelValidations(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer func() {
log.SetOutput(os.Stdout)
}()
continueOnErrors := false
initModelFixtures()
dassert := assert.New(t)
t.Logf("INFO: model specs tested: %d", len(testedModels))
for _, fixt := range testedModels {
fixture := fixt
if fixture.SpecFile == "" {
continue
}
fixtureSpec := fixture.SpecFile
runTitle := strings.Join([]string{"codegen", strings.TrimSuffix(path.Base(fixtureSpec), path.Ext(fixtureSpec))}, "-")
t.Run(runTitle, func(t *testing.T) {
// gave up with parallel testing: when $ref analysis is involved, it is not possible to to parallelize
//t.Parallel()
log.SetOutput(ioutil.Discard)
for _, fixtureRun := range fixture.Runs {
// workaround race condition with underlying pkg: go-openapi/spec works with a global cache
// which does not support concurrent use for different specs.
//modelTestMutex.Lock()
specDoc, err := loads.Spec(fixtureSpec)
if !dassert.NoErrorf(err, "unexpected failure loading spec %s: %v", fixtureSpec, err) {
//modelTestMutex.Unlock()
t.FailNow()
return
}
opts := fixtureRun.FixtureOpts
// this is the expanded or flattened spec
newSpecDoc, er0 := validateAndFlattenSpec(opts, specDoc)
if !dassert.NoErrorf(er0, "could not expand/flatten fixture %s: %v", fixtureSpec, er0) {
//modelTestMutex.Unlock()
t.FailNow()
return
}
//modelTestMutex.Unlock()
definitions := newSpecDoc.Spec().Definitions
for k, fixtureExpectations := range fixtureRun.Definitions {
// pick definition to test
var schema *spec.Schema
var definitionName string
for def, s := range definitions {
// please do not inject fixtures with case conflicts on defs...
// this one is just easier to retrieve model back from file names when capturing
// the generated code.
if strings.EqualFold(def, k) {
schema = &s
definitionName = def
break
}
}
if !dassert.NotNil(schema, "expected to find definition %q in model fixture %s", k, fixtureSpec) {
//modelTestMutex.Unlock()
t.FailNow()
return
}
checkDefinitionCodegen(t, definitionName, fixtureSpec, schema, newSpecDoc, opts, fixtureExpectations, continueOnErrors)
}
}
})
}
}
func checkContinue(t *testing.T, continueOnErrors bool) {
if continueOnErrors {
t.Fail()
} else {
t.FailNow()
}
}
func checkDefinitionCodegen(t *testing.T, definitionName, fixtureSpec string, schema *spec.Schema, specDoc *loads.Document, opts *GenOpts, fixtureExpectations *modelExpectations, continueOnErrors bool) {
// prepare assertions on log output (e.g. generation warnings)
var logCapture bytes.Buffer
var msg string
dassert := assert.New(t)
if len(fixtureExpectations.ExpectedLogs) > 0 || len(fixtureExpectations.NotExpectedLogs) > 0 {
// lock when capturing shared log resource (hopefully not for all testcases)
modelTestMutex.Lock()
log.SetOutput(&logCapture)
}
// generate the schema for this definition
genModel, er1 := makeGenDefinition(definitionName, "models", *schema, specDoc, opts)
if len(fixtureExpectations.ExpectedLogs) > 0 || len(fixtureExpectations.NotExpectedLogs) > 0 {
msg = logCapture.String()
log.SetOutput(ioutil.Discard)
modelTestMutex.Unlock()
}
if fixtureExpectations.ExpectFailure && !dassert.Errorf(er1, "Expected an error during generation of definition %q from spec fixture %s", definitionName, fixtureSpec) {
// expected an error here, and it has not happened
checkContinue(t, continueOnErrors)
return
}
if !dassert.NoErrorf(er1, "could not generate model definition %q from spec fixture %s: %v", definitionName, fixtureSpec, er1) {
// expected smooth generation
checkContinue(t, continueOnErrors)
return
}
if len(fixtureExpectations.ExpectedLogs) > 0 || len(fixtureExpectations.NotExpectedLogs) > 0 {
// assert logged output
for line, logLine := range fixtureExpectations.ExpectedLogs {
if !assertInCode(t, strings.TrimSpace(logLine), msg) {
t.Logf("log expected did not match for definition %q in fixture %s at (fixture) log line %d", definitionName, fixtureSpec, line)
}
}
for line, logLine := range fixtureExpectations.NotExpectedLogs {
if !assertNotInCode(t, strings.TrimSpace(logLine), msg) {
t.Logf("log unexpectedly matched for definition %q in fixture %s at (fixture) log line %d", definitionName, fixtureSpec, line)
}
}
if t.Failed() && !continueOnErrors {
t.FailNow()
return
}
}
// execute the model template with this schema
buf := bytes.NewBuffer(nil)
er2 := templates.MustGet("model").Execute(buf, genModel)
if !dassert.NoErrorf(er2, "could not render model template for definition %q in spec fixture %s: %v", definitionName, fixtureSpec, er2) {
checkContinue(t, continueOnErrors)
return
}
outputName := fixtureExpectations.GeneratedFile
if outputName == "" {
outputName = swag.ToFileName(definitionName) + ".go"
}
// run goimport, gofmt on the generated code
formatted, er3 := opts.LanguageOpts.FormatContent(outputName, buf.Bytes())
if !dassert.NoErrorf(er3, "could not render model template for definition %q in spec fixture %s: %v", definitionName, fixtureSpec, er2) {
checkContinue(t, continueOnErrors)
return
}
// asserts generated code (see fixture file)
res := string(formatted)
for line, codeLine := range fixtureExpectations.ExpectedLines {
if !assertInCode(t, strings.TrimSpace(codeLine), res) {
t.Logf("code expected did not match for definition %q in fixture %s at (fixture) line %d", definitionName, fixtureSpec, line)
}
}
for line, codeLine := range fixtureExpectations.NotExpectedLines {
if !assertNotInCode(t, strings.TrimSpace(codeLine), res) {
t.Logf("code unexpectedly matched for definition %q in fixture %s at (fixture) line %d", definitionName, fixtureSpec, line)
}
}
if t.Failed() && !continueOnErrors {
t.FailNow()
return
}
}
/*
// Gave up with parallel testing
// TestModelRace verifies how much of the load/expand/flatten process may be parallelized:
// by placing proper locks, global cache pollution in go-openapi/spec may be avoided.
func TestModelRace(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer func() {
log.SetOutput(os.Stdout)
}()
initModelFixtures()
dassert := assert.New(t)
for i := 0; i < 10; i++ {
t.Logf("Iteration: %d", i)
for _, fixt := range testedModels {
fixture := fixt
if fixture.SpecFile == "" {
continue
}
fixtureSpec := fixture.SpecFile
runTitle := strings.Join([]string{"codegen", strings.TrimSuffix(path.Base(fixtureSpec), path.Ext(fixtureSpec))}, "-")
t.Run(runTitle, func(t *testing.T) {
t.Parallel()
log.SetOutput(ioutil.Discard)
for _, fixtureRun := range fixture.Runs {
// loads defines the start of the critical section because it comes with the global cache initialized
// TODO: should make this cache more manageable in go-openapi/spec
modelTestMutex.Lock()
specDoc, err := loads.Spec(fixtureSpec)
if !dassert.NoErrorf(err, "unexpected failure loading spec %s: %v", fixtureSpec, err) {
modelTestMutex.Unlock()
t.FailNow()
return
}
opts := fixtureRun.FixtureOpts
// this is the expanded or flattened spec
newSpecDoc, er0 := validateAndFlattenSpec(opts, specDoc)
if !dassert.NoErrorf(er0, "could not expand/flatten fixture %s: %v", fixtureSpec, er0) {
modelTestMutex.Unlock()
t.FailNow()
return
}
modelTestMutex.Unlock()
definitions := newSpecDoc.Spec().Definitions
for k := range fixtureRun.Definitions {
// pick definition to test
var schema *spec.Schema
for def, s := range definitions {
if strings.EqualFold(def, k) {
schema = &s
break
}
}
if !dassert.NotNil(schema, "expected to find definition %q in model fixture %s", k, fixtureSpec) {
t.FailNow()
return
}
}
}
})
}
}
}
*/