PythonMutator: allow insert 'resources' and 'resources.jobs' (#1555)

## Changes
Allow insert 'resources' and 'resources.jobs' because they can be absent
in incoming bundle.

## Tests
Unit tests
This commit is contained in:
Gleb Kanterov 2024-07-03 10:33:23 +02:00 committed by GitHub
parent b9e3c98723
commit 4787edba36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 0 deletions

View File

@ -309,6 +309,7 @@ func createOverrideVisitor(ctx context.Context, phase phase) (merge.OverrideVisi
// During load, it's only possible to create new resources, and not modify or // During load, it's only possible to create new resources, and not modify or
// delete existing ones. // delete existing ones.
func createLoadOverrideVisitor(ctx context.Context) merge.OverrideVisitor { func createLoadOverrideVisitor(ctx context.Context) merge.OverrideVisitor {
resourcesPath := dyn.NewPath(dyn.Key("resources"))
jobsPath := dyn.NewPath(dyn.Key("resources"), dyn.Key("jobs")) jobsPath := dyn.NewPath(dyn.Key("resources"), dyn.Key("jobs"))
return merge.OverrideVisitor{ return merge.OverrideVisitor{
@ -320,6 +321,11 @@ func createLoadOverrideVisitor(ctx context.Context) merge.OverrideVisitor {
return fmt.Errorf("unexpected change at %q (delete)", valuePath.String()) return fmt.Errorf("unexpected change at %q (delete)", valuePath.String())
}, },
VisitInsert: func(valuePath dyn.Path, right dyn.Value) (dyn.Value, error) { VisitInsert: func(valuePath dyn.Path, right dyn.Value) (dyn.Value, error) {
// insert 'resources' or 'resources.jobs' if it didn't exist before
if valuePath.Equal(resourcesPath) || valuePath.Equal(jobsPath) {
return right, nil
}
if !valuePath.HasPrefix(jobsPath) { if !valuePath.HasPrefix(jobsPath) {
return dyn.InvalidValue, fmt.Errorf("unexpected change at %q (insert)", valuePath.String()) return dyn.InvalidValue, fmt.Errorf("unexpected change at %q (insert)", valuePath.String())
} }
@ -346,6 +352,7 @@ func createLoadOverrideVisitor(ctx context.Context) merge.OverrideVisitor {
// During the init phase it's possible to create new resources, modify existing // During the init phase it's possible to create new resources, modify existing
// resources, but not delete existing resources. // resources, but not delete existing resources.
func createInitOverrideVisitor(ctx context.Context) merge.OverrideVisitor { func createInitOverrideVisitor(ctx context.Context) merge.OverrideVisitor {
resourcesPath := dyn.NewPath(dyn.Key("resources"))
jobsPath := dyn.NewPath(dyn.Key("resources"), dyn.Key("jobs")) jobsPath := dyn.NewPath(dyn.Key("resources"), dyn.Key("jobs"))
return merge.OverrideVisitor{ return merge.OverrideVisitor{
@ -370,6 +377,11 @@ func createInitOverrideVisitor(ctx context.Context) merge.OverrideVisitor {
return nil return nil
}, },
VisitInsert: func(valuePath dyn.Path, right dyn.Value) (dyn.Value, error) { VisitInsert: func(valuePath dyn.Path, right dyn.Value) (dyn.Value, error) {
// insert 'resources' or 'resources.jobs' if it didn't exist before
if valuePath.Equal(resourcesPath) || valuePath.Equal(jobsPath) {
return right, nil
}
if !valuePath.HasPrefix(jobsPath) { if !valuePath.HasPrefix(jobsPath) {
return dyn.InvalidValue, fmt.Errorf("unexpected change at %q (insert)", valuePath.String()) return dyn.InvalidValue, fmt.Errorf("unexpected change at %q (insert)", valuePath.String())
} }

View File

@ -325,6 +325,18 @@ func TestCreateOverrideVisitor(t *testing.T) {
deletePath: dyn.MustPathFromString("resources.jobs.job0"), deletePath: dyn.MustPathFromString("resources.jobs.job0"),
deleteError: fmt.Errorf("unexpected change at \"resources.jobs.job0\" (delete)"), deleteError: fmt.Errorf("unexpected change at \"resources.jobs.job0\" (delete)"),
}, },
{
name: "load: can insert 'resources'",
phase: PythonMutatorPhaseLoad,
insertPath: dyn.MustPathFromString("resources"),
insertError: nil,
},
{
name: "load: can insert 'resources.jobs'",
phase: PythonMutatorPhaseLoad,
insertPath: dyn.MustPathFromString("resources.jobs"),
insertError: nil,
},
{ {
name: "load: can insert a job", name: "load: can insert a job",
phase: PythonMutatorPhaseLoad, phase: PythonMutatorPhaseLoad,
@ -357,6 +369,18 @@ func TestCreateOverrideVisitor(t *testing.T) {
deletePath: dyn.MustPathFromString("resources.jobs.job0"), deletePath: dyn.MustPathFromString("resources.jobs.job0"),
deleteError: fmt.Errorf("unexpected change at \"resources.jobs.job0\" (delete)"), deleteError: fmt.Errorf("unexpected change at \"resources.jobs.job0\" (delete)"),
}, },
{
name: "init: can insert 'resources'",
phase: PythonMutatorPhaseInit,
insertPath: dyn.MustPathFromString("resources"),
insertError: nil,
},
{
name: "init: can insert 'resources.jobs'",
phase: PythonMutatorPhaseInit,
insertPath: dyn.MustPathFromString("resources.jobs"),
insertError: nil,
},
{ {
name: "init: can insert a job", name: "init: can insert a job",
phase: PythonMutatorPhaseInit, phase: PythonMutatorPhaseInit,