Skip to content

Commit 66dc3a7

Browse files
committed
feat: support to validate the YAML file via schema
1 parent 4944157 commit 66dc3a7

File tree

9 files changed

+38
-4
lines changed

9 files changed

+38
-4
lines changed

cmd/run_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestRunSuite(t *testing.T) {
2727
gock.New(urlFoo).
2828
Get("/bar").
2929
Reply(http.StatusOK).
30-
JSON("{}")
30+
File("testdata/simple-suite.yaml")
3131
},
3232
hasError: false,
3333
}, {

cmd/testdata/simple-suite.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ api: http://foo/
33
items:
44
- request:
55
api: /bar
6+
name: bar

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ require (
1313
golang.org/x/sync v0.1.0
1414
google.golang.org/grpc v1.54.0
1515
google.golang.org/protobuf v1.30.0
16-
gopkg.in/yaml.v2 v2.4.0
1716
)
1817

1918
require (
2019
github.com/Masterminds/goutils v1.1.1 // indirect
2120
github.com/Masterminds/semver/v3 v3.2.0 // indirect
2221
github.com/davecgh/go-spew v1.1.1 // indirect
22+
github.com/ghodss/yaml v1.0.0 // indirect
2323
github.com/golang/protobuf v1.5.2 // indirect
2424
github.com/google/uuid v1.3.0 // indirect
2525
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
@@ -44,5 +44,6 @@ require (
4444
golang.org/x/sys v0.6.0 // indirect
4545
golang.org/x/text v0.8.0 // indirect
4646
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
47+
gopkg.in/yaml.v2 v2.3.0 // indirect
4748
gopkg.in/yaml.v3 v3.0.1 // indirect
4849
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ github.com/envoyproxy/go-control-plane v0.10.3/go.mod h1:fJJn/j26vwOu972OllsvAgJ
438438
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
439439
github.com/envoyproxy/protoc-gen-validate v0.6.7/go.mod h1:dyJXwwfPK2VSqiB9Klm1J6romD608Ba7Hij42vrOBCo=
440440
github.com/envoyproxy/protoc-gen-validate v0.9.1/go.mod h1:OKNgG7TCp5pF4d6XftA0++PMirau2/yoOwVac3AbF2w=
441+
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
441442
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
442443
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
443444
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
@@ -1178,6 +1179,7 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
11781179
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
11791180
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
11801181
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
1182+
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
11811183
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
11821184
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
11831185
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

pkg/testing/case.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type Request struct {
3535

3636
// Response is the expected response
3737
type Response struct {
38-
StatusCode int `yaml:"statusCode" json:"bodyFromFile,omitempty"`
38+
StatusCode int `yaml:"statusCode" json:"statusCode,omitempty"`
3939
Body string `yaml:"body" json:"body,omitempty"`
4040
Header map[string]string `yaml:"header" json:"header,omitempty"`
4141
BodyFieldsExpect map[string]interface{} `yaml:"bodyFieldsExpect" json:"bodyFieldsExpect,omitempty"`

pkg/testing/parser.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import (
1010
"os"
1111
"strings"
1212

13+
"github.com/ghodss/yaml"
1314
"github.com/linuxsuren/api-testing/pkg/render"
1415
"github.com/linuxsuren/api-testing/pkg/util"
15-
"gopkg.in/yaml.v2"
16+
"github.com/linuxsuren/api-testing/sample"
17+
"github.com/xeipuuv/gojsonschema"
1618
)
1719

1820
// Parse parses a file and returns the test suite
@@ -21,6 +23,23 @@ func Parse(configFile string) (testSuite *TestSuite, err error) {
2123
if data, err = os.ReadFile(configFile); err == nil {
2224
testSuite, err = ParseFromData(data)
2325
}
26+
27+
// schema validation
28+
if err == nil {
29+
// convert YAML to JSON
30+
var jsonData []byte
31+
if jsonData, err = yaml.YAMLToJSON(data); err == nil {
32+
schemaLoader := gojsonschema.NewStringLoader(sample.Schema)
33+
documentLoader := gojsonschema.NewBytesLoader(jsonData)
34+
35+
var result *gojsonschema.Result
36+
if result, err = gojsonschema.Validate(schemaLoader, documentLoader); err == nil {
37+
if !result.Valid() {
38+
err = fmt.Errorf("%v", result.Errors())
39+
}
40+
}
41+
}
42+
}
2443
return
2544
}
2645

pkg/testing/parser_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ func TestParse(t *testing.T) {
3030
},
3131
}, suite.Items[0])
3232
}
33+
34+
_, err = Parse("testdata/invalid-testcase.yaml")
35+
assert.NotNil(t, err)
3336
}
3437

3538
func TestRequestRender(t *testing.T) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: projects
2+
request:
3+
api: https://foo
4+
expect:
5+
statuscode: 200

sample/constants.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ import _ "embed"
44

55
//go:embed testsuite-gitlab.yaml
66
var TestSuiteGitLab string
7+
8+
//go:embed api-testing-schema.json
9+
var Schema string

0 commit comments

Comments
 (0)