fixup patcher test to include field manager

This commit is contained in:
Alexander Zielenski 2023-03-15 14:42:07 -07:00
parent ad5cda4d21
commit 077cc20f8a
2 changed files with 19 additions and 1 deletions

View File

@ -344,6 +344,11 @@ func (p *jsonPatcher) applyPatchToCurrentObject(requestContext context.Context,
} }
} }
if p.options == nil {
// Provide a more informative error for the crash that would
// happen on the next line
panic("PatchOptions required but not provided")
}
objToUpdate = p.fieldManager.UpdateNoErrors(currentObject, objToUpdate, managerOrUserAgent(p.options.FieldManager, p.userAgent)) objToUpdate = p.fieldManager.UpdateNoErrors(currentObject, objToUpdate, managerOrUserAgent(p.options.FieldManager, p.userAgent))
return objToUpdate, nil return objToUpdate, nil
} }

View File

@ -40,6 +40,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/serializer" "k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/json" "k8s.io/apimachinery/pkg/util/json"
"k8s.io/apimachinery/pkg/util/managedfields"
utilruntime "k8s.io/apimachinery/pkg/util/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/strategicpatch" "k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/apimachinery/pkg/util/yaml" "k8s.io/apimachinery/pkg/util/yaml"
@ -451,6 +452,13 @@ func (tc *patchTestCase) Run(t *testing.T) {
schemaReferenceObj := &examplev1.Pod{} schemaReferenceObj := &examplev1.Pod{}
hubVersion := example.SchemeGroupVersion hubVersion := example.SchemeGroupVersion
fieldmanager, err := managedfields.NewDefaultFieldManager(
managedfields.NewDeducedTypeConverter(),
convertor, defaulter, creater, kind, hubVersion, "", nil)
if err != nil {
t.Fatalf("failed to create field manager: %v", err)
}
for _, patchType := range []types.PatchType{types.JSONPatchType, types.MergePatchType, types.StrategicMergePatchType} { for _, patchType := range []types.PatchType{types.JSONPatchType, types.MergePatchType, types.StrategicMergePatchType} {
// This needs to be reset on each iteration. // This needs to be reset on each iteration.
testPatcher := &testPatcher{ testPatcher := &testPatcher{
@ -536,10 +544,15 @@ func (tc *patchTestCase) Run(t *testing.T) {
name: name, name: name,
patchType: patchType, patchType: patchType,
patchBytes: patch, patchBytes: patch,
options: &metav1.PatchOptions{
FieldManager: "test-manager",
},
} }
ctx, cancel := context.WithTimeout(ctx, time.Second) ctx, cancel := context.WithTimeout(ctx, time.Second)
resultObj, _, err := p.patchResource(ctx, &RequestScope{}) resultObj, _, err := p.patchResource(ctx, &RequestScope{
FieldManager: fieldmanager,
})
cancel() cancel()
if len(tc.expectedError) != 0 { if len(tc.expectedError) != 0 {