feat: agent metadata with custom collectors

This commit is contained in:
2023-03-02 13:05:24 +01:00
parent 3310c09320
commit 1ff29ae1fb
40 changed files with 998 additions and 256 deletions

View File

@ -11,26 +11,26 @@ import (
)
type validatorTestCase struct {
Name string
Source string
ExpectedResult bool
Name string
Source string
ShouldFail bool
}
var validatorTestCases = []validatorTestCase{
{
Name: "SpecOK",
Source: "testdata/spec-ok.json",
ExpectedResult: true,
Name: "SpecOK",
Source: "testdata/spec-ok.json",
ShouldFail: false,
},
{
Name: "SpecMissingProp",
Source: "testdata/spec-missing-prop.json",
ExpectedResult: false,
Name: "SpecMissingProp",
Source: "testdata/spec-missing-prop.json",
ShouldFail: true,
},
{
Name: "SpecAdditionalProp",
Source: "testdata/spec-additional-prop.json",
ExpectedResult: false,
Name: "SpecAdditionalProp",
Source: "testdata/spec-additional-prop.json",
ShouldFail: true,
},
}
@ -43,7 +43,7 @@ func TestValidator(t *testing.T) {
}
for _, tc := range validatorTestCases {
func(tc *validatorTestCase) {
func(tc validatorTestCase) {
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
@ -60,16 +60,16 @@ func TestValidator(t *testing.T) {
ctx := context.Background()
result, err := validator.Validate(ctx, &spec)
err = validator.Validate(ctx, &spec)
if e, g := tc.ExpectedResult, result; e != g {
t.Errorf("result: expected '%v', got '%v'", e, g)
}
if tc.ExpectedResult && err != nil {
if !tc.ShouldFail && err != nil {
t.Errorf("+%v", errors.WithStack(err))
}
if tc.ShouldFail && err == nil {
t.Error("validation should have failed")
}
})
}(&tc)
}(tc)
}
}

View File

@ -1,15 +1,17 @@
package spec
import "forge.cadoles.com/Cadoles/emissary/internal/agent/metadata"
type Spec interface {
SpecName() Name
SpecRevision() int
SpecData() map[string]any
SpecData() metadata.Metadata
}
type RawSpec struct {
Name Name `json:"name"`
Revision int `json:"revision"`
Data map[string]any `json:"data"`
Name Name `json:"name"`
Revision int `json:"revision"`
Data metadata.Metadata `json:"data"`
}
func (s *RawSpec) SpecName() Name {
@ -20,6 +22,6 @@ func (s *RawSpec) SpecRevision() int {
return s.Revision
}
func (s *RawSpec) SpecData() map[string]any {
func (s *RawSpec) SpecData() metadata.Metadata {
return s.Data
}

View File

@ -11,21 +11,21 @@ import (
)
type validatorTestCase struct {
Name string
Source string
ExpectedResult bool
Name string
Source string
ShouldFail bool
}
var validatorTestCases = []validatorTestCase{
{
Name: "SpecOK",
Source: "testdata/spec-ok.json",
ExpectedResult: true,
Name: "SpecOK",
Source: "testdata/spec-ok.json",
ShouldFail: false,
},
{
Name: "SpecMissingProp",
Source: "testdata/spec-missing-prop.json",
ExpectedResult: false,
Name: "SpecMissingProp",
Source: "testdata/spec-missing-prop.json",
ShouldFail: true,
},
}
@ -38,7 +38,7 @@ func TestValidator(t *testing.T) {
}
for _, tc := range validatorTestCases {
func(tc *validatorTestCase) {
func(tc validatorTestCase) {
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
@ -55,16 +55,16 @@ func TestValidator(t *testing.T) {
ctx := context.Background()
result, err := validator.Validate(ctx, &spec)
err = validator.Validate(ctx, &spec)
if e, g := tc.ExpectedResult, result; e != g {
t.Errorf("result: expected '%v', got '%v'", e, g)
}
if tc.ExpectedResult && err != nil {
if !tc.ShouldFail && err != nil {
t.Errorf("+%v", errors.WithStack(err))
}
if tc.ShouldFail && err == nil {
t.Error("validation should have failed")
}
})
}(&tc)
}(tc)
}
}

View File

@ -23,18 +23,18 @@ func (v *Validator) Register(name Name, rawSchema []byte) error {
return nil
}
func (v *Validator) Validate(ctx context.Context, spec Spec) (bool, error) {
func (v *Validator) Validate(ctx context.Context, spec Spec) error {
schema, exists := v.schemas[spec.SpecName()]
if !exists {
return false, errors.WithStack(ErrUnknownSchema)
return errors.WithStack(ErrUnknownSchema)
}
state := schema.Validate(ctx, spec.SpecData())
state := schema.Validate(ctx, map[string]any(spec.SpecData()))
if !state.IsValid() {
return false, errors.WithStack(&ValidationError{*state.Errs})
return errors.WithStack(&ValidationError{*state.Errs})
}
return true, nil
return nil
}
func NewValidator() *Validator {
@ -53,11 +53,10 @@ func Register(name Name, rawSchema []byte) error {
return nil
}
func Validate(ctx context.Context, spec Spec) (bool, error) {
ok, err := defaultValidator.Validate(ctx, spec)
if err != nil {
return ok, errors.WithStack(err)
func Validate(ctx context.Context, spec Spec) error {
if err := defaultValidator.Validate(ctx, spec); err != nil {
return errors.WithStack(err)
}
return ok, nil
return nil
}