1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package validate
16
17 import (
18 "log"
19 "os"
20 "path/filepath"
21 "strings"
22 "testing"
23
24 "github.com/go-openapi/loads"
25 "github.com/go-openapi/loads/fmts"
26 "github.com/go-openapi/strfmt"
27 "github.com/stretchr/testify/assert"
28 "github.com/stretchr/testify/require"
29 )
30
31 func init() {
32 loads.AddLoader(fmts.YAMLMatcher, fmts.YAMLDoc)
33 }
34
35 func skipNotifyGoSwagger(t *testing.T) {
36 t.Log("To enable this long running test, use -args -enable-go-swagger in your go test command line")
37 }
38
39
40
41
42
43
44 func Test_GoSwaggerTestCases(t *testing.T) {
45 if !enableGoSwaggerTests {
46 skipNotifyGoSwagger(t)
47 t.SkipNow()
48 }
49
50 expectedLoadFailures := map[string]bool{
51 "fixtures/go-swagger/bugs/342/fixture-342.yaml": false,
52 "fixtures/go-swagger/bugs/342/fixture-342-2.yaml": true,
53 }
54
55
56 expectedFailures := map[string]bool{
57 "fixtures/go-swagger/bugs/1010/swagger.yml": true,
58 "fixtures/go-swagger/bugs/103/swagger.json": true,
59 "fixtures/go-swagger/bugs/106/swagger.json": true,
60 "fixtures/go-swagger/bugs/1171/swagger.yaml": true,
61 "fixtures/go-swagger/bugs/1238/swagger.yaml": true,
62 "fixtures/go-swagger/bugs/1289/fixture-1289-2.yaml": true,
63 "fixtures/go-swagger/bugs/1289/fixture-1289.yaml": true,
64 "fixtures/go-swagger/bugs/193/spec2.json": true,
65 "fixtures/go-swagger/bugs/195/swagger.json": true,
66 "fixtures/go-swagger/bugs/248/swagger.json": true,
67 "fixtures/go-swagger/bugs/249/swagger.json": true,
68 "fixtures/go-swagger/bugs/342/fixture-342-2.yaml": true,
69 "fixtures/go-swagger/bugs/342/fixture-342.yaml": true,
70 "fixtures/go-swagger/bugs/423/swagger.json": true,
71 "fixtures/go-swagger/bugs/453/swagger.yml": true,
72 "fixtures/go-swagger/bugs/455/swagger.yml": true,
73 "fixtures/go-swagger/bugs/628/swagger.yml": true,
74 "fixtures/go-swagger/bugs/733/swagger.json": false,
75 "fixtures/go-swagger/bugs/763/swagger.yml": true,
76 "fixtures/go-swagger/bugs/774/swagger.yml": true,
77 "fixtures/go-swagger/bugs/776/error.yaml": true,
78 "fixtures/go-swagger/bugs/776/item.yaml": true,
79 "fixtures/go-swagger/bugs/809/swagger.yml": true,
80 "fixtures/go-swagger/bugs/825/swagger.yml": true,
81 "fixtures/go-swagger/bugs/890/path/health_check.yaml": true,
82 "fixtures/go-swagger/bugs/981/swagger.json": true,
83 "fixtures/go-swagger/canary/docker/swagger.json": true,
84 "fixtures/go-swagger/canary/ms-cog-sci/swagger.json": true,
85 "fixtures/go-swagger/codegen/azure-text-analyis.json": true,
86 "fixtures/go-swagger/codegen/issue72.json": true,
87 "fixtures/go-swagger/codegen/simplesearch.yml": true,
88 "fixtures/go-swagger/codegen/swagger-codegen-tests.json": true,
89 "fixtures/go-swagger/codegen/todolist.allparams.yml": true,
90 "fixtures/go-swagger/codegen/todolist.bodyparams.yml": true,
91 "fixtures/go-swagger/codegen/todolist.discriminators.yml": true,
92 "fixtures/go-swagger/codegen/todolist.enums.yml": true,
93 "fixtures/go-swagger/codegen/todolist.models.yml": true,
94 "fixtures/go-swagger/codegen/todolist.responses.yml": true,
95 "fixtures/go-swagger/codegen/todolist.schemavalidation.yml": true,
96 "fixtures/go-swagger/codegen/todolist.simplepath.yml": true,
97 "fixtures/go-swagger/codegen/todolist.simple.yml": true,
98 "fixtures/go-swagger/codegen/todolist.url.basepath.yml": true,
99 "fixtures/go-swagger/codegen/todolist.url.simple.yml": true,
100 "fixtures/go-swagger/expansion/all-the-things.json": true,
101 "fixtures/go-swagger/expansion/circularRefs.json": true,
102 "fixtures/go-swagger/expansion/invalid-refs.json": true,
103 "fixtures/go-swagger/expansion/params.json": true,
104 "fixtures/go-swagger/expansion/schemas1.json": true,
105 "fixtures/go-swagger/expansion/schemas2.json": true,
106 "fixtures/go-swagger/petstores/petstore-expanded.json": true,
107 "fixtures/go-swagger/petstores/petstore-simple.json": true,
108 "fixtures/go-swagger/petstores/petstore-with-external-docs.json": true,
109 "fixtures/go-swagger/remotes/folder/folderInteger.json": true,
110 "fixtures/go-swagger/remotes/integer.json": true,
111 "fixtures/go-swagger/remotes/subSchemas.json": true,
112 "fixtures/go-swagger/specs/deeper/arrayProp.json": true,
113 "fixtures/go-swagger/specs/deeper/stringProp.json": true,
114 "fixtures/go-swagger/specs/refed.json": true,
115 "fixtures/go-swagger/specs/resolution2.json": true,
116 "fixtures/go-swagger/specs/resolution.json": true,
117 }
118
119 testGoSwaggerSpecs(t, filepath.Join(".", "fixtures", "go-swagger"), expectedFailures, expectedLoadFailures, true)
120 }
121
122 func wantSwaggerTest(info os.FileInfo) bool {
123 f := info.Name()
124 return !info.IsDir() && (strings.HasSuffix(f, ".yaml") || strings.HasSuffix(f, ".yml") || strings.HasSuffix(f, ".json"))
125 }
126
127
128
129 func testGoSwaggerSpecs(t *testing.T, path string, expectToFail, expectToFailOnLoad map[string]bool, haltOnErrors bool) {
130 err := filepath.Walk(path,
131 func(path string, info os.FileInfo, _ error) error {
132 t.Run(path, func(t *testing.T) {
133 if !DebugTest {
134 t.Parallel()
135 }
136
137 npath := filepath.ToSlash(path)
138 shouldNotLoad := expectToFailOnLoad[npath]
139 shouldFail := expectToFail[npath]
140 if !wantSwaggerTest(info) {
141 return
142 }
143 if DebugTest {
144 t.Logf("Testing validation status for spec: %s", path)
145 }
146
147 doc, err := loads.Spec(path)
148 if shouldNotLoad {
149 require.Errorf(t, err, "expected this spec not to load: %s", path)
150 return
151 }
152 require.NoErrorf(t, err, "expected this spec to load without error: %s", path)
153
154 if haltOnErrors && t.Failed() {
155 assert.FailNow(t, "test Halted: stop on error mode")
156 return
157 }
158
159
160 validator := NewSpecValidator(doc.Schema(), strfmt.Default)
161 validator.SetContinueOnErrors(true)
162 res, _ := validator.Validate(doc)
163 if shouldFail {
164 assert.Falsef(t, res.IsValid(), "expected this spec to be invalid: %s", path)
165 } else {
166 assert.Truef(t, res.IsValid(), "expected this spec to be valid: %s", path)
167 }
168 t.Logf("Errors reported by validation on %s", path)
169 for _, e := range res.Errors {
170 t.Log(e)
171 }
172
173 if haltOnErrors && t.Failed() {
174 assert.FailNow(t, "Test halted: stop on error mode")
175 return
176 }
177 })
178 return nil
179 })
180 require.NoErrorf(t, err, "walk: %v", err)
181 if t.Failed() {
182 log.Printf("A change in expected validation status has been detected")
183 }
184 }
185
View as plain text