2023-02-28 15:50:35 +01:00
|
|
|
package uci
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/spec"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type validatorTestCase struct {
|
2023-03-02 13:05:24 +01:00
|
|
|
Name string
|
|
|
|
Source string
|
|
|
|
ShouldFail bool
|
2023-02-28 15:50:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var validatorTestCases = []validatorTestCase{
|
|
|
|
{
|
2023-03-02 13:05:24 +01:00
|
|
|
Name: "SpecOK",
|
|
|
|
Source: "testdata/spec-ok.json",
|
|
|
|
ShouldFail: false,
|
2023-02-28 15:50:35 +01:00
|
|
|
},
|
|
|
|
{
|
2023-03-02 13:05:24 +01:00
|
|
|
Name: "SpecMissingProp",
|
|
|
|
Source: "testdata/spec-missing-prop.json",
|
|
|
|
ShouldFail: true,
|
2023-02-28 15:50:35 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidator(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
validator := spec.NewValidator()
|
|
|
|
if err := validator.Register(NameUCI, schema); err != nil {
|
|
|
|
t.Fatalf("+%v", errors.WithStack(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range validatorTestCases {
|
2023-03-02 13:05:24 +01:00
|
|
|
func(tc validatorTestCase) {
|
2023-02-28 15:50:35 +01:00
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
rawSpec, err := ioutil.ReadFile(tc.Source)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("+%v", errors.WithStack(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
var spec spec.RawSpec
|
|
|
|
|
|
|
|
if err := json.Unmarshal(rawSpec, &spec); err != nil {
|
|
|
|
t.Fatalf("+%v", errors.WithStack(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-03-02 13:05:24 +01:00
|
|
|
err = validator.Validate(ctx, &spec)
|
2023-02-28 15:50:35 +01:00
|
|
|
|
2023-03-02 13:05:24 +01:00
|
|
|
if !tc.ShouldFail && err != nil {
|
|
|
|
t.Errorf("+%v", errors.WithStack(err))
|
2023-02-28 15:50:35 +01:00
|
|
|
}
|
|
|
|
|
2023-03-02 13:05:24 +01:00
|
|
|
if tc.ShouldFail && err == nil {
|
|
|
|
t.Error("validation should have failed")
|
2023-02-28 15:50:35 +01:00
|
|
|
}
|
|
|
|
})
|
2023-03-02 13:05:24 +01:00
|
|
|
}(tc)
|
2023-02-28 15:50:35 +01:00
|
|
|
}
|
|
|
|
}
|