2023-03-24 23:17:55 +01:00
|
|
|
package sysupgrade
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
2024-03-12 16:22:35 +01:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/datastore/memory"
|
2023-03-24 23:17:55 +01:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/spec"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type validatorTestCase struct {
|
|
|
|
Name string
|
|
|
|
Source string
|
|
|
|
ShouldFail bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var validatorTestCases = []validatorTestCase{
|
|
|
|
{
|
|
|
|
Name: "SpecOK",
|
|
|
|
Source: "testdata/spec-ok.json",
|
|
|
|
ShouldFail: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidator(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-03-12 16:22:35 +01:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
repo := memory.NewSpecDefinitionRepository()
|
|
|
|
if _, err := repo.Upsert(ctx, Name, Version, schema); err != nil {
|
2023-03-24 23:17:55 +01:00
|
|
|
t.Fatalf("+%v", errors.WithStack(err))
|
|
|
|
}
|
|
|
|
|
2024-03-12 16:22:35 +01:00
|
|
|
validator := spec.NewValidator(repo)
|
|
|
|
|
2023-03-24 23:17:55 +01:00
|
|
|
for _, tc := range validatorTestCases {
|
|
|
|
func(tc validatorTestCase) {
|
|
|
|
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()
|
|
|
|
|
|
|
|
err = validator.Validate(ctx, &spec)
|
|
|
|
|
|
|
|
if !tc.ShouldFail && err != nil {
|
|
|
|
t.Errorf("+%v", errors.WithStack(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.ShouldFail && err == nil {
|
|
|
|
t.Error("validation should have failed")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}(tc)
|
|
|
|
}
|
|
|
|
}
|