75 lines
1.5 KiB
Go
75 lines
1.5 KiB
Go
|
package metadata
|
||
|
|
||
|
import (
|
||
|
"io/ioutil"
|
||
|
"path/filepath"
|
||
|
"testing"
|
||
|
|
||
|
"forge.cadoles.com/arcad/edge/pkg/app"
|
||
|
"github.com/pkg/errors"
|
||
|
"gopkg.in/yaml.v2"
|
||
|
)
|
||
|
|
||
|
type validatorTestCase struct {
|
||
|
File string
|
||
|
ExpectValid bool
|
||
|
ExpectError bool
|
||
|
}
|
||
|
|
||
|
var validatorTestCases = []validatorTestCase{
|
||
|
{
|
||
|
File: "valid.yml",
|
||
|
ExpectValid: true,
|
||
|
},
|
||
|
{
|
||
|
File: "invalid-paths.yml",
|
||
|
ExpectValid: false,
|
||
|
ExpectError: true,
|
||
|
},
|
||
|
{
|
||
|
File: "invalid-minimum-role.yml",
|
||
|
ExpectValid: false,
|
||
|
ExpectError: true,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
var validators = []app.MetadataValidator{
|
||
|
WithMinimumRoleValidator("visitor", "user", "superuser", "admin", "superadmin"),
|
||
|
WithNamedPathsValidator(NamedPathAdmin, NamedPathIcon),
|
||
|
}
|
||
|
|
||
|
func TestManifestValidate(t *testing.T) {
|
||
|
for _, tc := range validatorTestCases {
|
||
|
func(tc *validatorTestCase) {
|
||
|
t.Run(tc.File, func(t *testing.T) {
|
||
|
data, err := ioutil.ReadFile(filepath.Join("testdata/manifests", tc.File))
|
||
|
if err != nil {
|
||
|
t.Fatalf("%+v", errors.WithStack(err))
|
||
|
}
|
||
|
|
||
|
var manifest app.Manifest
|
||
|
|
||
|
if err := yaml.Unmarshal(data, &manifest); err != nil {
|
||
|
t.Fatalf("%+v", errors.WithStack(err))
|
||
|
}
|
||
|
|
||
|
valid, err := manifest.Validate(validators...)
|
||
|
|
||
|
t.Logf("[RESULT] valid:%v, err:%v", valid, err)
|
||
|
|
||
|
if e, g := tc.ExpectValid, valid; e != g {
|
||
|
t.Errorf("valid: expected '%v', got '%v'", e, g)
|
||
|
}
|
||
|
|
||
|
if tc.ExpectError && err == nil {
|
||
|
t.Error("err should not be nil")
|
||
|
}
|
||
|
|
||
|
if !tc.ExpectError && err != nil {
|
||
|
t.Errorf("err: expected nil, got '%+v'", err)
|
||
|
}
|
||
|
})
|
||
|
}(&tc)
|
||
|
}
|
||
|
}
|