Fix spec validation & merge so that default values are correctly applied
Signed-off-by: Solomon Hykes <sh.github.6811@hykes.org>
This commit is contained in:
@@ -11,29 +11,24 @@ type Component struct {
|
||||
// Source value for the component, without spec merged
|
||||
// eg. `{ string, #dagger: compute: [{do:"fetch-container", ...}]}`
|
||||
v *Value
|
||||
|
||||
// Annotation value for the component , with spec merged.
|
||||
// -> the contents of #dagger.compute
|
||||
// eg. `compute: [{do:"fetch-container", ...}]`
|
||||
//
|
||||
// The spec is merged at this level because the Cue API
|
||||
// does not support merging embedded scalar with nested definition.
|
||||
config *Value
|
||||
}
|
||||
|
||||
func NewComponent(v *Value) (*Component, error) {
|
||||
config := v.Get("#dagger")
|
||||
if !config.Exists() {
|
||||
return nil, os.ErrNotExist
|
||||
if !v.Exists() {
|
||||
// Component value does not exist
|
||||
return nil, ErrNotExist
|
||||
}
|
||||
spec := v.cc.Spec()
|
||||
config, err := spec.Get("#ComponentConfig").Merge(v.Get("#dagger"))
|
||||
if !v.Get("#dagger").Exists() {
|
||||
// Component value exists but has no `#dagger` definition
|
||||
return nil, ErrNotExist
|
||||
}
|
||||
// Validate & merge with spec
|
||||
final, err := v.Finalize(v.cc.Spec().Get("#Component"))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "invalid component config")
|
||||
return nil, errors.Wrap(err, "invalid component")
|
||||
}
|
||||
return &Component{
|
||||
v: v,
|
||||
config: config,
|
||||
v: final,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -46,19 +41,6 @@ func (c *Component) Config() *Value {
|
||||
return c.Value().Get("#dagger")
|
||||
}
|
||||
|
||||
// Verify that this component respects the dagger component spec.
|
||||
//
|
||||
// NOTE: calling matchSpec("#Component") is not enough because
|
||||
// it does not match embedded scalars.
|
||||
func (c *Component) Validate() error {
|
||||
// FIXME: this crashes on `#dagger:compute:_`
|
||||
// see TestValidateEmptyComponent
|
||||
// Using a workaround for now.
|
||||
// return c.Config().Validate("#ComponentConfig")
|
||||
|
||||
return c.Config().Validate()
|
||||
}
|
||||
|
||||
// Return this component's compute script.
|
||||
func (c *Component) ComputeScript() (*Script, error) {
|
||||
return newScript(c.Config().Get("compute"))
|
||||
|
@@ -40,7 +40,6 @@ foo: #dagger: {}
|
||||
// Test that default values in spec are applied at the component level
|
||||
// See issue #19
|
||||
func TestComponentDefaults(t *testing.T) {
|
||||
t.Skip("FIXME: issue #19")
|
||||
cc := &Compiler{}
|
||||
v, err := cc.Compile("", `
|
||||
#dagger: compute: [
|
||||
|
@@ -37,11 +37,15 @@ package dagger
|
||||
// by scripts defining how to compute it, present it to a user,
|
||||
// encrypt it, etc.
|
||||
|
||||
// FIXME: #Component will not match embedded scalars.
|
||||
// use Runtime.isComponent() for a reliable check
|
||||
#Component: {
|
||||
// Match structs
|
||||
#dagger: #ComponentConfig
|
||||
...
|
||||
} | {
|
||||
// Match embedded strings
|
||||
// FIXME: match all embedded scalar types
|
||||
string
|
||||
#dagger: #ComponentConfig
|
||||
}
|
||||
|
||||
// The contents of a #dagger annotation
|
||||
@@ -86,7 +90,7 @@ package dagger
|
||||
env?: [string]: string
|
||||
always?: true | *false
|
||||
dir: string | *"/"
|
||||
mount?: [string]: #MountTmp | #MountCache | #MountComponent | #MountScript
|
||||
mount: [string]: #MountTmp | #MountCache | #MountComponent | #MountScript
|
||||
}
|
||||
|
||||
#MountTmp: "tmpfs"
|
||||
|
@@ -40,13 +40,10 @@ func TestCopyMatch(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
op, err := newOp(v)
|
||||
op, err := NewOp(v)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := op.Validate("#Copy"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
n := 0
|
||||
err = op.Walk(ctx, func(op *Op) error {
|
||||
n++
|
||||
|
@@ -17,8 +17,8 @@ type Script struct {
|
||||
}
|
||||
|
||||
func NewScript(v *Value) (*Script, error) {
|
||||
spec := v.cc.Spec().Get("#Script")
|
||||
final, err := spec.Merge(v)
|
||||
// Validate & merge with spec
|
||||
final, err := v.Finalize(v.cc.Spec().Get("#Script"))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "invalid script")
|
||||
}
|
||||
@@ -30,8 +30,6 @@ func newScript(v *Value) (*Script, error) {
|
||||
if !v.Exists() {
|
||||
return nil, ErrNotExist
|
||||
}
|
||||
// Assume script is valid.
|
||||
// Spec validation is already done at component creation.
|
||||
return &Script{
|
||||
v: v,
|
||||
}, nil
|
||||
@@ -66,8 +64,9 @@ func (s *Script) Execute(ctx context.Context, fs FS, out *Fillable) (FS, error)
|
||||
log.
|
||||
Ctx(ctx).
|
||||
Warn().
|
||||
Err(err).
|
||||
Msg("script is unspecified, aborting execution")
|
||||
Int("op", idx).
|
||||
// FIXME: tell user which inputs are missing (by inspecting references)
|
||||
Msg("script is missing inputs and has not been fully executed")
|
||||
return ErrAbortExecution
|
||||
}
|
||||
op, err := newOp(v)
|
||||
|
@@ -6,6 +6,41 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Test that a script with missing fields DOES NOT cause an error
|
||||
// NOTE: this behavior may change in the future.
|
||||
func TestScriptMissingFields(t *testing.T) {
|
||||
cc := &Compiler{}
|
||||
s, err := cc.CompileScript("test.cue", `
|
||||
[
|
||||
{
|
||||
do: "fetch-container"
|
||||
// Missing ref, should cause an error
|
||||
}
|
||||
]
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatalf("err=%v\nval=%v\n", err, s.v.val)
|
||||
}
|
||||
}
|
||||
|
||||
// Test that a script with defined, but unfinished fields is ignored.
|
||||
func TestScriptUnfinishedField(t *testing.T) {
|
||||
// nOps=1 to make sure only 1 op is counted
|
||||
mkScript(t, 1, `
|
||||
[
|
||||
{
|
||||
do: "fetch-container"
|
||||
// Unfinished op: should ignore subsequent ops.
|
||||
ref: string
|
||||
},
|
||||
{
|
||||
do: "exec"
|
||||
args: ["echo", "hello"]
|
||||
}
|
||||
]
|
||||
`)
|
||||
}
|
||||
|
||||
// Test a script which loads a nested script
|
||||
func TestScriptLoadScript(t *testing.T) {
|
||||
mkScript(t, 2, `
|
||||
@@ -74,7 +109,6 @@ func TestScriptDefaults(t *testing.T) {
|
||||
if dir != "/" {
|
||||
t.Fatal(dir)
|
||||
}
|
||||
t.Skip("FIXME: issue #19")
|
||||
// Walk triggers issue #19 UNLESS optional fields removed from spec.cue
|
||||
if err := op.Walk(context.TODO(), func(op *Op) error {
|
||||
return nil
|
||||
|
@@ -32,11 +32,15 @@ package dagger
|
||||
// by scripts defining how to compute it, present it to a user,
|
||||
// encrypt it, etc.
|
||||
|
||||
// FIXME: #Component will not match embedded scalars.
|
||||
// use Runtime.isComponent() for a reliable check
|
||||
#Component: {
|
||||
// Match structs
|
||||
#dagger: #ComponentConfig
|
||||
...
|
||||
} | {
|
||||
// Match embedded strings
|
||||
// FIXME: match all embedded scalar types
|
||||
string
|
||||
#dagger: #ComponentConfig
|
||||
}
|
||||
|
||||
// The contents of a #dagger annotation
|
||||
@@ -81,7 +85,7 @@ package dagger
|
||||
env?: [string]: string
|
||||
always?: true | *false
|
||||
dir: string | *"/"
|
||||
mount?: [string]: #MountTmp | #MountCache | #MountComponent | #MountScript
|
||||
mount: [string]: #MountTmp | #MountCache | #MountComponent | #MountScript
|
||||
}
|
||||
|
||||
#MountTmp: "tmpfs"
|
||||
|
@@ -134,6 +134,28 @@ func (v *Value) RangeStruct(fn func(string, *Value) error) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Finalize a value using the given spec. This means:
|
||||
// 1. Check that the value matches the spec.
|
||||
// 2. Merge the value and the spec, and return the result.
|
||||
func (v *Value) Finalize(spec *Value) (*Value, error) {
|
||||
v.cc.Lock()
|
||||
unified := spec.val.Unify(v.val)
|
||||
v.cc.Unlock()
|
||||
// FIXME: temporary debug message, remove before merging.
|
||||
// fmt.Printf("Finalize:\n spec=%v\n v=%v\n unified=%v", spec.val, v.val, unified)
|
||||
|
||||
// OPTION 1: unfinished fields should pass, but don't
|
||||
// if err := unified.Validate(cue.Concrete(true)); err != nil {
|
||||
// OPTION 2: missing fields should fail, but don't
|
||||
// We choose option 2 for now, because it's easier to layer a
|
||||
// fix on top (we access individual fields so have an opportunity
|
||||
// to return an error if they are not there).
|
||||
if err := unified.Validate(cue.Final()); err != nil {
|
||||
return nil, cueErr(err)
|
||||
}
|
||||
return v.Merge(spec)
|
||||
}
|
||||
|
||||
// FIXME: receive string path?
|
||||
func (v *Value) Merge(x interface{}, path ...string) (*Value, error) {
|
||||
if xval, ok := x.(*Value); ok {
|
||||
|
@@ -4,6 +4,93 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValueFinalize(t *testing.T) {
|
||||
cc := &Compiler{}
|
||||
root, err := cc.Compile("test.cue",
|
||||
`
|
||||
#FetchContainer: {
|
||||
do: "fetch-container"
|
||||
ref: string
|
||||
tag: string | *"latest"
|
||||
}
|
||||
|
||||
good: {
|
||||
do: "fetch-container"
|
||||
ref: "scratch"
|
||||
}
|
||||
|
||||
missing: {
|
||||
do: "fetch-container"
|
||||
// missing ref
|
||||
}
|
||||
|
||||
unfinished: {
|
||||
do: "fetch-container"
|
||||
ref: string // unfinished but present: should pass validation
|
||||
}
|
||||
|
||||
forbidden: {
|
||||
do: "fetch-container"
|
||||
foo: "bar" // forbidden field
|
||||
}
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
spec := root.Get("#FetchContainer")
|
||||
if _, err := root.Get("good").Finalize(spec); err != nil {
|
||||
// Should not fail
|
||||
t.Errorf("'good': validation should not fail. err=%q", err)
|
||||
}
|
||||
if _, err := root.Get("missing").Finalize(spec); err != nil {
|
||||
// SHOULD NOT fail
|
||||
// NOTE: this behavior may change in the future.
|
||||
t.Errorf("'missing': validation should fail")
|
||||
}
|
||||
if _, err := root.Get("forbidden").Finalize(spec); err == nil {
|
||||
// SHOULD fail
|
||||
t.Errorf("'forbidden': validation should fail")
|
||||
}
|
||||
if _, err := root.Get("unfinished").Finalize(spec); err != nil {
|
||||
// Should not fail
|
||||
t.Errorf("'unfinished': validation should not fail. err=%q", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Test that a non-existing field is detected correctly
|
||||
func TestFieldNotExist(t *testing.T) {
|
||||
cc := &Compiler{}
|
||||
root, err := cc.Compile("test.cue", `foo: "bar"`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if v := root.Get("foo"); !v.Exists() {
|
||||
// value should exist
|
||||
t.Fatal(v)
|
||||
}
|
||||
if v := root.Get("bar"); v.Exists() {
|
||||
// value should NOT exist
|
||||
t.Fatal(v)
|
||||
}
|
||||
}
|
||||
|
||||
// Test that a non-existing definition is detected correctly
|
||||
func TestDefNotExist(t *testing.T) {
|
||||
cc := &Compiler{}
|
||||
root, err := cc.Compile("test.cue", `foo: #bla: "bar"`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if v := root.Get("foo.#bla"); !v.Exists() {
|
||||
// value should exist
|
||||
t.Fatal(v)
|
||||
}
|
||||
if v := root.Get("foo.#nope"); v.Exists() {
|
||||
// value should NOT exist
|
||||
t.Fatal(v)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSimple(t *testing.T) {
|
||||
cc := &Compiler{}
|
||||
_, err := cc.EmptyStruct()
|
||||
|
Reference in New Issue
Block a user