From 4787edba3635b1efe4b8c03cae24b3406edd2daa Mon Sep 17 00:00:00 2001 From: Gleb Kanterov Date: Wed, 3 Jul 2024 10:33:23 +0200 Subject: [PATCH] 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 --- .../config/mutator/python/python_mutator.go | 12 ++++++++++ .../mutator/python/python_mutator_test.go | 24 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/bundle/config/mutator/python/python_mutator.go b/bundle/config/mutator/python/python_mutator.go index 26b6c54fc..f9febe5b5 100644 --- a/bundle/config/mutator/python/python_mutator.go +++ b/bundle/config/mutator/python/python_mutator.go @@ -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 // delete existing ones. func createLoadOverrideVisitor(ctx context.Context) merge.OverrideVisitor { + resourcesPath := dyn.NewPath(dyn.Key("resources")) jobsPath := dyn.NewPath(dyn.Key("resources"), dyn.Key("jobs")) return merge.OverrideVisitor{ @@ -320,6 +321,11 @@ func createLoadOverrideVisitor(ctx context.Context) merge.OverrideVisitor { return fmt.Errorf("unexpected change at %q (delete)", valuePath.String()) }, 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) { 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 // resources, but not delete existing resources. func createInitOverrideVisitor(ctx context.Context) merge.OverrideVisitor { + resourcesPath := dyn.NewPath(dyn.Key("resources")) jobsPath := dyn.NewPath(dyn.Key("resources"), dyn.Key("jobs")) return merge.OverrideVisitor{ @@ -370,6 +377,11 @@ func createInitOverrideVisitor(ctx context.Context) merge.OverrideVisitor { return nil }, 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) { return dyn.InvalidValue, fmt.Errorf("unexpected change at %q (insert)", valuePath.String()) } diff --git a/bundle/config/mutator/python/python_mutator_test.go b/bundle/config/mutator/python/python_mutator_test.go index 64a2a1a65..9a0ed8c3a 100644 --- a/bundle/config/mutator/python/python_mutator_test.go +++ b/bundle/config/mutator/python/python_mutator_test.go @@ -325,6 +325,18 @@ func TestCreateOverrideVisitor(t *testing.T) { deletePath: dyn.MustPathFromString("resources.jobs.job0"), 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", phase: PythonMutatorPhaseLoad, @@ -357,6 +369,18 @@ func TestCreateOverrideVisitor(t *testing.T) { deletePath: dyn.MustPathFromString("resources.jobs.job0"), 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", phase: PythonMutatorPhaseInit,